/home/mdboom/Work/builds/cpython/Objects/typeobject.c
Line | Count | Source (jump to first uncovered line) |
1 | /* Type object implementation */ |
2 | |
3 | #include "Python.h" |
4 | #include "pycore_call.h" |
5 | #include "pycore_code.h" // CO_FAST_FREE |
6 | #include "pycore_compile.h" // _Py_Mangle() |
7 | #include "pycore_initconfig.h" // _PyStatus_OK() |
8 | #include "pycore_moduleobject.h" // _PyModule_GetDef() |
9 | #include "pycore_object.h" // _PyType_HasFeature() |
10 | #include "pycore_pyerrors.h" // _PyErr_Occurred() |
11 | #include "pycore_pystate.h" // _PyThreadState_GET() |
12 | #include "pycore_typeobject.h" // struct type_cache |
13 | #include "pycore_unionobject.h" // _Py_union_type_or |
14 | #include "pycore_frame.h" // _PyInterpreterFrame |
15 | #include "opcode.h" // MAKE_CELL |
16 | #include "structmember.h" // PyMemberDef |
17 | |
18 | #include <ctype.h> |
19 | |
20 | /*[clinic input] |
21 | class type "PyTypeObject *" "&PyType_Type" |
22 | class object "PyObject *" "&PyBaseObject_Type" |
23 | [clinic start generated code]*/ |
24 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b94608d231c434b]*/ |
25 | |
26 | #include "clinic/typeobject.c.h" |
27 | |
28 | /* Support type attribute lookup cache */ |
29 | |
30 | /* The cache can keep references to the names alive for longer than |
31 | they normally would. This is why the maximum size is limited to |
32 | MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large |
33 | strings are used as attribute names. */ |
34 | #define MCACHE_MAX_ATTR_SIZE 100 |
35 | #define MCACHE_HASH(version, name_hash) \ |
36 | (((unsigned int)(version) ^ (unsigned int)(name_hash)) \ |
37 | & ((1 << MCACHE_SIZE_EXP) - 1)) |
38 | |
39 | #define MCACHE_HASH_METHOD(type, name) \ |
40 | MCACHE_HASH((type)->tp_version_tag, ((Py_ssize_t)(name)) >> 3) |
41 | #define MCACHE_CACHEABLE_NAME(name) \ |
42 | PyUnicode_CheckExact(name) && \ |
43 | PyUnicode_IS_READY5.78M (name) && \ |
44 | (5.78M PyUnicode_GET_LENGTH5.78M (name) <= MCACHE_MAX_ATTR_SIZE5.78M ) |
45 | |
46 | // bpo-42745: next_version_tag remains shared by all interpreters because of static types |
47 | // Used to set PyTypeObject.tp_version_tag |
48 | static unsigned int next_version_tag = 1; |
49 | |
50 | typedef struct PySlot_Offset { |
51 | short subslot_offset; |
52 | short slot_offset; |
53 | } PySlot_Offset; |
54 | |
55 | |
56 | static PyObject * |
57 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
58 | |
59 | static void |
60 | clear_slotdefs(void); |
61 | |
62 | static PyObject * |
63 | lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound); |
64 | |
65 | static int |
66 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value); |
67 | |
68 | /* |
69 | * finds the beginning of the docstring's introspection signature. |
70 | * if present, returns a pointer pointing to the first '('. |
71 | * otherwise returns NULL. |
72 | * |
73 | * doesn't guarantee that the signature is valid, only that it |
74 | * has a valid prefix. (the signature must also pass skip_signature.) |
75 | */ |
76 | static const char * |
77 | find_signature(const char *name, const char *doc) |
78 | { |
79 | const char *dot; |
80 | size_t length; |
81 | |
82 | if (!doc) Branch (82:9): [True: 227, False: 35.3k]
|
83 | return NULL; |
84 | |
85 | assert(name != NULL); |
86 | |
87 | /* for dotted names like classes, only use the last component */ |
88 | dot = strrchr(name, '.'); |
89 | if (dot) Branch (89:9): [True: 15.1k, False: 20.1k]
|
90 | name = dot + 1; |
91 | |
92 | length = strlen(name); |
93 | if (strncmp(doc, name, length)) Branch (93:9): [True: 17.6k, False: 17.6k]
|
94 | return NULL; |
95 | doc += length; |
96 | if (*doc != '(') Branch (96:9): [True: 3.32k, False: 14.3k]
|
97 | return NULL; |
98 | return doc; |
99 | } |
100 | |
101 | #define SIGNATURE_END_MARKER ")\n--\n\n" |
102 | #define SIGNATURE_END_MARKER_LENGTH 6 |
103 | /* |
104 | * skips past the end of the docstring's introspection signature. |
105 | * (assumes doc starts with a valid signature prefix.) |
106 | */ |
107 | static const char * |
108 | skip_signature(const char *doc) |
109 | { |
110 | while (*doc) { Branch (110:12): [True: 610k, False: 545]
|
111 | if ((*doc == *SIGNATURE_END_MARKER) && Branch (111:13): [True: 19.3k, False: 590k]
|
112 | !strncmp(doc, 19.3k SIGNATURE_END_MARKER19.3k , SIGNATURE_END_MARKER_LENGTH19.3k )) Branch (112:13): [True: 11.1k, False: 8.19k]
|
113 | return doc + SIGNATURE_END_MARKER_LENGTH; |
114 | if ((*doc == '\n') && (doc[1] == '\n')8.56k ) Branch (114:13): [True: 8.56k, False: 590k]
Branch (114:31): [True: 2.69k, False: 5.87k]
|
115 | return NULL; |
116 | doc++; |
117 | } |
118 | return NULL; |
119 | } |
120 | |
121 | int |
122 | _PyType_CheckConsistency(PyTypeObject *type) |
123 | { |
124 | #define CHECK(expr) \ |
125 | do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG((PyObject *)type, Py_STRINGIFY(expr)); } } while (0) |
126 |
|
127 | CHECK(!_PyObject_IsFreed((PyObject *)type)); |
128 | |
129 | if (!(type->tp_flags & Py_TPFLAGS_READY)) { Branch (129:9): [True: 0, False: 0]
|
130 | /* don't check static types before PyType_Ready() */ |
131 | return 1; |
132 | } |
133 | |
134 | CHECK(Py_REFCNT(type) >= 1); |
135 | CHECK(PyType_Check(type)); |
136 | |
137 | CHECK(!(type->tp_flags & Py_TPFLAGS_READYING)); |
138 | CHECK(type->tp_dict != NULL); |
139 | |
140 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { Branch (140:9): [True: 0, False: 0]
|
141 | // bpo-44263: tp_traverse is required if Py_TPFLAGS_HAVE_GC is set. |
142 | // Note: tp_clear is optional. |
143 | CHECK(type->tp_traverse != NULL); |
144 | } |
145 | |
146 | if (type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION) { Branch (146:9): [True: 0, False: 0]
|
147 | CHECK(type->tp_new == NULL); |
148 | CHECK(PyDict_Contains(type->tp_dict, &_Py_ID(__new__)) == 0); |
149 | } |
150 | |
151 | return 1; |
152 | #undef CHECK |
153 | } |
154 | |
155 | static const char * |
156 | _PyType_DocWithoutSignature(const char *name, const char *internal_doc) |
157 | { |
158 | const char *doc = find_signature(name, internal_doc); |
159 | |
160 | if (doc) { Branch (160:9): [True: 12.3k, False: 21.0k]
|
161 | doc = skip_signature(doc); |
162 | if (doc) Branch (162:13): [True: 9.22k, False: 3.09k]
|
163 | return doc; |
164 | } |
165 | return internal_doc; |
166 | } |
167 | |
168 | PyObject * |
169 | _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc) |
170 | { |
171 | const char *doc = _PyType_DocWithoutSignature(name, internal_doc); |
172 | |
173 | if (!doc || *doc == '\0'10.1k ) { Branch (173:9): [True: 161, False: 10.1k]
Branch (173:17): [True: 101, False: 10.0k]
|
174 | Py_RETURN_NONE; |
175 | } |
176 | |
177 | return PyUnicode_FromString(doc); |
178 | } |
179 | |
180 | PyObject * |
181 | _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_doc) |
182 | { |
183 | const char *start = find_signature(name, internal_doc); |
184 | const char *end; |
185 | |
186 | if (start) Branch (186:9): [True: 2.02k, False: 165]
|
187 | end = skip_signature(start); |
188 | else |
189 | end = NULL; |
190 | if (!end) { Branch (190:9): [True: 304, False: 1.88k]
|
191 | Py_RETURN_NONE; |
192 | } |
193 | |
194 | /* back "end" up until it points just past the final ')' */ |
195 | end -= SIGNATURE_END_MARKER_LENGTH - 1; |
196 | assert((end - start) >= 2); /* should be "()" at least */ |
197 | assert(end[-1] == ')'); |
198 | assert(end[0] == '\n'); |
199 | return PyUnicode_FromStringAndSize(start, end - start); |
200 | } |
201 | |
202 | |
203 | static struct type_cache* |
204 | get_type_cache(void) |
205 | { |
206 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
207 | return &interp->type_cache; |
208 | } |
209 | |
210 | |
211 | static void |
212 | type_cache_clear(struct type_cache *cache, PyObject *value) |
213 | { |
214 | for (Py_ssize_t i = 0; i < (1 << MCACHE_SIZE_EXP); i++1.24M ) { Branch (214:28): [True: 1.24M, False: 303]
|
215 | struct type_cache_entry *entry = &cache->hashtable[i]; |
216 | entry->version = 0; |
217 | Py_XSETREF(entry->name, _Py_XNewRef(value)); |
218 | entry->value = NULL; |
219 | } |
220 | } |
221 | |
222 | |
223 | void |
224 | _PyType_InitCache(PyInterpreterState *interp) |
225 | { |
226 | struct type_cache *cache = &interp->type_cache; |
227 | for (Py_ssize_t i = 0; i < (1 << MCACHE_SIZE_EXP); i++1.13M ) { Branch (227:28): [True: 1.13M, False: 278]
|
228 | struct type_cache_entry *entry = &cache->hashtable[i]; |
229 | assert(entry->name == NULL); |
230 | |
231 | entry->version = 0; |
232 | // Set to None so _PyType_Lookup() can use Py_SETREF(), |
233 | // rather than using slower Py_XSETREF(). |
234 | entry->name = Py_NewRef(Py_None); |
235 | entry->value = NULL; |
236 | } |
237 | } |
238 | |
239 | |
240 | static unsigned int |
241 | _PyType_ClearCache(PyInterpreterState *interp) |
242 | { |
243 | struct type_cache *cache = &interp->type_cache; |
244 | #if MCACHE_STATS |
245 | size_t total = cache->hits + cache->collisions + cache->misses; |
246 | fprintf(stderr, "-- Method cache hits = %zd (%d%%)\n", |
247 | cache->hits, (int) (100.0 * cache->hits / total)); |
248 | fprintf(stderr, "-- Method cache true misses = %zd (%d%%)\n", |
249 | cache->misses, (int) (100.0 * cache->misses / total)); |
250 | fprintf(stderr, "-- Method cache collisions = %zd (%d%%)\n", |
251 | cache->collisions, (int) (100.0 * cache->collisions / total)); |
252 | fprintf(stderr, "-- Method cache size = %zd KiB\n", |
253 | sizeof(cache->hashtable) / 1024); |
254 | #endif |
255 | |
256 | // Set to None, rather than NULL, so _PyType_Lookup() can |
257 | // use Py_SETREF() rather than using slower Py_XSETREF(). |
258 | type_cache_clear(cache, Py_None); |
259 | |
260 | return next_version_tag - 1; |
261 | } |
262 | |
263 | |
264 | unsigned int |
265 | PyType_ClearCache(void) |
266 | { |
267 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
268 | return _PyType_ClearCache(interp); |
269 | } |
270 | |
271 | |
272 | void |
273 | _PyTypes_Fini(PyInterpreterState *interp) |
274 | { |
275 | struct type_cache *cache = &interp->type_cache; |
276 | type_cache_clear(cache, NULL); |
277 | if (_Py_IsMainInterpreter(interp)) { Branch (277:9): [True: 103, False: 169]
|
278 | clear_slotdefs(); |
279 | } |
280 | } |
281 | |
282 | |
283 | void |
284 | PyType_Modified(PyTypeObject *type) |
285 | { |
286 | /* Invalidate any cached data for the specified type and all |
287 | subclasses. This function is called after the base |
288 | classes, mro, or attributes of the type are altered. |
289 | |
290 | Invariants: |
291 | |
292 | - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type, |
293 | it must first be set on all super types. |
294 | |
295 | This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a |
296 | type (so it must first clear it on all subclasses). The |
297 | tp_version_tag value is meaningless unless this flag is set. |
298 | We don't assign new version tags eagerly, but only as |
299 | needed. |
300 | */ |
301 | if (!_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { Branch (301:9): [True: 526k, False: 190k]
|
302 | return; |
303 | } |
304 | |
305 | PyObject *subclasses = type->tp_subclasses; |
306 | if (subclasses != NULL) { Branch (306:9): [True: 8.57k, False: 181k]
|
307 | assert(PyDict_CheckExact(subclasses)); |
308 | |
309 | Py_ssize_t i = 0; |
310 | PyObject *ref; |
311 | while (PyDict_Next(subclasses, &i, NULL, &ref)) { Branch (311:16): [True: 20.7k, False: 8.57k]
|
312 | assert(PyWeakref_CheckRef(ref)); |
313 | PyObject *obj = PyWeakref_GET_OBJECT(ref); |
314 | if (obj == Py_None) { Branch (314:17): [True: 18.7k, False: 2.03k]
|
315 | continue; |
316 | } |
317 | PyType_Modified(_PyType_CAST(obj)); |
318 | } |
319 | } |
320 | |
321 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
322 | type->tp_version_tag = 0; /* 0 is not a valid version tag */ |
323 | } |
324 | |
325 | static void |
326 | type_mro_modified(PyTypeObject *type, PyObject *bases) { |
327 | /* |
328 | Check that all base classes or elements of the MRO of type are |
329 | able to be cached. This function is called after the base |
330 | classes or mro of the type are altered. |
331 | |
332 | Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type |
333 | has a custom MRO that includes a type which is not officially |
334 | super type, or if the type implements its own mro() method. |
335 | |
336 | Called from mro_internal, which will subsequently be called on |
337 | each subclass when their mro is recursively updated. |
338 | */ |
339 | Py_ssize_t i, n; |
340 | int custom = !Py_IS_TYPE(type, &PyType_Type); |
341 | int unbound; |
342 | |
343 | if (custom) { Branch (343:9): [True: 34.8k, False: 195k]
|
344 | PyObject *mro_meth, *type_mro_meth; |
345 | mro_meth = lookup_maybe_method( |
346 | (PyObject *)type, &_Py_ID(mro), &unbound); |
347 | if (mro_meth == NULL) { Branch (347:13): [True: 0, False: 34.8k]
|
348 | goto clear; |
349 | } |
350 | type_mro_meth = lookup_maybe_method( |
351 | (PyObject *)&PyType_Type, &_Py_ID(mro), &unbound); |
352 | if (type_mro_meth == NULL) { Branch (352:13): [True: 0, False: 34.8k]
|
353 | Py_DECREF(mro_meth); |
354 | goto clear; |
355 | } |
356 | int custom_mro = (mro_meth != type_mro_meth); |
357 | Py_DECREF(mro_meth); |
358 | Py_DECREF(type_mro_meth); |
359 | if (custom_mro) { Branch (359:13): [True: 86, False: 34.7k]
|
360 | goto clear; |
361 | } |
362 | } |
363 | n = PyTuple_GET_SIZE(bases); |
364 | for (i = 0; i < n; i++550k ) { Branch (364:17): [True: 550k, False: 229k]
|
365 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
366 | PyTypeObject *cls = _PyType_CAST(b); |
367 | |
368 | if (!PyType_IsSubtype(type, cls)) { Branch (368:13): [True: 1, False: 550k]
|
369 | goto clear; |
370 | } |
371 | } |
372 | return; |
373 | clear: |
374 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; |
375 | type->tp_version_tag = 0; /* 0 is not a valid version tag */ |
376 | } |
377 | |
378 | static int |
379 | assign_version_tag(struct type_cache *cache, PyTypeObject *type) |
380 | { |
381 | /* Ensure that the tp_version_tag is valid and set |
382 | Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this |
383 | must first be done on all super classes. Return 0 if this |
384 | cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG. |
385 | */ |
386 | if (_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { Branch (386:9): [True: 5.80M, False: 195k]
|
387 | return 1; |
388 | } |
389 | if (!_PyType_HasFeature(type, Py_TPFLAGS_READY)) { Branch (389:9): [True: 0, False: 195k]
|
390 | return 0; |
391 | } |
392 | |
393 | if (next_version_tag == 0) { Branch (393:9): [True: 0, False: 195k]
|
394 | /* We have run out of version numbers */ |
395 | return 0; |
396 | } |
397 | type->tp_version_tag = next_version_tag++; |
398 | assert (type->tp_version_tag != 0); |
399 | |
400 | PyObject *bases = type->tp_bases; |
401 | Py_ssize_t n = PyTuple_GET_SIZE(bases); |
402 | for (Py_ssize_t i = 0; i < n; i++217k ) { Branch (402:28): [True: 217k, False: 195k]
|
403 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
404 | if (!assign_version_tag(cache, _PyType_CAST(b))) Branch (404:13): [True: 0, False: 217k]
|
405 | return 0; |
406 | } |
407 | type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG; |
408 | return 1; |
409 | } |
410 | |
411 | |
412 | static PyMemberDef type_members[] = { |
413 | {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
414 | {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
415 | {"__flags__", T_ULONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
416 | {"__weakrefoffset__", T_PYSSIZET, |
417 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
418 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
419 | {"__dictoffset__", T_PYSSIZET, |
420 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
421 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
422 | {0} |
423 | }; |
424 | |
425 | static int |
426 | check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) |
427 | { |
428 | if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) { Branch (428:9): [True: 2, False: 2.19k]
|
429 | PyErr_Format(PyExc_TypeError, |
430 | "cannot set '%s' attribute of immutable type '%s'", |
431 | name, type->tp_name); |
432 | return 0; |
433 | } |
434 | if (!value) { Branch (434:9): [True: 3, False: 2.19k]
|
435 | PyErr_Format(PyExc_TypeError, |
436 | "cannot delete '%s' attribute of immutable type '%s'", |
437 | name, type->tp_name); |
438 | return 0; |
439 | } |
440 | |
441 | if (PySys_Audit("object.__setattr__", "OsO", Branch (441:9): [True: 0, False: 2.19k]
|
442 | type, name, value) < 0) { |
443 | return 0; |
444 | } |
445 | |
446 | return 1; |
447 | } |
448 | |
449 | const char * |
450 | _PyType_Name(PyTypeObject *type) |
451 | { |
452 | assert(type->tp_name != NULL); |
453 | const char *s = strrchr(type->tp_name, '.'); |
454 | if (s == NULL) { Branch (454:9): [True: 535k, False: 34.3k]
|
455 | s = type->tp_name; |
456 | } |
457 | else { |
458 | s++; |
459 | } |
460 | return s; |
461 | } |
462 | |
463 | static PyObject * |
464 | type_name(PyTypeObject *type, void *context) |
465 | { |
466 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { Branch (466:9): [True: 543k, False: 533k]
|
467 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
468 | |
469 | Py_INCREF(et->ht_name); |
470 | return et->ht_name; |
471 | } |
472 | else { |
473 | return PyUnicode_FromString(_PyType_Name(type)); |
474 | } |
475 | } |
476 | |
477 | static PyObject * |
478 | type_qualname(PyTypeObject *type, void *context) |
479 | { |
480 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { Branch (480:9): [True: 245k, False: 24.7k]
|
481 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
482 | Py_INCREF(et->ht_qualname); |
483 | return et->ht_qualname; |
484 | } |
485 | else { |
486 | return PyUnicode_FromString(_PyType_Name(type)); |
487 | } |
488 | } |
489 | |
490 | static int |
491 | type_set_name(PyTypeObject *type, PyObject *value, void *context) |
492 | { |
493 | const char *tp_name; |
494 | Py_ssize_t name_size; |
495 | |
496 | if (!check_set_special_type_attr(type, value, "__name__")) Branch (496:9): [True: 0, False: 263]
|
497 | return -1; |
498 | if (!PyUnicode_Check(value)) { Branch (498:9): [True: 1, False: 262]
|
499 | PyErr_Format(PyExc_TypeError, |
500 | "can only assign string to %s.__name__, not '%s'", |
501 | type->tp_name, Py_TYPE(value)->tp_name); |
502 | return -1; |
503 | } |
504 | |
505 | tp_name = PyUnicode_AsUTF8AndSize(value, &name_size); |
506 | if (tp_name == NULL) Branch (506:9): [True: 1, False: 261]
|
507 | return -1; |
508 | if (strlen(tp_name) != (size_t)name_size) { Branch (508:9): [True: 1, False: 260]
|
509 | PyErr_SetString(PyExc_ValueError, |
510 | "type name must not contain null characters"); |
511 | return -1; |
512 | } |
513 | |
514 | type->tp_name = tp_name; |
515 | Py_INCREF(value); |
516 | Py_SETREF(((PyHeapTypeObject*)type)->ht_name, value); |
517 | |
518 | return 0; |
519 | } |
520 | |
521 | static int |
522 | type_set_qualname(PyTypeObject *type, PyObject *value, void *context) |
523 | { |
524 | PyHeapTypeObject* et; |
525 | |
526 | if (!check_set_special_type_attr(type, value, "__qualname__")) Branch (526:9): [True: 2, False: 290]
|
527 | return -1; |
528 | if (!PyUnicode_Check(value)) { Branch (528:9): [True: 1, False: 289]
|
529 | PyErr_Format(PyExc_TypeError, |
530 | "can only assign string to %s.__qualname__, not '%s'", |
531 | type->tp_name, Py_TYPE(value)->tp_name); |
532 | return -1; |
533 | } |
534 | |
535 | et = (PyHeapTypeObject*)type; |
536 | Py_INCREF(value); |
537 | Py_SETREF(et->ht_qualname, value); |
538 | return 0; |
539 | } |
540 | |
541 | static PyObject * |
542 | type_module(PyTypeObject *type, void *context) |
543 | { |
544 | PyObject *mod; |
545 | |
546 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { Branch (546:9): [True: 259k, False: 28.4k]
|
547 | mod = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__module__)); |
548 | if (mod == NULL) { Branch (548:13): [True: 0, False: 259k]
|
549 | if (!PyErr_Occurred()) { Branch (549:17): [True: 0, False: 0]
|
550 | PyErr_Format(PyExc_AttributeError, "__module__"); |
551 | } |
552 | return NULL; |
553 | } |
554 | Py_INCREF(mod); |
555 | } |
556 | else { |
557 | const char *s = strrchr(type->tp_name, '.'); |
558 | if (s != NULL) { Branch (558:13): [True: 14.0k, False: 14.3k]
|
559 | mod = PyUnicode_FromStringAndSize( |
560 | type->tp_name, (Py_ssize_t)(s - type->tp_name)); |
561 | if (mod != NULL) Branch (561:17): [True: 14.0k, False: 0]
|
562 | PyUnicode_InternInPlace(&mod); |
563 | } |
564 | else { |
565 | mod = &_Py_ID(builtins); |
566 | Py_INCREF(mod); |
567 | } |
568 | } |
569 | return mod; |
570 | } |
571 | |
572 | static int |
573 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
574 | { |
575 | if (!check_set_special_type_attr(type, value, "__module__")) Branch (575:9): [True: 0, False: 1.12k]
|
576 | return -1; |
577 | |
578 | PyType_Modified(type); |
579 | |
580 | return PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), value); |
581 | } |
582 | |
583 | static PyObject * |
584 | type_abstractmethods(PyTypeObject *type, void *context) |
585 | { |
586 | PyObject *mod = NULL; |
587 | /* type itself has an __abstractmethods__ descriptor (this). Don't return |
588 | that. */ |
589 | if (type != &PyType_Type) Branch (589:9): [True: 15.8k, False: 6]
|
590 | mod = PyDict_GetItemWithError(type->tp_dict, |
591 | &_Py_ID(__abstractmethods__)); |
592 | if (!mod) { Branch (592:9): [True: 5.08k, False: 10.7k]
|
593 | if (!PyErr_Occurred()) { Branch (593:13): [True: 5.08k, False: 0]
|
594 | PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__)); |
595 | } |
596 | return NULL; |
597 | } |
598 | Py_INCREF(mod); |
599 | return mod; |
600 | } |
601 | |
602 | static int |
603 | type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) |
604 | { |
605 | /* __abstractmethods__ should only be set once on a type, in |
606 | abc.ABCMeta.__new__, so this function doesn't do anything |
607 | special to update subclasses. |
608 | */ |
609 | int abstract, res; |
610 | if (value != NULL) { Branch (610:9): [True: 11.1k, False: 1]
|
611 | abstract = PyObject_IsTrue(value); |
612 | if (abstract < 0) Branch (612:13): [True: 0, False: 11.1k]
|
613 | return -1; |
614 | res = PyDict_SetItem(type->tp_dict, &_Py_ID(__abstractmethods__), value); |
615 | } |
616 | else { |
617 | abstract = 0; |
618 | res = PyDict_DelItem(type->tp_dict, &_Py_ID(__abstractmethods__)); |
619 | if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { Branch (619:13): [True: 1, False: 0]
Branch (619:20): [True: 1, False: 0]
|
620 | PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__)); |
621 | return -1; |
622 | } |
623 | } |
624 | if (res == 0) { Branch (624:9): [True: 11.1k, False: 0]
|
625 | PyType_Modified(type); |
626 | if (abstract) Branch (626:13): [True: 6.47k, False: 4.63k]
|
627 | type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; |
628 | else |
629 | type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; |
630 | } |
631 | return res; |
632 | } |
633 | |
634 | static PyObject * |
635 | type_get_bases(PyTypeObject *type, void *context) |
636 | { |
637 | Py_INCREF(type->tp_bases); |
638 | return type->tp_bases; |
639 | } |
640 | |
641 | static PyTypeObject *best_base(PyObject *); |
642 | static int mro_internal(PyTypeObject *, PyObject **); |
643 | static int type_is_subtype_base_chain(PyTypeObject *, PyTypeObject *); |
644 | static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, const char *); |
645 | static int add_subclass(PyTypeObject*, PyTypeObject*); |
646 | static int add_all_subclasses(PyTypeObject *type, PyObject *bases); |
647 | static void remove_subclass(PyTypeObject *, PyTypeObject *); |
648 | static void remove_all_subclasses(PyTypeObject *type, PyObject *bases); |
649 | static void update_all_slots(PyTypeObject *); |
650 | |
651 | typedef int (*update_callback)(PyTypeObject *, void *); |
652 | static int update_subclasses(PyTypeObject *type, PyObject *attr_name, |
653 | update_callback callback, void *data); |
654 | static int recurse_down_subclasses(PyTypeObject *type, PyObject *name, |
655 | update_callback callback, void *data); |
656 | |
657 | static int |
658 | mro_hierarchy(PyTypeObject *type, PyObject *temp) |
659 | { |
660 | PyObject *old_mro; |
661 | int res = mro_internal(type, &old_mro); |
662 | if (res <= 0) { Branch (662:9): [True: 21, False: 142]
|
663 | /* error / reentrance */ |
664 | return res; |
665 | } |
666 | PyObject *new_mro = type->tp_mro; |
667 | |
668 | PyObject *tuple; |
669 | if (old_mro != NULL) { Branch (669:9): [True: 141, False: 1]
|
670 | tuple = PyTuple_Pack(3, type, new_mro, old_mro); |
671 | } |
672 | else { |
673 | tuple = PyTuple_Pack(2, type, new_mro); |
674 | } |
675 | |
676 | if (tuple != NULL) { Branch (676:9): [True: 142, False: 0]
|
677 | res = PyList_Append(temp, tuple); |
678 | } |
679 | else { |
680 | res = -1; |
681 | } |
682 | Py_XDECREF(tuple); |
683 | |
684 | if (res < 0) { Branch (684:9): [True: 0, False: 142]
|
685 | type->tp_mro = old_mro; |
686 | Py_DECREF(new_mro); |
687 | return -1; |
688 | } |
689 | Py_XDECREF(old_mro); |
690 | |
691 | // Avoid creating an empty list if there is no subclass |
692 | if (type->tp_subclasses != NULL) { Branch (692:9): [True: 40, False: 102]
|
693 | /* Obtain a copy of subclasses list to iterate over. |
694 | |
695 | Otherwise type->tp_subclasses might be altered |
696 | in the middle of the loop, for example, through a custom mro(), |
697 | by invoking type_set_bases on some subclass of the type |
698 | which in turn calls remove_subclass/add_subclass on this type. |
699 | |
700 | Finally, this makes things simple avoiding the need to deal |
701 | with dictionary iterators and weak references. |
702 | */ |
703 | PyObject *subclasses = _PyType_GetSubclasses(type); |
704 | if (subclasses == NULL) { Branch (704:13): [True: 0, False: 40]
|
705 | return -1; |
706 | } |
707 | |
708 | Py_ssize_t n = PyList_GET_SIZE(subclasses); |
709 | for (Py_ssize_t i = 0; i < n; i++108 ) { Branch (709:32): [True: 110, False: 38]
|
710 | PyTypeObject *subclass = _PyType_CAST(PyList_GET_ITEM(subclasses, i)); |
711 | res = mro_hierarchy(subclass, temp); |
712 | if (res < 0) { Branch (712:17): [True: 2, False: 108]
|
713 | break; |
714 | } |
715 | } |
716 | Py_DECREF(subclasses); |
717 | } |
718 | |
719 | return res; |
720 | } |
721 | |
722 | static int |
723 | type_set_bases(PyTypeObject *type, PyObject *new_bases, void *context) |
724 | { |
725 | // Check arguments |
726 | if (!check_set_special_type_attr(type, new_bases, "__bases__")) { Branch (726:9): [True: 1, False: 64]
|
727 | return -1; |
728 | } |
729 | assert(new_bases != NULL); |
730 | |
731 | if (!PyTuple_Check(new_bases)) { Branch (731:9): [True: 0, False: 64]
|
732 | PyErr_Format(PyExc_TypeError, |
733 | "can only assign tuple to %s.__bases__, not %s", |
734 | type->tp_name, Py_TYPE(new_bases)->tp_name); |
735 | return -1; |
736 | } |
737 | if (PyTuple_GET_SIZE(new_bases) == 0) { Branch (737:9): [True: 1, False: 63]
|
738 | PyErr_Format(PyExc_TypeError, |
739 | "can only assign non-empty tuple to %s.__bases__, not ()", |
740 | type->tp_name); |
741 | return -1; |
742 | } |
743 | Py_ssize_t n = PyTuple_GET_SIZE(new_bases); |
744 | for (Py_ssize_t i = 0; i < n; i++74 ) { Branch (744:28): [True: 77, False: 60]
|
745 | PyObject *ob = PyTuple_GET_ITEM(new_bases, i); |
746 | if (!PyType_Check(ob)) { Branch (746:13): [True: 0, False: 77]
|
747 | PyErr_Format(PyExc_TypeError, |
748 | "%s.__bases__ must be tuple of classes, not '%s'", |
749 | type->tp_name, Py_TYPE(ob)->tp_name); |
750 | return -1; |
751 | } |
752 | PyTypeObject *base = (PyTypeObject*)ob; |
753 | |
754 | if (PyType_IsSubtype(base, type) || Branch (754:13): [True: 2, False: 75]
|
755 | /* In case of reentering here again through a custom mro() |
756 | the above check is not enough since it relies on |
757 | base->tp_mro which would gonna be updated inside |
758 | mro_internal only upon returning from the mro(). |
759 | |
760 | However, base->tp_base has already been assigned (see |
761 | below), which in turn may cause an inheritance cycle |
762 | through tp_base chain. And this is definitely |
763 | not what you want to ever happen. */ |
764 | (75 base->tp_mro != NULL75 && type_is_subtype_base_chain(base, type)75 )) Branch (764:14): [True: 75, False: 0]
Branch (764:38): [True: 1, False: 74]
|
765 | { |
766 | PyErr_SetString(PyExc_TypeError, |
767 | "a __bases__ item causes an inheritance cycle"); |
768 | return -1; |
769 | } |
770 | } |
771 | |
772 | // Compute the new MRO and the new base class |
773 | PyTypeObject *new_base = best_base(new_bases); |
774 | if (new_base == NULL) Branch (774:9): [True: 5, False: 55]
|
775 | return -1; |
776 | |
777 | if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) { Branch (777:9): [True: 2, False: 53]
|
778 | return -1; |
779 | } |
780 | |
781 | PyObject *old_bases = type->tp_bases; |
782 | assert(old_bases != NULL); |
783 | PyTypeObject *old_base = type->tp_base; |
784 | |
785 | type->tp_bases = Py_NewRef(new_bases); |
786 | type->tp_base = (PyTypeObject *)Py_NewRef(new_base); |
787 | |
788 | PyObject *temp = PyList_New(0); |
789 | if (temp == NULL) { Branch (789:9): [True: 0, False: 53]
|
790 | goto bail; |
791 | } |
792 | if (mro_hierarchy(type, temp) < 0) { Branch (792:9): [True: 7, False: 46]
|
793 | goto undo; |
794 | } |
795 | Py_DECREF(temp); |
796 | |
797 | /* Take no action in case if type->tp_bases has been replaced |
798 | through reentrance. */ |
799 | int res; |
800 | if (type->tp_bases == new_bases) { Branch (800:9): [True: 45, False: 1]
|
801 | /* any base that was in __bases__ but now isn't, we |
802 | need to remove |type| from its tp_subclasses. |
803 | conversely, any class now in __bases__ that wasn't |
804 | needs to have |type| added to its subclasses. */ |
805 | |
806 | /* for now, sod that: just remove from all old_bases, |
807 | add to all new_bases */ |
808 | remove_all_subclasses(type, old_bases); |
809 | res = add_all_subclasses(type, new_bases); |
810 | update_all_slots(type); |
811 | } |
812 | else { |
813 | res = 0; |
814 | } |
815 | |
816 | Py_DECREF(old_bases); |
817 | Py_DECREF(old_base); |
818 | |
819 | assert(_PyType_CheckConsistency(type)); |
820 | return res; |
821 | |
822 | undo: |
823 | n = PyList_GET_SIZE(temp); |
824 | for (Py_ssize_t i = n - 1; i >= 0; i--3 ) { Branch (824:32): [True: 3, False: 7]
|
825 | PyTypeObject *cls; |
826 | PyObject *new_mro, *old_mro = NULL; |
827 | |
828 | PyArg_UnpackTuple(PyList_GET_ITEM(temp, i), |
829 | "", 2, 3, &cls, &new_mro, &old_mro); |
830 | /* Do not rollback if cls has a newer version of MRO. */ |
831 | if (cls->tp_mro == new_mro) { Branch (831:13): [True: 3, False: 0]
|
832 | Py_XINCREF(old_mro); |
833 | cls->tp_mro = old_mro; |
834 | Py_DECREF(new_mro); |
835 | } |
836 | } |
837 | Py_DECREF(temp); |
838 | |
839 | bail: |
840 | if (type->tp_bases == new_bases) { Branch (840:9): [True: 6, False: 1]
|
841 | assert(type->tp_base == new_base); |
842 | |
843 | type->tp_bases = old_bases; |
844 | type->tp_base = old_base; |
845 | |
846 | Py_DECREF(new_bases); |
847 | Py_DECREF(new_base); |
848 | } |
849 | else { |
850 | Py_DECREF(old_bases); |
851 | Py_DECREF(old_base); |
852 | } |
853 | |
854 | assert(_PyType_CheckConsistency(type)); |
855 | return -1; |
856 | } |
857 | |
858 | static PyObject * |
859 | type_dict(PyTypeObject *type, void *context) |
860 | { |
861 | if (type->tp_dict == NULL) { Branch (861:9): [True: 0, False: 330k]
|
862 | Py_RETURN_NONE; |
863 | } |
864 | return PyDictProxy_New(type->tp_dict); |
865 | } |
866 | |
867 | static PyObject * |
868 | type_get_doc(PyTypeObject *type, void *context) |
869 | { |
870 | PyObject *result; |
871 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL4.44k ) { Branch (871:9): [True: 4.44k, False: 11.9k]
Branch (871:52): [True: 3.92k, False: 521]
|
872 | return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc); |
873 | } |
874 | result = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__doc__)); |
875 | if (result == NULL) { Branch (875:9): [True: 0, False: 12.4k]
|
876 | if (!PyErr_Occurred()) { Branch (876:13): [True: 0, False: 0]
|
877 | result = Py_None; |
878 | Py_INCREF(result); |
879 | } |
880 | } |
881 | else if (Py_TYPE(result)->tp_descr_get) { Branch (881:14): [True: 525, False: 11.9k]
|
882 | result = Py_TYPE(result)->tp_descr_get(result, NULL, |
883 | (PyObject *)type); |
884 | } |
885 | else { |
886 | Py_INCREF(result); |
887 | } |
888 | return result; |
889 | } |
890 | |
891 | static PyObject * |
892 | type_get_text_signature(PyTypeObject *type, void *context) |
893 | { |
894 | return _PyType_GetTextSignatureFromInternalDoc(type->tp_name, type->tp_doc); |
895 | } |
896 | |
897 | static int |
898 | type_set_doc(PyTypeObject *type, PyObject *value, void *context) |
899 | { |
900 | if (!check_set_special_type_attr(type, value, "__doc__")) Branch (900:9): [True: 2, False: 450]
|
901 | return -1; |
902 | PyType_Modified(type); |
903 | return PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), value); |
904 | } |
905 | |
906 | static PyObject * |
907 | type_get_annotations(PyTypeObject *type, void *context) |
908 | { |
909 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { Branch (909:9): [True: 2.04k, False: 668]
|
910 | PyErr_Format(PyExc_AttributeError, "type object '%s' has no attribute '__annotations__'", type->tp_name); |
911 | return NULL; |
912 | } |
913 | |
914 | PyObject *annotations; |
915 | /* there's no _PyDict_GetItemId without WithError, so let's LBYL. */ |
916 | if (PyDict_Contains(type->tp_dict, &_Py_ID(__annotations__))) { Branch (916:9): [True: 326, False: 342]
|
917 | annotations = PyDict_GetItemWithError( |
918 | type->tp_dict, &_Py_ID(__annotations__)); |
919 | /* |
920 | ** PyDict_GetItemWithError could still fail, |
921 | ** for instance with a well-timed Ctrl-C or a MemoryError. |
922 | ** so let's be totally safe. |
923 | */ |
924 | if (annotations) { Branch (924:13): [True: 326, False: 0]
|
925 | if (Py_TYPE(annotations)->tp_descr_get) { Branch (925:17): [True: 0, False: 326]
|
926 | annotations = Py_TYPE(annotations)->tp_descr_get( |
927 | annotations, NULL, (PyObject *)type); |
928 | } else { |
929 | Py_INCREF(annotations); |
930 | } |
931 | } |
932 | } else { |
933 | annotations = PyDict_New(); |
934 | if (annotations) { Branch (934:13): [True: 342, False: 0]
|
935 | int result = PyDict_SetItem( |
936 | type->tp_dict, &_Py_ID(__annotations__), annotations); |
937 | if (result) { Branch (937:17): [True: 0, False: 342]
|
938 | Py_CLEAR(annotations); |
939 | } else { |
940 | PyType_Modified(type); |
941 | } |
942 | } |
943 | } |
944 | return annotations; |
945 | } |
946 | |
947 | static int |
948 | type_set_annotations(PyTypeObject *type, PyObject *value, void *context) |
949 | { |
950 | if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) { Branch (950:9): [True: 0, False: 96]
|
951 | PyErr_Format(PyExc_TypeError, |
952 | "cannot set '__annotations__' attribute of immutable type '%s'", |
953 | type->tp_name); |
954 | return -1; |
955 | } |
956 | |
957 | int result; |
958 | if (value != NULL) { Branch (958:9): [True: 87, False: 9]
|
959 | /* set */ |
960 | result = PyDict_SetItem(type->tp_dict, &_Py_ID(__annotations__), value); |
961 | } else { |
962 | /* delete */ |
963 | if (!PyDict_Contains(type->tp_dict, &_Py_ID(__annotations__))) { Branch (963:13): [True: 1, False: 8]
|
964 | PyErr_Format(PyExc_AttributeError, "__annotations__"); |
965 | return -1; |
966 | } |
967 | result = PyDict_DelItem(type->tp_dict, &_Py_ID(__annotations__)); |
968 | } |
969 | |
970 | if (result == 0) { Branch (970:9): [True: 95, False: 0]
|
971 | PyType_Modified(type); |
972 | } |
973 | return result; |
974 | } |
975 | |
976 | |
977 | /*[clinic input] |
978 | type.__instancecheck__ -> bool |
979 | |
980 | instance: object |
981 | / |
982 | |
983 | Check if an object is an instance. |
984 | [clinic start generated code]*/ |
985 | |
986 | static int |
987 | type___instancecheck___impl(PyTypeObject *self, PyObject *instance) |
988 | /*[clinic end generated code: output=08b6bf5f591c3618 input=cdbfeaee82c01a0f]*/ |
989 | { |
990 | return _PyObject_RealIsInstance(instance, (PyObject *)self); |
991 | } |
992 | |
993 | /*[clinic input] |
994 | type.__subclasscheck__ -> bool |
995 | |
996 | subclass: object |
997 | / |
998 | |
999 | Check if a class is a subclass. |
1000 | [clinic start generated code]*/ |
1001 | |
1002 | static int |
1003 | type___subclasscheck___impl(PyTypeObject *self, PyObject *subclass) |
1004 | /*[clinic end generated code: output=97a4e51694500941 input=071b2ca9e03355f4]*/ |
1005 | { |
1006 | return _PyObject_RealIsSubclass(subclass, (PyObject *)self); |
1007 | } |
1008 | |
1009 | |
1010 | static PyGetSetDef type_getsets[] = { |
1011 | {"__name__", (getter)type_name, (setter)type_set_name, NULL}, |
1012 | {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL}, |
1013 | {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL}, |
1014 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
1015 | {"__abstractmethods__", (getter)type_abstractmethods, |
1016 | (setter)type_set_abstractmethods, NULL}, |
1017 | {"__dict__", (getter)type_dict, NULL, NULL}, |
1018 | {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, |
1019 | {"__text_signature__", (getter)type_get_text_signature, NULL, NULL}, |
1020 | {"__annotations__", (getter)type_get_annotations, (setter)type_set_annotations, NULL}, |
1021 | {0} |
1022 | }; |
1023 | |
1024 | static PyObject * |
1025 | type_repr(PyTypeObject *type) |
1026 | { |
1027 | if (type->tp_name == NULL) { Branch (1027:9): [True: 0, False: 37.2k]
|
1028 | // type_repr() called before the type is fully initialized |
1029 | // by PyType_Ready(). |
1030 | return PyUnicode_FromFormat("<class at %p>", type); |
1031 | } |
1032 | |
1033 | PyObject *mod, *name, *rtn; |
1034 | |
1035 | mod = type_module(type, NULL); |
1036 | if (mod == NULL) Branch (1036:9): [True: 0, False: 37.2k]
|
1037 | PyErr_Clear(); |
1038 | else if (!PyUnicode_Check(mod)) { Branch (1038:14): [True: 0, False: 37.2k]
|
1039 | Py_DECREF(mod); |
1040 | mod = NULL; |
1041 | } |
1042 | name = type_qualname(type, NULL); |
1043 | if (name == NULL) { Branch (1043:9): [True: 0, False: 37.2k]
|
1044 | Py_XDECREF(mod); |
1045 | return NULL; |
1046 | } |
1047 | |
1048 | if (mod != NULL && !_PyUnicode_Equal(mod, &_Py_ID(builtins))) Branch (1048:9): [True: 37.2k, False: 0]
Branch (1048:24): [True: 36.0k, False: 1.19k]
|
1049 | rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); |
1050 | else |
1051 | rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); |
1052 | |
1053 | Py_XDECREF(mod); |
1054 | Py_DECREF(name); |
1055 | return rtn; |
1056 | } |
1057 | |
1058 | static PyObject * |
1059 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
1060 | { |
1061 | PyObject *obj; |
1062 | PyThreadState *tstate = _PyThreadState_GET(); |
1063 | |
1064 | #ifdef Py_DEBUG |
1065 | /* type_call() must not be called with an exception set, |
1066 | because it can clear it (directly or indirectly) and so the |
1067 | caller loses its exception */ |
1068 | assert(!_PyErr_Occurred(tstate)); |
1069 | #endif |
1070 | |
1071 | /* Special case: type(x) should return Py_TYPE(x) */ |
1072 | /* We only want type itself to accept the one-argument form (#27157) */ |
1073 | if (type == &PyType_Type) { Branch (1073:9): [True: 69.6k, False: 27.6M]
|
1074 | assert(args != NULL && PyTuple_Check(args)); |
1075 | assert(kwds == NULL || PyDict_Check(kwds)); |
1076 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
1077 | |
1078 | if (nargs == 1 && (0 kwds == NULL0 || !0 PyDict_GET_SIZE0 (kwds))) { Branch (1078:13): [True: 0, False: 69.6k]
Branch (1078:28): [True: 0, False: 0]
Branch (1078:44): [True: 0, False: 0]
|
1079 | obj = (PyObject *) Py_TYPE(PyTuple_GET_ITEM(args, 0)); |
1080 | Py_INCREF(obj); |
1081 | return obj; |
1082 | } |
1083 | |
1084 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
1085 | arguments. But PyArg_ParseTuple in type_new may give |
1086 | a msg saying type() needs exactly 3. */ |
1087 | if (nargs != 3) { Branch (1087:13): [True: 8, False: 69.6k]
|
1088 | PyErr_SetString(PyExc_TypeError, |
1089 | "type() takes 1 or 3 arguments"); |
1090 | return NULL; |
1091 | } |
1092 | } |
1093 | |
1094 | if (type->tp_new == NULL) { Branch (1094:9): [True: 53, False: 27.7M]
|
1095 | _PyErr_Format(tstate, PyExc_TypeError, |
1096 | "cannot create '%s' instances", type->tp_name); |
1097 | return NULL; |
1098 | } |
1099 | |
1100 | obj = type->tp_new(type, args, kwds); |
1101 | obj = _Py_CheckFunctionResult(tstate, (PyObject*)type, obj, NULL); |
1102 | if (obj == NULL) Branch (1102:9): [True: 49.5k, False: 27.6M]
|
1103 | return NULL; |
1104 | |
1105 | /* If the returned object is not an instance of type, |
1106 | it won't be initialized. */ |
1107 | if (!PyObject_TypeCheck(obj, type)) Branch (1107:9): [True: 167, False: 27.6M]
|
1108 | return obj; |
1109 | |
1110 | type = Py_TYPE(obj); |
1111 | if (type->tp_init != NULL) { Branch (1111:9): [True: 27.6M, False: 1]
|
1112 | int res = type->tp_init(obj, args, kwds); |
1113 | if (res < 0) { Branch (1113:13): [True: 43.5k, False: 27.6M]
|
1114 | assert(_PyErr_Occurred(tstate)); |
1115 | Py_DECREF(obj); |
1116 | obj = NULL; |
1117 | } |
1118 | else { |
1119 | assert(!_PyErr_Occurred(tstate)); |
1120 | } |
1121 | } |
1122 | return obj; |
1123 | } |
1124 | |
1125 | PyObject * |
1126 | _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems) |
1127 | { |
1128 | PyObject *obj; |
1129 | const size_t size = _PyObject_VAR_SIZE(type, nitems+1); |
1130 | /* note that we need to add one, for the sentinel */ |
1131 | |
1132 | const size_t presize = _PyType_PreHeaderSize(type); |
1133 | char *alloc = PyObject_Malloc(size + presize); |
1134 | if (alloc == NULL) { Branch (1134:9): [True: 0, False: 31.3M]
|
1135 | return PyErr_NoMemory(); |
1136 | } |
1137 | obj = (PyObject *)(alloc + presize); |
1138 | if (presize) { Branch (1138:9): [True: 30.3M, False: 1.01M]
|
1139 | ((PyObject **)alloc)[0] = NULL; |
1140 | ((PyObject **)alloc)[1] = NULL; |
1141 | _PyObject_GC_Link(obj); |
1142 | } |
1143 | memset(obj, '\0', size); |
1144 | |
1145 | if (type->tp_itemsize == 0) { Branch (1145:9): [True: 30.1M, False: 1.16M]
|
1146 | _PyObject_Init(obj, type); |
1147 | } |
1148 | else { |
1149 | _PyObject_InitVar((PyVarObject *)obj, type, nitems); |
1150 | } |
1151 | return obj; |
1152 | } |
1153 | |
1154 | PyObject * |
1155 | PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
1156 | { |
1157 | PyObject *obj = _PyType_AllocNoTrack(type, nitems); |
1158 | if (obj == NULL) { Branch (1158:9): [True: 0, False: 31.2M]
|
1159 | return NULL; |
1160 | } |
1161 | |
1162 | if (_PyType_IS_GC(type)) { |
1163 | _PyObject_GC_TRACK(obj); |
1164 | } |
1165 | return obj; |
1166 | } |
1167 | |
1168 | PyObject * |
1169 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
1170 | { |
1171 | return type->tp_alloc(type, 0); |
1172 | } |
1173 | |
1174 | /* Helpers for subtyping */ |
1175 | |
1176 | static int |
1177 | traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) |
1178 | { |
1179 | Py_ssize_t i, n; |
1180 | PyMemberDef *mp; |
1181 | |
1182 | n = Py_SIZE(type); |
1183 | mp = _PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
1184 | for (i = 0; i < n; i++, mp++79.9M ) { Branch (1184:17): [True: 79.9M, False: 20.2M]
|
1185 | if (mp->type == T_OBJECT_EX) { Branch (1185:13): [True: 79.9M, False: 0]
|
1186 | char *addr = (char *)self + mp->offset; |
1187 | PyObject *obj = *(PyObject **)addr; |
1188 | if (obj != NULL) { Branch (1188:17): [True: 72.5M, False: 7.41M]
|
1189 | int err = visit(obj, arg); |
1190 | if (err) Branch (1190:21): [True: 0, False: 72.5M]
|
1191 | return err; |
1192 | } |
1193 | } |
1194 | } |
1195 | return 0; |
1196 | } |
1197 | |
1198 | static int |
1199 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
1200 | { |
1201 | PyTypeObject *type, *base; |
1202 | traverseproc basetraverse; |
1203 | |
1204 | /* Find the nearest base with a different tp_traverse, |
1205 | and traverse slots while we're at it */ |
1206 | type = Py_TYPE(self); |
1207 | base = type; |
1208 | while ((basetraverse = base->tp_traverse) == subtype_traverse) { Branch (1208:12): [True: 465M, False: 299M]
|
1209 | if (Py_SIZE(base)) { |
1210 | int err = traverse_slots(base, self, visit, arg); |
1211 | if (err) Branch (1211:17): [True: 0, False: 20.2M]
|
1212 | return err; |
1213 | } |
1214 | base = base->tp_base; |
1215 | assert(base); |
1216 | } |
1217 | |
1218 | if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) { Branch (1218:9): [True: 236M, False: 62.6M]
|
1219 | assert(type->tp_dictoffset); |
1220 | int err = _PyObject_VisitInstanceAttributes(self, visit, arg); |
1221 | if (err) { Branch (1221:13): [True: 0, False: 236M]
|
1222 | return err; |
1223 | } |
1224 | } |
1225 | |
1226 | if (type->tp_dictoffset != base->tp_dictoffset) { Branch (1226:9): [True: 256M, False: 42.3M]
|
1227 | PyObject **dictptr = _PyObject_DictPointer(self); |
1228 | if (dictptr && *dictptr) Branch (1228:13): [True: 256M, False: 0]
Branch (1228:24): [True: 37.7M, False: 219M]
|
1229 | Py_VISIT(*dictptr); |
1230 | } |
1231 | |
1232 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE Branch (1232:9): [True: 299M, False: 0]
|
1233 | && (!basetraverse || !(base->tp_flags & 37.2M Py_TPFLAGS_HEAPTYPE37.2M ))) { Branch (1233:13): [True: 262M, False: 37.2M]
Branch (1233:30): [True: 32.0M, False: 5.21M]
|
1234 | /* For a heaptype, the instances count as references |
1235 | to the type. Traverse the type so the collector |
1236 | can find cycles involving this link. |
1237 | Skip this visit if basetraverse belongs to a heap type: in that |
1238 | case, basetraverse will visit the type when we call it later. |
1239 | */ |
1240 | Py_VISIT(type); |
1241 | } |
1242 | |
1243 | if (basetraverse) Branch (1243:9): [True: 37.2M, False: 262M]
|
1244 | return basetraverse(self, visit, arg); |
1245 | return 0; |
1246 | } |
1247 | |
1248 | static void |
1249 | clear_slots(PyTypeObject *type, PyObject *self) |
1250 | { |
1251 | Py_ssize_t i, n; |
1252 | PyMemberDef *mp; |
1253 | |
1254 | n = Py_SIZE(type); |
1255 | mp = _PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type); |
1256 | for (i = 0; i < n; i++, mp++6.67M ) { Branch (1256:17): [True: 6.67M, False: 2.15M]
|
1257 | if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) { Branch (1257:13): [True: 6.67M, False: 0]
Branch (1257:40): [True: 6.67M, False: 0]
|
1258 | char *addr = (char *)self + mp->offset; |
1259 | PyObject *obj = *(PyObject **)addr; |
1260 | if (obj != NULL) { Branch (1260:17): [True: 6.20M, False: 468k]
|
1261 | *(PyObject **)addr = NULL; |
1262 | Py_DECREF(obj); |
1263 | } |
1264 | } |
1265 | } |
1266 | } |
1267 | |
1268 | static int |
1269 | subtype_clear(PyObject *self) |
1270 | { |
1271 | PyTypeObject *type, *base; |
1272 | inquiry baseclear; |
1273 | |
1274 | /* Find the nearest base with a different tp_clear |
1275 | and clear slots while we're at it */ |
1276 | type = Py_TYPE(self); |
1277 | base = type; |
1278 | while ((baseclear = base->tp_clear) == subtype_clear) { Branch (1278:12): [True: 2.11M, False: 1.85M]
|
1279 | if (Py_SIZE(base)) |
1280 | clear_slots(base, self); |
1281 | base = base->tp_base; |
1282 | assert(base); |
1283 | } |
1284 | |
1285 | /* Clear the instance dict (if any), to break cycles involving only |
1286 | __dict__ slots (as in the case 'self.__dict__ is self'). */ |
1287 | if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) { Branch (1287:9): [True: 1.84M, False: 17.9k]
|
1288 | _PyObject_ClearInstanceAttributes(self); |
1289 | } |
1290 | if (type->tp_dictoffset != base->tp_dictoffset) { Branch (1290:9): [True: 1.84M, False: 17.3k]
|
1291 | PyObject **dictptr = _PyObject_DictPointer(self); |
1292 | if (dictptr && *dictptr) Branch (1292:13): [True: 1.84M, False: 0]
Branch (1292:24): [True: 45.5k, False: 1.79M]
|
1293 | Py_CLEAR(*dictptr); |
1294 | } |
1295 | |
1296 | if (baseclear) Branch (1296:9): [True: 18.6k, False: 1.84M]
|
1297 | return baseclear(self); |
1298 | return 0; |
1299 | } |
1300 | |
1301 | static void |
1302 | subtype_dealloc(PyObject *self) |
1303 | { |
1304 | PyTypeObject *type, *base; |
1305 | destructor basedealloc; |
1306 | int has_finalizer; |
1307 | |
1308 | /* Extract the type; we expect it to be a heap type */ |
1309 | type = Py_TYPE(self); |
1310 | _PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
1311 | |
1312 | /* Test whether the type has GC exactly once */ |
1313 | |
1314 | if (!_PyType_IS_GC(type)) { Branch (1314:9): [True: 263, False: 13.3M]
|
1315 | /* A non GC dynamic type allows certain simplifications: |
1316 | there's no need to call clear_slots(), or DECREF the dict, |
1317 | or clear weakrefs. */ |
1318 | |
1319 | /* Maybe call finalizer; exit early if resurrected */ |
1320 | if (type->tp_finalize) { Branch (1320:13): [True: 5, False: 258]
|
1321 | if (PyObject_CallFinalizerFromDealloc(self) < 0) Branch (1321:17): [True: 2, False: 3]
|
1322 | return; |
1323 | } |
1324 | if (type->tp_del) { Branch (1324:13): [True: 0, False: 261]
|
1325 | type->tp_del(self); |
1326 | if (Py_REFCNT(self) > 0) { Branch (1326:17): [True: 0, False: 0]
|
1327 | return; |
1328 | } |
1329 | } |
1330 | |
1331 | /* Find the nearest base with a different tp_dealloc */ |
1332 | base = type; |
1333 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { Branch (1333:16): [True: 264, False: 261]
|
1334 | base = base->tp_base; |
1335 | assert(base); |
1336 | } |
1337 | |
1338 | /* Extract the type again; tp_del may have changed it */ |
1339 | type = Py_TYPE(self); |
1340 | |
1341 | // Don't read type memory after calling basedealloc() since basedealloc() |
1342 | // can deallocate the type and free its memory. |
1343 | int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE Branch (1343:34): [True: 261, False: 0]
|
1344 | && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE)); Branch (1344:37): [True: 23, False: 238]
|
1345 | |
1346 | assert((type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0); |
1347 | |
1348 | /* Call the base tp_dealloc() */ |
1349 | assert(basedealloc); |
1350 | basedealloc(self); |
1351 | |
1352 | /* Can't reference self beyond this point. It's possible tp_del switched |
1353 | our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about |
1354 | reference counting. Only decref if the base type is not already a heap |
1355 | allocated type. Otherwise, basedealloc should have decref'd it already */ |
1356 | if (type_needs_decref) { Branch (1356:13): [True: 23, False: 238]
|
1357 | Py_DECREF(type); |
1358 | } |
1359 | |
1360 | /* Done */ |
1361 | return; |
1362 | } |
1363 | |
1364 | /* We get here only if the type has GC */ |
1365 | |
1366 | /* UnTrack and re-Track around the trashcan macro, alas */ |
1367 | /* See explanation at end of function for full disclosure */ |
1368 | PyObject_GC_UnTrack(self); |
1369 | Py_TRASHCAN_BEGIN(self, subtype_dealloc); |
1370 | |
1371 | /* Find the nearest base with a different tp_dealloc */ |
1372 | base = type; |
1373 | while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) { Branch (1373:12): [True: 21.9M, False: 13.3M]
|
1374 | base = base->tp_base; |
1375 | assert(base); |
1376 | } |
1377 | |
1378 | has_finalizer = type->tp_finalize || type->tp_del13.1M ; Branch (1378:21): [True: 274k, False: 13.1M]
Branch (1378:42): [True: 6, False: 13.1M]
|
1379 | |
1380 | if (type->tp_finalize) { Branch (1380:9): [True: 274k, False: 13.1M]
|
1381 | _PyObject_GC_TRACK(self); |
1382 | if (PyObject_CallFinalizerFromDealloc(self) < 0) { Branch (1382:13): [True: 41, False: 274k]
|
1383 | /* Resurrected */ |
1384 | goto endlabel; |
1385 | } |
1386 | _PyObject_GC_UNTRACK(self); |
1387 | } |
1388 | /* |
1389 | If we added a weaklist, we clear it. Do this *before* calling tp_del, |
1390 | clearing slots, or clearing the instance dict. |
1391 | |
1392 | GC tracking must be off at this point. weakref callbacks (if any, and |
1393 | whether directly here or indirectly in something we call) may trigger GC, |
1394 | and if self is tracked at that point, it will look like trash to GC and GC |
1395 | will try to delete self again. |
1396 | */ |
1397 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset10.4M ) { Branch (1397:9): [True: 10.4M, False: 2.90M]
Branch (1397:36): [True: 10.4M, False: 86.3k]
|
1398 | PyObject_ClearWeakRefs(self); |
1399 | } |
1400 | |
1401 | if (type->tp_del) { Branch (1401:9): [True: 11, False: 13.3M]
|
1402 | _PyObject_GC_TRACK(self); |
1403 | type->tp_del(self); |
1404 | if (Py_REFCNT(self) > 0) { Branch (1404:13): [True: 2, False: 9]
|
1405 | /* Resurrected */ |
1406 | goto endlabel; |
1407 | } |
1408 | _PyObject_GC_UNTRACK(self); |
1409 | } |
1410 | if (has_finalizer) { Branch (1410:9): [True: 274k, False: 13.1M]
|
1411 | /* New weakrefs could be created during the finalizer call. |
1412 | If this occurs, clear them out without calling their |
1413 | finalizers since they might rely on part of the object |
1414 | being finalized that has already been destroyed. */ |
1415 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset274k ) { Branch (1415:13): [True: 274k, False: 20]
Branch (1415:40): [True: 218k, False: 55.6k]
|
1416 | /* Modeled after GET_WEAKREFS_LISTPTR() */ |
1417 | PyWeakReference **list = (PyWeakReference **) \ |
1418 | _PyObject_GET_WEAKREFS_LISTPTR(self); |
1419 | while (*list) Branch (1419:20): [True: 0, False: 218k]
|
1420 | _PyWeakref_ClearRef(*list); |
1421 | } |
1422 | } |
1423 | |
1424 | /* Clear slots up to the nearest base with a different tp_dealloc */ |
1425 | base = type; |
1426 | while ((basedealloc = base->tp_dealloc) == subtype_dealloc) { Branch (1426:12): [True: 21.9M, False: 13.3M]
|
1427 | if (Py_SIZE(base)) |
1428 | clear_slots(base, self); |
1429 | base = base->tp_base; |
1430 | assert(base); |
1431 | } |
1432 | |
1433 | /* If we added a dict, DECREF it, or free inline values. */ |
1434 | if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) { Branch (1434:9): [True: 9.24M, False: 4.15M]
|
1435 | PyObject **dictptr = _PyObject_ManagedDictPointer(self); |
1436 | if (*dictptr != NULL) { Branch (1436:13): [True: 872k, False: 8.37M]
|
1437 | assert(*_PyObject_ValuesPointer(self) == NULL); |
1438 | Py_DECREF(*dictptr); |
1439 | *dictptr = NULL; |
1440 | } |
1441 | else { |
1442 | _PyObject_FreeInstanceAttributes(self); |
1443 | } |
1444 | } |
1445 | else if (type->tp_dictoffset && !base->tp_dictoffset1.77M ) { Branch (1445:14): [True: 1.77M, False: 2.37M]
Branch (1445:37): [True: 760k, False: 1.01M]
|
1446 | PyObject **dictptr = _PyObject_DictPointer(self); |
1447 | if (dictptr != NULL) { Branch (1447:13): [True: 760k, False: 0]
|
1448 | PyObject *dict = *dictptr; |
1449 | if (dict != NULL) { Branch (1449:17): [True: 257k, False: 502k]
|
1450 | Py_DECREF(dict); |
1451 | *dictptr = NULL; |
1452 | } |
1453 | } |
1454 | } |
1455 | |
1456 | /* Extract the type again; tp_del may have changed it */ |
1457 | type = Py_TYPE(self); |
1458 | |
1459 | /* Call the base tp_dealloc(); first retrack self if |
1460 | * basedealloc knows about gc. |
1461 | */ |
1462 | if (_PyType_IS_GC(base)) { |
1463 | _PyObject_GC_TRACK(self); |
1464 | } |
1465 | |
1466 | // Don't read type memory after calling basedealloc() since basedealloc() |
1467 | // can deallocate the type and free its memory. |
1468 | int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE Branch (1468:30): [True: 13.3M, False: 0]
|
1469 | && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE)); Branch (1469:33): [True: 12.5M, False: 856k]
|
1470 | |
1471 | assert(basedealloc); |
1472 | basedealloc(self); |
1473 | |
1474 | /* Can't reference self beyond this point. It's possible tp_del switched |
1475 | our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about |
1476 | reference counting. Only decref if the base type is not already a heap |
1477 | allocated type. Otherwise, basedealloc should have decref'd it already */ |
1478 | if (type_needs_decref) { Branch (1478:9): [True: 12.5M, False: 856k]
|
1479 | Py_DECREF(type); |
1480 | } |
1481 | |
1482 | endlabel: |
1483 | Py_TRASHCAN_END |
1484 | |
1485 | /* Explanation of the weirdness around the trashcan macros: |
1486 | |
1487 | Q. What do the trashcan macros do? |
1488 | |
1489 | A. Read the comment titled "Trashcan mechanism" in object.h. |
1490 | For one, this explains why there must be a call to GC-untrack |
1491 | before the trashcan begin macro. Without understanding the |
1492 | trashcan code, the answers to the following questions don't make |
1493 | sense. |
1494 | |
1495 | Q. Why do we GC-untrack before the trashcan and then immediately |
1496 | GC-track again afterward? |
1497 | |
1498 | A. In the case that the base class is GC-aware, the base class |
1499 | probably GC-untracks the object. If it does that using the |
1500 | UNTRACK macro, this will crash when the object is already |
1501 | untracked. Because we don't know what the base class does, the |
1502 | only safe thing is to make sure the object is tracked when we |
1503 | call the base class dealloc. But... The trashcan begin macro |
1504 | requires that the object is *untracked* before it is called. So |
1505 | the dance becomes: |
1506 | |
1507 | GC untrack |
1508 | trashcan begin |
1509 | GC track |
1510 | |
1511 | Q. Why did the last question say "immediately GC-track again"? |
1512 | It's nowhere near immediately. |
1513 | |
1514 | A. Because the code *used* to re-track immediately. Bad Idea. |
1515 | self has a refcount of 0, and if gc ever gets its hands on it |
1516 | (which can happen if any weakref callback gets invoked), it |
1517 | looks like trash to gc too, and gc also tries to delete self |
1518 | then. But we're already deleting self. Double deallocation is |
1519 | a subtle disaster. |
1520 | */ |
1521 | } |
1522 | |
1523 | static PyTypeObject *solid_base(PyTypeObject *type); |
1524 | |
1525 | /* type test with subclassing support */ |
1526 | |
1527 | static int |
1528 | type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b) |
1529 | { |
1530 | do { |
1531 | if (a == b) Branch (1531:13): [True: 20.4k, False: 47.3k]
|
1532 | return 1; |
1533 | a = a->tp_base; |
1534 | } while (a != NULL); Branch (1534:14): [True: 47.2k, False: 77]
|
1535 | |
1536 | return (b == &PyBaseObject_Type); |
1537 | } |
1538 | |
1539 | int |
1540 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
1541 | { |
1542 | PyObject *mro; |
1543 | |
1544 | mro = a->tp_mro; |
1545 | if (mro != NULL) { Branch (1545:9): [True: 93.1M, False: 20.4k]
|
1546 | /* Deal with multiple inheritance without recursion |
1547 | by walking the MRO tuple */ |
1548 | Py_ssize_t i, n; |
1549 | assert(PyTuple_Check(mro)); |
1550 | n = PyTuple_GET_SIZE(mro); |
1551 | for (i = 0; i < n; i++170M ) { Branch (1551:21): [True: 221M, False: 42.7M]
|
1552 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) Branch (1552:17): [True: 50.3M, False: 170M]
|
1553 | return 1; |
1554 | } |
1555 | return 0; |
1556 | } |
1557 | else |
1558 | /* a is not completely initialized yet; follow tp_base */ |
1559 | return type_is_subtype_base_chain(a, b); |
1560 | } |
1561 | |
1562 | /* Routines to do a method lookup in the type without looking in the |
1563 | instance dictionary (so we can't use PyObject_GetAttr) but still |
1564 | binding it to the instance. |
1565 | |
1566 | Variants: |
1567 | |
1568 | - _PyObject_LookupSpecial() returns NULL without raising an exception |
1569 | when the _PyType_Lookup() call fails; |
1570 | |
1571 | - lookup_maybe_method() and lookup_method() are internal routines similar |
1572 | to _PyObject_LookupSpecial(), but can return unbound PyFunction |
1573 | to avoid temporary method object. Pass self as first argument when |
1574 | unbound == 1. |
1575 | */ |
1576 | |
1577 | PyObject * |
1578 | _PyObject_LookupSpecial(PyObject *self, PyObject *attr) |
1579 | { |
1580 | PyObject *res; |
1581 | |
1582 | res = _PyType_Lookup(Py_TYPE(self), attr); |
1583 | if (res != NULL) { Branch (1583:9): [True: 14.1M, False: 2.12M]
|
1584 | descrgetfunc f; |
1585 | if ((f = Py_TYPE(res)->tp_descr_get) == NULL) Branch (1585:13): [True: 29, False: 14.1M]
|
1586 | Py_INCREF(res); |
1587 | else |
1588 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
1589 | } |
1590 | return res; |
1591 | } |
1592 | |
1593 | PyObject * |
1594 | _PyObject_LookupSpecialId(PyObject *self, _Py_Identifier *attrid) |
1595 | { |
1596 | PyObject *attr = _PyUnicode_FromId(attrid); /* borrowed */ |
1597 | if (attr == NULL) Branch (1597:9): [True: 0, False: 11]
|
1598 | return NULL; |
1599 | return _PyObject_LookupSpecial(self, attr); |
1600 | } |
1601 | |
1602 | static PyObject * |
1603 | lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound) |
1604 | { |
1605 | PyObject *res = _PyType_Lookup(Py_TYPE(self), attr); |
1606 | if (res == NULL) { Branch (1606:9): [True: 33, False: 27.5M]
|
1607 | return NULL; |
1608 | } |
1609 | |
1610 | if (_PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR)) { Branch (1610:9): [True: 27.5M, False: 2.68k]
|
1611 | /* Avoid temporary PyMethodObject */ |
1612 | *unbound = 1; |
1613 | Py_INCREF(res); |
1614 | } |
1615 | else { |
1616 | *unbound = 0; |
1617 | descrgetfunc f = Py_TYPE(res)->tp_descr_get; |
1618 | if (f == NULL) { Branch (1618:13): [True: 2.39k, False: 287]
|
1619 | Py_INCREF(res); |
1620 | } |
1621 | else { |
1622 | res = f(res, self, (PyObject *)(Py_TYPE(self))); |
1623 | } |
1624 | } |
1625 | return res; |
1626 | } |
1627 | |
1628 | static PyObject * |
1629 | lookup_method(PyObject *self, PyObject *attr, int *unbound) |
1630 | { |
1631 | PyObject *res = lookup_maybe_method(self, attr, unbound); |
1632 | if (res == NULL && !PyErr_Occurred()2 ) { Branch (1632:9): [True: 2, False: 15.2M]
Branch (1632:24): [True: 0, False: 2]
|
1633 | PyErr_SetObject(PyExc_AttributeError, attr); |
1634 | } |
1635 | return res; |
1636 | } |
1637 | |
1638 | |
1639 | static inline PyObject* |
1640 | vectorcall_unbound(PyThreadState *tstate, int unbound, PyObject *func, |
1641 | PyObject *const *args, Py_ssize_t nargs) |
1642 | { |
1643 | size_t nargsf = nargs; |
1644 | if (!unbound) { Branch (1644:9): [True: 518, False: 14.6M]
|
1645 | /* Skip self argument, freeing up args[0] to use for |
1646 | * PY_VECTORCALL_ARGUMENTS_OFFSET */ |
1647 | args++; |
1648 | nargsf = nargsf - 1 + PY_VECTORCALL_ARGUMENTS_OFFSET; |
1649 | } |
1650 | EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_SLOT, func); |
1651 | return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL); |
1652 | } |
1653 | |
1654 | static PyObject* |
1655 | call_unbound_noarg(int unbound, PyObject *func, PyObject *self) |
1656 | { |
1657 | if (unbound) { Branch (1657:9): [True: 1.68M, False: 164]
|
1658 | return PyObject_CallOneArg(func, self); |
1659 | } |
1660 | else { |
1661 | return _PyObject_CallNoArgs(func); |
1662 | } |
1663 | } |
1664 | |
1665 | /* A variation of PyObject_CallMethod* that uses lookup_method() |
1666 | instead of PyObject_GetAttrString(). |
1667 | |
1668 | args is an argument vector of length nargs. The first element in this |
1669 | vector is the special object "self" which is used for the method lookup */ |
1670 | static PyObject * |
1671 | vectorcall_method(PyObject *name, PyObject *const *args, Py_ssize_t nargs) |
1672 | { |
1673 | assert(nargs >= 1); |
1674 | |
1675 | PyThreadState *tstate = _PyThreadState_GET(); |
1676 | int unbound; |
1677 | PyObject *self = args[0]; |
1678 | PyObject *func = lookup_method(self, name, &unbound); |
1679 | if (func == NULL) { Branch (1679:9): [True: 1, False: 4.08M]
|
1680 | return NULL; |
1681 | } |
1682 | PyObject *retval = vectorcall_unbound(tstate, unbound, func, args, nargs); |
1683 | Py_DECREF(func); |
1684 | return retval; |
1685 | } |
1686 | |
1687 | /* Clone of vectorcall_method() that returns NotImplemented |
1688 | * when the lookup fails. */ |
1689 | static PyObject * |
1690 | vectorcall_maybe(PyThreadState *tstate, PyObject *name, |
1691 | PyObject *const *args, Py_ssize_t nargs) |
1692 | { |
1693 | assert(nargs >= 1); |
1694 | |
1695 | int unbound; |
1696 | PyObject *self = args[0]; |
1697 | PyObject *func = lookup_maybe_method(self, name, &unbound); |
1698 | if (func == NULL) { Branch (1698:9): [True: 33, False: 500k]
|
1699 | if (!PyErr_Occurred()) Branch (1699:13): [True: 33, False: 0]
|
1700 | Py_RETURN_NOTIMPLEMENTED; |
1701 | return NULL; |
1702 | } |
1703 | PyObject *retval = vectorcall_unbound(tstate, unbound, func, args, nargs); |
1704 | Py_DECREF(func); |
1705 | return retval; |
1706 | } |
1707 | |
1708 | /* |
1709 | Method resolution order algorithm C3 described in |
1710 | "A Monotonic Superclass Linearization for Dylan", |
1711 | by Kim Barrett, Bob Cassel, Paul Haahr, |
1712 | David A. Moon, Keith Playford, and P. Tucker Withington. |
1713 | (OOPSLA 1996) |
1714 | |
1715 | Some notes about the rules implied by C3: |
1716 | |
1717 | No duplicate bases. |
1718 | It isn't legal to repeat a class in a list of base classes. |
1719 | |
1720 | The next three properties are the 3 constraints in "C3". |
1721 | |
1722 | Local precedence order. |
1723 | If A precedes B in C's MRO, then A will precede B in the MRO of all |
1724 | subclasses of C. |
1725 | |
1726 | Monotonicity. |
1727 | The MRO of a class must be an extension without reordering of the |
1728 | MRO of each of its superclasses. |
1729 | |
1730 | Extended Precedence Graph (EPG). |
1731 | Linearization is consistent if there is a path in the EPG from |
1732 | each class to all its successors in the linearization. See |
1733 | the paper for definition of EPG. |
1734 | */ |
1735 | |
1736 | static int |
1737 | tail_contains(PyObject *tuple, int whence, PyObject *o) |
1738 | { |
1739 | Py_ssize_t j, size; |
1740 | size = PyTuple_GET_SIZE(tuple); |
1741 | |
1742 | for (j = whence+1; j < size; j++180k ) { Branch (1742:24): [True: 207k, False: 228k]
|
1743 | if (PyTuple_GET_ITEM(tuple, j) == o) Branch (1743:13): [True: 27.0k, False: 180k]
|
1744 | return 1; |
1745 | } |
1746 | return 0; |
1747 | } |
1748 | |
1749 | static PyObject * |
1750 | class_name(PyObject *cls) |
1751 | { |
1752 | PyObject *name; |
1753 | if (_PyObject_LookupAttr(cls, &_Py_ID(__name__), &name) == 0) { Branch (1753:9): [True: 0, False: 26]
|
1754 | name = PyObject_Repr(cls); |
1755 | } |
1756 | return name; |
1757 | } |
1758 | |
1759 | static int |
1760 | check_duplicates(PyObject *tuple) |
1761 | { |
1762 | Py_ssize_t i, j, n; |
1763 | /* Let's use a quadratic time algorithm, |
1764 | assuming that the bases tuples is short. |
1765 | */ |
1766 | n = PyTuple_GET_SIZE(tuple); |
1767 | for (i = 0; i < n; i++28.5k ) { Branch (1767:17): [True: 28.5k, False: 13.9k]
|
1768 | PyObject *o = PyTuple_GET_ITEM(tuple, i); |
1769 | for (j = i + 1; j < n; j++15.7k ) { Branch (1769:25): [True: 15.7k, False: 28.5k]
|
1770 | if (PyTuple_GET_ITEM(tuple, j) == o) { Branch (1770:17): [True: 5, False: 15.7k]
|
1771 | o = class_name(o); |
1772 | if (o != NULL) { Branch (1772:21): [True: 5, False: 0]
|
1773 | if (PyUnicode_Check(o)) { |
1774 | PyErr_Format(PyExc_TypeError, |
1775 | "duplicate base class %U", o); |
1776 | } |
1777 | else { |
1778 | PyErr_SetString(PyExc_TypeError, |
1779 | "duplicate base class"); |
1780 | } |
1781 | Py_DECREF(o); |
1782 | } |
1783 | return -1; |
1784 | } |
1785 | } |
1786 | } |
1787 | return 0; |
1788 | } |
1789 | |
1790 | /* Raise a TypeError for an MRO order disagreement. |
1791 | |
1792 | It's hard to produce a good error message. In the absence of better |
1793 | insight into error reporting, report the classes that were candidates |
1794 | to be put next into the MRO. There is some conflict between the |
1795 | order in which they should be put in the MRO, but it's hard to |
1796 | diagnose what constraint can't be satisfied. |
1797 | */ |
1798 | |
1799 | static void |
1800 | set_mro_error(PyObject **to_merge, Py_ssize_t to_merge_size, int *remain) |
1801 | { |
1802 | Py_ssize_t i, n, off; |
1803 | char buf[1000]; |
1804 | PyObject *k, *v; |
1805 | PyObject *set = PyDict_New(); |
1806 | if (!set) return0 ; Branch (1806:9): [True: 0, False: 10]
|
1807 | |
1808 | for (i = 0; 10 i < to_merge_size; i++31 ) { Branch (1808:17): [True: 31, False: 10]
|
1809 | PyObject *L = to_merge[i]; |
1810 | if (remain[i] < PyTuple_GET_SIZE(L)) { Branch (1810:13): [True: 27, False: 4]
|
1811 | PyObject *c = PyTuple_GET_ITEM(L, remain[i]); |
1812 | if (PyDict_SetItem(set, c, Py_None) < 0) { Branch (1812:17): [True: 0, False: 27]
|
1813 | Py_DECREF(set); |
1814 | return; |
1815 | } |
1816 | } |
1817 | } |
1818 | n = PyDict_GET_SIZE(set); |
1819 | |
1820 | off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \ |
1821 | consistent method resolution\norder (MRO) for bases"); |
1822 | i = 0; |
1823 | while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)21 ) { Branch (1823:12): [True: 21, False: 10]
Branch (1823:44): [True: 21, False: 0]
|
1824 | PyObject *name = class_name(k); |
1825 | const char *name_str = NULL; |
1826 | if (name != NULL) { Branch (1826:13): [True: 21, False: 0]
|
1827 | if (PyUnicode_Check(name)) { |
1828 | name_str = PyUnicode_AsUTF8(name); |
1829 | } |
1830 | else { |
1831 | name_str = "?"; |
1832 | } |
1833 | } |
1834 | if (name_str == NULL) { Branch (1834:13): [True: 0, False: 21]
|
1835 | Py_XDECREF(name); |
1836 | Py_DECREF(set); |
1837 | return; |
1838 | } |
1839 | off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str); |
1840 | Py_XDECREF(name); |
1841 | if (--n && (size_t)(off+1) < sizeof(buf)11 ) { Branch (1841:13): [True: 11, False: 10]
Branch (1841:20): [True: 11, False: 0]
|
1842 | buf[off++] = ','; |
1843 | buf[off] = '\0'; |
1844 | } |
1845 | } |
1846 | PyErr_SetString(PyExc_TypeError, buf); |
1847 | Py_DECREF(set); |
1848 | } |
1849 | |
1850 | static int |
1851 | pmerge(PyObject *acc, PyObject **to_merge, Py_ssize_t to_merge_size) |
1852 | { |
1853 | int res = 0; |
1854 | Py_ssize_t i, j, empty_cnt; |
1855 | int *remain; |
1856 | |
1857 | /* remain stores an index into each sublist of to_merge. |
1858 | remain[i] is the index of the next base in to_merge[i] |
1859 | that is not included in acc. |
1860 | */ |
1861 | remain = PyMem_New(int, to_merge_size); |
1862 | if (remain == NULL) { Branch (1862:9): [True: 0, False: 13.9k]
|
1863 | PyErr_NoMemory(); |
1864 | return -1; |
1865 | } |
1866 | for (i = 0; 13.9k i < to_merge_size; i++42.4k ) Branch (1866:17): [True: 42.4k, False: 13.9k]
|
1867 | remain[i] = 0; |
1868 | |
1869 | again: |
1870 | empty_cnt = 0; |
1871 | for (i = 0; i < to_merge_size; i++69.4k ) { Branch (1871:17): [True: 134k, False: 13.9k]
|
1872 | PyObject *candidate; |
1873 | |
1874 | PyObject *cur_tuple = to_merge[i]; |
1875 | |
1876 | if (remain[i] >= PyTuple_GET_SIZE(cur_tuple)) { Branch (1876:13): [True: 42.4k, False: 91.8k]
|
1877 | empty_cnt++; |
1878 | continue; |
1879 | } |
1880 | |
1881 | /* Choose next candidate for MRO. |
1882 | |
1883 | The input sequences alone can determine the choice. |
1884 | If not, choose the class which appears in the MRO |
1885 | of the earliest direct superclass of the new class. |
1886 | */ |
1887 | |
1888 | candidate = PyTuple_GET_ITEM(cur_tuple, remain[i]); |
1889 | for (j = 0; j < to_merge_size; j++228k ) { Branch (1889:21): [True: 255k, False: 64.8k]
|
1890 | PyObject *j_lst = to_merge[j]; |
1891 | if (tail_contains(j_lst, remain[j], candidate)) Branch (1891:17): [True: 27.0k, False: 228k]
|
1892 | goto skip; /* continue outer loop */ |
1893 | } |
1894 | res = PyList_Append(acc, candidate); |
1895 | if (res < 0) Branch (1895:13): [True: 0, False: 64.8k]
|
1896 | goto out; |
1897 | |
1898 | for (j = 0; 64.8k j < to_merge_size; j++199k ) { Branch (1898:21): [True: 199k, False: 64.8k]
|
1899 | PyObject *j_lst = to_merge[j]; |
1900 | if (remain[j] < PyTuple_GET_SIZE(j_lst) && Branch (1900:17): [True: 170k, False: 29.0k]
|
1901 | PyTuple_GET_ITEM170k (j_lst, remain[j]) == candidate170k ) { Branch (1901:17): [True: 112k, False: 57.5k]
|
1902 | remain[j]++; |
1903 | } |
1904 | } |
1905 | goto again; |
1906 | skip: ; |
1907 | } |
1908 | |
1909 | if (empty_cnt != to_merge_size) { Branch (1909:9): [True: 10, False: 13.9k]
|
1910 | set_mro_error(to_merge, to_merge_size, remain); |
1911 | res = -1; |
1912 | } |
1913 | |
1914 | out: |
1915 | PyMem_Free(remain); |
1916 | |
1917 | return res; |
1918 | } |
1919 | |
1920 | static PyObject * |
1921 | mro_implementation(PyTypeObject *type) |
1922 | { |
1923 | if (!_PyType_IsReady(type)) { Branch (1923:9): [True: 0, False: 115k]
|
1924 | if (PyType_Ready(type) < 0) Branch (1924:13): [True: 0, False: 0]
|
1925 | return NULL; |
1926 | } |
1927 | |
1928 | PyObject *bases = type->tp_bases; |
1929 | Py_ssize_t n = PyTuple_GET_SIZE(bases); |
1930 | for (Py_ssize_t i = 0; i < n; i++129k ) { Branch (1930:28): [True: 129k, False: 115k]
|
1931 | PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i)); |
1932 | if (base->tp_mro == NULL) { Branch (1932:13): [True: 1, False: 129k]
|
1933 | PyErr_Format(PyExc_TypeError, |
1934 | "Cannot extend an incomplete type '%.100s'", |
1935 | base->tp_name); |
1936 | return NULL; |
1937 | } |
1938 | assert(PyTuple_Check(base->tp_mro)); |
1939 | } |
1940 | |
1941 | if (n == 1) { Branch (1941:9): [True: 101k, False: 13.9k]
|
1942 | /* Fast path: if there is a single base, constructing the MRO |
1943 | * is trivial. |
1944 | */ |
1945 | PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, 0)); |
1946 | Py_ssize_t k = PyTuple_GET_SIZE(base->tp_mro); |
1947 | PyObject *result = PyTuple_New(k + 1); |
1948 | if (result == NULL) { Branch (1948:13): [True: 0, False: 101k]
|
1949 | return NULL; |
1950 | } |
1951 | |
1952 | Py_INCREF(type); |
1953 | PyTuple_SET_ITEM(result, 0, (PyObject *) type); |
1954 | for (Py_ssize_t i = 0; i < k; i++241k ) { Branch (1954:32): [True: 241k, False: 101k]
|
1955 | PyObject *cls = PyTuple_GET_ITEM(base->tp_mro, i); |
1956 | Py_INCREF(cls); |
1957 | PyTuple_SET_ITEM(result, i + 1, cls); |
1958 | } |
1959 | return result; |
1960 | } |
1961 | |
1962 | /* This is just a basic sanity check. */ |
1963 | if (check_duplicates(bases) < 0) { Branch (1963:9): [True: 5, False: 13.9k]
|
1964 | return NULL; |
1965 | } |
1966 | |
1967 | /* Find a superclass linearization that honors the constraints |
1968 | of the explicit tuples of bases and the constraints implied by |
1969 | each base class. |
1970 | |
1971 | to_merge is an array of tuples, where each tuple is a superclass |
1972 | linearization implied by a base class. The last element of |
1973 | to_merge is the declared tuple of bases. |
1974 | */ |
1975 | PyObject **to_merge = PyMem_New(PyObject *, n + 1); |
1976 | if (to_merge == NULL) { Branch (1976:9): [True: 0, False: 13.9k]
|
1977 | PyErr_NoMemory(); |
1978 | return NULL; |
1979 | } |
1980 | |
1981 | for (Py_ssize_t i = 0; 13.9k i < n; i++28.5k ) { Branch (1981:28): [True: 28.5k, False: 13.9k]
|
1982 | PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i)); |
1983 | to_merge[i] = base->tp_mro; |
1984 | } |
1985 | to_merge[n] = bases; |
1986 | |
1987 | PyObject *result = PyList_New(1); |
1988 | if (result == NULL) { Branch (1988:9): [True: 0, False: 13.9k]
|
1989 | PyMem_Free(to_merge); |
1990 | return NULL; |
1991 | } |
1992 | |
1993 | Py_INCREF(type); |
1994 | PyList_SET_ITEM(result, 0, (PyObject *)type); |
1995 | if (pmerge(result, to_merge, n + 1) < 0) { Branch (1995:9): [True: 10, False: 13.9k]
|
1996 | Py_CLEAR(result); |
1997 | } |
1998 | PyMem_Free(to_merge); |
1999 | |
2000 | return result; |
2001 | } |
2002 | |
2003 | /*[clinic input] |
2004 | type.mro |
2005 | |
2006 | Return a type's method resolution order. |
2007 | [clinic start generated code]*/ |
2008 | |
2009 | static PyObject * |
2010 | type_mro_impl(PyTypeObject *self) |
2011 | /*[clinic end generated code: output=bffc4a39b5b57027 input=28414f4e156db28d]*/ |
2012 | { |
2013 | PyObject *seq; |
2014 | seq = mro_implementation(self); |
2015 | if (seq != NULL && !17.5k PyList_Check17.5k (seq)) { Branch (2015:9): [True: 17.5k, False: 6]
Branch (2015:24): [True: 14.4k, False: 3.07k]
|
2016 | Py_SETREF(seq, PySequence_List(seq)); |
2017 | } |
2018 | return seq; |
2019 | } |
2020 | |
2021 | static int |
2022 | mro_check(PyTypeObject *type, PyObject *mro) |
2023 | { |
2024 | PyTypeObject *solid; |
2025 | Py_ssize_t i, n; |
2026 | |
2027 | solid = solid_base(type); |
2028 | |
2029 | n = PyTuple_GET_SIZE(mro); |
2030 | for (i = 0; i < n; i++83.6k ) { Branch (2030:17): [True: 83.6k, False: 17.4k]
|
2031 | PyObject *obj = PyTuple_GET_ITEM(mro, i); |
2032 | if (!PyType_Check(obj)) { Branch (2032:13): [True: 1, False: 83.6k]
|
2033 | PyErr_Format( |
2034 | PyExc_TypeError, |
2035 | "mro() returned a non-class ('%.500s')", |
2036 | Py_TYPE(obj)->tp_name); |
2037 | return -1; |
2038 | } |
2039 | PyTypeObject *base = (PyTypeObject*)obj; |
2040 | |
2041 | if (!PyType_IsSubtype(solid, solid_base(base))) { Branch (2041:13): [True: 1, False: 83.6k]
|
2042 | PyErr_Format( |
2043 | PyExc_TypeError, |
2044 | "mro() returned base with unsuitable layout ('%.500s')", |
2045 | base->tp_name); |
2046 | return -1; |
2047 | } |
2048 | } |
2049 | |
2050 | return 0; |
2051 | } |
2052 | |
2053 | /* Lookups an mcls.mro method, invokes it and checks the result (if needed, |
2054 | in case of a custom mro() implementation). |
2055 | |
2056 | Keep in mind that during execution of this function type->tp_mro |
2057 | can be replaced due to possible reentrance (for example, |
2058 | through type_set_bases): |
2059 | |
2060 | - when looking up the mcls.mro attribute (it could be |
2061 | a user-provided descriptor); |
2062 | |
2063 | - from inside a custom mro() itself; |
2064 | |
2065 | - through a finalizer of the return value of mro(). |
2066 | */ |
2067 | static PyObject * |
2068 | mro_invoke(PyTypeObject *type) |
2069 | { |
2070 | PyObject *mro_result; |
2071 | PyObject *new_mro; |
2072 | const int custom = !Py_IS_TYPE(type, &PyType_Type); |
2073 | |
2074 | if (custom) { Branch (2074:9): [True: 17.4k, False: 97.5k]
|
2075 | int unbound; |
2076 | PyObject *mro_meth = lookup_method( |
2077 | (PyObject *)type, &_Py_ID(mro), &unbound); |
2078 | if (mro_meth == NULL) Branch (2078:13): [True: 0, False: 17.4k]
|
2079 | return NULL; |
2080 | mro_result = call_unbound_noarg(unbound, mro_meth, (PyObject *)type); |
2081 | Py_DECREF(mro_meth); |
2082 | } |
2083 | else { |
2084 | mro_result = mro_implementation(type); |
2085 | } |
2086 | if (mro_result == NULL) Branch (2086:9): [True: 20, False: 114k]
|
2087 | return NULL; |
2088 | |
2089 | new_mro = PySequence_Tuple(mro_result); |
2090 | Py_DECREF(mro_result); |
2091 | if (new_mro == NULL) { Branch (2091:9): [True: 1, False: 114k]
|
2092 | return NULL; |
2093 | } |
2094 | |
2095 | if (PyTuple_GET_SIZE(new_mro) == 0) { Branch (2095:9): [True: 0, False: 114k]
|
2096 | Py_DECREF(new_mro); |
2097 | PyErr_Format(PyExc_TypeError, "type MRO must not be empty"); |
2098 | return NULL; |
2099 | } |
2100 | |
2101 | if (custom && mro_check(type, new_mro) < 017.4k ) { Branch (2101:9): [True: 17.4k, False: 97.5k]
Branch (2101:19): [True: 2, False: 17.4k]
|
2102 | Py_DECREF(new_mro); |
2103 | return NULL; |
2104 | } |
2105 | return new_mro; |
2106 | } |
2107 | |
2108 | /* Calculates and assigns a new MRO to type->tp_mro. |
2109 | Return values and invariants: |
2110 | |
2111 | - Returns 1 if a new MRO value has been set to type->tp_mro due to |
2112 | this call of mro_internal (no tricky reentrancy and no errors). |
2113 | |
2114 | In case if p_old_mro argument is not NULL, a previous value |
2115 | of type->tp_mro is put there, and the ownership of this |
2116 | reference is transferred to a caller. |
2117 | Otherwise, the previous value (if any) is decref'ed. |
2118 | |
2119 | - Returns 0 in case when type->tp_mro gets changed because of |
2120 | reentering here through a custom mro() (see a comment to mro_invoke). |
2121 | |
2122 | In this case, a refcount of an old type->tp_mro is adjusted |
2123 | somewhere deeper in the call stack (by the innermost mro_internal |
2124 | or its caller) and may become zero upon returning from here. |
2125 | This also implies that the whole hierarchy of subclasses of the type |
2126 | has seen the new value and updated their MRO accordingly. |
2127 | |
2128 | - Returns -1 in case of an error. |
2129 | */ |
2130 | static int |
2131 | mro_internal(PyTypeObject *type, PyObject **p_old_mro) |
2132 | { |
2133 | PyObject *new_mro, *old_mro; |
2134 | int reent; |
2135 | |
2136 | /* Keep a reference to be able to do a reentrancy check below. |
2137 | Don't let old_mro be GC'ed and its address be reused for |
2138 | another object, like (suddenly!) a new tp_mro. */ |
2139 | old_mro = type->tp_mro; |
2140 | Py_XINCREF(old_mro); |
2141 | new_mro = mro_invoke(type); /* might cause reentrance */ |
2142 | reent = (type->tp_mro != old_mro); |
2143 | Py_XDECREF(old_mro); |
2144 | if (new_mro == NULL) { Branch (2144:9): [True: 23, False: 114k]
|
2145 | return -1; |
2146 | } |
2147 | |
2148 | if (reent) { Branch (2148:9): [True: 15, False: 114k]
|
2149 | Py_DECREF(new_mro); |
2150 | return 0; |
2151 | } |
2152 | |
2153 | type->tp_mro = new_mro; |
2154 | |
2155 | type_mro_modified(type, type->tp_mro); |
2156 | /* corner case: the super class might have been hidden |
2157 | from the custom MRO */ |
2158 | type_mro_modified(type, type->tp_bases); |
2159 | |
2160 | PyType_Modified(type); |
2161 | |
2162 | if (p_old_mro != NULL) Branch (2162:9): [True: 142, False: 114k]
|
2163 | *p_old_mro = old_mro; /* transfer the ownership */ |
2164 | else |
2165 | Py_XDECREF(old_mro); |
2166 | |
2167 | return 1; |
2168 | } |
2169 | |
2170 | /* Calculate the best base amongst multiple base classes. |
2171 | This is the first one that's on the path to the "solid base". */ |
2172 | |
2173 | static PyTypeObject * |
2174 | best_base(PyObject *bases) |
2175 | { |
2176 | Py_ssize_t i, n; |
2177 | PyTypeObject *base, *winner, *candidate; |
2178 | |
2179 | assert(PyTuple_Check(bases)); |
2180 | n = PyTuple_GET_SIZE(bases); |
2181 | assert(n > 0); |
2182 | base = NULL; |
2183 | winner = NULL; |
2184 | for (i = 0; i < n; i++79.3k ) { Branch (2184:17): [True: 79.3k, False: 64.6k]
|
2185 | PyObject *base_proto = PyTuple_GET_ITEM(bases, i); |
2186 | if (!PyType_Check(base_proto)) { Branch (2186:13): [True: 0, False: 79.3k]
|
2187 | PyErr_SetString( |
2188 | PyExc_TypeError, |
2189 | "bases must be types"); |
2190 | return NULL; |
2191 | } |
2192 | PyTypeObject *base_i = (PyTypeObject *)base_proto; |
2193 | |
2194 | if (!_PyType_IsReady(base_i)) { Branch (2194:13): [True: 1, False: 79.3k]
|
2195 | if (PyType_Ready(base_i) < 0) Branch (2195:17): [True: 0, False: 1]
|
2196 | return NULL; |
2197 | } |
2198 | if (!_PyType_HasFeature(base_i, Py_TPFLAGS_BASETYPE)) { Branch (2198:13): [True: 17, False: 79.3k]
|
2199 | PyErr_Format(PyExc_TypeError, |
2200 | "type '%.100s' is not an acceptable base type", |
2201 | base_i->tp_name); |
2202 | return NULL; |
2203 | } |
2204 | candidate = solid_base(base_i); |
2205 | if (winner == NULL) { Branch (2205:13): [True: 64.6k, False: 14.7k]
|
2206 | winner = candidate; |
2207 | base = base_i; |
2208 | } |
2209 | else if (PyType_IsSubtype(winner, candidate)) Branch (2209:18): [True: 8.01k, False: 6.69k]
|
2210 | ; |
2211 | else if (PyType_IsSubtype(candidate, winner)) { Branch (2211:18): [True: 6.69k, False: 3]
|
2212 | winner = candidate; |
2213 | base = base_i; |
2214 | } |
2215 | else { |
2216 | PyErr_SetString( |
2217 | PyExc_TypeError, |
2218 | "multiple bases have " |
2219 | "instance lay-out conflict"); |
2220 | return NULL; |
2221 | } |
2222 | } |
2223 | assert (base != NULL); |
2224 | |
2225 | return base; |
2226 | } |
2227 | |
2228 | static int |
2229 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
2230 | { |
2231 | size_t t_size = type->tp_basicsize; |
2232 | size_t b_size = base->tp_basicsize; |
2233 | |
2234 | assert(t_size >= b_size); /* Else type smaller than base! */ |
2235 | if (type->tp_itemsize || base->tp_itemsize471k ) { Branch (2235:9): [True: 20.5k, False: 471k]
Branch (2235:30): [True: 0, False: 471k]
|
2236 | /* If itemsize is involved, stricter rules */ |
2237 | return t_size != b_size || Branch (2237:16): [True: 16.8k, False: 3.67k]
|
2238 | type->tp_itemsize != base->tp_itemsize3.67k ; Branch (2238:13): [True: 0, False: 3.67k]
|
2239 | } |
2240 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0132k && Branch (2240:9): [True: 132k, False: 339k]
Branch (2240:36): [True: 126k, False: 6.08k]
|
2241 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size126k && Branch (2241:9): [True: 125k, False: 403]
|
2242 | type->tp_flags & 125k Py_TPFLAGS_HEAPTYPE125k ) Branch (2242:9): [True: 118k, False: 7.66k]
|
2243 | t_size -= sizeof(PyObject *); |
2244 | return t_size != b_size; |
2245 | } |
2246 | |
2247 | static PyTypeObject * |
2248 | solid_base(PyTypeObject *type) |
2249 | { |
2250 | PyTypeObject *base; |
2251 | |
2252 | if (type->tp_base) Branch (2252:9): [True: 311k, False: 180k]
|
2253 | base = solid_base(type->tp_base); |
2254 | else |
2255 | base = &PyBaseObject_Type; |
2256 | if (extra_ivars(type, base)) Branch (2256:9): [True: 71.9k, False: 420k]
|
2257 | return type; |
2258 | else |
2259 | return base; |
2260 | } |
2261 | |
2262 | static void object_dealloc(PyObject *); |
2263 | static PyObject *object_new(PyTypeObject *, PyObject *, PyObject *); |
2264 | static int object_init(PyObject *, PyObject *, PyObject *); |
2265 | static int update_slot(PyTypeObject *, PyObject *); |
2266 | static void fixup_slot_dispatchers(PyTypeObject *); |
2267 | static int type_new_set_names(PyTypeObject *); |
2268 | static int type_new_init_subclass(PyTypeObject *, PyObject *); |
2269 | |
2270 | /* |
2271 | * Helpers for __dict__ descriptor. We don't want to expose the dicts |
2272 | * inherited from various builtin types. The builtin base usually provides |
2273 | * its own __dict__ descriptor, so we use that when we can. |
2274 | */ |
2275 | static PyTypeObject * |
2276 | get_builtin_base_with_dict(PyTypeObject *type) |
2277 | { |
2278 | while (type->tp_base != NULL) { Branch (2278:12): [True: 1.35M, False: 414k]
|
2279 | if (type->tp_dictoffset != 0 && Branch (2279:13): [True: 1.33M, False: 16.7k]
|
2280 | !(type->tp_flags & 1.33M Py_TPFLAGS_HEAPTYPE1.33M )) Branch (2280:13): [True: 12, False: 1.33M]
|
2281 | return type; |
2282 | type = type->tp_base; |
2283 | } |
2284 | return NULL; |
2285 | } |
2286 | |
2287 | static PyObject * |
2288 | get_dict_descriptor(PyTypeObject *type) |
2289 | { |
2290 | PyObject *descr; |
2291 | |
2292 | descr = _PyType_Lookup(type, &_Py_ID(__dict__)); |
2293 | if (descr == NULL || !PyDescr_IsData(descr)) Branch (2293:9): [True: 0, False: 12]
Branch (2293:26): [True: 0, False: 12]
|
2294 | return NULL; |
2295 | |
2296 | return descr; |
2297 | } |
2298 | |
2299 | static void |
2300 | raise_dict_descr_error(PyObject *obj) |
2301 | { |
2302 | PyErr_Format(PyExc_TypeError, |
2303 | "this __dict__ descriptor does not support " |
2304 | "'%.200s' objects", Py_TYPE(obj)->tp_name); |
2305 | } |
2306 | |
2307 | static PyObject * |
2308 | subtype_dict(PyObject *obj, void *context) |
2309 | { |
2310 | PyTypeObject *base; |
2311 | |
2312 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
2313 | if (base != NULL) { Branch (2313:9): [True: 2, False: 413k]
|
2314 | descrgetfunc func; |
2315 | PyObject *descr = get_dict_descriptor(base); |
2316 | if (descr == NULL) { Branch (2316:13): [True: 0, False: 2]
|
2317 | raise_dict_descr_error(obj); |
2318 | return NULL; |
2319 | } |
2320 | func = Py_TYPE(descr)->tp_descr_get; |
2321 | if (func == NULL) { Branch (2321:13): [True: 0, False: 2]
|
2322 | raise_dict_descr_error(obj); |
2323 | return NULL; |
2324 | } |
2325 | return func(descr, obj, (PyObject *)(Py_TYPE(obj))); |
2326 | } |
2327 | return PyObject_GenericGetDict(obj, context); |
2328 | } |
2329 | |
2330 | static int |
2331 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
2332 | { |
2333 | PyObject **dictptr; |
2334 | PyTypeObject *base; |
2335 | |
2336 | base = get_builtin_base_with_dict(Py_TYPE(obj)); |
2337 | if (base != NULL) { Branch (2337:9): [True: 10, False: 582]
|
2338 | descrsetfunc func; |
2339 | PyObject *descr = get_dict_descriptor(base); |
2340 | if (descr == NULL) { Branch (2340:13): [True: 0, False: 10]
|
2341 | raise_dict_descr_error(obj); |
2342 | return -1; |
2343 | } |
2344 | func = Py_TYPE(descr)->tp_descr_set; |
2345 | if (func == NULL) { Branch (2345:13): [True: 0, False: 10]
|
2346 | raise_dict_descr_error(obj); |
2347 | return -1; |
2348 | } |
2349 | return func(descr, obj, value); |
2350 | } |
2351 | /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */ |
2352 | dictptr = _PyObject_GetDictPtr(obj); |
2353 | if (dictptr == NULL) { Branch (2353:9): [True: 0, False: 582]
|
2354 | PyErr_SetString(PyExc_AttributeError, |
2355 | "This object has no __dict__"); |
2356 | return -1; |
2357 | } |
2358 | if (value != NULL && !581 PyDict_Check581 (value)) { Branch (2358:9): [True: 581, False: 1]
Branch (2358:26): [True: 3, False: 578]
|
2359 | PyErr_Format(PyExc_TypeError, |
2360 | "__dict__ must be set to a dictionary, " |
2361 | "not a '%.200s'", Py_TYPE(value)->tp_name); |
2362 | return -1; |
2363 | } |
2364 | Py_XINCREF(value); |
2365 | Py_XSETREF(*dictptr, value); |
2366 | return 0; |
2367 | } |
2368 | |
2369 | static PyObject * |
2370 | subtype_getweakref(PyObject *obj, void *context) |
2371 | { |
2372 | PyObject **weaklistptr; |
2373 | PyObject *result; |
2374 | PyTypeObject *type = Py_TYPE(obj); |
2375 | |
2376 | if (type->tp_weaklistoffset == 0) { Branch (2376:9): [True: 0, False: 76]
|
2377 | PyErr_SetString(PyExc_AttributeError, |
2378 | "This object has no __weakref__"); |
2379 | return NULL; |
2380 | } |
2381 | _PyObject_ASSERT((PyObject *)type, |
2382 | type->tp_weaklistoffset > 0); |
2383 | _PyObject_ASSERT((PyObject *)type, |
2384 | ((type->tp_weaklistoffset + sizeof(PyObject *)) |
2385 | <= (size_t)(type->tp_basicsize))); |
2386 | weaklistptr = (PyObject **)((char *)obj + type->tp_weaklistoffset); |
2387 | if (*weaklistptr == NULL) Branch (2387:9): [True: 76, False: 0]
|
2388 | result = Py_None; |
2389 | else |
2390 | result = *weaklistptr; |
2391 | Py_INCREF(result); |
2392 | return result; |
2393 | } |
2394 | |
2395 | /* Three variants on the subtype_getsets list. */ |
2396 | |
2397 | static PyGetSetDef subtype_getsets_full[] = { |
2398 | {"__dict__", subtype_dict, subtype_setdict, |
2399 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
2400 | {"__weakref__", subtype_getweakref, NULL, |
2401 | PyDoc_STR("list of weak references to the object (if defined)")}, |
2402 | {0} |
2403 | }; |
2404 | |
2405 | static PyGetSetDef subtype_getsets_dict_only[] = { |
2406 | {"__dict__", subtype_dict, subtype_setdict, |
2407 | PyDoc_STR("dictionary for instance variables (if defined)")}, |
2408 | {0} |
2409 | }; |
2410 | |
2411 | static PyGetSetDef subtype_getsets_weakref_only[] = { |
2412 | {"__weakref__", subtype_getweakref, NULL, |
2413 | PyDoc_STR("list of weak references to the object (if defined)")}, |
2414 | {0} |
2415 | }; |
2416 | |
2417 | static int |
2418 | valid_identifier(PyObject *s) |
2419 | { |
2420 | if (!PyUnicode_Check(s)) { Branch (2420:9): [True: 3, False: 3.02k]
|
2421 | PyErr_Format(PyExc_TypeError, |
2422 | "__slots__ items must be strings, not '%.200s'", |
2423 | Py_TYPE(s)->tp_name); |
2424 | return 0; |
2425 | } |
2426 | if (!PyUnicode_IsIdentifier(s)) { Branch (2426:9): [True: 8, False: 3.01k]
|
2427 | PyErr_SetString(PyExc_TypeError, |
2428 | "__slots__ must be identifiers"); |
2429 | return 0; |
2430 | } |
2431 | return 1; |
2432 | } |
2433 | |
2434 | static int |
2435 | type_init(PyObject *cls, PyObject *args, PyObject *kwds) |
2436 | { |
2437 | assert(args != NULL && PyTuple_Check(args)); |
2438 | assert(kwds == NULL || PyDict_Check(kwds)); |
2439 | |
2440 | if (kwds != NULL && PyTuple_GET_SIZE3.32k (args) == 13.32k && Branch (2440:9): [True: 3.32k, False: 82.7k]
Branch (2440:25): [True: 0, False: 3.32k]
|
2441 | PyDict_GET_SIZE0 (kwds) != 00 ) { Branch (2441:9): [True: 0, False: 0]
|
2442 | PyErr_SetString(PyExc_TypeError, |
2443 | "type.__init__() takes no keyword arguments"); |
2444 | return -1; |
2445 | } |
2446 | |
2447 | if ((PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) { Branch (2447:10): [True: 86.0k, False: 0]
Branch (2447:41): [True: 0, False: 86.0k]
|
2448 | PyErr_SetString(PyExc_TypeError, |
2449 | "type.__init__() takes 1 or 3 arguments"); |
2450 | return -1; |
2451 | } |
2452 | |
2453 | return 0; |
2454 | } |
2455 | |
2456 | |
2457 | unsigned long |
2458 | PyType_GetFlags(PyTypeObject *type) |
2459 | { |
2460 | return type->tp_flags; |
2461 | } |
2462 | |
2463 | |
2464 | int |
2465 | PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) |
2466 | { |
2467 | return _PyType_SUPPORTS_WEAKREFS(type); |
2468 | } |
2469 | |
2470 | |
2471 | /* Determine the most derived metatype. */ |
2472 | PyTypeObject * |
2473 | _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) |
2474 | { |
2475 | Py_ssize_t i, nbases; |
2476 | PyTypeObject *winner; |
2477 | PyObject *tmp; |
2478 | PyTypeObject *tmptype; |
2479 | |
2480 | /* Determine the proper metatype to deal with this, |
2481 | and check for metatype conflicts while we're at it. |
2482 | Note that if some other metatype wins to contract, |
2483 | it's possible that its instances are not types. */ |
2484 | |
2485 | nbases = PyTuple_GET_SIZE(bases); |
2486 | winner = metatype; |
2487 | for (i = 0; i < nbases; i++124k ) { Branch (2487:17): [True: 124k, False: 129k]
|
2488 | tmp = PyTuple_GET_ITEM(bases, i); |
2489 | tmptype = Py_TYPE(tmp); |
2490 | if (PyType_IsSubtype(winner, tmptype)) Branch (2490:13): [True: 122k, False: 1.78k]
|
2491 | continue; |
2492 | if (PyType_IsSubtype(tmptype, winner)) { Branch (2492:13): [True: 1.78k, False: 7]
|
2493 | winner = tmptype; |
2494 | continue; |
2495 | } |
2496 | /* else: */ |
2497 | PyErr_SetString(PyExc_TypeError, |
2498 | "metaclass conflict: " |
2499 | "the metaclass of a derived class " |
2500 | "must be a (non-strict) subclass " |
2501 | "of the metaclasses of all its bases"); |
2502 | return NULL; |
2503 | } |
2504 | return winner; |
2505 | } |
2506 | |
2507 | |
2508 | // Forward declaration |
2509 | static PyObject * |
2510 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds); |
2511 | |
2512 | typedef struct { |
2513 | PyTypeObject *metatype; |
2514 | PyObject *args; |
2515 | PyObject *kwds; |
2516 | PyObject *orig_dict; |
2517 | PyObject *name; |
2518 | PyObject *bases; |
2519 | PyTypeObject *base; |
2520 | PyObject *slots; |
2521 | Py_ssize_t nslot; |
2522 | int add_dict; |
2523 | int add_weak; |
2524 | int may_add_dict; |
2525 | int may_add_weak; |
2526 | } type_new_ctx; |
2527 | |
2528 | |
2529 | /* Check for valid slot names and two special cases */ |
2530 | static int |
2531 | type_new_visit_slots(type_new_ctx *ctx) |
2532 | { |
2533 | PyObject *slots = ctx->slots; |
2534 | Py_ssize_t nslot = ctx->nslot; |
2535 | for (Py_ssize_t i = 0; i < nslot; i++3.01k ) { Branch (2535:28): [True: 3.02k, False: 9.03k]
|
2536 | PyObject *name = PyTuple_GET_ITEM(slots, i); |
2537 | if (!valid_identifier(name)) { Branch (2537:13): [True: 11, False: 3.01k]
|
2538 | return -1; |
2539 | } |
2540 | assert(PyUnicode_Check(name)); |
2541 | if (_PyUnicode_Equal(name, &_Py_ID(__dict__))) { Branch (2541:13): [True: 104, False: 2.91k]
|
2542 | if (!ctx->may_add_dict || ctx->add_dict != 0103 ) { Branch (2542:17): [True: 1, False: 103]
Branch (2542:39): [True: 1, False: 102]
|
2543 | PyErr_SetString(PyExc_TypeError, |
2544 | "__dict__ slot disallowed: " |
2545 | "we already got one"); |
2546 | return -1; |
2547 | } |
2548 | ctx->add_dict++; |
2549 | } |
2550 | if (_PyUnicode_Equal(name, &_Py_ID(__weakref__))) { Branch (2550:13): [True: 233, False: 2.77k]
|
2551 | if (!ctx->may_add_weak || ctx->add_weak != 0232 ) { Branch (2551:17): [True: 1, False: 232]
Branch (2551:39): [True: 1, False: 231]
|
2552 | PyErr_SetString(PyExc_TypeError, |
2553 | "__weakref__ slot disallowed: " |
2554 | "either we already got one, " |
2555 | "or __itemsize__ != 0"); |
2556 | return -1; |
2557 | } |
2558 | ctx->add_weak++; |
2559 | } |
2560 | } |
2561 | return 0; |
2562 | } |
2563 | |
2564 | |
2565 | /* Copy slots into a list, mangle names and sort them. |
2566 | Sorted names are needed for __class__ assignment. |
2567 | Convert them back to tuple at the end. |
2568 | */ |
2569 | static PyObject* |
2570 | type_new_copy_slots(type_new_ctx *ctx, PyObject *dict) |
2571 | { |
2572 | PyObject *slots = ctx->slots; |
2573 | Py_ssize_t nslot = ctx->nslot; |
2574 | |
2575 | Py_ssize_t new_nslot = nslot - ctx->add_dict - ctx->add_weak; |
2576 | PyObject *new_slots = PyList_New(new_nslot); |
2577 | if (new_slots == NULL) { Branch (2577:9): [True: 0, False: 9.03k]
|
2578 | return NULL; |
2579 | } |
2580 | |
2581 | Py_ssize_t j = 0; |
2582 | for (Py_ssize_t i = 0; i < nslot; i++3.00k ) { Branch (2582:28): [True: 3.00k, False: 9.03k]
|
2583 | PyObject *slot = PyTuple_GET_ITEM(slots, i); |
2584 | if ((ctx->add_dict && _PyUnicode_Equal(slot, &476 _Py_ID476 (__dict__))) || Branch (2584:14): [True: 476, False: 2.53k]
Branch (2584:31): [True: 101, False: 375]
|
2585 | (2.90k ctx->add_weak2.90k && _PyUnicode_Equal(slot, &858 _Py_ID858 (__weakref__)))) Branch (2585:14): [True: 858, False: 2.04k]
Branch (2585:31): [True: 230, False: 628]
|
2586 | { |
2587 | continue; |
2588 | } |
2589 | |
2590 | slot =_Py_Mangle(ctx->name, slot); |
2591 | if (!slot) { Branch (2591:13): [True: 0, False: 2.67k]
|
2592 | goto error; |
2593 | } |
2594 | PyList_SET_ITEM(new_slots, j, slot); |
2595 | |
2596 | int r = PyDict_Contains(dict, slot); |
2597 | if (r < 0) { Branch (2597:13): [True: 0, False: 2.67k]
|
2598 | goto error; |
2599 | } |
2600 | if (r > 0) { Branch (2600:13): [True: 6, False: 2.67k]
|
2601 | /* CPython inserts __qualname__ and __classcell__ (when needed) |
2602 | into the namespace when creating a class. They will be deleted |
2603 | below so won't act as class variables. */ |
2604 | if (!_PyUnicode_Equal(slot, &_Py_ID(__qualname__)) && Branch (2604:17): [True: 4, False: 2]
|
2605 | !_PyUnicode_Equal(slot, &4 _Py_ID4 (__classcell__))) Branch (2605:17): [True: 2, False: 2]
|
2606 | { |
2607 | PyErr_Format(PyExc_ValueError, |
2608 | "%R in __slots__ conflicts with class variable", |
2609 | slot); |
2610 | goto error; |
2611 | } |
2612 | } |
2613 | |
2614 | j++; |
2615 | } |
2616 | assert(j == new_nslot); |
2617 | |
2618 | if (PyList_Sort(new_slots) == -1) { Branch (2618:9): [True: 0, False: 9.03k]
|
2619 | goto error; |
2620 | } |
2621 | |
2622 | PyObject *tuple = PyList_AsTuple(new_slots); |
2623 | Py_DECREF(new_slots); |
2624 | if (tuple == NULL) { Branch (2624:9): [True: 0, False: 9.03k]
|
2625 | return NULL; |
2626 | } |
2627 | |
2628 | assert(PyTuple_GET_SIZE(tuple) == new_nslot); |
2629 | return tuple; |
2630 | |
2631 | error: |
2632 | Py_DECREF(new_slots); |
2633 | return NULL; |
2634 | } |
2635 | |
2636 | |
2637 | static void |
2638 | type_new_slots_bases(type_new_ctx *ctx) |
2639 | { |
2640 | Py_ssize_t nbases = PyTuple_GET_SIZE(ctx->bases); |
2641 | if (nbases > 1 && Branch (2641:9): [True: 1.41k, False: 7.61k]
|
2642 | (1.41k (1.41k ctx->may_add_dict1.41k && ctx->add_dict == 01.41k ) || Branch (2642:11): [True: 1.41k, False: 2]
Branch (2642:32): [True: 1.41k, False: 0]
|
2643 | (2 ctx->may_add_weak2 && ctx->add_weak == 01 ))) Branch (2643:11): [True: 1, False: 1]
Branch (2643:32): [True: 1, False: 0]
|
2644 | { |
2645 | for (Py_ssize_t i = 0; i < nbases; i++3.09k ) { Branch (2645:32): [True: 3.10k, False: 1.40k]
|
2646 | PyObject *obj = PyTuple_GET_ITEM(ctx->bases, i); |
2647 | if (obj == (PyObject *)ctx->base) { Branch (2647:17): [True: 1.41k, False: 1.69k]
|
2648 | /* Skip primary base */ |
2649 | continue; |
2650 | } |
2651 | PyTypeObject *base = _PyType_CAST(obj); |
2652 | |
2653 | if (ctx->may_add_dict && ctx->add_dict == 01.69k && Branch (2653:17): [True: 1.69k, False: 1]
Branch (2653:38): [True: 1.69k, False: 0]
|
2654 | base->tp_dictoffset != 01.69k ) Branch (2654:17): [True: 4, False: 1.68k]
|
2655 | { |
2656 | ctx->add_dict++; |
2657 | } |
2658 | if (ctx->may_add_weak && ctx->add_weak == 01.67k && Branch (2658:17): [True: 1.67k, False: 12]
Branch (2658:38): [True: 1.67k, False: 2]
|
2659 | base->tp_weaklistoffset != 01.67k ) Branch (2659:17): [True: 4, False: 1.67k]
|
2660 | { |
2661 | ctx->add_weak++; |
2662 | } |
2663 | if (ctx->may_add_dict && ctx->add_dict == 01.69k ) { Branch (2663:17): [True: 1.69k, False: 1]
Branch (2663:38): [True: 1.68k, False: 4]
|
2664 | continue; |
2665 | } |
2666 | if (ctx->may_add_weak && ctx->add_weak == 04 ) { Branch (2666:17): [True: 4, False: 1]
Branch (2666:38): [True: 0, False: 4]
|
2667 | continue; |
2668 | } |
2669 | /* Nothing more to check */ |
2670 | break; |
2671 | } |
2672 | } |
2673 | } |
2674 | |
2675 | |
2676 | static int |
2677 | type_new_slots_impl(type_new_ctx *ctx, PyObject *dict) |
2678 | { |
2679 | /* Are slots allowed? */ |
2680 | if (ctx->nslot > 0 && ctx->base->tp_itemsize != 01.21k ) { Branch (2680:9): [True: 1.21k, False: 7.83k]
Branch (2680:27): [True: 1, False: 1.21k]
|
2681 | PyErr_Format(PyExc_TypeError, |
2682 | "nonempty __slots__ not supported for subtype of '%s'", |
2683 | ctx->base->tp_name); |
2684 | return -1; |
2685 | } |
2686 | |
2687 | if (type_new_visit_slots(ctx) < 0) { Branch (2687:9): [True: 15, False: 9.03k]
|
2688 | return -1; |
2689 | } |
2690 | |
2691 | PyObject *new_slots = type_new_copy_slots(ctx, dict); |
2692 | if (new_slots == NULL) { Branch (2692:9): [True: 2, False: 9.03k]
|
2693 | return -1; |
2694 | } |
2695 | assert(PyTuple_CheckExact(new_slots)); |
2696 | |
2697 | Py_XSETREF(ctx->slots, new_slots); |
2698 | ctx->nslot = PyTuple_GET_SIZE(new_slots); |
2699 | |
2700 | /* Secondary bases may provide weakrefs or dict */ |
2701 | type_new_slots_bases(ctx); |
2702 | return 0; |
2703 | } |
2704 | |
2705 | |
2706 | static Py_ssize_t |
2707 | type_new_slots(type_new_ctx *ctx, PyObject *dict) |
2708 | { |
2709 | // Check for a __slots__ sequence variable in dict, and count it |
2710 | ctx->add_dict = 0; |
2711 | ctx->add_weak = 0; |
2712 | ctx->may_add_dict = (ctx->base->tp_dictoffset == 0); |
2713 | ctx->may_add_weak = (ctx->base->tp_weaklistoffset == 0 Branch (2713:26): [True: 47.9k, False: 38.8k]
|
2714 | && ctx->base->tp_itemsize == 047.9k ); Branch (2714:29): [True: 45.4k, False: 2.51k]
|
2715 | |
2716 | if (ctx->slots == NULL) { Branch (2716:9): [True: 77.7k, False: 9.05k]
|
2717 | if (ctx->may_add_dict) { Branch (2717:13): [True: 34.8k, False: 42.8k]
|
2718 | ctx->add_dict++; |
2719 | } |
2720 | if (ctx->may_add_weak) { Branch (2720:13): [True: 37.1k, False: 40.5k]
|
2721 | ctx->add_weak++; |
2722 | } |
2723 | } |
2724 | else { |
2725 | /* Have slots */ |
2726 | if (type_new_slots_impl(ctx, dict) < 0) { Branch (2726:13): [True: 18, False: 9.03k]
|
2727 | return -1; |
2728 | } |
2729 | } |
2730 | return 0; |
2731 | } |
2732 | |
2733 | |
2734 | static PyTypeObject* |
2735 | type_new_alloc(type_new_ctx *ctx) |
2736 | { |
2737 | PyTypeObject *metatype = ctx->metatype; |
2738 | PyTypeObject *type; |
2739 | |
2740 | // Allocate the type object |
2741 | type = (PyTypeObject *)metatype->tp_alloc(metatype, ctx->nslot); |
2742 | if (type == NULL) { Branch (2742:9): [True: 0, False: 86.7k]
|
2743 | return NULL; |
2744 | } |
2745 | PyHeapTypeObject *et = (PyHeapTypeObject *)type; |
2746 | |
2747 | // Initialize tp_flags. |
2748 | // All heap types need GC, since we can create a reference cycle by storing |
2749 | // an instance on one of its parents. |
2750 | type->tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
2751 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC); |
2752 | |
2753 | // Initialize essential fields |
2754 | type->tp_as_async = &et->as_async; |
2755 | type->tp_as_number = &et->as_number; |
2756 | type->tp_as_sequence = &et->as_sequence; |
2757 | type->tp_as_mapping = &et->as_mapping; |
2758 | type->tp_as_buffer = &et->as_buffer; |
2759 | |
2760 | type->tp_bases = Py_NewRef(ctx->bases); |
2761 | type->tp_base = (PyTypeObject *)Py_NewRef(ctx->base); |
2762 | |
2763 | type->tp_dealloc = subtype_dealloc; |
2764 | /* Always override allocation strategy to use regular heap */ |
2765 | type->tp_alloc = PyType_GenericAlloc; |
2766 | type->tp_free = PyObject_GC_Del; |
2767 | |
2768 | type->tp_traverse = subtype_traverse; |
2769 | type->tp_clear = subtype_clear; |
2770 | |
2771 | et->ht_name = Py_NewRef(ctx->name); |
2772 | et->ht_module = NULL; |
2773 | et->_ht_tpname = NULL; |
2774 | |
2775 | return type; |
2776 | } |
2777 | |
2778 | |
2779 | static int |
2780 | type_new_set_name(const type_new_ctx *ctx, PyTypeObject *type) |
2781 | { |
2782 | Py_ssize_t name_size; |
2783 | type->tp_name = PyUnicode_AsUTF8AndSize(ctx->name, &name_size); |
2784 | if (!type->tp_name) { Branch (2784:9): [True: 1, False: 86.7k]
|
2785 | return -1; |
2786 | } |
2787 | if (strlen(type->tp_name) != (size_t)name_size) { Branch (2787:9): [True: 1, False: 86.7k]
|
2788 | PyErr_SetString(PyExc_ValueError, |
2789 | "type name must not contain null characters"); |
2790 | return -1; |
2791 | } |
2792 | return 0; |
2793 | } |
2794 | |
2795 | |
2796 | /* Set __module__ in the dict */ |
2797 | static int |
2798 | type_new_set_module(PyTypeObject *type) |
2799 | { |
2800 | int r = PyDict_Contains(type->tp_dict, &_Py_ID(__module__)); |
2801 | if (r < 0) { Branch (2801:9): [True: 0, False: 86.7k]
|
2802 | return -1; |
2803 | } |
2804 | if (r > 0) { Branch (2804:9): [True: 68.0k, False: 18.7k]
|
2805 | return 0; |
2806 | } |
2807 | |
2808 | PyObject *globals = PyEval_GetGlobals(); |
2809 | if (globals == NULL) { Branch (2809:9): [True: 0, False: 18.7k]
|
2810 | return 0; |
2811 | } |
2812 | |
2813 | PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__)); |
2814 | if (module == NULL) { Branch (2814:9): [True: 0, False: 18.7k]
|
2815 | if (PyErr_Occurred()) { Branch (2815:13): [True: 0, False: 0]
|
2816 | return -1; |
2817 | } |
2818 | return 0; |
2819 | } |
2820 | |
2821 | if (PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), module) < 0) { Branch (2821:9): [True: 0, False: 18.7k]
|
2822 | return -1; |
2823 | } |
2824 | return 0; |
2825 | } |
2826 | |
2827 | |
2828 | /* Set ht_qualname to dict['__qualname__'] if available, else to |
2829 | __name__. The __qualname__ accessor will look for ht_qualname. */ |
2830 | static int |
2831 | type_new_set_ht_name(PyTypeObject *type) |
2832 | { |
2833 | PyHeapTypeObject *et = (PyHeapTypeObject *)type; |
2834 | PyObject *qualname = PyDict_GetItemWithError( |
2835 | type->tp_dict, &_Py_ID(__qualname__)); |
2836 | if (qualname != NULL) { Branch (2836:9): [True: 64.8k, False: 21.8k]
|
2837 | if (!PyUnicode_Check(qualname)) { Branch (2837:13): [True: 3, False: 64.8k]
|
2838 | PyErr_Format(PyExc_TypeError, |
2839 | "type __qualname__ must be a str, not %s", |
2840 | Py_TYPE(qualname)->tp_name); |
2841 | return -1; |
2842 | } |
2843 | et->ht_qualname = Py_NewRef(qualname); |
2844 | if (PyDict_DelItem(type->tp_dict, &_Py_ID(__qualname__)) < 0) { Branch (2844:13): [True: 0, False: 64.8k]
|
2845 | return -1; |
2846 | } |
2847 | } |
2848 | else { |
2849 | if (PyErr_Occurred()) { Branch (2849:13): [True: 0, False: 21.8k]
|
2850 | return -1; |
2851 | } |
2852 | et->ht_qualname = Py_NewRef(et->ht_name); |
2853 | } |
2854 | return 0; |
2855 | } |
2856 | |
2857 | |
2858 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
2859 | and is a string. The __doc__ accessor will first look for tp_doc; |
2860 | if that fails, it will still look into __dict__. */ |
2861 | static int |
2862 | type_new_set_doc(PyTypeObject *type) |
2863 | { |
2864 | PyObject *doc = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__doc__)); |
2865 | if (doc == NULL) { Branch (2865:9): [True: 54.9k, False: 31.7k]
|
2866 | if (PyErr_Occurred()) { Branch (2866:13): [True: 0, False: 54.9k]
|
2867 | return -1; |
2868 | } |
2869 | // no __doc__ key |
2870 | return 0; |
2871 | } |
2872 | if (!PyUnicode_Check(doc)) { Branch (2872:9): [True: 72, False: 31.7k]
|
2873 | // ignore non-string __doc__ |
2874 | return 0; |
2875 | } |
2876 | |
2877 | const char *doc_str = PyUnicode_AsUTF8(doc); |
2878 | if (doc_str == NULL) { Branch (2878:9): [True: 1, False: 31.7k]
|
2879 | return -1; |
2880 | } |
2881 | |
2882 | // Silently truncate the docstring if it contains a null byte |
2883 | Py_ssize_t size = strlen(doc_str) + 1; |
2884 | char *tp_doc = (char *)PyObject_Malloc(size); |
2885 | if (tp_doc == NULL) { Branch (2885:9): [True: 0, False: 31.7k]
|
2886 | PyErr_NoMemory(); |
2887 | return -1; |
2888 | } |
2889 | |
2890 | memcpy(tp_doc, doc_str, size); |
2891 | type->tp_doc = tp_doc; |
2892 | return 0; |
2893 | } |
2894 | |
2895 | |
2896 | static int |
2897 | type_new_staticmethod(PyTypeObject *type, PyObject *attr) |
2898 | { |
2899 | PyObject *func = PyDict_GetItemWithError(type->tp_dict, attr); |
2900 | if (func == NULL) { Branch (2900:9): [True: 84.6k, False: 2.05k]
|
2901 | if (PyErr_Occurred()) { Branch (2901:13): [True: 0, False: 84.6k]
|
2902 | return -1; |
2903 | } |
2904 | return 0; |
2905 | } |
2906 | if (!PyFunction_Check(func)) { Branch (2906:9): [True: 13, False: 2.04k]
|
2907 | return 0; |
2908 | } |
2909 | |
2910 | PyObject *static_func = PyStaticMethod_New(func); |
2911 | if (static_func == NULL) { Branch (2911:9): [True: 0, False: 2.04k]
|
2912 | return -1; |
2913 | } |
2914 | if (PyDict_SetItem(type->tp_dict, attr, static_func) < 0) { Branch (2914:9): [True: 0, False: 2.04k]
|
2915 | Py_DECREF(static_func); |
2916 | return -1; |
2917 | } |
2918 | Py_DECREF(static_func); |
2919 | return 0; |
2920 | } |
2921 | |
2922 | |
2923 | static int |
2924 | type_new_classmethod(PyTypeObject *type, PyObject *attr) |
2925 | { |
2926 | PyObject *func = PyDict_GetItemWithError(type->tp_dict, attr); |
2927 | if (func == NULL) { Branch (2927:9): [True: 171k, False: 2.40k]
|
2928 | if (PyErr_Occurred()) { Branch (2928:13): [True: 0, False: 171k]
|
2929 | return -1; |
2930 | } |
2931 | return 0; |
2932 | } |
2933 | if (!PyFunction_Check(func)) { Branch (2933:9): [True: 2.29k, False: 106]
|
2934 | return 0; |
2935 | } |
2936 | |
2937 | PyObject *method = PyClassMethod_New(func); |
2938 | if (method == NULL) { Branch (2938:9): [True: 0, False: 106]
|
2939 | return -1; |
2940 | } |
2941 | |
2942 | if (PyDict_SetItem(type->tp_dict, attr, method) < 0) { Branch (2942:9): [True: 0, False: 106]
|
2943 | Py_DECREF(method); |
2944 | return -1; |
2945 | } |
2946 | Py_DECREF(method); |
2947 | return 0; |
2948 | } |
2949 | |
2950 | |
2951 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
2952 | static int |
2953 | type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type) |
2954 | { |
2955 | PyHeapTypeObject *et = (PyHeapTypeObject *)type; |
2956 | Py_ssize_t slotoffset = ctx->base->tp_basicsize; |
2957 | if (et->ht_slots != NULL) { Branch (2957:9): [True: 9.03k, False: 77.7k]
|
2958 | PyMemberDef *mp = _PyHeapType_GET_MEMBERS(et); |
2959 | Py_ssize_t nslot = PyTuple_GET_SIZE(et->ht_slots); |
2960 | for (Py_ssize_t i = 0; i < nslot; i++, mp++2.67k ) { Branch (2960:32): [True: 2.67k, False: 9.03k]
|
2961 | mp->name = PyUnicode_AsUTF8( |
2962 | PyTuple_GET_ITEM(et->ht_slots, i)); |
2963 | if (mp->name == NULL) { Branch (2963:17): [True: 0, False: 2.67k]
|
2964 | return -1; |
2965 | } |
2966 | mp->type = T_OBJECT_EX; |
2967 | mp->offset = slotoffset; |
2968 | |
2969 | /* __dict__ and __weakref__ are already filtered out */ |
2970 | assert(strcmp(mp->name, "__dict__") != 0); |
2971 | assert(strcmp(mp->name, "__weakref__") != 0); |
2972 | |
2973 | slotoffset += sizeof(PyObject *); |
2974 | } |
2975 | } |
2976 | |
2977 | if (ctx->add_dict && ctx->base->tp_itemsize34.9k ) { Branch (2977:9): [True: 34.9k, False: 51.7k]
Branch (2977:26): [True: 812, False: 34.1k]
|
2978 | type->tp_dictoffset = -(long)sizeof(PyObject *); |
2979 | slotoffset += sizeof(PyObject *); |
2980 | } |
2981 | |
2982 | if (ctx->add_weak) { Branch (2982:9): [True: 37.3k, False: 49.3k]
|
2983 | assert(!ctx->base->tp_itemsize); |
2984 | type->tp_weaklistoffset = slotoffset; |
2985 | slotoffset += sizeof(PyObject *); |
2986 | } |
2987 | if (ctx->add_dict && ctx->base->tp_itemsize == 034.9k ) { Branch (2987:9): [True: 34.9k, False: 51.7k]
Branch (2987:26): [True: 34.1k, False: 812]
|
2988 | assert((type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0); |
2989 | type->tp_flags |= Py_TPFLAGS_MANAGED_DICT; |
2990 | type->tp_dictoffset = -slotoffset - sizeof(PyObject *)*3; |
2991 | } |
2992 | |
2993 | type->tp_basicsize = slotoffset; |
2994 | type->tp_itemsize = ctx->base->tp_itemsize; |
2995 | type->tp_members = _PyHeapType_GET_MEMBERS(et); |
2996 | return 0; |
2997 | } |
2998 | |
2999 | |
3000 | static void |
3001 | type_new_set_slots(const type_new_ctx *ctx, PyTypeObject *type) |
3002 | { |
3003 | if (type->tp_weaklistoffset && type->tp_dictoffset37.3k ) { Branch (3003:9): [True: 37.3k, False: 49.3k]
Branch (3003:36): [True: 33.9k, False: 3.40k]
|
3004 | type->tp_getset = subtype_getsets_full; |
3005 | } |
3006 | else if (type->tp_weaklistoffset && !type->tp_dictoffset3.40k ) { Branch (3006:14): [True: 3.40k, False: 49.3k]
Branch (3006:41): [True: 3.40k, False: 0]
|
3007 | type->tp_getset = subtype_getsets_weakref_only; |
3008 | } |
3009 | else if (!type->tp_weaklistoffset && type->tp_dictoffset) { Branch (3009:14): [True: 49.3k, False: 0]
Branch (3009:42): [True: 970, False: 48.3k]
|
3010 | type->tp_getset = subtype_getsets_dict_only; |
3011 | } |
3012 | else { |
3013 | type->tp_getset = NULL; |
3014 | } |
3015 | |
3016 | /* Special case some slots */ |
3017 | if (type->tp_dictoffset != 0 || ctx->nslot > 051.7k ) { Branch (3017:9): [True: 34.9k, False: 51.7k]
Branch (3017:37): [True: 1.08k, False: 50.7k]
|
3018 | PyTypeObject *base = ctx->base; |
3019 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) { Branch (3019:13): [True: 36.0k, False: 0]
Branch (3019:41): [True: 0, False: 36.0k]
|
3020 | type->tp_getattro = PyObject_GenericGetAttr; |
3021 | } |
3022 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) { Branch (3022:13): [True: 36.0k, False: 0]
Branch (3022:41): [True: 0, False: 36.0k]
|
3023 | type->tp_setattro = PyObject_GenericSetAttr; |
3024 | } |
3025 | } |
3026 | } |
3027 | |
3028 | |
3029 | /* store type in class' cell if one is supplied */ |
3030 | static int |
3031 | type_new_set_classcell(PyTypeObject *type) |
3032 | { |
3033 | PyObject *cell = PyDict_GetItemWithError( |
3034 | type->tp_dict, &_Py_ID(__classcell__)); |
3035 | if (cell == NULL) { Branch (3035:9): [True: 83.5k, False: 3.16k]
|
3036 | if (PyErr_Occurred()) { Branch (3036:13): [True: 0, False: 83.5k]
|
3037 | return -1; |
3038 | } |
3039 | return 0; |
3040 | } |
3041 | |
3042 | /* At least one method requires a reference to its defining class */ |
3043 | if (!PyCell_Check(cell)) { Branch (3043:9): [True: 5, False: 3.16k]
|
3044 | PyErr_Format(PyExc_TypeError, |
3045 | "__classcell__ must be a nonlocal cell, not %.200R", |
3046 | Py_TYPE(cell)); |
3047 | return -1; |
3048 | } |
3049 | |
3050 | (void)PyCell_Set(cell, (PyObject *) type); |
3051 | if (PyDict_DelItem(type->tp_dict, &_Py_ID(__classcell__)) < 0) { Branch (3051:9): [True: 0, False: 3.16k]
|
3052 | return -1; |
3053 | } |
3054 | return 0; |
3055 | } |
3056 | |
3057 | |
3058 | static int |
3059 | type_new_set_attrs(const type_new_ctx *ctx, PyTypeObject *type) |
3060 | { |
3061 | if (type_new_set_name(ctx, type) < 0) { Branch (3061:9): [True: 2, False: 86.7k]
|
3062 | return -1; |
3063 | } |
3064 | |
3065 | if (type_new_set_module(type) < 0) { Branch (3065:9): [True: 0, False: 86.7k]
|
3066 | return -1; |
3067 | } |
3068 | |
3069 | if (type_new_set_ht_name(type) < 0) { Branch (3069:9): [True: 3, False: 86.7k]
|
3070 | return -1; |
3071 | } |
3072 | |
3073 | if (type_new_set_doc(type) < 0) { Branch (3073:9): [True: 1, False: 86.7k]
|
3074 | return -1; |
3075 | } |
3076 | |
3077 | /* Special-case __new__: if it's a plain function, |
3078 | make it a static function */ |
3079 | if (type_new_staticmethod(type, &_Py_ID(__new__)) < 0) { Branch (3079:9): [True: 0, False: 86.7k]
|
3080 | return -1; |
3081 | } |
3082 | |
3083 | /* Special-case __init_subclass__ and __class_getitem__: |
3084 | if they are plain functions, make them classmethods */ |
3085 | if (type_new_classmethod(type, &_Py_ID(__init_subclass__)) < 0) { Branch (3085:9): [True: 0, False: 86.7k]
|
3086 | return -1; |
3087 | } |
3088 | if (type_new_classmethod(type, &_Py_ID(__class_getitem__)) < 0) { Branch (3088:9): [True: 0, False: 86.7k]
|
3089 | return -1; |
3090 | } |
3091 | |
3092 | if (type_new_descriptors(ctx, type) < 0) { Branch (3092:9): [True: 0, False: 86.7k]
|
3093 | return -1; |
3094 | } |
3095 | |
3096 | type_new_set_slots(ctx, type); |
3097 | |
3098 | if (type_new_set_classcell(type) < 0) { Branch (3098:9): [True: 5, False: 86.7k]
|
3099 | return -1; |
3100 | } |
3101 | return 0; |
3102 | } |
3103 | |
3104 | |
3105 | static int |
3106 | type_new_get_slots(type_new_ctx *ctx, PyObject *dict) |
3107 | { |
3108 | PyObject *slots = PyDict_GetItemWithError(dict, &_Py_ID(__slots__)); |
3109 | if (slots == NULL) { Branch (3109:9): [True: 77.7k, False: 9.05k]
|
3110 | if (PyErr_Occurred()) { Branch (3110:13): [True: 0, False: 77.7k]
|
3111 | return -1; |
3112 | } |
3113 | ctx->slots = NULL; |
3114 | ctx->nslot = 0; |
3115 | return 0; |
3116 | } |
3117 | |
3118 | // Make it into a tuple |
3119 | PyObject *new_slots; |
3120 | if (PyUnicode_Check(slots)) { |
3121 | new_slots = PyTuple_Pack(1, slots); |
3122 | } |
3123 | else { |
3124 | new_slots = PySequence_Tuple(slots); |
3125 | } |
3126 | if (new_slots == NULL) { Branch (3126:9): [True: 1, False: 9.05k]
|
3127 | return -1; |
3128 | } |
3129 | assert(PyTuple_CheckExact(new_slots)); |
3130 | ctx->slots = new_slots; |
3131 | ctx->nslot = PyTuple_GET_SIZE(new_slots); |
3132 | return 0; |
3133 | } |
3134 | |
3135 | |
3136 | static PyTypeObject* |
3137 | type_new_init(type_new_ctx *ctx) |
3138 | { |
3139 | PyObject *dict = PyDict_Copy(ctx->orig_dict); |
3140 | if (dict == NULL) { Branch (3140:9): [True: 0, False: 86.7k]
|
3141 | goto error; |
3142 | } |
3143 | |
3144 | if (type_new_get_slots(ctx, dict) < 0) { Branch (3144:9): [True: 1, False: 86.7k]
|
3145 | goto error; |
3146 | } |
3147 | assert(!PyErr_Occurred()); |
3148 | |
3149 | if (type_new_slots(ctx, dict) < 0) { Branch (3149:9): [True: 18, False: 86.7k]
|
3150 | goto error; |
3151 | } |
3152 | |
3153 | PyTypeObject *type = type_new_alloc(ctx); |
3154 | if (type == NULL) { Branch (3154:9): [True: 0, False: 86.7k]
|
3155 | goto error; |
3156 | } |
3157 | |
3158 | type->tp_dict = dict; |
3159 | |
3160 | PyHeapTypeObject *et = (PyHeapTypeObject*)type; |
3161 | et->ht_slots = ctx->slots; |
3162 | ctx->slots = NULL; |
3163 | |
3164 | return type; |
3165 | |
3166 | error: |
3167 | Py_CLEAR(ctx->slots); |
3168 | Py_XDECREF(dict); |
3169 | return NULL; |
3170 | } |
3171 | |
3172 | |
3173 | static PyObject* |
3174 | type_new_impl(type_new_ctx *ctx) |
3175 | { |
3176 | PyTypeObject *type = type_new_init(ctx); |
3177 | if (type == NULL) { Branch (3177:9): [True: 19, False: 86.7k]
|
3178 | return NULL; |
3179 | } |
3180 | |
3181 | if (type_new_set_attrs(ctx, type) < 0) { Branch (3181:9): [True: 11, False: 86.7k]
|
3182 | goto error; |
3183 | } |
3184 | |
3185 | /* Initialize the rest */ |
3186 | if (PyType_Ready(type) < 0) { Branch (3186:9): [True: 16, False: 86.7k]
|
3187 | goto error; |
3188 | } |
3189 | |
3190 | // Put the proper slots in place |
3191 | fixup_slot_dispatchers(type); |
3192 | |
3193 | if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) { Branch (3193:9): [True: 68.2k, False: 18.5k]
|
3194 | PyHeapTypeObject *et = (PyHeapTypeObject*)type; |
3195 | et->ht_cached_keys = _PyDict_NewKeysForClass(); |
3196 | } |
3197 | |
3198 | if (type_new_set_names(type) < 0) { Branch (3198:9): [True: 12, False: 86.7k]
|
3199 | goto error; |
3200 | } |
3201 | |
3202 | if (type_new_init_subclass(type, ctx->kwds) < 0) { Branch (3202:9): [True: 44, False: 86.6k]
|
3203 | goto error; |
3204 | } |
3205 | |
3206 | assert(_PyType_CheckConsistency(type)); |
3207 | |
3208 | return (PyObject *)type; |
3209 | |
3210 | error: |
3211 | Py_DECREF(type); |
3212 | return NULL; |
3213 | } |
3214 | |
3215 | |
3216 | static int |
3217 | type_new_get_bases(type_new_ctx *ctx, PyObject **type) |
3218 | { |
3219 | Py_ssize_t nbases = PyTuple_GET_SIZE(ctx->bases); |
3220 | if (nbases == 0) { Branch (3220:9): [True: 27.8k, False: 59.2k]
|
3221 | // Adjust for empty tuple bases |
3222 | ctx->base = &PyBaseObject_Type; |
3223 | PyObject *new_bases = PyTuple_Pack(1, ctx->base); |
3224 | if (new_bases == NULL) { Branch (3224:13): [True: 0, False: 27.8k]
|
3225 | return -1; |
3226 | } |
3227 | ctx->bases = new_bases; |
3228 | return 0; |
3229 | } |
3230 | |
3231 | for (Py_ssize_t i = 0; 59.2k i < nbases; i++73.9k ) { Branch (3231:28): [True: 73.9k, False: 59.2k]
|
3232 | PyObject *base = PyTuple_GET_ITEM(ctx->bases, i); |
3233 | if (PyType_Check(base)) { |
3234 | continue; |
3235 | } |
3236 | PyObject *mro_entries; |
3237 | if (_PyObject_LookupAttr(base, &_Py_ID(__mro_entries__), Branch (3237:13): [True: 0, False: 2]
|
3238 | &mro_entries) < 0) { |
3239 | return -1; |
3240 | } |
3241 | if (mro_entries != NULL) { Branch (3241:13): [True: 1, False: 1]
|
3242 | PyErr_SetString(PyExc_TypeError, |
3243 | "type() doesn't support MRO entry resolution; " |
3244 | "use types.new_class()"); |
3245 | Py_DECREF(mro_entries); |
3246 | return -1; |
3247 | } |
3248 | } |
3249 | |
3250 | // Search the bases for the proper metatype to deal with this |
3251 | PyTypeObject *winner; |
3252 | winner = _PyType_CalculateMetaclass(ctx->metatype, ctx->bases); |
3253 | if (winner == NULL) { Branch (3253:9): [True: 1, False: 59.2k]
|
3254 | return -1; |
3255 | } |
3256 | |
3257 | if (winner != ctx->metatype) { Branch (3257:9): [True: 265, False: 58.9k]
|
3258 | if (winner->tp_new != type_new) { Branch (3258:13): [True: 265, False: 0]
|
3259 | /* Pass it to the winner */ |
3260 | *type = winner->tp_new(winner, ctx->args, ctx->kwds); |
3261 | if (*type == NULL) { Branch (3261:17): [True: 1, False: 264]
|
3262 | return -1; |
3263 | } |
3264 | return 1; |
3265 | } |
3266 | |
3267 | ctx->metatype = winner; |
3268 | } |
3269 | |
3270 | /* Calculate best base, and check that all bases are type objects */ |
3271 | PyTypeObject *base = best_base(ctx->bases); |
3272 | if (base == NULL) { Branch (3272:9): [True: 15, False: 58.9k]
|
3273 | return -1; |
3274 | } |
3275 | |
3276 | ctx->base = base; |
3277 | ctx->bases = Py_NewRef(ctx->bases); |
3278 | return 0; |
3279 | } |
3280 | |
3281 | |
3282 | static PyObject * |
3283 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
3284 | { |
3285 | assert(args != NULL && PyTuple_Check(args)); |
3286 | assert(kwds == NULL || PyDict_Check(kwds)); |
3287 | |
3288 | /* Parse arguments: (name, bases, dict) */ |
3289 | PyObject *name, *bases, *orig_dict; |
3290 | if (!PyArg_ParseTuple(args, "UO!O!:type.__new__", Branch (3290:9): [True: 7, False: 87.0k]
|
3291 | &name, |
3292 | &PyTuple_Type, &bases, |
3293 | &PyDict_Type, &orig_dict)) |
3294 | { |
3295 | return NULL; |
3296 | } |
3297 | |
3298 | type_new_ctx ctx = { |
3299 | .metatype = metatype, |
3300 | .args = args, |
3301 | .kwds = kwds, |
3302 | .orig_dict = orig_dict, |
3303 | .name = name, |
3304 | .bases = bases, |
3305 | .base = NULL, |
3306 | .slots = NULL, |
3307 | .nslot = 0, |
3308 | .add_dict = 0, |
3309 | .add_weak = 0, |
3310 | .may_add_dict = 0, |
3311 | .may_add_weak = 0}; |
3312 | PyObject *type = NULL; |
3313 | int res = type_new_get_bases(&ctx, &type); |
3314 | if (res < 0) { Branch (3314:9): [True: 18, False: 87.0k]
|
3315 | assert(PyErr_Occurred()); |
3316 | return NULL; |
3317 | } |
3318 | if (res == 1) { Branch (3318:9): [True: 264, False: 86.7k]
|
3319 | assert(type != NULL); |
3320 | return type; |
3321 | } |
3322 | assert(ctx.base != NULL); |
3323 | assert(ctx.bases != NULL); |
3324 | |
3325 | type = type_new_impl(&ctx); |
3326 | Py_DECREF(ctx.bases); |
3327 | return type; |
3328 | } |
3329 | |
3330 | |
3331 | static PyObject * |
3332 | type_vectorcall(PyObject *metatype, PyObject *const *args, |
3333 | size_t nargsf, PyObject *kwnames) |
3334 | { |
3335 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
3336 | if (nargs == 1 && metatype == (PyObject *)&PyType_Type265k ){ Branch (3336:9): [True: 265k, False: 69.6k]
Branch (3336:23): [True: 265k, False: 0]
|
3337 | if (!_PyArg_NoKwnames("type", kwnames)) { |
3338 | return NULL; |
3339 | } |
3340 | return Py_NewRef(Py_TYPE(args[0])); |
3341 | } |
3342 | /* In other (much less common) cases, fall back to |
3343 | more flexible calling conventions. */ |
3344 | PyThreadState *tstate = _PyThreadState_GET(); |
3345 | return _PyObject_MakeTpCall(tstate, metatype, args, nargs, kwnames); |
3346 | } |
3347 | |
3348 | /* An array of type slot offsets corresponding to Py_tp_* constants, |
3349 | * for use in e.g. PyType_Spec and PyType_GetSlot. |
3350 | * Each entry has two offsets: "slot_offset" and "subslot_offset". |
3351 | * If is subslot_offset is -1, slot_offset is an offset within the |
3352 | * PyTypeObject struct. |
3353 | * Otherwise slot_offset is an offset to a pointer to a sub-slots struct |
3354 | * (such as "tp_as_number"), and subslot_offset is the offset within |
3355 | * that struct. |
3356 | * The actual table is generated by a script. |
3357 | */ |
3358 | static const PySlot_Offset pyslot_offsets[] = { |
3359 | {0, 0}, |
3360 | #include "typeslots.inc" |
3361 | }; |
3362 | |
3363 | /* Given a PyType_FromMetaclass `bases` argument (NULL, type, or tuple of |
3364 | * types), return a tuple of types. |
3365 | */ |
3366 | inline static PyObject * |
3367 | get_bases_tuple(PyObject *bases_in, PyType_Spec *spec) |
3368 | { |
3369 | if (!bases_in) { Branch (3369:9): [True: 3.01k, False: 2.63k]
|
3370 | /* Default: look in the spec, fall back to (type,). */ |
3371 | PyTypeObject *base = &PyBaseObject_Type; // borrowed ref |
3372 | PyObject *bases = NULL; // borrowed ref |
3373 | const PyType_Slot *slot; |
3374 | for (slot = spec->slots; slot->slot; slot++19.0k ) { Branch (3374:34): [True: 19.0k, False: 3.01k]
|
3375 | switch (slot->slot) { Branch (3375:21): [True: 18.9k, False: 20]
|
3376 | case Py_tp_base: Branch (3376:17): [True: 20, False: 18.9k]
|
3377 | base = slot->pfunc; |
3378 | break; |
3379 | case Py_tp_bases: Branch (3379:17): [True: 0, False: 19.0k]
|
3380 | bases = slot->pfunc; |
3381 | break; |
3382 | } |
3383 | } |
3384 | if (!bases) { Branch (3384:13): [True: 3.01k, False: 0]
|
3385 | return PyTuple_Pack(1, base); |
3386 | } |
3387 | if (PyTuple_Check(bases)) { |
3388 | return Py_NewRef(bases); |
3389 | } |
3390 | PyErr_SetString(PyExc_SystemError, "Py_tp_bases is not a tuple"); |
3391 | return NULL; |
3392 | } |
3393 | if (PyTuple_Check(bases_in)) { |
3394 | return Py_NewRef(bases_in); |
3395 | } |
3396 | // Not a tuple, should be a single type |
3397 | return PyTuple_Pack(1, bases_in); |
3398 | } |
3399 | |
3400 | static inline int |
3401 | check_basicsize_includes_size_and_offsets(PyTypeObject* type) |
3402 | { |
3403 | if (type->tp_alloc != PyType_GenericAlloc) { Branch (3403:9): [True: 0, False: 5.65k]
|
3404 | // Custom allocators can ignore tp_basicsize |
3405 | return 1; |
3406 | } |
3407 | Py_ssize_t max = (Py_ssize_t)type->tp_basicsize; |
3408 | |
3409 | if (type->tp_base && type->tp_base->tp_basicsize > type->tp_basicsize) { Branch (3409:9): [True: 5.65k, False: 0]
Branch (3409:26): [True: 0, False: 5.65k]
|
3410 | PyErr_Format(PyExc_TypeError, |
3411 | "tp_basicsize for type '%s' (%d) is too small for base '%s' (%d)", |
3412 | type->tp_name, type->tp_basicsize, |
3413 | type->tp_base->tp_name, type->tp_base->tp_basicsize); |
3414 | return 0; |
3415 | } |
3416 | if (type->tp_weaklistoffset + (Py_ssize_t)sizeof(PyObject*) > max) { Branch (3416:9): [True: 0, False: 5.65k]
|
3417 | PyErr_Format(PyExc_TypeError, |
3418 | "weaklist offset %d is out of bounds for type '%s' (tp_basicsize = %d)", |
3419 | type->tp_weaklistoffset, |
3420 | type->tp_name, type->tp_basicsize); |
3421 | return 0; |
3422 | } |
3423 | if (type->tp_dictoffset + (Py_ssize_t)sizeof(PyObject*) > max) { Branch (3423:9): [True: 0, False: 5.65k]
|
3424 | PyErr_Format(PyExc_TypeError, |
3425 | "dict offset %d is out of bounds for type '%s' (tp_basicsize = %d)", |
3426 | type->tp_dictoffset, |
3427 | type->tp_name, type->tp_basicsize); |
3428 | return 0; |
3429 | } |
3430 | if (type->tp_vectorcall_offset + (Py_ssize_t)sizeof(vectorcallfunc*) > max) { Branch (3430:9): [True: 0, False: 5.65k]
|
3431 | PyErr_Format(PyExc_TypeError, |
3432 | "vectorcall offset %d is out of bounds for type '%s' (tp_basicsize = %d)", |
3433 | type->tp_vectorcall_offset, |
3434 | type->tp_name, type->tp_basicsize); |
3435 | return 0; |
3436 | } |
3437 | return 1; |
3438 | } |
3439 | |
3440 | PyObject * |
3441 | PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module, |
3442 | PyType_Spec *spec, PyObject *bases_in) |
3443 | { |
3444 | /* Invariant: A non-NULL value in one of these means this function holds |
3445 | * a strong reference or owns allocated memory. |
3446 | * These get decrefed/freed/returned at the end, on both success and error. |
3447 | */ |
3448 | PyHeapTypeObject *res = NULL; |
3449 | PyTypeObject *type; |
3450 | PyObject *bases = NULL; |
3451 | char *tp_doc = NULL; |
3452 | PyObject *ht_name = NULL; |
3453 | char *_ht_tpname = NULL; |
3454 | |
3455 | int r; |
3456 | |
3457 | /* Prepare slots that need special handling. |
3458 | * Keep in mind that a slot can be given multiple times: |
3459 | * if that would cause trouble (leaks, UB, ...), raise an exception. |
3460 | */ |
3461 | |
3462 | const PyType_Slot *slot; |
3463 | Py_ssize_t nmembers = 0; |
3464 | Py_ssize_t weaklistoffset, dictoffset, vectorcalloffset; |
3465 | char *res_start; |
3466 | |
3467 | nmembers = weaklistoffset = dictoffset = vectorcalloffset = 0; |
3468 | for (slot = spec->slots; slot->slot; slot++37.3k ) { Branch (3468:30): [True: 37.3k, False: 5.65k]
|
3469 | if (slot->slot < 0 Branch (3469:13): [True: 0, False: 37.3k]
|
3470 | || (size_t)slot->slot >= Py_ARRAY_LENGTH(pyslot_offsets)) { Branch (3470:16): [True: 0, False: 37.3k]
|
3471 | PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); |
3472 | goto finally; |
3473 | } |
3474 | switch (slot->slot) { Branch (3474:17): [True: 28.1k, False: 9.26k]
|
3475 | case Py_tp_members: Branch (3475:9): [True: 4.79k, False: 32.6k]
|
3476 | if (nmembers != 0) { Branch (3476:17): [True: 1, False: 4.79k]
|
3477 | PyErr_SetString( |
3478 | PyExc_SystemError, |
3479 | "Multiple Py_tp_members slots are not supported."); |
3480 | goto finally; |
3481 | } |
3482 | for (const PyMemberDef *memb = slot->pfunc; 4.79k memb->name != NULL; memb++21.3k ) { Branch (3482:57): [True: 21.3k, False: 4.79k]
|
3483 | nmembers++; |
3484 | if (strcmp(memb->name, "__weaklistoffset__") == 0) { Branch (3484:21): [True: 1.36k, False: 20.0k]
|
3485 | // The PyMemberDef must be a Py_ssize_t and readonly |
3486 | assert(memb->type == T_PYSSIZET); |
3487 | assert(memb->flags == READONLY); |
3488 | weaklistoffset = memb->offset; |
3489 | } |
3490 | if (strcmp(memb->name, "__dictoffset__") == 0) { Branch (3490:21): [True: 197, False: 21.1k]
|
3491 | // The PyMemberDef must be a Py_ssize_t and readonly |
3492 | assert(memb->type == T_PYSSIZET); |
3493 | assert(memb->flags == READONLY); |
3494 | dictoffset = memb->offset; |
3495 | } |
3496 | if (strcmp(memb->name, "__vectorcalloffset__") == 0) { Branch (3496:21): [True: 268, False: 21.1k]
|
3497 | // The PyMemberDef must be a Py_ssize_t and readonly |
3498 | assert(memb->type == T_PYSSIZET); |
3499 | assert(memb->flags == READONLY); |
3500 | vectorcalloffset = memb->offset; |
3501 | } |
3502 | } |
3503 | break; |
3504 | case Py_tp_doc: Branch (3504:9): [True: 4.47k, False: 32.9k]
|
3505 | /* For the docstring slot, which usually points to a static string |
3506 | literal, we need to make a copy */ |
3507 | if (tp_doc != NULL) { Branch (3507:17): [True: 1, False: 4.47k]
|
3508 | PyErr_SetString( |
3509 | PyExc_SystemError, |
3510 | "Multiple Py_tp_doc slots are not supported."); |
3511 | goto finally; |
3512 | } |
3513 | if (slot->pfunc == NULL) { Branch (3513:17): [True: 4, False: 4.46k]
|
3514 | PyObject_Free(tp_doc); |
3515 | tp_doc = NULL; |
3516 | } |
3517 | else { |
3518 | size_t len = strlen(slot->pfunc)+1; |
3519 | tp_doc = PyObject_Malloc(len); |
3520 | if (tp_doc == NULL) { Branch (3520:21): [True: 0, False: 4.46k]
|
3521 | PyErr_NoMemory(); |
3522 | goto finally; |
3523 | } |
3524 | memcpy(tp_doc, slot->pfunc, len); |
3525 | } |
3526 | break; |
3527 | } |
3528 | } |
3529 | |
3530 | /* Prepare the type name and qualname */ |
3531 | |
3532 | if (spec->name == NULL) { Branch (3532:9): [True: 0, False: 5.65k]
|
3533 | PyErr_SetString(PyExc_SystemError, |
3534 | "Type spec does not define the name field."); |
3535 | goto finally; |
3536 | } |
3537 | |
3538 | const char *s = strrchr(spec->name, '.'); |
3539 | if (s == NULL) { Branch (3539:9): [True: 0, False: 5.65k]
|
3540 | s = spec->name; |
3541 | } |
3542 | else { |
3543 | s++; |
3544 | } |
3545 | |
3546 | ht_name = PyUnicode_FromString(s); |
3547 | if (!ht_name) { Branch (3547:9): [True: 0, False: 5.65k]
|
3548 | goto finally; |
3549 | } |
3550 | |
3551 | /* Copy spec->name to a buffer we own. |
3552 | * |
3553 | * Unfortunately, we can't use tp_name directly (with some |
3554 | * flag saying that it should be deallocated with the type), |
3555 | * because tp_name is public API and may be set independently |
3556 | * of any such flag. |
3557 | * So, we use a separate buffer, _ht_tpname, that's always |
3558 | * deallocated with the type (if it's non-NULL). |
3559 | */ |
3560 | Py_ssize_t name_buf_len = strlen(spec->name) + 1; |
3561 | _ht_tpname = PyMem_Malloc(name_buf_len); |
3562 | if (_ht_tpname == NULL) { Branch (3562:9): [True: 0, False: 5.65k]
|
3563 | goto finally; |
3564 | } |
3565 | memcpy(_ht_tpname, spec->name, name_buf_len); |
3566 | |
3567 | /* Get a tuple of bases. |
3568 | * bases is a strong reference (unlike bases_in). |
3569 | */ |
3570 | bases = get_bases_tuple(bases_in, spec); |
3571 | if (!bases) { Branch (3571:9): [True: 0, False: 5.65k]
|
3572 | goto finally; |
3573 | } |
3574 | |
3575 | /* Calculate the metaclass */ |
3576 | |
3577 | if (!metaclass) { Branch (3577:9): [True: 5.64k, False: 4]
|
3578 | metaclass = &PyType_Type; |
3579 | } |
3580 | metaclass = _PyType_CalculateMetaclass(metaclass, bases); |
3581 | if (metaclass == NULL) { Branch (3581:9): [True: 1, False: 5.65k]
|
3582 | goto finally; |
3583 | } |
3584 | if (!PyType_Check(metaclass)) { Branch (3584:9): [True: 0, False: 5.65k]
|
3585 | PyErr_Format(PyExc_TypeError, |
3586 | "Metaclass '%R' is not a subclass of 'type'.", |
3587 | metaclass); |
3588 | goto finally; |
3589 | } |
3590 | if (metaclass->tp_new != PyType_Type.tp_new) { Branch (3590:9): [True: 1, False: 5.65k]
|
3591 | PyErr_SetString(PyExc_TypeError, |
3592 | "Metaclasses with custom tp_new are not supported."); |
3593 | goto finally; |
3594 | } |
3595 | |
3596 | /* Calculate best base, and check that all bases are type objects */ |
3597 | PyTypeObject *base = best_base(bases); // borrowed ref |
3598 | if (base == NULL) { Branch (3598:9): [True: 0, False: 5.65k]
|
3599 | goto finally; |
3600 | } |
3601 | // best_base should check Py_TPFLAGS_BASETYPE & raise a proper exception, |
3602 | // here we just check its work |
3603 | assert(_PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)); |
3604 | |
3605 | /* Allocate the new type |
3606 | * |
3607 | * Between here and PyType_Ready, we should limit: |
3608 | * - calls to Python code |
3609 | * - raising exceptions |
3610 | * - memory allocations |
3611 | */ |
3612 | |
3613 | res = (PyHeapTypeObject*)metaclass->tp_alloc(metaclass, nmembers); |
3614 | if (res == NULL) { Branch (3614:9): [True: 0, False: 5.65k]
|
3615 | goto finally; |
3616 | } |
3617 | res_start = (char*)res; |
3618 | |
3619 | type = &res->ht_type; |
3620 | /* The flags must be initialized early, before the GC traverses us */ |
3621 | type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; |
3622 | |
3623 | res->ht_module = Py_XNewRef(module); |
3624 | |
3625 | /* Initialize essential fields */ |
3626 | |
3627 | type->tp_as_async = &res->as_async; |
3628 | type->tp_as_number = &res->as_number; |
3629 | type->tp_as_sequence = &res->as_sequence; |
3630 | type->tp_as_mapping = &res->as_mapping; |
3631 | type->tp_as_buffer = &res->as_buffer; |
3632 | |
3633 | /* Set slots we have prepared */ |
3634 | |
3635 | type->tp_base = (PyTypeObject *)Py_NewRef(base); |
3636 | type->tp_bases = bases; |
3637 | bases = NULL; // We give our reference to bases to the type |
3638 | |
3639 | type->tp_doc = tp_doc; |
3640 | tp_doc = NULL; // Give ownership of the allocated memory to the type |
3641 | |
3642 | res->ht_qualname = Py_NewRef(ht_name); |
3643 | res->ht_name = ht_name; |
3644 | ht_name = NULL; // Give our reference to to the type |
3645 | |
3646 | type->tp_name = _ht_tpname; |
3647 | res->_ht_tpname = _ht_tpname; |
3648 | _ht_tpname = NULL; // Give ownership to to the type |
3649 | |
3650 | /* Copy the sizes */ |
3651 | |
3652 | type->tp_basicsize = spec->basicsize; |
3653 | type->tp_itemsize = spec->itemsize; |
3654 | |
3655 | /* Copy all the ordinary slots */ |
3656 | |
3657 | for (slot = spec->slots; slot->slot; slot++37.3k ) { Branch (3657:30): [True: 37.3k, False: 5.65k]
|
3658 | switch (slot->slot) { |
3659 | case Py_tp_base: Branch (3659:9): [True: 20, False: 37.3k]
|
3660 | case Py_tp_bases: Branch (3660:9): [True: 0, False: 37.3k]
|
3661 | case Py_tp_doc: Branch (3661:9): [True: 4.47k, False: 32.9k]
|
3662 | /* Processed above */ |
3663 | break; |
3664 | case Py_tp_members: Branch (3664:9): [True: 4.78k, False: 32.6k]
|
3665 | { |
3666 | /* Move the slots to the heap type itself */ |
3667 | size_t len = Py_TYPE(type)->tp_itemsize * nmembers; |
3668 | memcpy(_PyHeapType_GET_MEMBERS(res), slot->pfunc, len); |
3669 | type->tp_members = _PyHeapType_GET_MEMBERS(res); |
3670 | } |
3671 | break; |
3672 | default: Branch (3672:9): [True: 28.1k, False: 9.27k]
|
3673 | { |
3674 | /* Copy other slots directly */ |
3675 | PySlot_Offset slotoffsets = pyslot_offsets[slot->slot]; |
3676 | short slot_offset = slotoffsets.slot_offset; |
3677 | if (slotoffsets.subslot_offset == -1) { Branch (3677:21): [True: 28.0k, False: 98]
|
3678 | *(void**)((char*)res_start + slot_offset) = slot->pfunc; |
3679 | } |
3680 | else { |
3681 | void *procs = *(void**)((char*)res_start + slot_offset); |
3682 | short subslot_offset = slotoffsets.subslot_offset; |
3683 | *(void**)((char*)procs + subslot_offset) = slot->pfunc; |
3684 | } |
3685 | } |
3686 | break; |
3687 | } |
3688 | } |
3689 | if (type->tp_dealloc == NULL) { Branch (3689:9): [True: 67, False: 5.58k]
|
3690 | /* It's a heap type, so needs the heap types' dealloc. |
3691 | subtype_dealloc will call the base type's tp_dealloc, if |
3692 | necessary. */ |
3693 | type->tp_dealloc = subtype_dealloc; |
3694 | } |
3695 | |
3696 | /* Set up offsets */ |
3697 | |
3698 | type->tp_vectorcall_offset = vectorcalloffset; |
3699 | type->tp_weaklistoffset = weaklistoffset; |
3700 | type->tp_dictoffset = dictoffset; |
3701 | |
3702 | /* Ready the type (which includes inheritance). |
3703 | * |
3704 | * After this call we should generally only touch up what's |
3705 | * accessible to Python code, like __dict__. |
3706 | */ |
3707 | |
3708 | if (PyType_Ready(type) < 0) { Branch (3708:9): [True: 0, False: 5.65k]
|
3709 | goto finally; |
3710 | } |
3711 | |
3712 | if (!check_basicsize_includes_size_and_offsets(type)) { Branch (3712:9): [True: 0, False: 5.65k]
|
3713 | goto finally; |
3714 | } |
3715 | |
3716 | if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) { Branch (3716:9): [True: 1, False: 5.65k]
|
3717 | res->ht_cached_keys = _PyDict_NewKeysForClass(); |
3718 | } |
3719 | |
3720 | if (type->tp_doc) { Branch (3720:9): [True: 4.46k, False: 1.18k]
|
3721 | PyObject *__doc__ = PyUnicode_FromString(_PyType_DocWithoutSignature(type->tp_name, type->tp_doc)); |
3722 | if (!__doc__) { Branch (3722:13): [True: 0, False: 4.46k]
|
3723 | goto finally; |
3724 | } |
3725 | r = PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), __doc__); |
3726 | Py_DECREF(__doc__); |
3727 | if (r < 0) { Branch (3727:13): [True: 0, False: 4.46k]
|
3728 | goto finally; |
3729 | } |
3730 | } |
3731 | |
3732 | if (weaklistoffset) { Branch (3732:9): [True: 1.36k, False: 4.28k]
|
3733 | if (PyDict_DelItem((PyObject *)type->tp_dict, &_Py_ID(__weaklistoffset__)) < 0) { Branch (3733:13): [True: 0, False: 1.36k]
|
3734 | goto finally; |
3735 | } |
3736 | } |
3737 | if (dictoffset) { Branch (3737:9): [True: 197, False: 5.45k]
|
3738 | if (PyDict_DelItem((PyObject *)type->tp_dict, &_Py_ID(__dictoffset__)) < 0) { Branch (3738:13): [True: 0, False: 197]
|
3739 | goto finally; |
3740 | } |
3741 | } |
3742 | |
3743 | /* Set type.__module__ */ |
3744 | r = PyDict_Contains(type->tp_dict, &_Py_ID(__module__)); |
3745 | if (r < 0) { Branch (3745:9): [True: 0, False: 5.65k]
|
3746 | goto finally; |
3747 | } |
3748 | if (r == 0) { Branch (3748:9): [True: 5.65k, False: 0]
|
3749 | s = strrchr(spec->name, '.'); |
3750 | if (s != NULL) { Branch (3750:13): [True: 5.65k, False: 0]
|
3751 | PyObject *modname = PyUnicode_FromStringAndSize( |
3752 | spec->name, (Py_ssize_t)(s - spec->name)); |
3753 | if (modname == NULL) { Branch (3753:17): [True: 0, False: 5.65k]
|
3754 | goto finally; |
3755 | } |
3756 | r = PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), modname); |
3757 | Py_DECREF(modname); |
3758 | if (r != 0) { Branch (3758:17): [True: 0, False: 5.65k]
|
3759 | goto finally; |
3760 | } |
3761 | } |
3762 | else { |
3763 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, Branch (3763:17): [True: 0, False: 0]
|
3764 | "builtin type %.200s has no __module__ attribute", |
3765 | spec->name)) |
3766 | goto finally; |
3767 | } |
3768 | } |
3769 | |
3770 | assert(_PyType_CheckConsistency(type)); |
3771 | |
3772 | finally: |
3773 | if (PyErr_Occurred()) { Branch (3773:9): [True: 4, False: 5.65k]
|
3774 | Py_CLEAR(res); |
3775 | } |
3776 | Py_XDECREF(bases); |
3777 | PyObject_Free(tp_doc); |
3778 | Py_XDECREF(ht_name); |
3779 | PyMem_Free(_ht_tpname); |
3780 | return (PyObject*)res; |
3781 | } |
3782 | |
3783 | PyObject * |
3784 | PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases) |
3785 | { |
3786 | return PyType_FromMetaclass(NULL, module, spec, bases); |
3787 | } |
3788 | |
3789 | PyObject * |
3790 | PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) |
3791 | { |
3792 | return PyType_FromMetaclass(NULL, NULL, spec, bases); |
3793 | } |
3794 | |
3795 | PyObject * |
3796 | PyType_FromSpec(PyType_Spec *spec) |
3797 | { |
3798 | return PyType_FromMetaclass(NULL, NULL, spec, NULL); |
3799 | } |
3800 | |
3801 | PyObject * |
3802 | PyType_GetName(PyTypeObject *type) |
3803 | { |
3804 | return type_name(type, NULL); |
3805 | } |
3806 | |
3807 | PyObject * |
3808 | PyType_GetQualName(PyTypeObject *type) |
3809 | { |
3810 | return type_qualname(type, NULL); |
3811 | } |
3812 | |
3813 | void * |
3814 | PyType_GetSlot(PyTypeObject *type, int slot) |
3815 | { |
3816 | void *parent_slot; |
3817 | int slots_len = Py_ARRAY_LENGTH(pyslot_offsets); |
3818 | |
3819 | if (slot <= 0 || slot >= slots_len1.47M ) { Branch (3819:9): [True: 1, False: 1.47M]
Branch (3819:22): [True: 0, False: 1.47M]
|
3820 | PyErr_BadInternalCall(); |
3821 | return NULL; |
3822 | } |
3823 | |
3824 | parent_slot = *(void**)((char*)type + pyslot_offsets[slot].slot_offset); |
3825 | if (parent_slot == NULL) { Branch (3825:9): [True: 3, False: 1.47M]
|
3826 | return NULL; |
3827 | } |
3828 | /* Return slot directly if we have no sub slot. */ |
3829 | if (pyslot_offsets[slot].subslot_offset == -1) { Branch (3829:9): [True: 1.47M, False: 1]
|
3830 | return parent_slot; |
3831 | } |
3832 | return *(void**)((char*)parent_slot + pyslot_offsets[slot].subslot_offset); |
3833 | } |
3834 | |
3835 | PyObject * |
3836 | PyType_GetModule(PyTypeObject *type) |
3837 | { |
3838 | assert(PyType_Check(type)); |
3839 | if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) { Branch (3839:9): [True: 0, False: 5.27M]
|
3840 | PyErr_Format( |
3841 | PyExc_TypeError, |
3842 | "PyType_GetModule: Type '%s' is not a heap type", |
3843 | type->tp_name); |
3844 | return NULL; |
3845 | } |
3846 | |
3847 | PyHeapTypeObject* et = (PyHeapTypeObject*)type; |
3848 | if (!et->ht_module) { Branch (3848:9): [True: 0, False: 5.27M]
|
3849 | PyErr_Format( |
3850 | PyExc_TypeError, |
3851 | "PyType_GetModule: Type '%s' has no associated module", |
3852 | type->tp_name); |
3853 | return NULL; |
3854 | } |
3855 | return et->ht_module; |
3856 | |
3857 | } |
3858 | |
3859 | void * |
3860 | PyType_GetModuleState(PyTypeObject *type) |
3861 | { |
3862 | PyObject *m = PyType_GetModule(type); |
3863 | if (m == NULL) { Branch (3863:9): [True: 0, False: 155k]
|
3864 | return NULL; |
3865 | } |
3866 | return _PyModule_GetState(m); |
3867 | } |
3868 | |
3869 | |
3870 | /* Get the module of the first superclass where the module has the |
3871 | * given PyModuleDef. |
3872 | */ |
3873 | PyObject * |
3874 | PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def) |
3875 | { |
3876 | assert(PyType_Check(type)); |
3877 | |
3878 | PyObject *mro = type->tp_mro; |
3879 | // The type must be ready |
3880 | assert(mro != NULL); |
3881 | assert(PyTuple_Check(mro)); |
3882 | // mro_invoke() ensures that the type MRO cannot be empty, so we don't have |
3883 | // to check i < PyTuple_GET_SIZE(mro) at the first loop iteration. |
3884 | assert(PyTuple_GET_SIZE(mro) >= 1); |
3885 | |
3886 | Py_ssize_t n = PyTuple_GET_SIZE(mro); |
3887 | for (Py_ssize_t i = 0; i < n; i++36.5k ) { Branch (3887:28): [True: 10.6M, False: 1]
|
3888 | PyObject *super = PyTuple_GET_ITEM(mro, i); |
3889 | if(!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) { Branch (3889:12): [True: 1, False: 10.6M]
|
3890 | // Static types in the MRO need to be skipped |
3891 | continue; |
3892 | } |
3893 | |
3894 | PyHeapTypeObject *ht = (PyHeapTypeObject*)super; |
3895 | PyObject *module = ht->ht_module; |
3896 | if (module && _PyModule_GetDef(module) == def10.6M ) { Branch (3896:13): [True: 10.6M, False: 36.5k]
Branch (3896:23): [True: 10.6M, False: 1]
|
3897 | return module; |
3898 | } |
3899 | } |
3900 | |
3901 | PyErr_Format( |
3902 | PyExc_TypeError, |
3903 | "PyType_GetModuleByDef: No superclass of '%s' has the given module", |
3904 | type->tp_name); |
3905 | return NULL; |
3906 | } |
3907 | |
3908 | |
3909 | /* Internal API to look for a name through the MRO, bypassing the method cache. |
3910 | This returns a borrowed reference, and might set an exception. |
3911 | 'error' is set to: -1: error with exception; 1: error without exception; 0: ok */ |
3912 | static PyObject * |
3913 | find_name_in_mro(PyTypeObject *type, PyObject *name, int *error) |
3914 | { |
3915 | Py_hash_t hash; |
3916 | if (!PyUnicode_CheckExact(name) || Branch (3916:9): [True: 6, False: 14.4M]
|
3917 | (hash = 14.4M _PyASCIIObject_CAST14.4M (name)->hash) == -1) Branch (3917:9): [True: 3.09M, False: 11.3M]
|
3918 | { |
3919 | hash = PyObject_Hash(name); |
3920 | if (hash == -1) { Branch (3920:13): [True: 0, False: 3.09M]
|
3921 | *error = -1; |
3922 | return NULL; |
3923 | } |
3924 | } |
3925 | |
3926 | /* Look in tp_dict of types in MRO */ |
3927 | PyObject *mro = type->tp_mro; |
3928 | if (mro == NULL) { Branch (3928:9): [True: 3, False: 14.4M]
|
3929 | if ((type->tp_flags & Py_TPFLAGS_READYING) == 0) { Branch (3929:13): [True: 1, False: 2]
|
3930 | if (PyType_Ready(type) < 0) { Branch (3930:17): [True: 0, False: 1]
|
3931 | *error = -1; |
3932 | return NULL; |
3933 | } |
3934 | mro = type->tp_mro; |
3935 | } |
3936 | if (mro == NULL) { Branch (3936:13): [True: 2, False: 1]
|
3937 | *error = 1; |
3938 | return NULL; |
3939 | } |
3940 | } |
3941 | |
3942 | PyObject *res = NULL; |
3943 | /* Keep a strong reference to mro because type->tp_mro can be replaced |
3944 | during dict lookup, e.g. when comparing to non-string keys. */ |
3945 | Py_INCREF(mro); |
3946 | Py_ssize_t n = PyTuple_GET_SIZE(mro); |
3947 | for (Py_ssize_t i = 0; i < n; i++38.7M ) { Branch (3947:28): [True: 44.8M, False: 8.34M]
|
3948 | PyObject *base = PyTuple_GET_ITEM(mro, i); |
3949 | PyObject *dict = _PyType_CAST(base)->tp_dict; |
3950 | assert(dict && PyDict_Check(dict)); |
3951 | res = _PyDict_GetItem_KnownHash(dict, name, hash); |
3952 | if (res != NULL) { Branch (3952:13): [True: 6.13M, False: 38.7M]
|
3953 | break; |
3954 | } |
3955 | if (PyErr_Occurred()) { Branch (3955:13): [True: 0, False: 38.7M]
|
3956 | *error = -1; |
3957 | goto done; |
3958 | } |
3959 | } |
3960 | *error = 0; |
3961 | done: |
3962 | Py_DECREF(mro); |
3963 | return res; |
3964 | } |
3965 | |
3966 | /* Internal API to look for a name through the MRO. |
3967 | This returns a borrowed reference, and doesn't set an exception! */ |
3968 | PyObject * |
3969 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
3970 | { |
3971 | PyObject *res; |
3972 | int error; |
3973 | |
3974 | unsigned int h = MCACHE_HASH_METHOD(type, name); |
3975 | struct type_cache *cache = get_type_cache(); |
3976 | struct type_cache_entry *entry = &cache->hashtable[h]; |
3977 | if (entry->version == type->tp_version_tag && Branch (3977:9): [True: 235M, False: 2.64M]
|
3978 | entry->name == name235M ) { Branch (3978:9): [True: 232M, False: 3.14M]
|
3979 | #if MCACHE_STATS |
3980 | cache->hits++; |
3981 | #endif |
3982 | assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)); |
3983 | return entry->value; |
3984 | } |
3985 | |
3986 | /* We may end up clearing live exceptions below, so make sure it's ours. */ |
3987 | assert(!PyErr_Occurred()); |
3988 | |
3989 | res = find_name_in_mro(type, name, &error); |
3990 | /* Only put NULL results into cache if there was no error. */ |
3991 | if (error) { Branch (3991:9): [True: 2, False: 5.78M]
|
3992 | /* It's not ideal to clear the error condition, |
3993 | but this function is documented as not setting |
3994 | an exception, and I don't want to change that. |
3995 | E.g., when PyType_Ready() can't proceed, it won't |
3996 | set the "ready" flag, so future attempts to ready |
3997 | the same type will call it again -- hopefully |
3998 | in a context that propagates the exception out. |
3999 | */ |
4000 | if (error == -1) { Branch (4000:13): [True: 0, False: 2]
|
4001 | PyErr_Clear(); |
4002 | } |
4003 | return NULL; |
4004 | } |
4005 | |
4006 | if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(cache, type)5.78M ) { Branch (4006:40): [True: 5.78M, False: 0]
|
4007 | h = MCACHE_HASH_METHOD(type, name); |
4008 | struct type_cache_entry *entry = &cache->hashtable[h]; |
4009 | entry->version = type->tp_version_tag; |
4010 | entry->value = res; /* borrowed */ |
4011 | assert(_PyASCIIObject_CAST(name)->hash != -1); |
4012 | #if MCACHE_STATS |
4013 | if (entry->name != Py_None && entry->name != name) { |
4014 | cache->collisions++; |
4015 | } |
4016 | else { |
4017 | cache->misses++; |
4018 | } |
4019 | #endif |
4020 | assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)); |
4021 | Py_SETREF(entry->name, Py_NewRef(name)); |
4022 | } |
4023 | return res; |
4024 | } |
4025 | |
4026 | PyObject * |
4027 | _PyType_LookupId(PyTypeObject *type, _Py_Identifier *name) |
4028 | { |
4029 | PyObject *oname; |
4030 | oname = _PyUnicode_FromId(name); /* borrowed */ |
4031 | if (oname == NULL) Branch (4031:9): [True: 0, False: 0]
|
4032 | return NULL; |
4033 | return _PyType_Lookup(type, oname); |
4034 | } |
4035 | |
4036 | /* Check if the "readied" PyUnicode name |
4037 | is a double-underscore special name. */ |
4038 | static int |
4039 | is_dunder_name(PyObject *name) |
4040 | { |
4041 | Py_ssize_t length = PyUnicode_GET_LENGTH(name); |
4042 | int kind = PyUnicode_KIND(name); |
4043 | /* Special names contain at least "__x__" and are always ASCII. */ |
4044 | if (length > 4 && kind == PyUnicode_1BYTE_KIND488k ) { Branch (4044:9): [True: 488k, False: 7.01k]
Branch (4044:23): [True: 488k, False: 0]
|
4045 | const Py_UCS1 *characters = PyUnicode_1BYTE_DATA(name); |
4046 | return ( |
4047 | ((characters[length-2] == '_') && (characters[length-1] == '_')424k ) && Branch (4047:14): [True: 424k, False: 64.5k]
Branch (4047:47): [True: 424k, False: 183]
|
4048 | (424k (characters[0] == '_')424k && (characters[1] == '_')424k ) Branch (4048:14): [True: 424k, False: 0]
Branch (4048:40): [True: 424k, False: 0]
|
4049 | ); |
4050 | } |
4051 | return 0; |
4052 | } |
4053 | |
4054 | /* This is similar to PyObject_GenericGetAttr(), |
4055 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
4056 | static PyObject * |
4057 | type_getattro(PyTypeObject *type, PyObject *name) |
4058 | { |
4059 | PyTypeObject *metatype = Py_TYPE(type); |
4060 | PyObject *meta_attribute, *attribute; |
4061 | descrgetfunc meta_get; |
4062 | PyObject* res; |
4063 | |
4064 | if (!PyUnicode_Check(name)) { Branch (4064:9): [True: 1, False: 8.11M]
|
4065 | PyErr_Format(PyExc_TypeError, |
4066 | "attribute name must be string, not '%.200s'", |
4067 | Py_TYPE(name)->tp_name); |
4068 | return NULL; |
4069 | } |
4070 | |
4071 | /* Initialize this type (we'll assume the metatype is initialized) */ |
4072 | if (!_PyType_IsReady(type)) { Branch (4072:9): [True: 1, False: 8.11M]
|
4073 | if (PyType_Ready(type) < 0) Branch (4073:13): [True: 0, False: 1]
|
4074 | return NULL; |
4075 | } |
4076 | |
4077 | /* No readable descriptor found yet */ |
4078 | meta_get = NULL; |
4079 | |
4080 | /* Look for the attribute in the metatype */ |
4081 | meta_attribute = _PyType_Lookup(metatype, name); |
4082 | |
4083 | if (meta_attribute != NULL) { Branch (4083:9): [True: 5.88M, False: 2.23M]
|
4084 | Py_INCREF(meta_attribute); |
4085 | meta_get = Py_TYPE(meta_attribute)->tp_descr_get; |
4086 | |
4087 | if (meta_get != NULL && PyDescr_IsData(meta_attribute)3.52M ) { Branch (4087:13): [True: 3.52M, False: 2.36M]
Branch (4087:33): [True: 2.35M, False: 1.16M]
|
4088 | /* Data descriptors implement tp_descr_set to intercept |
4089 | * writes. Assume the attribute is not overridden in |
4090 | * type's tp_dict (and bases): call the descriptor now. |
4091 | */ |
4092 | res = meta_get(meta_attribute, (PyObject *)type, |
4093 | (PyObject *)metatype); |
4094 | Py_DECREF(meta_attribute); |
4095 | return res; |
4096 | } |
4097 | } |
4098 | |
4099 | /* No data descriptor found on metatype. Look in tp_dict of this |
4100 | * type and its bases */ |
4101 | attribute = _PyType_Lookup(type, name); |
4102 | if (attribute != NULL) { Branch (4102:9): [True: 5.60M, False: 162k]
|
4103 | /* Implement descriptor functionality, if any */ |
4104 | Py_INCREF(attribute); |
4105 | descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get; |
4106 | |
4107 | Py_XDECREF(meta_attribute); |
4108 | |
4109 | if (local_get != NULL) { Branch (4109:13): [True: 4.38M, False: 1.21M]
|
4110 | /* NULL 2nd argument indicates the descriptor was |
4111 | * found on the target object itself (or a base) */ |
4112 | res = local_get(attribute, (PyObject *)NULL, |
4113 | (PyObject *)type); |
4114 | Py_DECREF(attribute); |
4115 | return res; |
4116 | } |
4117 | |
4118 | return attribute; |
4119 | } |
4120 | |
4121 | /* No attribute found in local __dict__ (or bases): use the |
4122 | * descriptor from the metatype, if any */ |
4123 | if (meta_get != NULL) { Branch (4123:9): [True: 36.5k, False: 125k]
|
4124 | PyObject *res; |
4125 | res = meta_get(meta_attribute, (PyObject *)type, |
4126 | (PyObject *)metatype); |
4127 | Py_DECREF(meta_attribute); |
4128 | return res; |
4129 | } |
4130 | |
4131 | /* If an ordinary attribute was found on the metatype, return it now */ |
4132 | if (meta_attribute != NULL) { Branch (4132:9): [True: 6, False: 125k]
|
4133 | return meta_attribute; |
4134 | } |
4135 | |
4136 | /* Give up */ |
4137 | PyErr_Format(PyExc_AttributeError, |
4138 | "type object '%.50s' has no attribute '%U'", |
4139 | type->tp_name, name); |
4140 | return NULL; |
4141 | } |
4142 | |
4143 | static int |
4144 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
4145 | { |
4146 | int res; |
4147 | if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) { Branch (4147:9): [True: 585, False: 495k]
|
4148 | PyErr_Format( |
4149 | PyExc_TypeError, |
4150 | "cannot set %R attribute of immutable type '%s'", |
4151 | name, type->tp_name); |
4152 | return -1; |
4153 | } |
4154 | if (PyUnicode_Check(name)) { |
4155 | if (PyUnicode_CheckExact(name)) { |
4156 | if (PyUnicode_READY(name) == -1) Branch (4156:17): [True: 0, False: 495k]
|
4157 | return -1; |
4158 | Py_INCREF(name); |
4159 | } |
4160 | else { |
4161 | name = _PyUnicode_Copy(name); |
4162 | if (name == NULL) Branch (4162:17): [True: 0, False: 0]
|
4163 | return -1; |
4164 | } |
4165 | /* bpo-40521: Interned strings are shared by all subinterpreters */ |
4166 | if (!PyUnicode_CHECK_INTERNED(name)) { Branch (4166:13): [True: 2, False: 495k]
|
4167 | PyUnicode_InternInPlace(&name); |
4168 | if (!PyUnicode_CHECK_INTERNED(name)) { Branch (4168:17): [True: 0, False: 2]
|
4169 | PyErr_SetString(PyExc_MemoryError, |
4170 | "Out of memory interning an attribute name"); |
4171 | Py_DECREF(name); |
4172 | return -1; |
4173 | } |
4174 | } |
4175 | } |
4176 | else { |
4177 | /* Will fail in _PyObject_GenericSetAttrWithDict. */ |
4178 | Py_INCREF(name); |
4179 | } |
4180 | res = _PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL); |
4181 | if (res == 0) { Branch (4181:9): [True: 495k, False: 50]
|
4182 | /* Clear the VALID_VERSION flag of 'type' and all its |
4183 | subclasses. This could possibly be unified with the |
4184 | update_subclasses() recursion in update_slot(), but carefully: |
4185 | they each have their own conditions on which to stop |
4186 | recursing into subclasses. */ |
4187 | PyType_Modified(type); |
4188 | |
4189 | if (is_dunder_name(name)) { Branch (4189:13): [True: 424k, False: 71.7k]
|
4190 | res = update_slot(type, name); |
4191 | } |
4192 | assert(_PyType_CheckConsistency(type)); |
4193 | } |
4194 | Py_DECREF(name); |
4195 | return res; |
4196 | } |
4197 | |
4198 | extern void |
4199 | _PyDictKeys_DecRef(PyDictKeysObject *keys); |
4200 | |
4201 | |
4202 | static void |
4203 | type_dealloc_common(PyTypeObject *type) |
4204 | { |
4205 | if (type->tp_bases != NULL) { Branch (4205:9): [True: 111k, False: 0]
|
4206 | PyObject *tp, *val, *tb; |
4207 | PyErr_Fetch(&tp, &val, &tb); |
4208 | remove_all_subclasses(type, type->tp_bases); |
4209 | PyErr_Restore(tp, val, tb); |
4210 | } |
4211 | } |
4212 | |
4213 | |
4214 | void |
4215 | _PyStaticType_Dealloc(PyTypeObject *type) |
4216 | { |
4217 | // If a type still has subtypes, it cannot be deallocated. |
4218 | // A subtype can inherit attributes and methods of its parent type, |
4219 | // and a type must no longer be used once it's deallocated. |
4220 | if (type->tp_subclasses != NULL) { Branch (4220:9): [True: 103, False: 20.3k]
|
4221 | return; |
4222 | } |
4223 | |
4224 | type_dealloc_common(type); |
4225 | |
4226 | Py_CLEAR(type->tp_dict); |
4227 | Py_CLEAR(type->tp_bases); |
4228 | Py_CLEAR(type->tp_mro); |
4229 | Py_CLEAR(type->tp_cache); |
4230 | // type->tp_subclasses is NULL |
4231 | |
4232 | // PyObject_ClearWeakRefs() raises an exception if Py_REFCNT() != 0 |
4233 | if (Py_REFCNT(type) == 0) { Branch (4233:9): [True: 0, False: 20.3k]
|
4234 | PyObject_ClearWeakRefs((PyObject *)type); |
4235 | } |
4236 | |
4237 | type->tp_flags &= ~Py_TPFLAGS_READY; |
4238 | } |
4239 | |
4240 | |
4241 | static void |
4242 | type_dealloc(PyTypeObject *type) |
4243 | { |
4244 | // Assert this is a heap-allocated type object |
4245 | _PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
4246 | |
4247 | _PyObject_GC_UNTRACK(type); |
4248 | |
4249 | type_dealloc_common(type); |
4250 | |
4251 | // PyObject_ClearWeakRefs() raises an exception if Py_REFCNT() != 0 |
4252 | assert(Py_REFCNT(type) == 0); |
4253 | PyObject_ClearWeakRefs((PyObject *)type); |
4254 | |
4255 | Py_XDECREF(type->tp_base); |
4256 | Py_XDECREF(type->tp_dict); |
4257 | Py_XDECREF(type->tp_bases); |
4258 | Py_XDECREF(type->tp_mro); |
4259 | Py_XDECREF(type->tp_cache); |
4260 | Py_XDECREF(type->tp_subclasses); |
4261 | |
4262 | /* A type's tp_doc is heap allocated, unlike the tp_doc slots |
4263 | * of most other objects. It's okay to cast it to char *. |
4264 | */ |
4265 | PyObject_Free((char *)type->tp_doc); |
4266 | |
4267 | PyHeapTypeObject *et = (PyHeapTypeObject *)type; |
4268 | Py_XDECREF(et->ht_name); |
4269 | Py_XDECREF(et->ht_qualname); |
4270 | Py_XDECREF(et->ht_slots); |
4271 | if (et->ht_cached_keys) { Branch (4271:9): [True: 67.8k, False: 23.4k]
|
4272 | _PyDictKeys_DecRef(et->ht_cached_keys); |
4273 | } |
4274 | Py_XDECREF(et->ht_module); |
4275 | PyMem_Free(et->_ht_tpname); |
4276 | Py_TYPE(type)->tp_free((PyObject *)type); |
4277 | } |
4278 | |
4279 | |
4280 | PyObject* |
4281 | _PyType_GetSubclasses(PyTypeObject *self) |
4282 | { |
4283 | PyObject *list = PyList_New(0); |
4284 | if (list == NULL) { Branch (4284:9): [True: 0, False: 18.7k]
|
4285 | return NULL; |
4286 | } |
4287 | |
4288 | PyObject *subclasses = self->tp_subclasses; // borrowed ref |
4289 | if (subclasses == NULL) { Branch (4289:9): [True: 14.7k, False: 3.98k]
|
4290 | return list; |
4291 | } |
4292 | assert(PyDict_CheckExact(subclasses)); |
4293 | // The loop cannot modify tp_subclasses, there is no need |
4294 | // to hold a strong reference (use a borrowed reference). |
4295 | |
4296 | Py_ssize_t i = 0; |
4297 | PyObject *ref; // borrowed ref |
4298 | while (PyDict_Next(subclasses, &i, NULL, &ref)) { Branch (4298:12): [True: 15.0k, False: 3.98k]
|
4299 | assert(PyWeakref_CheckRef(ref)); |
4300 | PyObject *obj = PyWeakref_GET_OBJECT(ref); // borrowed ref |
4301 | if (obj == Py_None) { Branch (4301:13): [True: 0, False: 15.0k]
|
4302 | continue; |
4303 | } |
4304 | assert(PyType_Check(obj)); |
4305 | |
4306 | if (PyList_Append(list, obj) < 0) { Branch (4306:13): [True: 0, False: 15.0k]
|
4307 | Py_DECREF(list); |
4308 | return NULL; |
4309 | } |
4310 | } |
4311 | return list; |
4312 | } |
4313 | |
4314 | |
4315 | /*[clinic input] |
4316 | type.__subclasses__ |
4317 | |
4318 | Return a list of immediate subclasses. |
4319 | [clinic start generated code]*/ |
4320 | |
4321 | static PyObject * |
4322 | type___subclasses___impl(PyTypeObject *self) |
4323 | /*[clinic end generated code: output=eb5eb54485942819 input=5af66132436f9a7b]*/ |
4324 | { |
4325 | return _PyType_GetSubclasses(self); |
4326 | } |
4327 | |
4328 | static PyObject * |
4329 | type_prepare(PyObject *self, PyObject *const *args, Py_ssize_t nargs, |
4330 | PyObject *kwnames) |
4331 | { |
4332 | return PyDict_New(); |
4333 | } |
4334 | |
4335 | |
4336 | /* |
4337 | Merge the __dict__ of aclass into dict, and recursively also all |
4338 | the __dict__s of aclass's base classes. The order of merging isn't |
4339 | defined, as it's expected that only the final set of dict keys is |
4340 | interesting. |
4341 | Return 0 on success, -1 on error. |
4342 | */ |
4343 | |
4344 | static int |
4345 | merge_class_dict(PyObject *dict, PyObject *aclass) |
4346 | { |
4347 | PyObject *classdict; |
4348 | PyObject *bases; |
4349 | |
4350 | assert(PyDict_Check(dict)); |
4351 | assert(aclass); |
4352 | |
4353 | /* Merge in the type's dict (if any). */ |
4354 | if (_PyObject_LookupAttr(aclass, &_Py_ID(__dict__), &classdict) < 0) { Branch (4354:9): [True: 0, False: 57.5k]
|
4355 | return -1; |
4356 | } |
4357 | if (classdict != NULL) { Branch (4357:9): [True: 57.5k, False: 0]
|
4358 | int status = PyDict_Update(dict, classdict); |
4359 | Py_DECREF(classdict); |
4360 | if (status < 0) Branch (4360:13): [True: 0, False: 57.5k]
|
4361 | return -1; |
4362 | } |
4363 | |
4364 | /* Recursively merge in the base types' (if any) dicts. */ |
4365 | if (_PyObject_LookupAttr(aclass, &_Py_ID(__bases__), &bases) < 0) { Branch (4365:9): [True: 0, False: 57.5k]
|
4366 | return -1; |
4367 | } |
4368 | if (bases != NULL) { Branch (4368:9): [True: 57.5k, False: 0]
|
4369 | /* We have no guarantee that bases is a real tuple */ |
4370 | Py_ssize_t i, n; |
4371 | n = PySequence_Size(bases); /* This better be right */ |
4372 | if (n < 0) { Branch (4372:13): [True: 0, False: 57.5k]
|
4373 | Py_DECREF(bases); |
4374 | return -1; |
4375 | } |
4376 | else { |
4377 | for (i = 0; i < n; i++39.4k ) { Branch (4377:25): [True: 39.4k, False: 57.5k]
|
4378 | int status; |
4379 | PyObject *base = PySequence_GetItem(bases, i); |
4380 | if (base == NULL) { Branch (4380:21): [True: 0, False: 39.4k]
|
4381 | Py_DECREF(bases); |
4382 | return -1; |
4383 | } |
4384 | status = merge_class_dict(dict, base); |
4385 | Py_DECREF(base); |
4386 | if (status < 0) { Branch (4386:21): [True: 0, False: 39.4k]
|
4387 | Py_DECREF(bases); |
4388 | return -1; |
4389 | } |
4390 | } |
4391 | } |
4392 | Py_DECREF(bases); |
4393 | } |
4394 | return 0; |
4395 | } |
4396 | |
4397 | /* __dir__ for type objects: returns __dict__ and __bases__. |
4398 | We deliberately don't suck up its __class__, as methods belonging to the |
4399 | metaclass would probably be more confusing than helpful. |
4400 | */ |
4401 | /*[clinic input] |
4402 | type.__dir__ |
4403 | |
4404 | Specialized __dir__ implementation for types. |
4405 | [clinic start generated code]*/ |
4406 | |
4407 | static PyObject * |
4408 | type___dir___impl(PyTypeObject *self) |
4409 | /*[clinic end generated code: output=69d02fe92c0f15fa input=7733befbec645968]*/ |
4410 | { |
4411 | PyObject *result = NULL; |
4412 | PyObject *dict = PyDict_New(); |
4413 | |
4414 | if (dict != NULL && merge_class_dict(dict, (PyObject *)self) == 0) Branch (4414:9): [True: 6.68k, False: 0]
Branch (4414:25): [True: 6.68k, False: 0]
|
4415 | result = PyDict_Keys(dict); |
4416 | |
4417 | Py_XDECREF(dict); |
4418 | return result; |
4419 | } |
4420 | |
4421 | /*[clinic input] |
4422 | type.__sizeof__ |
4423 | |
4424 | Return memory consumption of the type object. |
4425 | [clinic start generated code]*/ |
4426 | |
4427 | static PyObject * |
4428 | type___sizeof___impl(PyTypeObject *self) |
4429 | /*[clinic end generated code: output=766f4f16cd3b1854 input=99398f24b9cf45d6]*/ |
4430 | { |
4431 | Py_ssize_t size; |
4432 | if (self->tp_flags & Py_TPFLAGS_HEAPTYPE) { Branch (4432:9): [True: 2, False: 1]
|
4433 | PyHeapTypeObject* et = (PyHeapTypeObject*)self; |
4434 | size = sizeof(PyHeapTypeObject); |
4435 | if (et->ht_cached_keys) Branch (4435:13): [True: 2, False: 0]
|
4436 | size += _PyDict_KeysSize(et->ht_cached_keys); |
4437 | } |
4438 | else |
4439 | size = sizeof(PyTypeObject); |
4440 | return PyLong_FromSsize_t(size); |
4441 | } |
4442 | |
4443 | static PyMethodDef type_methods[] = { |
4444 | TYPE_MRO_METHODDEF |
4445 | TYPE___SUBCLASSES___METHODDEF |
4446 | {"__prepare__", _PyCFunction_CAST(type_prepare), |
4447 | METH_FASTCALL | METH_KEYWORDS | METH_CLASS, |
4448 | PyDoc_STR("__prepare__() -> dict\n" |
4449 | "used to create the namespace for the class statement")}, |
4450 | TYPE___INSTANCECHECK___METHODDEF |
4451 | TYPE___SUBCLASSCHECK___METHODDEF |
4452 | TYPE___DIR___METHODDEF |
4453 | TYPE___SIZEOF___METHODDEF |
4454 | {0} |
4455 | }; |
4456 | |
4457 | PyDoc_STRVAR(type_doc, |
4458 | "type(object) -> the object's type\n" |
4459 | "type(name, bases, dict, **kwds) -> a new type"); |
4460 | |
4461 | static int |
4462 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
4463 | { |
4464 | /* Because of type_is_gc(), the collector only calls this |
4465 | for heaptypes. */ |
4466 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { Branch (4466:9): [True: 0, False: 184M]
|
4467 | char msg[200]; |
4468 | sprintf(msg, "type_traverse() called on non-heap type '%.100s'", |
4469 | type->tp_name); |
4470 | _PyObject_ASSERT_FAILED_MSG((PyObject *)type, msg); |
4471 | } |
4472 | |
4473 | Py_VISIT(type->tp_dict); |
4474 | Py_VISIT(type->tp_cache); |
4475 | Py_VISIT(type->tp_mro); |
4476 | Py_VISIT(type->tp_bases); |
4477 | Py_VISIT(type->tp_base); |
4478 | Py_VISIT(((PyHeapTypeObject *)type)->ht_module); |
4479 | |
4480 | /* There's no need to visit others because they can't be involved |
4481 | in cycles: |
4482 | type->tp_subclasses is a list of weak references, |
4483 | ((PyHeapTypeObject *)type)->ht_slots is a tuple of strings, |
4484 | ((PyHeapTypeObject *)type)->ht_*name are strings. |
4485 | */ |
4486 | |
4487 | return 0; |
4488 | } |
4489 | |
4490 | static int |
4491 | type_clear(PyTypeObject *type) |
4492 | { |
4493 | /* Because of type_is_gc(), the collector only calls this |
4494 | for heaptypes. */ |
4495 | _PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
4496 | |
4497 | /* We need to invalidate the method cache carefully before clearing |
4498 | the dict, so that other objects caught in a reference cycle |
4499 | don't start calling destroyed methods. |
4500 | |
4501 | Otherwise, the we need to clear tp_mro, which is |
4502 | part of a hard cycle (its first element is the class itself) that |
4503 | won't be broken otherwise (it's a tuple and tuples don't have a |
4504 | tp_clear handler). |
4505 | We also need to clear ht_module, if present: the module usually holds a |
4506 | reference to its class. None of the other fields need to be |
4507 | |
4508 | cleared, and here's why: |
4509 | |
4510 | tp_cache: |
4511 | Not used; if it were, it would be a dict. |
4512 | |
4513 | tp_bases, tp_base: |
4514 | If these are involved in a cycle, there must be at least |
4515 | one other, mutable object in the cycle, e.g. a base |
4516 | class's dict; the cycle will be broken that way. |
4517 | |
4518 | tp_subclasses: |
4519 | A dict of weak references can't be part of a cycle; and |
4520 | dicts have their own tp_clear. |
4521 | |
4522 | slots (in PyHeapTypeObject): |
4523 | A tuple of strings can't be part of a cycle. |
4524 | */ |
4525 | |
4526 | PyType_Modified(type); |
4527 | if (type->tp_dict) { Branch (4527:9): [True: 91.2k, False: 0]
|
4528 | PyDict_Clear(type->tp_dict); |
4529 | } |
4530 | Py_CLEAR(((PyHeapTypeObject *)type)->ht_module); |
4531 | |
4532 | Py_CLEAR(type->tp_mro); |
4533 | |
4534 | return 0; |
4535 | } |
4536 | |
4537 | static int |
4538 | type_is_gc(PyTypeObject *type) |
4539 | { |
4540 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
4541 | } |
4542 | |
4543 | |
4544 | static PyNumberMethods type_as_number = { |
4545 | .nb_or = _Py_union_type_or, // Add __or__ function |
4546 | }; |
4547 | |
4548 | PyTypeObject PyType_Type = { |
4549 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
4550 | "type", /* tp_name */ |
4551 | sizeof(PyHeapTypeObject), /* tp_basicsize */ |
4552 | sizeof(PyMemberDef), /* tp_itemsize */ |
4553 | (destructor)type_dealloc, /* tp_dealloc */ |
4554 | offsetof(PyTypeObject, tp_vectorcall), /* tp_vectorcall_offset */ |
4555 | 0, /* tp_getattr */ |
4556 | 0, /* tp_setattr */ |
4557 | 0, /* tp_as_async */ |
4558 | (reprfunc)type_repr, /* tp_repr */ |
4559 | &type_as_number, /* tp_as_number */ |
4560 | 0, /* tp_as_sequence */ |
4561 | 0, /* tp_as_mapping */ |
4562 | 0, /* tp_hash */ |
4563 | (ternaryfunc)type_call, /* tp_call */ |
4564 | 0, /* tp_str */ |
4565 | (getattrofunc)type_getattro, /* tp_getattro */ |
4566 | (setattrofunc)type_setattro, /* tp_setattro */ |
4567 | 0, /* tp_as_buffer */ |
4568 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
4569 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS | |
4570 | Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */ |
4571 | type_doc, /* tp_doc */ |
4572 | (traverseproc)type_traverse, /* tp_traverse */ |
4573 | (inquiry)type_clear, /* tp_clear */ |
4574 | 0, /* tp_richcompare */ |
4575 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
4576 | 0, /* tp_iter */ |
4577 | 0, /* tp_iternext */ |
4578 | type_methods, /* tp_methods */ |
4579 | type_members, /* tp_members */ |
4580 | type_getsets, /* tp_getset */ |
4581 | 0, /* tp_base */ |
4582 | 0, /* tp_dict */ |
4583 | 0, /* tp_descr_get */ |
4584 | 0, /* tp_descr_set */ |
4585 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
4586 | type_init, /* tp_init */ |
4587 | 0, /* tp_alloc */ |
4588 | type_new, /* tp_new */ |
4589 | PyObject_GC_Del, /* tp_free */ |
4590 | (inquiry)type_is_gc, /* tp_is_gc */ |
4591 | .tp_vectorcall = type_vectorcall, |
4592 | }; |
4593 | |
4594 | |
4595 | /* The base type of all types (eventually)... except itself. */ |
4596 | |
4597 | /* You may wonder why object.__new__() only complains about arguments |
4598 | when object.__init__() is not overridden, and vice versa. |
4599 | |
4600 | Consider the use cases: |
4601 | |
4602 | 1. When neither is overridden, we want to hear complaints about |
4603 | excess (i.e., any) arguments, since their presence could |
4604 | indicate there's a bug. |
4605 | |
4606 | 2. When defining an Immutable type, we are likely to override only |
4607 | __new__(), since __init__() is called too late to initialize an |
4608 | Immutable object. Since __new__() defines the signature for the |
4609 | type, it would be a pain to have to override __init__() just to |
4610 | stop it from complaining about excess arguments. |
4611 | |
4612 | 3. When defining a Mutable type, we are likely to override only |
4613 | __init__(). So here the converse reasoning applies: we don't |
4614 | want to have to override __new__() just to stop it from |
4615 | complaining. |
4616 | |
4617 | 4. When __init__() is overridden, and the subclass __init__() calls |
4618 | object.__init__(), the latter should complain about excess |
4619 | arguments; ditto for __new__(). |
4620 | |
4621 | Use cases 2 and 3 make it unattractive to unconditionally check for |
4622 | excess arguments. The best solution that addresses all four use |
4623 | cases is as follows: __init__() complains about excess arguments |
4624 | unless __new__() is overridden and __init__() is not overridden |
4625 | (IOW, if __init__() is overridden or __new__() is not overridden); |
4626 | symmetrically, __new__() complains about excess arguments unless |
4627 | __init__() is overridden and __new__() is not overridden |
4628 | (IOW, if __new__() is overridden or __init__() is not overridden). |
4629 | |
4630 | However, for backwards compatibility, this breaks too much code. |
4631 | Therefore, in 2.6, we'll *warn* about excess arguments when both |
4632 | methods are overridden; for all other cases we'll use the above |
4633 | rules. |
4634 | |
4635 | */ |
4636 | |
4637 | /* Forward */ |
4638 | static PyObject * |
4639 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
4640 | |
4641 | static int |
4642 | excess_args(PyObject *args, PyObject *kwds) |
4643 | { |
4644 | return PyTuple_GET_SIZE(args) || |
4645 | (6.82M kwds6.82M && PyDict_Check6.82M (kwds) && PyDict_GET_SIZE746k (kwds)); Branch (4645:10): [True: 746k, False: 6.08M]
|
4646 | } |
4647 | |
4648 | static int |
4649 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
4650 | { |
4651 | PyTypeObject *type = Py_TYPE(self); |
4652 | if (excess_args(args, kwds)) { Branch (4652:9): [True: 9.44M, False: 1.47M]
|
4653 | if (type->tp_init != object_init) { Branch (4653:13): [True: 5, False: 9.44M]
|
4654 | PyErr_SetString(PyExc_TypeError, |
4655 | "object.__init__() takes exactly one argument (the instance to initialize)"); |
4656 | return -1; |
4657 | } |
4658 | if (type->tp_new == object_new) { Branch (4658:13): [True: 3, False: 9.44M]
|
4659 | PyErr_Format(PyExc_TypeError, |
4660 | "%.200s.__init__() takes exactly one argument (the instance to initialize)", |
4661 | type->tp_name); |
4662 | return -1; |
4663 | } |
4664 | } |
4665 | return 0; |
4666 | } |
4667 | |
4668 | static PyObject * |
4669 | object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
4670 | { |
4671 | if (excess_args(args, kwds)) { Branch (4671:9): [True: 5.03M, False: 5.22M]
|
4672 | if (type->tp_new != object_new) { Branch (4672:13): [True: 9, False: 5.03M]
|
4673 | PyErr_SetString(PyExc_TypeError, |
4674 | "object.__new__() takes exactly one argument (the type to instantiate)"); |
4675 | return NULL; |
4676 | } |
4677 | if (type->tp_init == object_init) { Branch (4677:13): [True: 57, False: 5.03M]
|
4678 | PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments", |
4679 | type->tp_name); |
4680 | return NULL; |
4681 | } |
4682 | } |
4683 | |
4684 | if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) { Branch (4684:9): [True: 80, False: 10.2M]
|
4685 | PyObject *abstract_methods; |
4686 | PyObject *sorted_methods; |
4687 | PyObject *joined; |
4688 | Py_ssize_t method_count; |
4689 | |
4690 | /* Compute ", ".join(sorted(type.__abstractmethods__)) |
4691 | into joined. */ |
4692 | abstract_methods = type_abstractmethods(type, NULL); |
4693 | if (abstract_methods == NULL) Branch (4693:13): [True: 0, False: 80]
|
4694 | return NULL; |
4695 | sorted_methods = PySequence_List(abstract_methods); |
4696 | Py_DECREF(abstract_methods); |
4697 | if (sorted_methods == NULL) Branch (4697:13): [True: 0, False: 80]
|
4698 | return NULL; |
4699 | if (PyList_Sort(sorted_methods)) { Branch (4699:13): [True: 0, False: 80]
|
4700 | Py_DECREF(sorted_methods); |
4701 | return NULL; |
4702 | } |
4703 | _Py_DECLARE_STR(comma_sep, ", "); |
4704 | joined = PyUnicode_Join(&_Py_STR(comma_sep), sorted_methods); |
4705 | method_count = PyObject_Length(sorted_methods); |
4706 | Py_DECREF(sorted_methods); |
4707 | if (joined == NULL) Branch (4707:13): [True: 0, False: 80]
|
4708 | return NULL; |
4709 | if (method_count == -1) Branch (4709:13): [True: 0, False: 80]
|
4710 | return NULL; |
4711 | |
4712 | PyErr_Format(PyExc_TypeError, |
4713 | "Can't instantiate abstract class %s " |
4714 | "without an implementation for abstract method%s %U", |
4715 | type->tp_name, |
4716 | method_count > 1 ? "s"12 : ""68 , Branch (4716:22): [True: 12, False: 68]
|
4717 | joined); |
4718 | Py_DECREF(joined); |
4719 | return NULL; |
4720 | } |
4721 | PyObject *obj = type->tp_alloc(type, 0); |
4722 | if (obj == NULL) { Branch (4722:9): [True: 0, False: 10.2M]
|
4723 | return NULL; |
4724 | } |
4725 | if (_PyObject_InitializeDict(obj)) { Branch (4725:9): [True: 0, False: 10.2M]
|
4726 | Py_DECREF(obj); |
4727 | return NULL; |
4728 | } |
4729 | return obj; |
4730 | } |
4731 | |
4732 | static void |
4733 | object_dealloc(PyObject *self) |
4734 | { |
4735 | Py_TYPE(self)->tp_free(self); |
4736 | } |
4737 | |
4738 | static PyObject * |
4739 | object_repr(PyObject *self) |
4740 | { |
4741 | PyTypeObject *type; |
4742 | PyObject *mod, *name, *rtn; |
4743 | |
4744 | type = Py_TYPE(self); |
4745 | mod = type_module(type, NULL); |
4746 | if (mod == NULL) Branch (4746:9): [True: 0, False: 3.91k]
|
4747 | PyErr_Clear(); |
4748 | else if (!PyUnicode_Check(mod)) { Branch (4748:14): [True: 0, False: 3.91k]
|
4749 | Py_DECREF(mod); |
4750 | mod = NULL; |
4751 | } |
4752 | name = type_qualname(type, NULL); |
4753 | if (name == NULL) { Branch (4753:9): [True: 0, False: 3.91k]
|
4754 | Py_XDECREF(mod); |
4755 | return NULL; |
4756 | } |
4757 | if (mod != NULL && !_PyUnicode_Equal(mod, &_Py_ID(builtins))) Branch (4757:9): [True: 3.91k, False: 0]
Branch (4757:24): [True: 3.79k, False: 123]
|
4758 | rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); |
4759 | else |
4760 | rtn = PyUnicode_FromFormat("<%s object at %p>", |
4761 | type->tp_name, self); |
4762 | Py_XDECREF(mod); |
4763 | Py_DECREF(name); |
4764 | return rtn; |
4765 | } |
4766 | |
4767 | static PyObject * |
4768 | object_str(PyObject *self) |
4769 | { |
4770 | unaryfunc f; |
4771 | |
4772 | f = Py_TYPE(self)->tp_repr; |
4773 | if (f == NULL) Branch (4773:9): [True: 0, False: 738k]
|
4774 | f = object_repr; |
4775 | return f(self); |
4776 | } |
4777 | |
4778 | static PyObject * |
4779 | object_richcompare(PyObject *self, PyObject *other, int op) |
4780 | { |
4781 | PyObject *res; |
4782 | |
4783 | switch (op) { |
4784 | |
4785 | case Py_EQ: Branch (4785:5): [True: 8.02M, False: 36.4k]
|
4786 | /* Return NotImplemented instead of False, so if two |
4787 | objects are compared, both get a chance at the |
4788 | comparison. See issue #1393. */ |
4789 | res = (self == other) ? Py_True : Py_NotImplemented7.35M ; Branch (4789:15): [True: 660k, False: 7.35M]
|
4790 | Py_INCREF(res); |
4791 | break; |
4792 | |
4793 | case Py_NE: Branch (4793:5): [True: 34.4k, False: 8.02M]
|
4794 | /* By default, __ne__() delegates to __eq__() and inverts the result, |
4795 | unless the latter returns NotImplemented. */ |
4796 | if (Py_TYPE(self)->tp_richcompare == NULL) { Branch (4796:13): [True: 0, False: 34.4k]
|
4797 | res = Py_NotImplemented; |
4798 | Py_INCREF(res); |
4799 | break; |
4800 | } |
4801 | res = (*Py_TYPE(self)->tp_richcompare)(self, other, Py_EQ); |
4802 | if (res != NULL && res != 34.4k Py_NotImplemented34.4k ) { Branch (4802:13): [True: 34.4k, False: 16]
Branch (4802:28): [True: 13.0k, False: 21.3k]
|
4803 | int ok = PyObject_IsTrue(res); |
4804 | Py_DECREF(res); |
4805 | if (ok < 0) Branch (4805:17): [True: 0, False: 13.0k]
|
4806 | res = NULL; |
4807 | else { |
4808 | if (ok) Branch (4808:21): [True: 11.4k, False: 1.56k]
|
4809 | res = Py_False; |
4810 | else |
4811 | res = Py_True; |
4812 | Py_INCREF(res); |
4813 | } |
4814 | } |
4815 | break; |
4816 | |
4817 | default: Branch (4817:5): [True: 2.03k, False: 8.05M]
|
4818 | res = Py_NotImplemented; |
4819 | Py_INCREF(res); |
4820 | break; |
4821 | } |
4822 | |
4823 | return res; |
4824 | } |
4825 | |
4826 | static PyObject * |
4827 | object_get_class(PyObject *self, void *closure) |
4828 | { |
4829 | Py_INCREF(Py_TYPE(self)); |
4830 | return (PyObject *)(Py_TYPE(self)); |
4831 | } |
4832 | |
4833 | static int |
4834 | compatible_with_tp_base(PyTypeObject *child) |
4835 | { |
4836 | PyTypeObject *parent = child->tp_base; |
4837 | return (parent != NULL && Branch (4837:13): [True: 828, False: 30]
|
4838 | child->tp_basicsize == parent->tp_basicsize828 && Branch (4838:13): [True: 144, False: 684]
|
4839 | child->tp_itemsize == parent->tp_itemsize144 && Branch (4839:13): [True: 144, False: 0]
|
4840 | child->tp_dictoffset == parent->tp_dictoffset144 && Branch (4840:13): [True: 128, False: 16]
|
4841 | child->tp_weaklistoffset == parent->tp_weaklistoffset128 && Branch (4841:13): [True: 128, False: 0]
|
4842 | ((child->tp_flags & 128 Py_TPFLAGS_HAVE_GC128 ) == Branch (4842:13): [True: 112, False: 16]
|
4843 | (parent->tp_flags & Py_TPFLAGS_HAVE_GC)) && |
4844 | (112 child->tp_dealloc == subtype_dealloc112 || Branch (4844:14): [True: 112, False: 0]
|
4845 | child->tp_dealloc == parent->tp_dealloc0 )); Branch (4845:14): [True: 0, False: 0]
|
4846 | } |
4847 | |
4848 | static int |
4849 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
4850 | { |
4851 | PyTypeObject *base = a->tp_base; |
4852 | Py_ssize_t size; |
4853 | PyObject *slots_a, *slots_b; |
4854 | |
4855 | assert(base == b->tp_base); |
4856 | size = base->tp_basicsize; |
4857 | if (a->tp_dictoffset == size && b->tp_dictoffset == size0 ) Branch (4857:9): [True: 0, False: 151]
Branch (4857:37): [True: 0, False: 0]
|
4858 | size += sizeof(PyObject *); |
4859 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size135 ) Branch (4859:9): [True: 135, False: 16]
Branch (4859:41): [True: 133, False: 2]
|
4860 | size += sizeof(PyObject *); |
4861 | |
4862 | /* Check slots compliance */ |
4863 | if (!(a->tp_flags & Py_TPFLAGS_HEAPTYPE) || Branch (4863:9): [True: 2, False: 149]
|
4864 | !(b->tp_flags & 149 Py_TPFLAGS_HEAPTYPE149 )) { Branch (4864:9): [True: 0, False: 149]
|
4865 | return 0; |
4866 | } |
4867 | slots_a = ((PyHeapTypeObject *)a)->ht_slots; |
4868 | slots_b = ((PyHeapTypeObject *)b)->ht_slots; |
4869 | if (slots_a && slots_b20 ) { Branch (4869:9): [True: 20, False: 129]
Branch (4869:20): [True: 18, False: 2]
|
4870 | if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1) Branch (4870:13): [True: 6, False: 12]
|
4871 | return 0; |
4872 | size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a); |
4873 | } |
4874 | return size == a->tp_basicsize && size == b->tp_basicsize141 ; Branch (4874:12): [True: 141, False: 2]
Branch (4874:39): [True: 139, False: 2]
|
4875 | } |
4876 | |
4877 | static int |
4878 | compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, const char* attr) |
4879 | { |
4880 | PyTypeObject *newbase, *oldbase; |
4881 | |
4882 | if (newto->tp_free != oldto->tp_free) { Branch (4882:9): [True: 0, False: 373]
|
4883 | PyErr_Format(PyExc_TypeError, |
4884 | "%s assignment: " |
4885 | "'%s' deallocator differs from '%s'", |
4886 | attr, |
4887 | newto->tp_name, |
4888 | oldto->tp_name); |
4889 | return 0; |
4890 | } |
4891 | /* |
4892 | It's tricky to tell if two arbitrary types are sufficiently compatible as |
4893 | to be interchangeable; e.g., even if they have the same tp_basicsize, they |
4894 | might have totally different struct fields. It's much easier to tell if a |
4895 | type and its supertype are compatible; e.g., if they have the same |
4896 | tp_basicsize, then that means they have identical fields. So to check |
4897 | whether two arbitrary types are compatible, we first find the highest |
4898 | supertype that each is compatible with, and then if those supertypes are |
4899 | compatible then the original types must also be compatible. |
4900 | */ |
4901 | newbase = newto; |
4902 | oldbase = oldto; |
4903 | while (compatible_with_tp_base(newbase)) Branch (4903:12): [True: 89, False: 373]
|
4904 | newbase = newbase->tp_base; |
4905 | while (compatible_with_tp_base(oldbase)) Branch (4905:12): [True: 23, False: 373]
|
4906 | oldbase = oldbase->tp_base; |
4907 | if (newbase != oldbase && Branch (4907:9): [True: 211, False: 162]
|
4908 | (211 newbase->tp_base != oldbase->tp_base211 || Branch (4908:10): [True: 60, False: 151]
|
4909 | !same_slots_added(newbase, oldbase)151 )) { Branch (4909:10): [True: 12, False: 139]
|
4910 | goto differs; |
4911 | } |
4912 | /* The above does not check for managed __dicts__ */ |
4913 | if ((oldto->tp_flags & Py_TPFLAGS_MANAGED_DICT) == Branch (4913:9): [True: 299, False: 2]
|
4914 | ((newto->tp_flags & Py_TPFLAGS_MANAGED_DICT))) |
4915 | { |
4916 | return 1; |
4917 | } |
4918 | differs: |
4919 | PyErr_Format(PyExc_TypeError, |
4920 | "%s assignment: " |
4921 | "'%s' object layout differs from '%s'", |
4922 | attr, |
4923 | newto->tp_name, |
4924 | oldto->tp_name); |
4925 | return 0; |
4926 | } |
4927 | |
4928 | static int |
4929 | object_set_class(PyObject *self, PyObject *value, void *closure) |
4930 | { |
4931 | PyTypeObject *oldto = Py_TYPE(self); |
4932 | |
4933 | if (value == NULL) { Branch (4933:9): [True: 101, False: 354]
|
4934 | PyErr_SetString(PyExc_TypeError, |
4935 | "can't delete __class__ attribute"); |
4936 | return -1; |
4937 | } |
4938 | if (!PyType_Check(value)) { Branch (4938:9): [True: 1, False: 353]
|
4939 | PyErr_Format(PyExc_TypeError, |
4940 | "__class__ must be set to a class, not '%s' object", |
4941 | Py_TYPE(value)->tp_name); |
4942 | return -1; |
4943 | } |
4944 | PyTypeObject *newto = (PyTypeObject *)value; |
4945 | |
4946 | if (PySys_Audit("object.__setattr__", "OsO", Branch (4946:9): [True: 0, False: 353]
|
4947 | self, "__class__", value) < 0) { |
4948 | return -1; |
4949 | } |
4950 | |
4951 | /* In versions of CPython prior to 3.5, the code in |
4952 | compatible_for_assignment was not set up to correctly check for memory |
4953 | layout / slot / etc. compatibility for non-HEAPTYPE classes, so we just |
4954 | disallowed __class__ assignment in any case that wasn't HEAPTYPE -> |
4955 | HEAPTYPE. |
4956 | |
4957 | During the 3.5 development cycle, we fixed the code in |
4958 | compatible_for_assignment to correctly check compatibility between |
4959 | arbitrary types, and started allowing __class__ assignment in all cases |
4960 | where the old and new types did in fact have compatible slots and |
4961 | memory layout (regardless of whether they were implemented as HEAPTYPEs |
4962 | or not). |
4963 | |
4964 | Just before 3.5 was released, though, we discovered that this led to |
4965 | problems with immutable types like int, where the interpreter assumes |
4966 | they are immutable and interns some values. Formerly this wasn't a |
4967 | problem, because they really were immutable -- in particular, all the |
4968 | types where the interpreter applied this interning trick happened to |
4969 | also be statically allocated, so the old HEAPTYPE rules were |
4970 | "accidentally" stopping them from allowing __class__ assignment. But |
4971 | with the changes to __class__ assignment, we started allowing code like |
4972 | |
4973 | class MyInt(int): |
4974 | ... |
4975 | # Modifies the type of *all* instances of 1 in the whole program, |
4976 | # including future instances (!), because the 1 object is interned. |
4977 | (1).__class__ = MyInt |
4978 | |
4979 | (see https://bugs.python.org/issue24912). |
4980 | |
4981 | In theory the proper fix would be to identify which classes rely on |
4982 | this invariant and somehow disallow __class__ assignment only for them, |
4983 | perhaps via some mechanism like a new Py_TPFLAGS_IMMUTABLE flag (a |
4984 | "denylisting" approach). But in practice, since this problem wasn't |
4985 | noticed late in the 3.5 RC cycle, we're taking the conservative |
4986 | approach and reinstating the same HEAPTYPE->HEAPTYPE check that we used |
4987 | to have, plus an "allowlist". For now, the allowlist consists only of |
4988 | ModuleType subtypes, since those are the cases that motivated the patch |
4989 | in the first place -- see https://bugs.python.org/issue22986 -- and |
4990 | since module objects are mutable we can be sure that they are |
4991 | definitely not being interned. So now we allow HEAPTYPE->HEAPTYPE *or* |
4992 | ModuleType subtype -> ModuleType subtype. |
4993 | |
4994 | So far as we know, all the code beyond the following 'if' statement |
4995 | will correctly handle non-HEAPTYPE classes, and the HEAPTYPE check is |
4996 | needed only to protect that subset of non-HEAPTYPE classes for which |
4997 | the interpreter has baked in the assumption that all instances are |
4998 | truly immutable. |
4999 | */ |
5000 | if (!(PyType_IsSubtype(newto, &PyModule_Type) && Branch (5000:11): [True: 20, False: 333]
|
5001 | PyType_IsSubtype(oldto, &PyModule_Type)20 ) && Branch (5001:11): [True: 20, False: 0]
|
5002 | (333 _PyType_HasFeature(newto, 333 Py_TPFLAGS_IMMUTABLETYPE333 ) || Branch (5002:10): [True: 17, False: 316]
|
5003 | _PyType_HasFeature(oldto, 316 Py_TPFLAGS_IMMUTABLETYPE316 ))) { Branch (5003:10): [True: 18, False: 298]
|
5004 | PyErr_Format(PyExc_TypeError, |
5005 | "__class__ assignment only supported for mutable types " |
5006 | "or ModuleType subclasses"); |
5007 | return -1; |
5008 | } |
5009 | |
5010 | if (compatible_for_assignment(oldto, newto, "__class__")) { Branch (5010:9): [True: 246, False: 72]
|
5011 | /* Changing the class will change the implicit dict keys, |
5012 | * so we must materialize the dictionary first. */ |
5013 | assert((oldto->tp_flags & Py_TPFLAGS_MANAGED_DICT) == (newto->tp_flags & Py_TPFLAGS_MANAGED_DICT)); |
5014 | _PyObject_GetDictPtr(self); |
5015 | if (oldto->tp_flags & Py_TPFLAGS_MANAGED_DICT && *_PyObject_ValuesPointer(self)219 ) { Branch (5015:13): [True: 219, False: 27]
Branch (5015:58): [True: 0, False: 219]
|
5016 | /* Was unable to convert to dict */ |
5017 | PyErr_NoMemory(); |
5018 | return -1; |
5019 | } |
5020 | if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) { Branch (5020:13): [True: 236, False: 10]
|
5021 | Py_INCREF(newto); |
5022 | } |
5023 | Py_SET_TYPE(self, newto); |
5024 | if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) Branch (5024:13): [True: 236, False: 10]
|
5025 | Py_DECREF(oldto); |
5026 | return 0; |
5027 | } |
5028 | else { |
5029 | return -1; |
5030 | } |
5031 | } |
5032 | |
5033 | static PyGetSetDef object_getsets[] = { |
5034 | {"__class__", object_get_class, object_set_class, |
5035 | PyDoc_STR("the object's class")}, |
5036 | {0} |
5037 | }; |
5038 | |
5039 | |
5040 | /* Stuff to implement __reduce_ex__ for pickle protocols >= 2. |
5041 | We fall back to helpers in copyreg for: |
5042 | - pickle protocols < 2 |
5043 | - calculating the list of slot names (done only once per class) |
5044 | - the __newobj__ function (which is used as a token but never called) |
5045 | */ |
5046 | |
5047 | static PyObject * |
5048 | import_copyreg(void) |
5049 | { |
5050 | /* Try to fetch cached copy of copyreg from sys.modules first in an |
5051 | attempt to avoid the import overhead. Previously this was implemented |
5052 | by storing a reference to the cached module in a static variable, but |
5053 | this broke when multiple embedded interpreters were in use (see issue |
5054 | #17408 and #19088). */ |
5055 | PyObject *copyreg_module = PyImport_GetModule(&_Py_ID(copyreg)); |
5056 | if (copyreg_module != NULL) { Branch (5056:9): [True: 81.9k, False: 0]
|
5057 | return copyreg_module; |
5058 | } |
5059 | if (PyErr_Occurred()) { Branch (5059:9): [True: 0, False: 0]
|
5060 | return NULL; |
5061 | } |
5062 | return PyImport_Import(&_Py_ID(copyreg)); |
5063 | } |
5064 | |
5065 | static PyObject * |
5066 | _PyType_GetSlotNames(PyTypeObject *cls) |
5067 | { |
5068 | PyObject *copyreg; |
5069 | PyObject *slotnames; |
5070 | |
5071 | assert(PyType_Check(cls)); |
5072 | |
5073 | /* Get the slot names from the cache in the class if possible. */ |
5074 | slotnames = PyDict_GetItemWithError(cls->tp_dict, &_Py_ID(__slotnames__)); |
5075 | if (slotnames != NULL) { Branch (5075:9): [True: 75.6k, False: 811]
|
5076 | if (slotnames != Py_None && !PyList_Check(slotnames)) { Branch (5076:13): [True: 75.6k, False: 0]
Branch (5076:37): [True: 0, False: 75.6k]
|
5077 | PyErr_Format(PyExc_TypeError, |
5078 | "%.200s.__slotnames__ should be a list or None, " |
5079 | "not %.200s", |
5080 | cls->tp_name, Py_TYPE(slotnames)->tp_name); |
5081 | return NULL; |
5082 | } |
5083 | Py_INCREF(slotnames); |
5084 | return slotnames; |
5085 | } |
5086 | else { |
5087 | if (PyErr_Occurred()) { Branch (5087:13): [True: 0, False: 811]
|
5088 | return NULL; |
5089 | } |
5090 | /* The class does not have the slot names cached yet. */ |
5091 | } |
5092 | |
5093 | copyreg = import_copyreg(); |
5094 | if (copyreg == NULL) Branch (5094:9): [True: 0, False: 811]
|
5095 | return NULL; |
5096 | |
5097 | /* Use _slotnames function from the copyreg module to find the slots |
5098 | by this class and its bases. This function will cache the result |
5099 | in __slotnames__. */ |
5100 | slotnames = PyObject_CallMethodOneArg( |
5101 | copyreg, &_Py_ID(_slotnames), (PyObject *)cls); |
5102 | Py_DECREF(copyreg); |
5103 | if (slotnames == NULL) Branch (5103:9): [True: 0, False: 811]
|
5104 | return NULL; |
5105 | |
5106 | if (slotnames != Py_None && !PyList_Check(slotnames)) { Branch (5106:9): [True: 811, False: 0]
Branch (5106:33): [True: 0, False: 811]
|
5107 | PyErr_SetString(PyExc_TypeError, |
5108 | "copyreg._slotnames didn't return a list or None"); |
5109 | Py_DECREF(slotnames); |
5110 | return NULL; |
5111 | } |
5112 | |
5113 | return slotnames; |
5114 | } |
5115 | |
5116 | static PyObject * |
5117 | object_getstate_default(PyObject *obj, int required) |
5118 | { |
5119 | PyObject *state; |
5120 | PyObject *slotnames; |
5121 | |
5122 | if (required && Py_TYPE71.6k (obj)->tp_itemsize71.6k ) { Branch (5122:9): [True: 71.6k, False: 4.84k]
Branch (5122:21): [True: 9, False: 71.6k]
|
5123 | PyErr_Format(PyExc_TypeError, |
5124 | "cannot pickle %.200s objects", |
5125 | Py_TYPE(obj)->tp_name); |
5126 | return NULL; |
5127 | } |
5128 | |
5129 | if (_PyObject_IsInstanceDictEmpty(obj)) { Branch (5129:9): [True: 3.25k, False: 73.2k]
|
5130 | state = Py_None; |
5131 | Py_INCREF(state); |
5132 | } |
5133 | else { |
5134 | state = PyObject_GenericGetDict(obj, NULL); |
5135 | if (state == NULL) { Branch (5135:13): [True: 0, False: 73.2k]
|
5136 | return NULL; |
5137 | } |
5138 | } |
5139 | |
5140 | slotnames = _PyType_GetSlotNames(Py_TYPE(obj)); |
5141 | if (slotnames == NULL) { Branch (5141:9): [True: 0, False: 76.4k]
|
5142 | Py_DECREF(state); |
5143 | return NULL; |
5144 | } |
5145 | |
5146 | assert(slotnames == Py_None || PyList_Check(slotnames)); |
5147 | if (required) { Branch (5147:9): [True: 71.6k, False: 4.84k]
|
5148 | Py_ssize_t basicsize = PyBaseObject_Type.tp_basicsize; |
5149 | if (Py_TYPE(obj)->tp_dictoffset && Branch (5149:13): [True: 70.5k, False: 1.06k]
|
5150 | (70.5k Py_TYPE70.5k (obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT70.5k ) == 0) Branch (5150:13): [True: 36, False: 70.5k]
|
5151 | { |
5152 | basicsize += sizeof(PyObject *); |
5153 | } |
5154 | if (Py_TYPE(obj)->tp_weaklistoffset) { Branch (5154:13): [True: 70.5k, False: 1.05k]
|
5155 | basicsize += sizeof(PyObject *); |
5156 | } |
5157 | if (slotnames != Py_None) { Branch (5157:13): [True: 71.6k, False: 0]
|
5158 | basicsize += sizeof(PyObject *) * PyList_GET_SIZE(slotnames); |
5159 | } |
5160 | if (Py_TYPE(obj)->tp_basicsize > basicsize) { Branch (5160:13): [True: 281, False: 71.3k]
|
5161 | Py_DECREF(slotnames); |
5162 | Py_DECREF(state); |
5163 | PyErr_Format(PyExc_TypeError, |
5164 | "cannot pickle '%.200s' object", |
5165 | Py_TYPE(obj)->tp_name); |
5166 | return NULL; |
5167 | } |
5168 | } |
5169 | |
5170 | if (slotnames != Py_None && PyList_GET_SIZE(slotnames) > 0) { Branch (5170:9): [True: 76.2k, False: 0]
Branch (5170:33): [True: 965, False: 75.2k]
|
5171 | PyObject *slots; |
5172 | Py_ssize_t slotnames_size, i; |
5173 | |
5174 | slots = PyDict_New(); |
5175 | if (slots == NULL) { Branch (5175:13): [True: 0, False: 965]
|
5176 | Py_DECREF(slotnames); |
5177 | Py_DECREF(state); |
5178 | return NULL; |
5179 | } |
5180 | |
5181 | slotnames_size = PyList_GET_SIZE(slotnames); |
5182 | for (i = 0; i < slotnames_size; i++15.7k ) { Branch (5182:21): [True: 15.7k, False: 965]
|
5183 | PyObject *name, *value; |
5184 | |
5185 | name = PyList_GET_ITEM(slotnames, i); |
5186 | Py_INCREF(name); |
5187 | if (_PyObject_LookupAttr(obj, name, &value) < 0) { Branch (5187:17): [True: 0, False: 15.7k]
|
5188 | Py_DECREF(name); |
5189 | goto error; |
5190 | } |
5191 | if (value == NULL) { Branch (5191:17): [True: 1.84k, False: 13.8k]
|
5192 | Py_DECREF(name); |
5193 | /* It is not an error if the attribute is not present. */ |
5194 | } |
5195 | else { |
5196 | int err = PyDict_SetItem(slots, name, value); |
5197 | Py_DECREF(name); |
5198 | Py_DECREF(value); |
5199 | if (err) { Branch (5199:21): [True: 0, False: 13.8k]
|
5200 | goto error; |
5201 | } |
5202 | } |
5203 | |
5204 | /* The list is stored on the class so it may mutate while we |
5205 | iterate over it */ |
5206 | if (slotnames_size != PyList_GET_SIZE(slotnames)) { Branch (5206:17): [True: 0, False: 15.7k]
|
5207 | PyErr_Format(PyExc_RuntimeError, |
5208 | "__slotsname__ changed size during iteration"); |
5209 | goto error; |
5210 | } |
5211 | |
5212 | /* We handle errors within the loop here. */ |
5213 | if (0) { Branch (5213:17): [Folded - Ignored]
|
5214 | error: |
5215 | Py_DECREF(slotnames); |
5216 | Py_DECREF(slots); |
5217 | Py_DECREF(state); |
5218 | return NULL; |
5219 | } |
5220 | } |
5221 | |
5222 | /* If we found some slot attributes, pack them in a tuple along |
5223 | the original attribute dictionary. */ |
5224 | if (PyDict_GET_SIZE(slots) > 0) { Branch (5224:13): [True: 958, False: 7]
|
5225 | PyObject *state2; |
5226 | |
5227 | state2 = PyTuple_Pack(2, state, slots); |
5228 | Py_DECREF(state); |
5229 | if (state2 == NULL) { Branch (5229:17): [True: 0, False: 958]
|
5230 | Py_DECREF(slotnames); |
5231 | Py_DECREF(slots); |
5232 | return NULL; |
5233 | } |
5234 | state = state2; |
5235 | } |
5236 | Py_DECREF(slots); |
5237 | } |
5238 | Py_DECREF(slotnames); |
5239 | |
5240 | return state; |
5241 | } |
5242 | |
5243 | static PyObject * |
5244 | object_getstate(PyObject *obj, int required) |
5245 | { |
5246 | PyObject *getstate, *state; |
5247 | |
5248 | getstate = PyObject_GetAttr(obj, &_Py_ID(__getstate__)); |
5249 | if (getstate == NULL) { Branch (5249:9): [True: 0, False: 80.1k]
|
5250 | return NULL; |
5251 | } |
5252 | if (PyCFunction_Check(getstate) && |
5253 | PyCFunction_GET_SELF74.8k (getstate) == obj74.8k && Branch (5253:9): [True: 74.8k, False: 0]
|
5254 | PyCFunction_GET_FUNCTION74.8k (getstate) == object___getstate__74.8k ) Branch (5254:9): [True: 74.7k, False: 106]
|
5255 | { |
5256 | /* If __getstate__ is not overriden pass the required argument. */ |
5257 | state = object_getstate_default(obj, required); |
5258 | } |
5259 | else { |
5260 | state = _PyObject_CallNoArgs(getstate); |
5261 | } |
5262 | Py_DECREF(getstate); |
5263 | return state; |
5264 | } |
5265 | |
5266 | PyObject * |
5267 | _PyObject_GetState(PyObject *obj) |
5268 | { |
5269 | return object_getstate(obj, 0); |
5270 | } |
5271 | |
5272 | /*[clinic input] |
5273 | object.__getstate__ |
5274 | |
5275 | Helper for pickle. |
5276 | [clinic start generated code]*/ |
5277 | |
5278 | static PyObject * |
5279 | object___getstate___impl(PyObject *self) |
5280 | /*[clinic end generated code: output=5a2500dcb6217e9e input=692314d8fbe194ee]*/ |
5281 | { |
5282 | return object_getstate_default(self, 0); |
5283 | } |
5284 | |
5285 | static int |
5286 | _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) |
5287 | { |
5288 | PyObject *getnewargs, *getnewargs_ex; |
5289 | |
5290 | if (args == NULL || kwargs == NULL) { Branch (5290:9): [True: 0, False: 79.4k]
Branch (5290:25): [True: 0, False: 79.4k]
|
5291 | PyErr_BadInternalCall(); |
5292 | return -1; |
5293 | } |
5294 | |
5295 | /* We first attempt to fetch the arguments for __new__ by calling |
5296 | __getnewargs_ex__ on the object. */ |
5297 | getnewargs_ex = _PyObject_LookupSpecial(obj, &_Py_ID(__getnewargs_ex__)); |
5298 | if (getnewargs_ex != NULL) { Branch (5298:9): [True: 92, False: 79.3k]
|
5299 | PyObject *newargs = _PyObject_CallNoArgs(getnewargs_ex); |
5300 | Py_DECREF(getnewargs_ex); |
5301 | if (newargs == NULL) { Branch (5301:13): [True: 4, False: 88]
|
5302 | return -1; |
5303 | } |
5304 | if (!PyTuple_Check(newargs)) { Branch (5304:13): [True: 4, False: 84]
|
5305 | PyErr_Format(PyExc_TypeError, |
5306 | "__getnewargs_ex__ should return a tuple, " |
5307 | "not '%.200s'", Py_TYPE(newargs)->tp_name); |
5308 | Py_DECREF(newargs); |
5309 | return -1; |
5310 | } |
5311 | if (PyTuple_GET_SIZE(newargs) != 2) { Branch (5311:13): [True: 4, False: 80]
|
5312 | PyErr_Format(PyExc_ValueError, |
5313 | "__getnewargs_ex__ should return a tuple of " |
5314 | "length 2, not %zd", PyTuple_GET_SIZE(newargs)); |
5315 | Py_DECREF(newargs); |
5316 | return -1; |
5317 | } |
5318 | *args = PyTuple_GET_ITEM(newargs, 0); |
5319 | Py_INCREF(*args); |
5320 | *kwargs = PyTuple_GET_ITEM(newargs, 1); |
5321 | Py_INCREF(*kwargs); |
5322 | Py_DECREF(newargs); |
5323 | |
5324 | /* XXX We should perhaps allow None to be passed here. */ |
5325 | if (!PyTuple_Check(*args)) { Branch (5325:13): [True: 4, False: 76]
|
5326 | PyErr_Format(PyExc_TypeError, |
5327 | "first item of the tuple returned by " |
5328 | "__getnewargs_ex__ must be a tuple, not '%.200s'", |
5329 | Py_TYPE(*args)->tp_name); |
5330 | Py_CLEAR(*args); |
5331 | Py_CLEAR(*kwargs); |
5332 | return -1; |
5333 | } |
5334 | if (!PyDict_Check(*kwargs)) { Branch (5334:13): [True: 4, False: 72]
|
5335 | PyErr_Format(PyExc_TypeError, |
5336 | "second item of the tuple returned by " |
5337 | "__getnewargs_ex__ must be a dict, not '%.200s'", |
5338 | Py_TYPE(*kwargs)->tp_name); |
5339 | Py_CLEAR(*args); |
5340 | Py_CLEAR(*kwargs); |
5341 | return -1; |
5342 | } |
5343 | return 0; |
5344 | } else if (PyErr_Occurred()) { Branch (5344:16): [True: 0, False: 79.3k]
|
5345 | return -1; |
5346 | } |
5347 | |
5348 | /* The object does not have __getnewargs_ex__ so we fallback on using |
5349 | __getnewargs__ instead. */ |
5350 | getnewargs = _PyObject_LookupSpecial(obj, &_Py_ID(__getnewargs__)); |
5351 | if (getnewargs != NULL) { Branch (5351:9): [True: 1.22k, False: 78.1k]
|
5352 | *args = _PyObject_CallNoArgs(getnewargs); |
5353 | Py_DECREF(getnewargs); |
5354 | if (*args == NULL) { Branch (5354:13): [True: 0, False: 1.22k]
|
5355 | return -1; |
5356 | } |
5357 | if (!PyTuple_Check(*args)) { Branch (5357:13): [True: 4, False: 1.21k]
|
5358 | PyErr_Format(PyExc_TypeError, |
5359 | "__getnewargs__ should return a tuple, " |
5360 | "not '%.200s'", Py_TYPE(*args)->tp_name); |
5361 | Py_CLEAR(*args); |
5362 | return -1; |
5363 | } |
5364 | *kwargs = NULL; |
5365 | return 0; |
5366 | } else if (PyErr_Occurred()) { Branch (5366:16): [True: 0, False: 78.1k]
|
5367 | return -1; |
5368 | } |
5369 | |
5370 | /* The object does not have __getnewargs_ex__ and __getnewargs__. This may |
5371 | mean __new__ does not takes any arguments on this object, or that the |
5372 | object does not implement the reduce protocol for pickling or |
5373 | copying. */ |
5374 | *args = NULL; |
5375 | *kwargs = NULL; |
5376 | return 0; |
5377 | } |
5378 | |
5379 | static int |
5380 | _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, |
5381 | PyObject **dictitems) |
5382 | { |
5383 | if (listitems == NULL || dictitems == NULL) { Branch (5383:9): [True: 0, False: 79.0k]
Branch (5383:30): [True: 0, False: 79.0k]
|
5384 | PyErr_BadInternalCall(); |
5385 | return -1; |
5386 | } |
5387 | |
5388 | if (!PyList_Check(obj)) { Branch (5388:9): [True: 78.1k, False: 906]
|
5389 | *listitems = Py_None; |
5390 | Py_INCREF(*listitems); |
5391 | } |
5392 | else { |
5393 | *listitems = PyObject_GetIter(obj); |
5394 | if (*listitems == NULL) Branch (5394:13): [True: 0, False: 906]
|
5395 | return -1; |
5396 | } |
5397 | |
5398 | if (!PyDict_Check(obj)) { Branch (5398:9): [True: 78.7k, False: 280]
|
5399 | *dictitems = Py_None; |
5400 | Py_INCREF(*dictitems); |
5401 | } |
5402 | else { |
5403 | PyObject *items = PyObject_CallMethodNoArgs(obj, &_Py_ID(items)); |
5404 | if (items == NULL) { Branch (5404:13): [True: 0, False: 280]
|
5405 | Py_CLEAR(*listitems); |
5406 | return -1; |
5407 | } |
5408 | *dictitems = PyObject_GetIter(items); |
5409 | Py_DECREF(items); |
5410 | if (*dictitems == NULL) { Branch (5410:13): [True: 0, False: 280]
|
5411 | Py_CLEAR(*listitems); |
5412 | return -1; |
5413 | } |
5414 | } |
5415 | |
5416 | assert(*listitems != NULL && *dictitems != NULL); |
5417 | |
5418 | return 0; |
5419 | } |
5420 | |
5421 | static PyObject * |
5422 | reduce_newobj(PyObject *obj) |
5423 | { |
5424 | PyObject *args = NULL, *kwargs = NULL; |
5425 | PyObject *copyreg; |
5426 | PyObject *newobj, *newargs, *state, *listitems, *dictitems; |
5427 | PyObject *result; |
5428 | int hasargs; |
5429 | |
5430 | if (Py_TYPE(obj)->tp_new == NULL) { Branch (5430:9): [True: 49, False: 79.4k]
|
5431 | PyErr_Format(PyExc_TypeError, |
5432 | "cannot pickle '%.200s' object", |
5433 | Py_TYPE(obj)->tp_name); |
5434 | return NULL; |
5435 | } |
5436 | if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) Branch (5436:9): [True: 24, False: 79.4k]
|
5437 | return NULL; |
5438 | |
5439 | copyreg = import_copyreg(); |
5440 | if (copyreg == NULL) { Branch (5440:9): [True: 0, False: 79.4k]
|
5441 | Py_XDECREF(args); |
5442 | Py_XDECREF(kwargs); |
5443 | return NULL; |
5444 | } |
5445 | hasargs = (args != NULL); |
5446 | if (kwargs == NULL || PyDict_GET_SIZE72 (kwargs) == 072 ) { Branch (5446:9): [True: 79.3k, False: 72]
Branch (5446:27): [True: 8, False: 64]
|
5447 | PyObject *cls; |
5448 | Py_ssize_t i, n; |
5449 | |
5450 | Py_XDECREF(kwargs); |
5451 | newobj = PyObject_GetAttr(copyreg, &_Py_ID(__newobj__)); |
5452 | Py_DECREF(copyreg); |
5453 | if (newobj == NULL) { Branch (5453:13): [True: 0, False: 79.3k]
|
5454 | Py_XDECREF(args); |
5455 | return NULL; |
5456 | } |
5457 | n = args ? PyTuple_GET_SIZE1.22k (args) : 078.1k ; Branch (5457:13): [True: 1.22k, False: 78.1k]
|
5458 | newargs = PyTuple_New(n+1); |
5459 | if (newargs == NULL) { Branch (5459:13): [True: 0, False: 79.3k]
|
5460 | Py_XDECREF(args); |
5461 | Py_DECREF(newobj); |
5462 | return NULL; |
5463 | } |
5464 | cls = (PyObject *) Py_TYPE(obj); |
5465 | Py_INCREF(cls); |
5466 | PyTuple_SET_ITEM(newargs, 0, cls); |
5467 | for (i = 0; i < n; i++2.57k ) { Branch (5467:21): [True: 2.57k, False: 79.3k]
|
5468 | PyObject *v = PyTuple_GET_ITEM(args, i); |
5469 | Py_INCREF(v); |
5470 | PyTuple_SET_ITEM(newargs, i+1, v); |
5471 | } |
5472 | Py_XDECREF(args); |
5473 | } |
5474 | else if (args != NULL) { Branch (5474:14): [True: 64, False: 0]
|
5475 | newobj = PyObject_GetAttr(copyreg, &_Py_ID(__newobj_ex__)); |
5476 | Py_DECREF(copyreg); |
5477 | if (newobj == NULL) { Branch (5477:13): [True: 0, False: 64]
|
5478 | Py_DECREF(args); |
5479 | Py_DECREF(kwargs); |
5480 | return NULL; |
5481 | } |
5482 | newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs); |
5483 | Py_DECREF(args); |
5484 | Py_DECREF(kwargs); |
5485 | if (newargs == NULL) { Branch (5485:13): [True: 0, False: 64]
|
5486 | Py_DECREF(newobj); |
5487 | return NULL; |
5488 | } |
5489 | } |
5490 | else { |
5491 | /* args == NULL */ |
5492 | Py_DECREF(kwargs); |
5493 | PyErr_BadInternalCall(); |
5494 | return NULL; |
5495 | } |
5496 | |
5497 | state = object_getstate(obj, !(hasargs || PyList_Check(obj) || PyDict_Check(obj))); Branch (5497:36): [True: 1.29k, False: 78.1k]
|
5498 | if (state == NULL) { Branch (5498:9): [True: 367, False: 79.0k]
|
5499 | Py_DECREF(newobj); |
5500 | Py_DECREF(newargs); |
5501 | return NULL; |
5502 | } |
5503 | if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) { Branch (5503:9): [True: 0, False: 79.0k]
|
5504 | Py_DECREF(newobj); |
5505 | Py_DECREF(newargs); |
5506 | Py_DECREF(state); |
5507 | return NULL; |
5508 | } |
5509 | |
5510 | result = PyTuple_Pack(5, newobj, newargs, state, listitems, dictitems); |
5511 | Py_DECREF(newobj); |
5512 | Py_DECREF(newargs); |
5513 | Py_DECREF(state); |
5514 | Py_DECREF(listitems); |
5515 | Py_DECREF(dictitems); |
5516 | return result; |
5517 | } |
5518 | |
5519 | /* |
5520 | * There were two problems when object.__reduce__ and object.__reduce_ex__ |
5521 | * were implemented in the same function: |
5522 | * - trying to pickle an object with a custom __reduce__ method that |
5523 | * fell back to object.__reduce__ in certain circumstances led to |
5524 | * infinite recursion at Python level and eventual RecursionError. |
5525 | * - Pickling objects that lied about their type by overwriting the |
5526 | * __class__ descriptor could lead to infinite recursion at C level |
5527 | * and eventual segfault. |
5528 | * |
5529 | * Because of backwards compatibility, the two methods still have to |
5530 | * behave in the same way, even if this is not required by the pickle |
5531 | * protocol. This common functionality was moved to the _common_reduce |
5532 | * function. |
5533 | */ |
5534 | static PyObject * |
5535 | _common_reduce(PyObject *self, int proto) |
5536 | { |
5537 | PyObject *copyreg, *res; |
5538 | |
5539 | if (proto >= 2) Branch (5539:9): [True: 79.5k, False: 1.65k]
|
5540 | return reduce_newobj(self); |
5541 | |
5542 | copyreg = import_copyreg(); |
5543 | if (!copyreg) Branch (5543:9): [True: 0, False: 1.65k]
|
5544 | return NULL; |
5545 | |
5546 | res = PyObject_CallMethod(copyreg, "_reduce_ex", "Oi", self, proto); |
5547 | Py_DECREF(copyreg); |
5548 | |
5549 | return res; |
5550 | } |
5551 | |
5552 | /*[clinic input] |
5553 | object.__reduce__ |
5554 | |
5555 | Helper for pickle. |
5556 | [clinic start generated code]*/ |
5557 | |
5558 | static PyObject * |
5559 | object___reduce___impl(PyObject *self) |
5560 | /*[clinic end generated code: output=d4ca691f891c6e2f input=11562e663947e18b]*/ |
5561 | { |
5562 | return _common_reduce(self, 0); |
5563 | } |
5564 | |
5565 | /*[clinic input] |
5566 | object.__reduce_ex__ |
5567 | |
5568 | protocol: int |
5569 | / |
5570 | |
5571 | Helper for pickle. |
5572 | [clinic start generated code]*/ |
5573 | |
5574 | static PyObject * |
5575 | object___reduce_ex___impl(PyObject *self, int protocol) |
5576 | /*[clinic end generated code: output=2e157766f6b50094 input=f326b43fb8a4c5ff]*/ |
5577 | { |
5578 | static PyObject *objreduce; |
5579 | PyObject *reduce, *res; |
5580 | |
5581 | if (objreduce == NULL) { Branch (5581:9): [True: 1, False: 167k]
|
5582 | objreduce = PyDict_GetItemWithError( |
5583 | PyBaseObject_Type.tp_dict, &_Py_ID(__reduce__)); |
5584 | if (objreduce == NULL && PyErr_Occurred()0 ) { Branch (5584:13): [True: 0, False: 1]
Branch (5584:34): [True: 0, False: 0]
|
5585 | return NULL; |
5586 | } |
5587 | } |
5588 | |
5589 | if (_PyObject_LookupAttr(self, &_Py_ID(__reduce__), &reduce) < 0) { Branch (5589:9): [True: 0, False: 167k]
|
5590 | return NULL; |
5591 | } |
5592 | if (reduce != NULL) { Branch (5592:9): [True: 167k, False: 0]
|
5593 | PyObject *cls, *clsreduce; |
5594 | int override; |
5595 | |
5596 | cls = (PyObject *) Py_TYPE(self); |
5597 | clsreduce = PyObject_GetAttr(cls, &_Py_ID(__reduce__)); |
5598 | if (clsreduce == NULL) { Branch (5598:13): [True: 0, False: 167k]
|
5599 | Py_DECREF(reduce); |
5600 | return NULL; |
5601 | } |
5602 | override = (clsreduce != objreduce); |
5603 | Py_DECREF(clsreduce); |
5604 | if (override) { Branch (5604:13): [True: 85.9k, False: 81.1k]
|
5605 | res = _PyObject_CallNoArgs(reduce); |
5606 | Py_DECREF(reduce); |
5607 | return res; |
5608 | } |
5609 | else |
5610 | Py_DECREF(reduce); |
5611 | } |
5612 | |
5613 | return _common_reduce(self, protocol); |
5614 | } |
5615 | |
5616 | static PyObject * |
5617 | object_subclasshook(PyObject *cls, PyObject *args) |
5618 | { |
5619 | Py_RETURN_NOTIMPLEMENTED; |
5620 | } |
5621 | |
5622 | PyDoc_STRVAR(object_subclasshook_doc, |
5623 | "Abstract classes can override this to customize issubclass().\n" |
5624 | "\n" |
5625 | "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n" |
5626 | "It should return True, False or NotImplemented. If it returns\n" |
5627 | "NotImplemented, the normal algorithm is used. Otherwise, it\n" |
5628 | "overrides the normal algorithm (and the outcome is cached).\n"); |
5629 | |
5630 | static PyObject * |
5631 | object_init_subclass(PyObject *cls, PyObject *arg) |
5632 | { |
5633 | Py_RETURN_NONE; |
5634 | } |
5635 | |
5636 | PyDoc_STRVAR(object_init_subclass_doc, |
5637 | "This method is called when a class is subclassed.\n" |
5638 | "\n" |
5639 | "The default implementation does nothing. It may be\n" |
5640 | "overridden to extend subclasses.\n"); |
5641 | |
5642 | /*[clinic input] |
5643 | object.__format__ |
5644 | |
5645 | format_spec: unicode |
5646 | / |
5647 | |
5648 | Default object formatter. |
5649 | [clinic start generated code]*/ |
5650 | |
5651 | static PyObject * |
5652 | object___format___impl(PyObject *self, PyObject *format_spec) |
5653 | /*[clinic end generated code: output=34897efb543a974b input=7c3b3bc53a6fb7fa]*/ |
5654 | { |
5655 | /* Issue 7994: If we're converting to a string, we |
5656 | should reject format specifications */ |
5657 | if (PyUnicode_GET_LENGTH(format_spec) > 0) { Branch (5657:9): [True: 10, False: 6.64k]
|
5658 | PyErr_Format(PyExc_TypeError, |
5659 | "unsupported format string passed to %.200s.__format__", |
5660 | Py_TYPE(self)->tp_name); |
5661 | return NULL; |
5662 | } |
5663 | return PyObject_Str(self); |
5664 | } |
5665 | |
5666 | /*[clinic input] |
5667 | object.__sizeof__ |
5668 | |
5669 | Size of object in memory, in bytes. |
5670 | [clinic start generated code]*/ |
5671 | |
5672 | static PyObject * |
5673 | object___sizeof___impl(PyObject *self) |
5674 | /*[clinic end generated code: output=73edab332f97d550 input=1200ff3dfe485306]*/ |
5675 | { |
5676 | Py_ssize_t res, isize; |
5677 | |
5678 | res = 0; |
5679 | isize = Py_TYPE(self)->tp_itemsize; |
5680 | if (isize > 0) Branch (5680:9): [True: 14, False: 77]
|
5681 | res = Py_SIZE(self) * isize; |
5682 | res += Py_TYPE(self)->tp_basicsize; |
5683 | |
5684 | return PyLong_FromSsize_t(res); |
5685 | } |
5686 | |
5687 | /* __dir__ for generic objects: returns __dict__, __class__, |
5688 | and recursively up the __class__.__bases__ chain. |
5689 | */ |
5690 | /*[clinic input] |
5691 | object.__dir__ |
5692 | |
5693 | Default dir() implementation. |
5694 | [clinic start generated code]*/ |
5695 | |
5696 | static PyObject * |
5697 | object___dir___impl(PyObject *self) |
5698 | /*[clinic end generated code: output=66dd48ea62f26c90 input=0a89305bec669b10]*/ |
5699 | { |
5700 | PyObject *result = NULL; |
5701 | PyObject *dict = NULL; |
5702 | PyObject *itsclass = NULL; |
5703 | |
5704 | /* Get __dict__ (which may or may not be a real dict...) */ |
5705 | if (_PyObject_LookupAttr(self, &_Py_ID(__dict__), &dict) < 0) { Branch (5705:9): [True: 0, False: 11.4k]
|
5706 | return NULL; |
5707 | } |
5708 | if (dict == NULL) { Branch (5708:9): [True: 9.01k, False: 2.46k]
|
5709 | dict = PyDict_New(); |
5710 | } |
5711 | else if (!PyDict_Check(dict)) { Branch (5711:14): [True: 0, False: 2.46k]
|
5712 | Py_DECREF(dict); |
5713 | dict = PyDict_New(); |
5714 | } |
5715 | else { |
5716 | /* Copy __dict__ to avoid mutating it. */ |
5717 | PyObject *temp = PyDict_Copy(dict); |
5718 | Py_DECREF(dict); |
5719 | dict = temp; |
5720 | } |
5721 | |
5722 | if (dict == NULL) Branch (5722:9): [True: 0, False: 11.4k]
|
5723 | goto error; |
5724 | |
5725 | /* Merge in attrs reachable from its class. */ |
5726 | if (_PyObject_LookupAttr(self, &_Py_ID(__class__), &itsclass) < 0) { Branch (5726:9): [True: 0, False: 11.4k]
|
5727 | goto error; |
5728 | } |
5729 | /* XXX(tomer): Perhaps fall back to Py_TYPE(obj) if no |
5730 | __class__ exists? */ |
5731 | if (itsclass != NULL && merge_class_dict(dict, itsclass) < 011.4k ) Branch (5731:9): [True: 11.4k, False: 2]
Branch (5731:29): [True: 0, False: 11.4k]
|
5732 | goto error; |
5733 | |
5734 | result = PyDict_Keys(dict); |
5735 | /* fall through */ |
5736 | error: |
5737 | Py_XDECREF(itsclass); |
5738 | Py_XDECREF(dict); |
5739 | return result; |
5740 | } |
5741 | |
5742 | static PyMethodDef object_methods[] = { |
5743 | OBJECT___REDUCE_EX___METHODDEF |
5744 | OBJECT___REDUCE___METHODDEF |
5745 | OBJECT___GETSTATE___METHODDEF |
5746 | {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS, |
5747 | object_subclasshook_doc}, |
5748 | {"__init_subclass__", object_init_subclass, METH_CLASS | METH_NOARGS, |
5749 | object_init_subclass_doc}, |
5750 | OBJECT___FORMAT___METHODDEF |
5751 | OBJECT___SIZEOF___METHODDEF |
5752 | OBJECT___DIR___METHODDEF |
5753 | {0} |
5754 | }; |
5755 | |
5756 | PyDoc_STRVAR(object_doc, |
5757 | "object()\n--\n\n" |
5758 | "The base class of the class hierarchy.\n\n" |
5759 | "When called, it accepts no arguments and returns a new featureless\n" |
5760 | "instance that has no instance attributes and cannot be given any.\n"); |
5761 | |
5762 | PyTypeObject PyBaseObject_Type = { |
5763 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
5764 | "object", /* tp_name */ |
5765 | sizeof(PyObject), /* tp_basicsize */ |
5766 | 0, /* tp_itemsize */ |
5767 | object_dealloc, /* tp_dealloc */ |
5768 | 0, /* tp_vectorcall_offset */ |
5769 | 0, /* tp_getattr */ |
5770 | 0, /* tp_setattr */ |
5771 | 0, /* tp_as_async */ |
5772 | object_repr, /* tp_repr */ |
5773 | 0, /* tp_as_number */ |
5774 | 0, /* tp_as_sequence */ |
5775 | 0, /* tp_as_mapping */ |
5776 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
5777 | 0, /* tp_call */ |
5778 | object_str, /* tp_str */ |
5779 | PyObject_GenericGetAttr, /* tp_getattro */ |
5780 | PyObject_GenericSetAttr, /* tp_setattro */ |
5781 | 0, /* tp_as_buffer */ |
5782 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
5783 | object_doc, /* tp_doc */ |
5784 | 0, /* tp_traverse */ |
5785 | 0, /* tp_clear */ |
5786 | object_richcompare, /* tp_richcompare */ |
5787 | 0, /* tp_weaklistoffset */ |
5788 | 0, /* tp_iter */ |
5789 | 0, /* tp_iternext */ |
5790 | object_methods, /* tp_methods */ |
5791 | 0, /* tp_members */ |
5792 | object_getsets, /* tp_getset */ |
5793 | 0, /* tp_base */ |
5794 | 0, /* tp_dict */ |
5795 | 0, /* tp_descr_get */ |
5796 | 0, /* tp_descr_set */ |
5797 | 0, /* tp_dictoffset */ |
5798 | object_init, /* tp_init */ |
5799 | PyType_GenericAlloc, /* tp_alloc */ |
5800 | object_new, /* tp_new */ |
5801 | PyObject_Del, /* tp_free */ |
5802 | }; |
5803 | |
5804 | |
5805 | static int |
5806 | type_add_method(PyTypeObject *type, PyMethodDef *meth) |
5807 | { |
5808 | PyObject *descr; |
5809 | int isdescr = 1; |
5810 | if (meth->ml_flags & METH_CLASS) { Branch (5810:9): [True: 3.02k, False: 79.7k]
|
5811 | if (meth->ml_flags & METH_STATIC) { Branch (5811:13): [True: 0, False: 3.02k]
|
5812 | PyErr_SetString(PyExc_ValueError, |
5813 | "method cannot be both class and static"); |
5814 | return -1; |
5815 | } |
5816 | descr = PyDescr_NewClassMethod(type, meth); |
5817 | } |
5818 | else if (meth->ml_flags & METH_STATIC) { Branch (5818:14): [True: 328, False: 79.4k]
|
5819 | PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL); |
5820 | if (cfunc == NULL) { Branch (5820:13): [True: 0, False: 328]
|
5821 | return -1; |
5822 | } |
5823 | descr = PyStaticMethod_New(cfunc); |
5824 | isdescr = 0; // PyStaticMethod is not PyDescrObject |
5825 | Py_DECREF(cfunc); |
5826 | } |
5827 | else { |
5828 | descr = PyDescr_NewMethod(type, meth); |
5829 | } |
5830 | if (descr == NULL) { Branch (5830:9): [True: 0, False: 82.7k]
|
5831 | return -1; |
5832 | } |
5833 | |
5834 | PyObject *name; |
5835 | if (isdescr) { Branch (5835:9): [True: 82.4k, False: 328]
|
5836 | name = PyDescr_NAME(descr); |
5837 | } |
5838 | else { |
5839 | name = PyUnicode_FromString(meth->ml_name); |
5840 | if (name == NULL) { Branch (5840:13): [True: 0, False: 328]
|
5841 | Py_DECREF(descr); |
5842 | return -1; |
5843 | } |
5844 | } |
5845 | |
5846 | int err; |
5847 | if (!(meth->ml_flags & METH_COEXIST)) { Branch (5847:9): [True: 82.2k, False: 529]
|
5848 | err = PyDict_SetDefault(type->tp_dict, name, descr) == NULL; |
5849 | } |
5850 | else { |
5851 | err = PyDict_SetItem(type->tp_dict, name, descr) < 0; |
5852 | } |
5853 | if (!isdescr) { Branch (5853:9): [True: 328, False: 82.4k]
|
5854 | Py_DECREF(name); |
5855 | } |
5856 | Py_DECREF(descr); |
5857 | if (err) { Branch (5857:9): [True: 0, False: 82.7k]
|
5858 | return -1; |
5859 | } |
5860 | return 0; |
5861 | } |
5862 | |
5863 | |
5864 | /* Add the methods from tp_methods to the __dict__ in a type object */ |
5865 | static int |
5866 | type_add_methods(PyTypeObject *type) |
5867 | { |
5868 | PyMethodDef *meth = type->tp_methods; |
5869 | if (meth == NULL) { Branch (5869:9): [True: 97.4k, False: 17.3k]
|
5870 | return 0; |
5871 | } |
5872 | |
5873 | for (; 17.3k meth->ml_name != NULL; meth++82.7k ) { Branch (5873:12): [True: 82.7k, False: 17.3k]
|
5874 | if (type_add_method(type, meth) < 0) { Branch (5874:13): [True: 0, False: 82.7k]
|
5875 | return -1; |
5876 | } |
5877 | } |
5878 | return 0; |
5879 | } |
5880 | |
5881 | |
5882 | static int |
5883 | type_add_members(PyTypeObject *type) |
5884 | { |
5885 | PyMemberDef *memb = type->tp_members; |
5886 | if (memb == NULL) { Branch (5886:9): [True: 17.2k, False: 97.5k]
|
5887 | return 0; |
5888 | } |
5889 | |
5890 | PyObject *dict = type->tp_dict; |
5891 | for (; memb->name != NULL; memb++44.6k ) { Branch (5891:12): [True: 44.6k, False: 97.5k]
|
5892 | PyObject *descr = PyDescr_NewMember(type, memb); |
5893 | if (descr == NULL) Branch (5893:13): [True: 0, False: 44.6k]
|
5894 | return -1; |
5895 | |
5896 | if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) { Branch (5896:13): [True: 0, False: 44.6k]
|
5897 | Py_DECREF(descr); |
5898 | return -1; |
5899 | } |
5900 | Py_DECREF(descr); |
5901 | } |
5902 | return 0; |
5903 | } |
5904 | |
5905 | |
5906 | static int |
5907 | type_add_getset(PyTypeObject *type) |
5908 | { |
5909 | PyGetSetDef *gsp = type->tp_getset; |
5910 | if (gsp == NULL) { Branch (5910:9): [True: 71.0k, False: 43.7k]
|
5911 | return 0; |
5912 | } |
5913 | |
5914 | PyObject *dict = type->tp_dict; |
5915 | for (; gsp->name != NULL; gsp++88.4k ) { Branch (5915:12): [True: 88.4k, False: 43.7k]
|
5916 | PyObject *descr = PyDescr_NewGetSet(type, gsp); |
5917 | if (descr == NULL) { Branch (5917:13): [True: 0, False: 88.4k]
|
5918 | return -1; |
5919 | } |
5920 | |
5921 | if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) { Branch (5921:13): [True: 0, False: 88.4k]
|
5922 | Py_DECREF(descr); |
5923 | return -1; |
5924 | } |
5925 | Py_DECREF(descr); |
5926 | } |
5927 | return 0; |
5928 | } |
5929 | |
5930 | |
5931 | static void |
5932 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
5933 | { |
5934 | /* Copying tp_traverse and tp_clear is connected to the GC flags */ |
5935 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && Branch (5935:9): [True: 4.36k, False: 110k]
|
5936 | (base->tp_flags & 4.36k Py_TPFLAGS_HAVE_GC4.36k ) && Branch (5936:9): [True: 527, False: 3.84k]
|
5937 | (527 !type->tp_traverse527 && !type->tp_clear527 )) { Branch (5937:10): [True: 527, False: 0]
Branch (5937:32): [True: 527, False: 0]
|
5938 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
5939 | if (type->tp_traverse == NULL) Branch (5939:13): [True: 527, False: 0]
|
5940 | type->tp_traverse = base->tp_traverse; |
5941 | if (type->tp_clear == NULL) Branch (5941:13): [True: 527, False: 0]
|
5942 | type->tp_clear = base->tp_clear; |
5943 | } |
5944 | type->tp_flags |= (base->tp_flags & Py_TPFLAGS_MANAGED_DICT); |
5945 | |
5946 | if (type->tp_basicsize == 0) Branch (5946:9): [True: 687, False: 114k]
|
5947 | type->tp_basicsize = base->tp_basicsize; |
5948 | |
5949 | /* Copy other non-function slots */ |
5950 | |
5951 | #define COPYVAL(SLOT) \ |
5952 | if (type->SLOT == 0) { type->SLOT = base->SLOT; }250k |
5953 | |
5954 | COPYVAL(tp_itemsize); |
5955 | COPYVAL(tp_weaklistoffset); |
5956 | COPYVAL(tp_dictoffset); |
5957 | #undef COPYVAL |
5958 | |
5959 | /* Setup fast subclass flags */ |
5960 | if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) { Branch (5960:9): [True: 9.57k, False: 105k]
|
5961 | type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS; |
5962 | } |
5963 | else if (PyType_IsSubtype(base, &PyType_Type)) { Branch (5963:14): [True: 526, False: 104k]
|
5964 | type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS; |
5965 | } |
5966 | else if (PyType_IsSubtype(base, &PyLong_Type)) { Branch (5966:14): [True: 1.65k, False: 102k]
|
5967 | type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS; |
5968 | } |
5969 | else if (PyType_IsSubtype(base, &PyBytes_Type)) { Branch (5969:14): [True: 88, False: 102k]
|
5970 | type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS; |
5971 | } |
5972 | else if (PyType_IsSubtype(base, &PyUnicode_Type)) { Branch (5972:14): [True: 7.60k, False: 95.2k]
|
5973 | type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS; |
5974 | } |
5975 | else if (PyType_IsSubtype(base, &PyTuple_Type)) { Branch (5975:14): [True: 4.35k, False: 90.9k]
|
5976 | type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS; |
5977 | } |
5978 | else if (PyType_IsSubtype(base, &PyList_Type)) { Branch (5978:14): [True: 247, False: 90.6k]
|
5979 | type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS; |
5980 | } |
5981 | else if (PyType_IsSubtype(base, &PyDict_Type)) { Branch (5981:14): [True: 601, False: 90.0k]
|
5982 | type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS; |
5983 | } |
5984 | if (PyType_HasFeature(base, _Py_TPFLAGS_MATCH_SELF)) { Branch (5984:9): [True: 15.1k, False: 99.5k]
|
5985 | type->tp_flags |= _Py_TPFLAGS_MATCH_SELF; |
5986 | } |
5987 | } |
5988 | |
5989 | static int |
5990 | overrides_hash(PyTypeObject *type) |
5991 | { |
5992 | PyObject *dict = type->tp_dict; |
5993 | |
5994 | assert(dict != NULL); |
5995 | int r = PyDict_Contains(dict, &_Py_ID(__eq__)); |
5996 | if (r == 0) { Branch (5996:9): [True: 89.2k, False: 20.2k]
|
5997 | r = PyDict_Contains(dict, &_Py_ID(__hash__)); |
5998 | } |
5999 | return r; |
6000 | } |
6001 | |
6002 | static int |
6003 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
6004 | { |
6005 | PyTypeObject *basebase; |
6006 | |
6007 | #undef SLOTDEFINED |
6008 | #undef COPYSLOT |
6009 | #undef COPYNUM |
6010 | #undef COPYSEQ |
6011 | #undef COPYMAP |
6012 | #undef COPYBUF |
6013 | |
6014 | #define SLOTDEFINED(SLOT) \ |
6015 | (base->SLOT != 0 && \ |
6016 | (1.17M basebase == NULL1.17M || base->SLOT != basebase->SLOT798k )) |
6017 | |
6018 | #define COPYSLOT(SLOT) \ |
6019 | if (!type->SLOT && SLOTDEFINED10.4M (SLOT)) type->SLOT = base->SLOT600k |
6020 | |
6021 | #define COPYASYNC(SLOT) COPYSLOT(tp_as_async->SLOT) |
6022 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
6023 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
6024 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
6025 | #define COPYBUF(SLOT) COPYSLOT282k (tp_as_buffer->SLOT) |
6026 | |
6027 | /* This won't inherit indirect slots (from tp_as_number etc.) |
6028 | if type doesn't provide the space. */ |
6029 | |
6030 | if (type->tp_as_number != NULL && base->tp_as_number != NULL263k ) { Branch (6030:9): [True: 263k, False: 41.6k]
Branch (6030:39): [True: 151k, False: 112k]
|
6031 | basebase = base->tp_base; |
6032 | if (basebase->tp_as_number == NULL) Branch (6032:13): [True: 72.7k, False: 78.6k]
|
6033 | basebase = NULL; |
6034 | COPYNUM(nb_add); |
6035 | COPYNUM(nb_subtract); |
6036 | COPYNUM(nb_multiply); |
6037 | COPYNUM(nb_remainder); |
6038 | COPYNUM(nb_divmod); |
6039 | COPYNUM(nb_power); |
6040 | COPYNUM(nb_negative); |
6041 | COPYNUM(nb_positive); |
6042 | COPYNUM(nb_absolute); |
6043 | COPYNUM(nb_bool); |
6044 | COPYNUM(nb_invert); |
6045 | COPYNUM(nb_lshift); |
6046 | COPYNUM(nb_rshift); |
6047 | COPYNUM(nb_and); |
6048 | COPYNUM(nb_xor); |
6049 | COPYNUM(nb_or); |
6050 | COPYNUM(nb_int); |
6051 | COPYNUM(nb_float); |
6052 | COPYNUM(nb_inplace_add); |
6053 | COPYNUM(nb_inplace_subtract); |
6054 | COPYNUM(nb_inplace_multiply); |
6055 | COPYNUM(nb_inplace_remainder); |
6056 | COPYNUM(nb_inplace_power); |
6057 | COPYNUM(nb_inplace_lshift); |
6058 | COPYNUM(nb_inplace_rshift); |
6059 | COPYNUM(nb_inplace_and); |
6060 | COPYNUM(nb_inplace_xor); |
6061 | COPYNUM(nb_inplace_or); |
6062 | COPYNUM(nb_true_divide); |
6063 | COPYNUM(nb_floor_divide); |
6064 | COPYNUM(nb_inplace_true_divide); |
6065 | COPYNUM(nb_inplace_floor_divide); |
6066 | COPYNUM(nb_index); |
6067 | COPYNUM(nb_matrix_multiply); |
6068 | COPYNUM(nb_inplace_matrix_multiply); |
6069 | } |
6070 | |
6071 | if (type->tp_as_async != NULL && base->tp_as_async != NULL261k ) { Branch (6071:9): [True: 261k, False: 43.9k]
Branch (6071:38): [True: 138k, False: 123k]
|
6072 | basebase = base->tp_base; |
6073 | if (basebase->tp_as_async == NULL) Branch (6073:13): [True: 69.0k, False: 69.1k]
|
6074 | basebase = NULL; |
6075 | COPYASYNC(am_await); |
6076 | COPYASYNC(am_aiter); |
6077 | COPYASYNC(am_anext); |
6078 | } |
6079 | |
6080 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL263k ) { Branch (6080:9): [True: 263k, False: 41.7k]
Branch (6080:41): [True: 151k, False: 111k]
|
6081 | basebase = base->tp_base; |
6082 | if (basebase->tp_as_sequence == NULL) Branch (6082:13): [True: 75.0k, False: 76.5k]
|
6083 | basebase = NULL; |
6084 | COPYSEQ(sq_length); |
6085 | COPYSEQ(sq_concat); |
6086 | COPYSEQ(sq_repeat); |
6087 | COPYSEQ(sq_item); |
6088 | COPYSEQ(sq_ass_item); |
6089 | COPYSEQ(sq_contains); |
6090 | COPYSEQ(sq_inplace_concat); |
6091 | COPYSEQ(sq_inplace_repeat); |
6092 | } |
6093 | |
6094 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL263k ) { Branch (6094:9): [True: 263k, False: 41.8k]
Branch (6094:40): [True: 151k, False: 111k]
|
6095 | basebase = base->tp_base; |
6096 | if (basebase->tp_as_mapping == NULL) Branch (6096:13): [True: 75.2k, False: 76.5k]
|
6097 | basebase = NULL; |
6098 | COPYMAP(mp_length); |
6099 | COPYMAP(mp_subscript); |
6100 | COPYMAP(mp_ass_subscript); |
6101 | } |
6102 | |
6103 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL261k ) { Branch (6103:9): [True: 261k, False: 44.0k]
Branch (6103:39): [True: 141k, False: 119k]
|
6104 | basebase = base->tp_base; |
6105 | if (basebase->tp_as_buffer == NULL) Branch (6105:13): [True: 70.5k, False: 70.7k]
|
6106 | basebase = NULL; |
6107 | COPYBUF(bf_getbuffer); |
6108 | COPYBUF(bf_releasebuffer); |
6109 | } |
6110 | |
6111 | basebase = base->tp_base; |
6112 | |
6113 | COPYSLOT(tp_dealloc); |
6114 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { Branch (6114:9): [True: 305k, False: 0]
Branch (6114:37): [True: 99.6k, False: 205k]
|
6115 | type->tp_getattr = base->tp_getattr; |
6116 | type->tp_getattro = base->tp_getattro; |
6117 | } |
6118 | if (type->tp_setattr == NULL && type->tp_setattro == NULL305k ) { Branch (6118:9): [True: 305k, False: 19]
Branch (6118:37): [True: 106k, False: 198k]
|
6119 | type->tp_setattr = base->tp_setattr; |
6120 | type->tp_setattro = base->tp_setattro; |
6121 | } |
6122 | COPYSLOT(tp_repr); |
6123 | /* tp_hash see tp_richcompare */ |
6124 | { |
6125 | /* Always inherit tp_vectorcall_offset to support PyVectorcall_Call(). |
6126 | * If Py_TPFLAGS_HAVE_VECTORCALL is not inherited, then vectorcall |
6127 | * won't be used automatically. */ |
6128 | COPYSLOT(tp_vectorcall_offset); |
6129 | |
6130 | /* Inherit Py_TPFLAGS_HAVE_VECTORCALL for non-heap types |
6131 | * if tp_call is not overridden */ |
6132 | if (!type->tp_call && Branch (6132:13): [True: 255k, False: 49.3k]
|
6133 | _PyType_HasFeature(base, 255k Py_TPFLAGS_HAVE_VECTORCALL255k ) && Branch (6133:13): [True: 916, False: 254k]
|
6134 | _PyType_HasFeature(type, 916 Py_TPFLAGS_IMMUTABLETYPE916 )) Branch (6134:13): [True: 80, False: 836]
|
6135 | { |
6136 | type->tp_flags |= Py_TPFLAGS_HAVE_VECTORCALL; |
6137 | } |
6138 | COPYSLOT(tp_call); |
6139 | } |
6140 | COPYSLOT(tp_str); |
6141 | { |
6142 | /* Copy comparison-related slots only when |
6143 | not overriding them anywhere */ |
6144 | if (type->tp_richcompare == NULL && Branch (6144:13): [True: 112k, False: 192k]
|
6145 | type->tp_hash == NULL112k ) Branch (6145:13): [True: 109k, False: 3.37k]
|
6146 | { |
6147 | int r = overrides_hash(type); |
6148 | if (r < 0) { Branch (6148:17): [True: 0, False: 109k]
|
6149 | return -1; |
6150 | } |
6151 | if (!r) { Branch (6151:17): [True: 88.6k, False: 20.9k]
|
6152 | type->tp_richcompare = base->tp_richcompare; |
6153 | type->tp_hash = base->tp_hash; |
6154 | } |
6155 | } |
6156 | } |
6157 | { |
6158 | COPYSLOT(tp_iter); |
6159 | COPYSLOT(tp_iternext); |
6160 | } |
6161 | { |
6162 | COPYSLOT(tp_descr_get); |
6163 | /* Inherit Py_TPFLAGS_METHOD_DESCRIPTOR if tp_descr_get was inherited, |
6164 | * but only for extension types */ |
6165 | if (base->tp_descr_get && Branch (6165:13): [True: 910, False: 304k]
|
6166 | type->tp_descr_get == base->tp_descr_get910 && Branch (6166:13): [True: 909, False: 1]
|
6167 | _PyType_HasFeature(type, 909 Py_TPFLAGS_IMMUTABLETYPE909 ) && Branch (6167:13): [True: 2, False: 907]
|
6168 | _PyType_HasFeature(base, 2 Py_TPFLAGS_METHOD_DESCRIPTOR2 )) Branch (6168:13): [True: 2, False: 0]
|
6169 | { |
6170 | type->tp_flags |= Py_TPFLAGS_METHOD_DESCRIPTOR; |
6171 | } |
6172 | COPYSLOT(tp_descr_set); |
6173 | COPYSLOT(tp_dictoffset); |
6174 | COPYSLOT(tp_init); |
6175 | COPYSLOT(tp_alloc); |
6176 | COPYSLOT(tp_is_gc); |
6177 | COPYSLOT(tp_finalize); |
6178 | if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) == Branch (6178:13): [True: 180k, False: 124k]
|
6179 | (base->tp_flags & Py_TPFLAGS_HAVE_GC)) { |
6180 | /* They agree about gc. */ |
6181 | COPYSLOT(tp_free); |
6182 | } |
6183 | else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) && Branch (6183:18): [True: 124k, False: 0]
|
6184 | type->tp_free == NULL && Branch (6184:18): [True: 6.25k, False: 118k]
|
6185 | base->tp_free == PyObject_Free6.25k ) { Branch (6185:18): [True: 6.25k, False: 0]
|
6186 | /* A bit of magic to plug in the correct default |
6187 | * tp_free function when a derived class adds gc, |
6188 | * didn't define tp_free, and the base uses the |
6189 | * default non-gc tp_free. |
6190 | */ |
6191 | type->tp_free = PyObject_GC_Del; |
6192 | } |
6193 | /* else they didn't agree about gc, and there isn't something |
6194 | * obvious to be done -- the type is on its own. |
6195 | */ |
6196 | } |
6197 | return 0; |
6198 | } |
6199 | |
6200 | static int add_operators(PyTypeObject *); |
6201 | static int add_tp_new_wrapper(PyTypeObject *type); |
6202 | |
6203 | #define COLLECTION_FLAGS (Py_TPFLAGS_SEQUENCE | Py_TPFLAGS_MAPPING) |
6204 | |
6205 | static int |
6206 | type_ready_pre_checks(PyTypeObject *type) |
6207 | { |
6208 | /* Consistency checks for PEP 590: |
6209 | * - Py_TPFLAGS_METHOD_DESCRIPTOR requires tp_descr_get |
6210 | * - Py_TPFLAGS_HAVE_VECTORCALL requires tp_call and |
6211 | * tp_vectorcall_offset > 0 |
6212 | * To avoid mistakes, we require this before inheriting. |
6213 | */ |
6214 | if (type->tp_flags & Py_TPFLAGS_METHOD_DESCRIPTOR) { Branch (6214:9): [True: 410, False: 114k]
|
6215 | _PyObject_ASSERT((PyObject *)type, type->tp_descr_get != NULL); |
6216 | } |
6217 | if (type->tp_flags & Py_TPFLAGS_HAVE_VECTORCALL) { Branch (6217:9): [True: 1.05k, False: 113k]
|
6218 | _PyObject_ASSERT((PyObject *)type, type->tp_vectorcall_offset > 0); |
6219 | _PyObject_ASSERT((PyObject *)type, type->tp_call != NULL); |
6220 | } |
6221 | |
6222 | /* Consistency checks for pattern matching |
6223 | * Py_TPFLAGS_SEQUENCE and Py_TPFLAGS_MAPPING are mutually exclusive */ |
6224 | _PyObject_ASSERT((PyObject *)type, (type->tp_flags & COLLECTION_FLAGS) != COLLECTION_FLAGS); |
6225 | |
6226 | if (type->tp_name == NULL) { Branch (6226:9): [True: 0, False: 114k]
|
6227 | PyErr_Format(PyExc_SystemError, |
6228 | "Type does not define the tp_name field."); |
6229 | return -1; |
6230 | } |
6231 | return 0; |
6232 | } |
6233 | |
6234 | |
6235 | static int |
6236 | type_ready_set_bases(PyTypeObject *type) |
6237 | { |
6238 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
6239 | PyTypeObject *base = type->tp_base; |
6240 | if (base == NULL && type != &PyBaseObject_Type9.07k ) { Branch (6240:9): [True: 9.07k, False: 105k]
Branch (6240:25): [True: 8.97k, False: 104]
|
6241 | base = &PyBaseObject_Type; |
6242 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { Branch (6242:13): [True: 0, False: 8.97k]
|
6243 | type->tp_base = (PyTypeObject*)Py_NewRef((PyObject*)base); |
6244 | } |
6245 | else { |
6246 | type->tp_base = base; |
6247 | } |
6248 | } |
6249 | assert(type->tp_base != NULL || type == &PyBaseObject_Type); |
6250 | |
6251 | /* Now the only way base can still be NULL is if type is |
6252 | * &PyBaseObject_Type. */ |
6253 | |
6254 | /* Initialize the base class */ |
6255 | if (base != NULL && !114k _PyType_IsReady114k (base)) { Branch (6255:9): [True: 114k, False: 104]
Branch (6255:25): [True: 0, False: 114k]
|
6256 | if (PyType_Ready(base) < 0) { Branch (6256:13): [True: 0, False: 0]
|
6257 | return -1; |
6258 | } |
6259 | } |
6260 | |
6261 | /* Initialize ob_type if NULL. This means extensions that want to be |
6262 | compilable separately on Windows can call PyType_Ready() instead of |
6263 | initializing the ob_type field of their type objects. */ |
6264 | /* The test for base != NULL is really unnecessary, since base is only |
6265 | NULL when type is &PyBaseObject_Type, and we know its ob_type is |
6266 | not NULL (it's initialized to &PyType_Type). But coverity doesn't |
6267 | know that. */ |
6268 | if (Py_IS_TYPE(type, NULL) && base != NULL7.85k ) { Branch (6268:35): [True: 7.85k, False: 0]
|
6269 | Py_SET_TYPE(type, Py_TYPE(base)); |
6270 | } |
6271 | |
6272 | /* Initialize tp_bases */ |
6273 | PyObject *bases = type->tp_bases; |
6274 | if (bases == NULL) { Branch (6274:9): [True: 22.4k, False: 92.3k]
|
6275 | PyTypeObject *base = type->tp_base; |
6276 | if (base == NULL) { Branch (6276:13): [True: 104, False: 22.3k]
|
6277 | bases = PyTuple_New(0); |
6278 | } |
6279 | else { |
6280 | bases = PyTuple_Pack(1, base); |
6281 | } |
6282 | if (bases == NULL) { Branch (6282:13): [True: 0, False: 22.4k]
|
6283 | return -1; |
6284 | } |
6285 | type->tp_bases = bases; |
6286 | } |
6287 | return 0; |
6288 | } |
6289 | |
6290 | |
6291 | static int |
6292 | type_ready_set_dict(PyTypeObject *type) |
6293 | { |
6294 | if (type->tp_dict != NULL) { Branch (6294:9): [True: 86.7k, False: 28.1k]
|
6295 | return 0; |
6296 | } |
6297 | |
6298 | PyObject *dict = PyDict_New(); |
6299 | if (dict == NULL) { Branch (6299:9): [True: 0, False: 28.1k]
|
6300 | return -1; |
6301 | } |
6302 | type->tp_dict = dict; |
6303 | return 0; |
6304 | } |
6305 | |
6306 | |
6307 | /* If the type dictionary doesn't contain a __doc__, set it from |
6308 | the tp_doc slot. */ |
6309 | static int |
6310 | type_dict_set_doc(PyTypeObject *type) |
6311 | { |
6312 | int r = PyDict_Contains(type->tp_dict, &_Py_ID(__doc__)); |
6313 | if (r < 0) { Branch (6313:9): [True: 0, False: 114k]
|
6314 | return -1; |
6315 | } |
6316 | if (r > 0) { Branch (6316:9): [True: 33.1k, False: 81.7k]
|
6317 | return 0; |
6318 | } |
6319 | |
6320 | if (type->tp_doc != NULL) { Branch (6320:9): [True: 18.5k, False: 63.1k]
|
6321 | const char *doc_str; |
6322 | doc_str = _PyType_DocWithoutSignature(type->tp_name, type->tp_doc); |
6323 | PyObject *doc = PyUnicode_FromString(doc_str); |
6324 | if (doc == NULL) { Branch (6324:13): [True: 0, False: 18.5k]
|
6325 | return -1; |
6326 | } |
6327 | |
6328 | if (PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), doc) < 0) { Branch (6328:13): [True: 0, False: 18.5k]
|
6329 | Py_DECREF(doc); |
6330 | return -1; |
6331 | } |
6332 | Py_DECREF(doc); |
6333 | } |
6334 | else { |
6335 | if (PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), Py_None) < 0) { Branch (6335:13): [True: 0, False: 63.1k]
|
6336 | return -1; |
6337 | } |
6338 | } |
6339 | return 0; |
6340 | } |
6341 | |
6342 | |
6343 | static int |
6344 | type_ready_fill_dict(PyTypeObject *type) |
6345 | { |
6346 | /* Add type-specific descriptors to tp_dict */ |
6347 | if (add_operators(type) < 0) { Branch (6347:9): [True: 0, False: 114k]
|
6348 | return -1; |
6349 | } |
6350 | if (type_add_methods(type) < 0) { Branch (6350:9): [True: 0, False: 114k]
|
6351 | return -1; |
6352 | } |
6353 | if (type_add_members(type) < 0) { Branch (6353:9): [True: 0, False: 114k]
|
6354 | return -1; |
6355 | } |
6356 | if (type_add_getset(type) < 0) { Branch (6356:9): [True: 0, False: 114k]
|
6357 | return -1; |
6358 | } |
6359 | if (type_dict_set_doc(type) < 0) { Branch (6359:9): [True: 0, False: 114k]
|
6360 | return -1; |
6361 | } |
6362 | return 0; |
6363 | } |
6364 | |
6365 | |
6366 | static int |
6367 | type_ready_mro(PyTypeObject *type) |
6368 | { |
6369 | /* Calculate method resolution order */ |
6370 | if (mro_internal(type, NULL) < 0) { Branch (6370:9): [True: 16, False: 114k]
|
6371 | return -1; |
6372 | } |
6373 | assert(type->tp_mro != NULL); |
6374 | assert(PyTuple_Check(type->tp_mro)); |
6375 | |
6376 | /* All bases of statically allocated type should be statically allocated */ |
6377 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { Branch (6377:9): [True: 22.4k, False: 92.3k]
|
6378 | PyObject *mro = type->tp_mro; |
6379 | Py_ssize_t n = PyTuple_GET_SIZE(mro); |
6380 | for (Py_ssize_t i = 0; i < n; i++67.0k ) { Branch (6380:32): [True: 67.0k, False: 22.4k]
|
6381 | PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(mro, i)); |
6382 | if (base->tp_flags & Py_TPFLAGS_HEAPTYPE) { Branch (6382:17): [True: 0, False: 67.0k]
|
6383 | PyErr_Format(PyExc_TypeError, |
6384 | "type '%.100s' is not dynamically allocated but " |
6385 | "its base type '%.100s' is dynamically allocated", |
6386 | type->tp_name, base->tp_name); |
6387 | return -1; |
6388 | } |
6389 | } |
6390 | } |
6391 | return 0; |
6392 | } |
6393 | |
6394 | |
6395 | // For static types, inherit tp_as_xxx structures from the base class |
6396 | // if it's NULL. |
6397 | // |
6398 | // For heap types, tp_as_xxx structures are not NULL: they are set to the |
6399 | // PyHeapTypeObject.as_xxx fields by type_new_alloc(). |
6400 | static void |
6401 | type_ready_inherit_as_structs(PyTypeObject *type, PyTypeObject *base) |
6402 | { |
6403 | if (type->tp_as_async == NULL) { Branch (6403:9): [True: 21.6k, False: 93.0k]
|
6404 | type->tp_as_async = base->tp_as_async; |
6405 | } |
6406 | if (type->tp_as_number == NULL) { Branch (6406:9): [True: 19.7k, False: 94.9k]
|
6407 | type->tp_as_number = base->tp_as_number; |
6408 | } |
6409 | if (type->tp_as_sequence == NULL) { Branch (6409:9): [True: 19.9k, False: 94.7k]
|
6410 | type->tp_as_sequence = base->tp_as_sequence; |
6411 | } |
6412 | if (type->tp_as_mapping == NULL) { Branch (6412:9): [True: 20.0k, False: 94.6k]
|
6413 | type->tp_as_mapping = base->tp_as_mapping; |
6414 | } |
6415 | if (type->tp_as_buffer == NULL) { Branch (6415:9): [True: 21.8k, False: 92.9k]
|
6416 | type->tp_as_buffer = base->tp_as_buffer; |
6417 | } |
6418 | } |
6419 | |
6420 | static void |
6421 | inherit_patma_flags(PyTypeObject *type, PyTypeObject *base) { |
6422 | if ((type->tp_flags & COLLECTION_FLAGS) == 0) { Branch (6422:9): [True: 289k, False: 15.8k]
|
6423 | type->tp_flags |= base->tp_flags & COLLECTION_FLAGS; |
6424 | } |
6425 | } |
6426 | |
6427 | static int |
6428 | type_ready_inherit(PyTypeObject *type) |
6429 | { |
6430 | /* Inherit special flags from dominant base */ |
6431 | PyTypeObject *base = type->tp_base; |
6432 | if (base != NULL) { Branch (6432:9): [True: 114k, False: 104]
|
6433 | inherit_special(type, base); |
6434 | } |
6435 | |
6436 | // Inherit slots |
6437 | PyObject *mro = type->tp_mro; |
6438 | Py_ssize_t n = PyTuple_GET_SIZE(type->tp_mro); |
6439 | for (Py_ssize_t i = 1; i < n; i++305k ) { Branch (6439:28): [True: 305k, False: 114k]
|
6440 | PyObject *b = PyTuple_GET_ITEM(mro, i); |
6441 | if (PyType_Check(b)) { |
6442 | if (inherit_slots(type, (PyTypeObject *)b) < 0) { Branch (6442:17): [True: 0, False: 305k]
|
6443 | return -1; |
6444 | } |
6445 | inherit_patma_flags(type, (PyTypeObject *)b); |
6446 | } |
6447 | } |
6448 | |
6449 | if (base != NULL) { Branch (6449:9): [True: 114k, False: 104]
|
6450 | type_ready_inherit_as_structs(type, base); |
6451 | } |
6452 | |
6453 | /* Sanity check for tp_free. */ |
6454 | if (_PyType_IS_GC(type) && (type->tp_flags & 110k Py_TPFLAGS_BASETYPE110k ) && Branch (6454:32): [True: 98.7k, False: 12.1k]
|
6455 | (98.7k type->tp_free == NULL98.7k || type->tp_free == 98.7k PyObject_Del98.7k )) Branch (6455:10): [True: 0, False: 98.7k]
Branch (6455:35): [True: 0, False: 98.7k]
|
6456 | { |
6457 | /* This base class needs to call tp_free, but doesn't have |
6458 | * one, or its tp_free is for non-gc'ed objects. |
6459 | */ |
6460 | PyErr_Format(PyExc_TypeError, "type '%.100s' participates in " |
6461 | "gc and is a base type but has inappropriate " |
6462 | "tp_free slot", |
6463 | type->tp_name); |
6464 | return -1; |
6465 | } |
6466 | |
6467 | return 0; |
6468 | } |
6469 | |
6470 | |
6471 | /* Hack for tp_hash and __hash__. |
6472 | If after all that, tp_hash is still NULL, and __hash__ is not in |
6473 | tp_dict, set tp_hash to PyObject_HashNotImplemented and |
6474 | tp_dict['__hash__'] equal to None. |
6475 | This signals that __hash__ is not inherited. */ |
6476 | static int |
6477 | type_ready_set_hash(PyTypeObject *type) |
6478 | { |
6479 | if (type->tp_hash != NULL) { Branch (6479:9): [True: 97.7k, False: 17.0k]
|
6480 | return 0; |
6481 | } |
6482 | |
6483 | int r = PyDict_Contains(type->tp_dict, &_Py_ID(__hash__)); |
6484 | if (r < 0) { Branch (6484:9): [True: 0, False: 17.0k]
|
6485 | return -1; |
6486 | } |
6487 | if (r > 0) { Branch (6487:9): [True: 14.8k, False: 2.21k]
|
6488 | return 0; |
6489 | } |
6490 | |
6491 | if (PyDict_SetItem(type->tp_dict, &_Py_ID(__hash__), Py_None) < 0) { Branch (6491:9): [True: 0, False: 2.21k]
|
6492 | return -1; |
6493 | } |
6494 | type->tp_hash = PyObject_HashNotImplemented; |
6495 | return 0; |
6496 | } |
6497 | |
6498 | |
6499 | /* Link into each base class's list of subclasses */ |
6500 | static int |
6501 | type_ready_add_subclasses(PyTypeObject *type) |
6502 | { |
6503 | PyObject *bases = type->tp_bases; |
6504 | Py_ssize_t nbase = PyTuple_GET_SIZE(bases); |
6505 | for (Py_ssize_t i = 0; i < nbase; i++129k ) { Branch (6505:28): [True: 129k, False: 114k]
|
6506 | PyObject *b = PyTuple_GET_ITEM(bases, i); |
6507 | if (PyType_Check(b) && add_subclass((PyTypeObject *)b, type) < 0) { Branch (6507:32): [True: 0, False: 129k]
|
6508 | return -1; |
6509 | } |
6510 | } |
6511 | return 0; |
6512 | } |
6513 | |
6514 | |
6515 | // Set tp_new and the "__new__" key in the type dictionary. |
6516 | // Use the Py_TPFLAGS_DISALLOW_INSTANTIATION flag. |
6517 | static int |
6518 | type_ready_set_new(PyTypeObject *type) |
6519 | { |
6520 | PyTypeObject *base = type->tp_base; |
6521 | /* The condition below could use some explanation. |
6522 | |
6523 | It appears that tp_new is not inherited for static types whose base |
6524 | class is 'object'; this seems to be a precaution so that old extension |
6525 | types don't suddenly become callable (object.__new__ wouldn't insure the |
6526 | invariants that the extension type's own factory function ensures). |
6527 | |
6528 | Heap types, of course, are under our control, so they do inherit tp_new; |
6529 | static extension types that specify some other built-in type as the |
6530 | default also inherit object.__new__. */ |
6531 | if (type->tp_new == NULL Branch (6531:9): [True: 97.5k, False: 17.3k]
|
6532 | && base == &PyBaseObject_Type97.5k Branch (6532:12): [True: 39.5k, False: 57.9k]
|
6533 | && !(type->tp_flags & 39.5k Py_TPFLAGS_HEAPTYPE39.5k )) Branch (6533:12): [True: 6.54k, False: 33.0k]
|
6534 | { |
6535 | type->tp_flags |= Py_TPFLAGS_DISALLOW_INSTANTIATION; |
6536 | } |
6537 | |
6538 | if (!(type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION)) { Branch (6538:9): [True: 106k, False: 8.26k]
|
6539 | if (type->tp_new != NULL) { Branch (6539:13): [True: 17.0k, False: 89.4k]
|
6540 | // If "__new__" key does not exists in the type dictionary, |
6541 | // set it to tp_new_wrapper(). |
6542 | if (add_tp_new_wrapper(type) < 0) { Branch (6542:17): [True: 0, False: 17.0k]
|
6543 | return -1; |
6544 | } |
6545 | } |
6546 | else { |
6547 | // tp_new is NULL: inherit tp_new from base |
6548 | type->tp_new = base->tp_new; |
6549 | } |
6550 | } |
6551 | else { |
6552 | // Py_TPFLAGS_DISALLOW_INSTANTIATION sets tp_new to NULL |
6553 | type->tp_new = NULL; |
6554 | } |
6555 | return 0; |
6556 | } |
6557 | |
6558 | |
6559 | static int |
6560 | type_ready_post_checks(PyTypeObject *type) |
6561 | { |
6562 | // bpo-44263: tp_traverse is required if Py_TPFLAGS_HAVE_GC is set. |
6563 | // Note: tp_clear is optional. |
6564 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC Branch (6564:9): [True: 110k, False: 3.94k]
|
6565 | && type->tp_traverse == NULL110k ) Branch (6565:12): [True: 0, False: 110k]
|
6566 | { |
6567 | PyErr_Format(PyExc_SystemError, |
6568 | "type %s has the Py_TPFLAGS_HAVE_GC flag " |
6569 | "but has no traverse function", |
6570 | type->tp_name); |
6571 | return -1; |
6572 | } |
6573 | return 0; |
6574 | } |
6575 | |
6576 | |
6577 | static int |
6578 | type_ready(PyTypeObject *type) |
6579 | { |
6580 | if (type_ready_pre_checks(type) < 0) { Branch (6580:9): [True: 0, False: 114k]
|
6581 | return -1; |
6582 | } |
6583 | |
6584 | #ifdef Py_TRACE_REFS |
6585 | /* PyType_Ready is the closest thing we have to a choke point |
6586 | * for type objects, so is the best place I can think of to try |
6587 | * to get type objects into the doubly-linked list of all objects. |
6588 | * Still, not all type objects go through PyType_Ready. |
6589 | */ |
6590 | _Py_AddToAllObjects((PyObject *)type, 0); |
6591 | #endif |
6592 | |
6593 | /* Initialize tp_dict: _PyType_IsReady() tests if tp_dict != NULL */ |
6594 | if (type_ready_set_dict(type) < 0) { Branch (6594:9): [True: 0, False: 114k]
|
6595 | return -1; |
6596 | } |
6597 | if (type_ready_set_bases(type) < 0) { Branch (6597:9): [True: 0, False: 114k]
|
6598 | return -1; |
6599 | } |
6600 | if (type_ready_mro(type) < 0) { Branch (6600:9): [True: 16, False: 114k]
|
6601 | return -1; |
6602 | } |
6603 | if (type_ready_set_new(type) < 0) { Branch (6603:9): [True: 0, False: 114k]
|
6604 | return -1; |
6605 | } |
6606 | if (type_ready_fill_dict(type) < 0) { Branch (6606:9): [True: 0, False: 114k]
|
6607 | return -1; |
6608 | } |
6609 | if (type_ready_inherit(type) < 0) { Branch (6609:9): [True: 0, False: 114k]
|
6610 | return -1; |
6611 | } |
6612 | if (type_ready_set_hash(type) < 0) { Branch (6612:9): [True: 0, False: 114k]
|
6613 | return -1; |
6614 | } |
6615 | if (type_ready_add_subclasses(type) < 0) { Branch (6615:9): [True: 0, False: 114k]
|
6616 | return -1; |
6617 | } |
6618 | if (type_ready_post_checks(type) < 0) { Branch (6618:9): [True: 0, False: 114k]
|
6619 | return -1; |
6620 | } |
6621 | return 0; |
6622 | } |
6623 | |
6624 | |
6625 | int |
6626 | PyType_Ready(PyTypeObject *type) |
6627 | { |
6628 | if (type->tp_flags & Py_TPFLAGS_READY) { Branch (6628:9): [True: 6.56k, False: 114k]
|
6629 | assert(_PyType_CheckConsistency(type)); |
6630 | return 0; |
6631 | } |
6632 | _PyObject_ASSERT((PyObject *)type, |
6633 | (type->tp_flags & Py_TPFLAGS_READYING) == 0); |
6634 | |
6635 | type->tp_flags |= Py_TPFLAGS_READYING; |
6636 | |
6637 | /* Historically, all static types were immutable. See bpo-43908 */ |
6638 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { Branch (6638:9): [True: 22.4k, False: 92.3k]
|
6639 | type->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE; |
6640 | } |
6641 | |
6642 | if (type_ready(type) < 0) { Branch (6642:9): [True: 16, False: 114k]
|
6643 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
6644 | return -1; |
6645 | } |
6646 | |
6647 | /* All done -- set the ready flag */ |
6648 | type->tp_flags = (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
6649 | assert(_PyType_CheckConsistency(type)); |
6650 | return 0; |
6651 | } |
6652 | |
6653 | |
6654 | static int |
6655 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
6656 | { |
6657 | PyObject *key = PyLong_FromVoidPtr((void *) type); |
6658 | if (key == NULL) Branch (6658:9): [True: 0, False: 129k]
|
6659 | return -1; |
6660 | |
6661 | PyObject *ref = PyWeakref_NewRef((PyObject *)type, NULL); |
6662 | if (ref == NULL) { Branch (6662:9): [True: 0, False: 129k]
|
6663 | Py_DECREF(key); |
6664 | return -1; |
6665 | } |
6666 | |
6667 | // Only get tp_subclasses after creating the key and value. |
6668 | // PyWeakref_NewRef() can trigger a garbage collection which can execute |
6669 | // arbitrary Python code and so modify base->tp_subclasses. |
6670 | PyObject *subclasses = base->tp_subclasses; |
6671 | if (subclasses == NULL) { Branch (6671:9): [True: 16.2k, False: 113k]
|
6672 | base->tp_subclasses = subclasses = PyDict_New(); |
6673 | if (subclasses == NULL) { Branch (6673:13): [True: 0, False: 16.2k]
|
6674 | Py_DECREF(key); |
6675 | Py_DECREF(ref); |
6676 | return -1; |
6677 | } |
6678 | } |
6679 | assert(PyDict_CheckExact(subclasses)); |
6680 | |
6681 | int result = PyDict_SetItem(subclasses, key, ref); |
6682 | Py_DECREF(ref); |
6683 | Py_DECREF(key); |
6684 | return result; |
6685 | } |
6686 | |
6687 | static int |
6688 | add_all_subclasses(PyTypeObject *type, PyObject *bases) |
6689 | { |
6690 | Py_ssize_t n = PyTuple_GET_SIZE(bases); |
6691 | int res = 0; |
6692 | for (Py_ssize_t i = 0; i < n; i++51 ) { Branch (6692:28): [True: 51, False: 45]
|
6693 | PyObject *obj = PyTuple_GET_ITEM(bases, i); |
6694 | // bases tuple must only contain types |
6695 | PyTypeObject *base = _PyType_CAST(obj); |
6696 | if (add_subclass(base, type) < 0) { Branch (6696:13): [True: 0, False: 51]
|
6697 | res = -1; |
6698 | } |
6699 | } |
6700 | return res; |
6701 | } |
6702 | |
6703 | static void |
6704 | remove_subclass(PyTypeObject *base, PyTypeObject *type) |
6705 | { |
6706 | PyObject *subclasses = base->tp_subclasses; // borrowed ref |
6707 | if (subclasses == NULL) { Branch (6707:9): [True: 12, False: 126k]
|
6708 | return; |
6709 | } |
6710 | assert(PyDict_CheckExact(subclasses)); |
6711 | |
6712 | PyObject *key = PyLong_FromVoidPtr((void *) type); |
6713 | if (key == NULL || PyDict_DelItem(subclasses, key)) { Branch (6713:9): [True: 0, False: 126k]
Branch (6713:24): [True: 31, False: 126k]
|
6714 | /* This can happen if the type initialization errored out before |
6715 | the base subclasses were updated (e.g. a non-str __qualname__ |
6716 | was passed in the type dict). */ |
6717 | PyErr_Clear(); |
6718 | } |
6719 | Py_XDECREF(key); |
6720 | |
6721 | if (PyDict_Size(subclasses) == 0) { Branch (6721:9): [True: 15.7k, False: 110k]
|
6722 | // Delete the dictionary to save memory. _PyStaticType_Dealloc() |
6723 | // callers also test if tp_subclasses is NULL to check if a static type |
6724 | // has no subclass. |
6725 | Py_CLEAR(base->tp_subclasses); |
6726 | } |
6727 | } |
6728 | |
6729 | static void |
6730 | remove_all_subclasses(PyTypeObject *type, PyObject *bases) |
6731 | { |
6732 | assert(bases != NULL); |
6733 | // remove_subclass() can clear the current exception |
6734 | assert(!PyErr_Occurred()); |
6735 | |
6736 | for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(bases); i++126k ) { Branch (6736:28): [True: 126k, False: 111k]
|
6737 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
6738 | if (PyType_Check(base)) { |
6739 | remove_subclass((PyTypeObject*) base, type); |
6740 | } |
6741 | } |
6742 | assert(!PyErr_Occurred()); |
6743 | } |
6744 | |
6745 | static int |
6746 | check_num_args(PyObject *ob, int n) |
6747 | { |
6748 | if (!PyTuple_CheckExact(ob)) { Branch (6748:9): [True: 0, False: 1.57M]
|
6749 | PyErr_SetString(PyExc_SystemError, |
6750 | "PyArg_UnpackTuple() argument list is not a tuple"); |
6751 | return 0; |
6752 | } |
6753 | if (n == PyTuple_GET_SIZE(ob)) Branch (6753:9): [True: 1.57M, False: 35]
|
6754 | return 1; |
6755 | PyErr_Format( |
6756 | PyExc_TypeError, |
6757 | "expected %d argument%s, got %zd", n, n == 1 ? ""34 : "s"1 , PyTuple_GET_SIZE(ob)); Branch (6757:47): [True: 34, False: 1]
|
6758 | return 0; |
6759 | } |
6760 | |
6761 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
6762 | |
6763 | /* There's a wrapper *function* for each distinct function typedef used |
6764 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
6765 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
6766 | Most tables have only one entry; the tables for binary operators have two |
6767 | entries, one regular and one with reversed arguments. */ |
6768 | |
6769 | static PyObject * |
6770 | wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) |
6771 | { |
6772 | lenfunc func = (lenfunc)wrapped; |
6773 | Py_ssize_t res; |
6774 | |
6775 | if (!check_num_args(args, 0)) Branch (6775:9): [True: 0, False: 4.42k]
|
6776 | return NULL; |
6777 | res = (*func)(self); |
6778 | if (res == -1 && PyErr_Occurred()0 ) Branch (6778:9): [True: 0, False: 4.42k]
Branch (6778:22): [True: 0, False: 0]
|
6779 | return NULL; |
6780 | return PyLong_FromSsize_t(res); |
6781 | } |
6782 | |
6783 | static PyObject * |
6784 | wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) |
6785 | { |
6786 | inquiry func = (inquiry)wrapped; |
6787 | int res; |
6788 | |
6789 | if (!check_num_args(args, 0)) Branch (6789:9): [True: 0, False: 2]
|
6790 | return NULL; |
6791 | res = (*func)(self); |
6792 | if (res == -1 && PyErr_Occurred()0 ) Branch (6792:9): [True: 0, False: 2]
Branch (6792:22): [True: 0, False: 0]
|
6793 | return NULL; |
6794 | return PyBool_FromLong((long)res); |
6795 | } |
6796 | |
6797 | static PyObject * |
6798 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
6799 | { |
6800 | binaryfunc func = (binaryfunc)wrapped; |
6801 | PyObject *other; |
6802 | |
6803 | if (!check_num_args(args, 1)) Branch (6803:9): [True: 14, False: 497k]
|
6804 | return NULL; |
6805 | other = PyTuple_GET_ITEM(args, 0); |
6806 | return (*func)(self, other); |
6807 | } |
6808 | |
6809 | static PyObject * |
6810 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
6811 | { |
6812 | binaryfunc func = (binaryfunc)wrapped; |
6813 | PyObject *other; |
6814 | |
6815 | if (!check_num_args(args, 1)) Branch (6815:9): [True: 1, False: 45.7k]
|
6816 | return NULL; |
6817 | other = PyTuple_GET_ITEM(args, 0); |
6818 | return (*func)(self, other); |
6819 | } |
6820 | |
6821 | static PyObject * |
6822 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
6823 | { |
6824 | binaryfunc func = (binaryfunc)wrapped; |
6825 | PyObject *other; |
6826 | |
6827 | if (!check_num_args(args, 1)) Branch (6827:9): [True: 0, False: 30]
|
6828 | return NULL; |
6829 | other = PyTuple_GET_ITEM(args, 0); |
6830 | return (*func)(other, self); |
6831 | } |
6832 | |
6833 | static PyObject * |
6834 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
6835 | { |
6836 | ternaryfunc func = (ternaryfunc)wrapped; |
6837 | PyObject *other; |
6838 | PyObject *third = Py_None; |
6839 | |
6840 | /* Note: This wrapper only works for __pow__() */ |
6841 | |
6842 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) Branch (6842:9): [True: 0, False: 8]
|
6843 | return NULL; |
6844 | return (*func)(self, other, third); |
6845 | } |
6846 | |
6847 | static PyObject * |
6848 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
6849 | { |
6850 | ternaryfunc func = (ternaryfunc)wrapped; |
6851 | PyObject *other; |
6852 | PyObject *third = Py_None; |
6853 | |
6854 | /* Note: This wrapper only works for __pow__() */ |
6855 | |
6856 | if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third)) Branch (6856:9): [True: 0, False: 1]
|
6857 | return NULL; |
6858 | return (*func)(other, self, third); |
6859 | } |
6860 | |
6861 | static PyObject * |
6862 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
6863 | { |
6864 | unaryfunc func = (unaryfunc)wrapped; |
6865 | |
6866 | if (!check_num_args(args, 0)) Branch (6866:9): [True: 1, False: 47.4k]
|
6867 | return NULL; |
6868 | return (*func)(self); |
6869 | } |
6870 | |
6871 | static PyObject * |
6872 | wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) |
6873 | { |
6874 | ssizeargfunc func = (ssizeargfunc)wrapped; |
6875 | PyObject* o; |
6876 | Py_ssize_t i; |
6877 | |
6878 | if (!PyArg_UnpackTuple(args, "", 1, 1, &o)) Branch (6878:9): [True: 1, False: 65]
|
6879 | return NULL; |
6880 | i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
6881 | if (i == -1 && PyErr_Occurred()28 ) Branch (6881:9): [True: 28, False: 37]
Branch (6881:20): [True: 27, False: 1]
|
6882 | return NULL; |
6883 | return (*func)(self, i); |
6884 | } |
6885 | |
6886 | static Py_ssize_t |
6887 | getindex(PyObject *self, PyObject *arg) |
6888 | { |
6889 | Py_ssize_t i; |
6890 | |
6891 | i = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
6892 | if (i == -1 && PyErr_Occurred()1 ) Branch (6892:9): [True: 1, False: 3]
Branch (6892:20): [True: 0, False: 1]
|
6893 | return -1; |
6894 | if (i < 0) { Branch (6894:9): [True: 2, False: 2]
|
6895 | PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; |
6896 | if (sq && sq->sq_length) { Branch (6896:13): [True: 2, False: 0]
Branch (6896:19): [True: 2, False: 0]
|
6897 | Py_ssize_t n = (*sq->sq_length)(self); |
6898 | if (n < 0) { Branch (6898:17): [True: 0, False: 2]
|
6899 | assert(PyErr_Occurred()); |
6900 | return -1; |
6901 | } |
6902 | i += n; |
6903 | } |
6904 | } |
6905 | return i; |
6906 | } |
6907 | |
6908 | static PyObject * |
6909 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
6910 | { |
6911 | ssizeargfunc func = (ssizeargfunc)wrapped; |
6912 | PyObject *arg; |
6913 | Py_ssize_t i; |
6914 | |
6915 | if (PyTuple_GET_SIZE(args) == 1) { Branch (6915:9): [True: 2, False: 0]
|
6916 | arg = PyTuple_GET_ITEM(args, 0); |
6917 | i = getindex(self, arg); |
6918 | if (i == -1 && PyErr_Occurred()1 ) Branch (6918:13): [True: 1, False: 1]
Branch (6918:24): [True: 0, False: 1]
|
6919 | return NULL; |
6920 | return (*func)(self, i); |
6921 | } |
6922 | check_num_args(args, 1); |
6923 | assert(PyErr_Occurred()); |
6924 | return NULL; |
6925 | } |
6926 | |
6927 | static PyObject * |
6928 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
6929 | { |
6930 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
6931 | Py_ssize_t i; |
6932 | int res; |
6933 | PyObject *arg, *value; |
6934 |
|
6935 | if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) Branch (6935:9): [True: 0, False: 0]
|
6936 | return NULL; |
6937 | i = getindex(self, arg); |
6938 | if (i == -1 && PyErr_Occurred()) Branch (6938:9): [True: 0, False: 0]
Branch (6938:20): [True: 0, False: 0]
|
6939 | return NULL; |
6940 | res = (*func)(self, i, value); |
6941 | if (res == -1 && PyErr_Occurred()) Branch (6941:9): [True: 0, False: 0]
Branch (6941:22): [True: 0, False: 0]
|
6942 | return NULL; |
6943 | Py_RETURN_NONE; |
6944 | } |
6945 | |
6946 | static PyObject * |
6947 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
6948 | { |
6949 | ssizeobjargproc func = (ssizeobjargproc)wrapped; |
6950 | Py_ssize_t i; |
6951 | int res; |
6952 | PyObject *arg; |
6953 | |
6954 | if (!check_num_args(args, 1)) Branch (6954:9): [True: 0, False: 2]
|
6955 | return NULL; |
6956 | arg = PyTuple_GET_ITEM(args, 0); |
6957 | i = getindex(self, arg); |
6958 | if (i == -1 && PyErr_Occurred()1 ) Branch (6958:9): [True: 1, False: 1]
Branch (6958:20): [True: 0, False: 1]
|
6959 | return NULL; |
6960 | res = (*func)(self, i, NULL); |
6961 | if (res == -1 && PyErr_Occurred()) Branch (6961:9): [True: 2, False: 0]
Branch (6961:22): [True: 2, False: 0]
|
6962 | return NULL; |
6963 | Py_RETURN_NONE0 ; |
6964 | } |
6965 | |
6966 | /* XXX objobjproc is a misnomer; should be objargpred */ |
6967 | static PyObject * |
6968 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
6969 | { |
6970 | objobjproc func = (objobjproc)wrapped; |
6971 | int res; |
6972 | PyObject *value; |
6973 | |
6974 | if (!check_num_args(args, 1)) Branch (6974:9): [True: 4, False: 64]
|
6975 | return NULL; |
6976 | value = PyTuple_GET_ITEM(args, 0); |
6977 | res = (*func)(self, value); |
6978 | if (res == -1 && PyErr_Occurred()6 ) Branch (6978:9): [True: 6, False: 58]
Branch (6978:22): [True: 6, False: 0]
|
6979 | return NULL; |
6980 | else |
6981 | return PyBool_FromLong(res); |
6982 | } |
6983 | |
6984 | static PyObject * |
6985 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
6986 | { |
6987 | objobjargproc func = (objobjargproc)wrapped; |
6988 | int res; |
6989 | PyObject *key, *value; |
6990 | |
6991 | if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value)) Branch (6991:9): [True: 29, False: 117k]
|
6992 | return NULL; |
6993 | res = (*func)(self, key, value); |
6994 | if (res == -1 && PyErr_Occurred()450 ) Branch (6994:9): [True: 450, False: 117k]
Branch (6994:22): [True: 450, False: 0]
|
6995 | return NULL; |
6996 | Py_RETURN_NONE117k ; |
6997 | } |
6998 | |
6999 | static PyObject * |
7000 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
7001 | { |
7002 | objobjargproc func = (objobjargproc)wrapped; |
7003 | int res; |
7004 | PyObject *key; |
7005 | |
7006 | if (!check_num_args(args, 1)) Branch (7006:9): [True: 14, False: 21.6k]
|
7007 | return NULL; |
7008 | key = PyTuple_GET_ITEM(args, 0); |
7009 | res = (*func)(self, key, NULL); |
7010 | if (res == -1 && PyErr_Occurred()50 ) Branch (7010:9): [True: 50, False: 21.6k]
Branch (7010:22): [True: 50, False: 0]
|
7011 | return NULL; |
7012 | Py_RETURN_NONE21.6k ; |
7013 | } |
7014 | |
7015 | /* Helper to check for object.__setattr__ or __delattr__ applied to a type. |
7016 | This is called the Carlo Verre hack after its discoverer. See |
7017 | https://mail.python.org/pipermail/python-dev/2003-April/034535.html |
7018 | */ |
7019 | static int |
7020 | hackcheck(PyObject *self, setattrofunc func, const char *what) |
7021 | { |
7022 | PyTypeObject *type = Py_TYPE(self); |
7023 | PyObject *mro = type->tp_mro; |
7024 | if (!mro) { Branch (7024:9): [True: 0, False: 463k]
|
7025 | /* Probably ok not to check the call in this case. */ |
7026 | return 1; |
7027 | } |
7028 | assert(PyTuple_Check(mro)); |
7029 | |
7030 | /* Find the (base) type that defined the type's slot function. */ |
7031 | PyTypeObject *defining_type = type; |
7032 | Py_ssize_t i; |
7033 | for (i = PyTuple_GET_SIZE463k (mro) - 1; i >= 0; i--1.98M ) { Branch (7033:41): [True: 1.98M, False: 463k]
|
7034 | PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(mro, i)); |
7035 | if (base->tp_setattro == slot_tp_setattro) { Branch (7035:13): [True: 967k, False: 1.01M]
|
7036 | /* Ignore Python classes: |
7037 | they never define their own C-level setattro. */ |
7038 | } |
7039 | else if (base->tp_setattro == type->tp_setattro) { Branch (7039:18): [True: 14, False: 1.01M]
|
7040 | defining_type = base; |
7041 | break; |
7042 | } |
7043 | } |
7044 | |
7045 | /* Reject calls that jump over intermediate C-level overrides. */ |
7046 | for (PyTypeObject *base = defining_type; base; base = base->tp_base707k ) { Branch (7046:46): [True: 1.17M, False: 0]
|
7047 | if (base->tp_setattro == func) { Branch (7047:13): [True: 463k, False: 707k]
|
7048 | /* 'func' is the right slot function to call. */ |
7049 | break; |
7050 | } |
7051 | else if (base->tp_setattro != slot_tp_setattro) { Branch (7051:18): [True: 3, False: 707k]
|
7052 | /* 'base' is not a Python class and overrides 'func'. |
7053 | Its tp_setattro should be called instead. */ |
7054 | PyErr_Format(PyExc_TypeError, |
7055 | "can't apply this %s to %s object", |
7056 | what, |
7057 | type->tp_name); |
7058 | return 0; |
7059 | } |
7060 | } |
7061 | return 1; |
7062 | } |
7063 | |
7064 | static PyObject * |
7065 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
7066 | { |
7067 | setattrofunc func = (setattrofunc)wrapped; |
7068 | int res; |
7069 | PyObject *name, *value; |
7070 | |
7071 | if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value)) Branch (7071:9): [True: 0, False: 445k]
|
7072 | return NULL; |
7073 | if (!hackcheck(self, func, "__setattr__")) Branch (7073:9): [True: 2, False: 445k]
|
7074 | return NULL; |
7075 | res = (*func)(self, name, value); |
7076 | if (res < 0) Branch (7076:9): [True: 3, False: 445k]
|
7077 | return NULL; |
7078 | Py_RETURN_NONE445k ; |
7079 | } |
7080 | |
7081 | static PyObject * |
7082 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
7083 | { |
7084 | setattrofunc func = (setattrofunc)wrapped; |
7085 | int res; |
7086 | PyObject *name; |
7087 | |
7088 | if (!check_num_args(args, 1)) Branch (7088:9): [True: 1, False: 18.2k]
|
7089 | return NULL; |
7090 | name = PyTuple_GET_ITEM(args, 0); |
7091 | if (!hackcheck(self, func, "__delattr__")) Branch (7091:9): [True: 1, False: 18.2k]
|
7092 | return NULL; |
7093 | res = (*func)(self, name, NULL); |
7094 | if (res < 0) Branch (7094:9): [True: 20, False: 18.1k]
|
7095 | return NULL; |
7096 | Py_RETURN_NONE18.1k ; |
7097 | } |
7098 | |
7099 | static PyObject * |
7100 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
7101 | { |
7102 | hashfunc func = (hashfunc)wrapped; |
7103 | Py_hash_t res; |
7104 | |
7105 | if (!check_num_args(args, 0)) Branch (7105:9): [True: 0, False: 606k]
|
7106 | return NULL; |
7107 | res = (*func)(self); |
7108 | if (res == -1 && PyErr_Occurred()4 ) Branch (7108:9): [True: 4, False: 606k]
Branch (7108:22): [True: 4, False: 0]
|
7109 | return NULL; |
7110 | return PyLong_FromSsize_t(res); |
7111 | } |
7112 | |
7113 | static PyObject * |
7114 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
7115 | { |
7116 | ternaryfunc func = (ternaryfunc)wrapped; |
7117 | |
7118 | return (*func)(self, args, kwds); |
7119 | } |
7120 | |
7121 | static PyObject * |
7122 | wrap_del(PyObject *self, PyObject *args, void *wrapped) |
7123 | { |
7124 | destructor func = (destructor)wrapped; |
7125 | |
7126 | if (!check_num_args(args, 0)) Branch (7126:9): [True: 0, False: 9]
|
7127 | return NULL; |
7128 | |
7129 | (*func)(self); |
7130 | Py_RETURN_NONE; |
7131 | } |
7132 | |
7133 | static PyObject * |
7134 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
7135 | { |
7136 | richcmpfunc func = (richcmpfunc)wrapped; |
7137 | PyObject *other; |
7138 | |
7139 | if (!check_num_args(args, 1)) Branch (7139:9): [True: 0, False: 125k]
|
7140 | return NULL; |
7141 | other = PyTuple_GET_ITEM(args, 0); |
7142 | return (*func)(self, other, op); |
7143 | } |
7144 | |
7145 | #undef RICHCMP_WRAPPER |
7146 | #define RICHCMP_WRAPPER(NAME, OP) \ |
7147 | static PyObject * \ |
7148 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
7149 | { \ |
7150 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
7151 | } Line | Count | Source | 7148 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ | 7149 | { \ | 7150 | return wrap_richcmpfunc(self, args, wrapped, OP); \ | 7151 | } |
Line | Count | Source | 7148 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ | 7149 | { \ | 7150 | return wrap_richcmpfunc(self, args, wrapped, OP); \ | 7151 | } |
Line | Count | Source | 7148 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ | 7149 | { \ | 7150 | return wrap_richcmpfunc(self, args, wrapped, OP); \ | 7151 | } |
Line | Count | Source | 7148 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ | 7149 | { \ | 7150 | return wrap_richcmpfunc(self, args, wrapped, OP); \ | 7151 | } |
Line | Count | Source | 7148 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ | 7149 | { \ | 7150 | return wrap_richcmpfunc(self, args, wrapped, OP); \ | 7151 | } |
Line | Count | Source | 7148 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ | 7149 | { \ | 7150 | return wrap_richcmpfunc(self, args, wrapped, OP); \ | 7151 | } |
|
7152 | |
7153 | RICHCMP_WRAPPER(lt, Py_LT) |
7154 | RICHCMP_WRAPPER(le, Py_LE) |
7155 | RICHCMP_WRAPPER(eq, Py_EQ) |
7156 | RICHCMP_WRAPPER(ne, Py_NE) |
7157 | RICHCMP_WRAPPER(gt, Py_GT) |
7158 | RICHCMP_WRAPPER(ge, Py_GE) |
7159 | |
7160 | static PyObject * |
7161 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
7162 | { |
7163 | unaryfunc func = (unaryfunc)wrapped; |
7164 | PyObject *res; |
7165 | |
7166 | if (!check_num_args(args, 0)) Branch (7166:9): [True: 0, False: 210k]
|
7167 | return NULL; |
7168 | res = (*func)(self); |
7169 | if (res == NULL && !PyErr_Occurred()5.99k ) Branch (7169:9): [True: 5.99k, False: 204k]
Branch (7169:24): [True: 5.91k, False: 71]
|
7170 | PyErr_SetNone(PyExc_StopIteration); |
7171 | return res; |
7172 | } |
7173 | |
7174 | static PyObject * |
7175 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
7176 | { |
7177 | descrgetfunc func = (descrgetfunc)wrapped; |
7178 | PyObject *obj; |
7179 | PyObject *type = NULL; |
7180 | |
7181 | if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type)) Branch (7181:9): [True: 0, False: 1.16k]
|
7182 | return NULL; |
7183 | if (obj == Py_None) Branch (7183:9): [True: 68, False: 1.10k]
|
7184 | obj = NULL; |
7185 | if (type == Py_None) Branch (7185:9): [True: 1, False: 1.16k]
|
7186 | type = NULL; |
7187 | if (type == NULL && obj == NULL1.01k ) { Branch (7187:9): [True: 1.01k, False: 154]
Branch (7187:25): [True: 1, False: 1.01k]
|
7188 | PyErr_SetString(PyExc_TypeError, |
7189 | "__get__(None, None) is invalid"); |
7190 | return NULL; |
7191 | } |
7192 | return (*func)(self, obj, type); |
7193 | } |
7194 | |
7195 | static PyObject * |
7196 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
7197 | { |
7198 | descrsetfunc func = (descrsetfunc)wrapped; |
7199 | PyObject *obj, *value; |
7200 | int ret; |
7201 | |
7202 | if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value)) Branch (7202:9): [True: 0, False: 585]
|
7203 | return NULL; |
7204 | ret = (*func)(self, obj, value); |
7205 | if (ret < 0) Branch (7205:9): [True: 16, False: 569]
|
7206 | return NULL; |
7207 | Py_RETURN_NONE569 ; |
7208 | } |
7209 | |
7210 | static PyObject * |
7211 | wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped) |
7212 | { |
7213 | descrsetfunc func = (descrsetfunc)wrapped; |
7214 | PyObject *obj; |
7215 | int ret; |
7216 | |
7217 | if (!check_num_args(args, 1)) Branch (7217:9): [True: 0, False: 3]
|
7218 | return NULL; |
7219 | obj = PyTuple_GET_ITEM(args, 0); |
7220 | ret = (*func)(self, obj, NULL); |
7221 | if (ret < 0) Branch (7221:9): [True: 2, False: 1]
|
7222 | return NULL; |
7223 | Py_RETURN_NONE1 ; |
7224 | } |
7225 | |
7226 | static PyObject * |
7227 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
7228 | { |
7229 | initproc func = (initproc)wrapped; |
7230 | |
7231 | if (func(self, args, kwds) < 0) Branch (7231:9): [True: 64, False: 823k]
|
7232 | return NULL; |
7233 | Py_RETURN_NONE823k ; |
7234 | } |
7235 | |
7236 | static PyObject * |
7237 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
7238 | { |
7239 | PyTypeObject *staticbase; |
7240 | PyObject *arg0, *res; |
7241 | |
7242 | if (self == NULL || !PyType_Check(self)) { Branch (7242:9): [True: 0, False: 3.18M]
Branch (7242:25): [True: 0, False: 3.18M]
|
7243 | PyErr_Format(PyExc_SystemError, |
7244 | "__new__() called with non-type 'self'"); |
7245 | return NULL; |
7246 | } |
7247 | PyTypeObject *type = (PyTypeObject *)self; |
7248 | |
7249 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { Branch (7249:9): [True: 0, False: 3.18M]
Branch (7249:33): [True: 3, False: 3.18M]
|
7250 | PyErr_Format(PyExc_TypeError, |
7251 | "%s.__new__(): not enough arguments", |
7252 | type->tp_name); |
7253 | return NULL; |
7254 | } |
7255 | arg0 = PyTuple_GET_ITEM(args, 0); |
7256 | if (!PyType_Check(arg0)) { Branch (7256:9): [True: 5, False: 3.18M]
|
7257 | PyErr_Format(PyExc_TypeError, |
7258 | "%s.__new__(X): X is not a type object (%s)", |
7259 | type->tp_name, |
7260 | Py_TYPE(arg0)->tp_name); |
7261 | return NULL; |
7262 | } |
7263 | PyTypeObject *subtype = (PyTypeObject *)arg0; |
7264 | |
7265 | if (!PyType_IsSubtype(subtype, type)) { Branch (7265:9): [True: 1, False: 3.18M]
|
7266 | PyErr_Format(PyExc_TypeError, |
7267 | "%s.__new__(%s): %s is not a subtype of %s", |
7268 | type->tp_name, |
7269 | subtype->tp_name, |
7270 | subtype->tp_name, |
7271 | type->tp_name); |
7272 | return NULL; |
7273 | } |
7274 | |
7275 | /* Check that the use doesn't do something silly and unsafe like |
7276 | object.__new__(dict). To do this, we check that the |
7277 | most derived base that's not a heap type is this type. */ |
7278 | staticbase = subtype; |
7279 | while (staticbase && (staticbase->tp_new == slot_tp_new)) Branch (7279:12): [True: 7.43M, False: 0]
Branch (7279:26): [True: 4.25M, False: 3.18M]
|
7280 | staticbase = staticbase->tp_base; |
7281 | /* If staticbase is NULL now, it is a really weird type. |
7282 | In the spirit of backwards compatibility (?), just shut up. */ |
7283 | if (staticbase && staticbase->tp_new != type->tp_new) { Branch (7283:9): [True: 3.18M, False: 0]
Branch (7283:23): [True: 4, False: 3.18M]
|
7284 | PyErr_Format(PyExc_TypeError, |
7285 | "%s.__new__(%s) is not safe, use %s.__new__()", |
7286 | type->tp_name, |
7287 | subtype->tp_name, |
7288 | staticbase->tp_name); |
7289 | return NULL; |
7290 | } |
7291 | |
7292 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
7293 | if (args == NULL) Branch (7293:9): [True: 0, False: 3.18M]
|
7294 | return NULL; |
7295 | res = type->tp_new(subtype, args, kwds); |
7296 | Py_DECREF(args); |
7297 | return res; |
7298 | } |
7299 | |
7300 | static struct PyMethodDef tp_new_methoddef[] = { |
7301 | {"__new__", _PyCFunction_CAST(tp_new_wrapper), METH_VARARGS|METH_KEYWORDS, |
7302 | PyDoc_STR("__new__($type, *args, **kwargs)\n--\n\n" |
7303 | "Create and return a new object. " |
7304 | "See help(type) for accurate signature.")}, |
7305 | {0} |
7306 | }; |
7307 | |
7308 | static int |
7309 | add_tp_new_wrapper(PyTypeObject *type) |
7310 | { |
7311 | int r = PyDict_Contains(type->tp_dict, &_Py_ID(__new__)); |
7312 | if (r > 0) { Branch (7312:9): [True: 0, False: 17.0k]
|
7313 | return 0; |
7314 | } |
7315 | if (r < 0) { Branch (7315:9): [True: 0, False: 17.0k]
|
7316 | return -1; |
7317 | } |
7318 | |
7319 | PyObject *func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL); |
7320 | if (func == NULL) { Branch (7320:9): [True: 0, False: 17.0k]
|
7321 | return -1; |
7322 | } |
7323 | r = PyDict_SetItem(type->tp_dict, &_Py_ID(__new__), func); |
7324 | Py_DECREF(func); |
7325 | return r; |
7326 | } |
7327 | |
7328 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
7329 | below at override_slots() for more explanation. */ |
7330 | |
7331 | #define SLOT0(FUNCNAME, DUNDER) \ |
7332 | static PyObject * \ |
7333 | FUNCNAME(PyObject *self) \ |
7334 | { \ |
7335 | PyObject* stack[1] = {self}; \ |
7336 | return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ |
7337 | } Line | Count | Source | 7333 | FUNCNAME(PyObject *self) \ | 7334 | { \ | 7335 | PyObject* stack[1] = {self}; \ | 7336 | return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ | 7337 | } |
typeobject.c:slot_nb_negative Line | Count | Source | 7333 | FUNCNAME(PyObject *self) \ | 7334 | { \ | 7335 | PyObject* stack[1] = {self}; \ | 7336 | return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ | 7337 | } |
typeobject.c:slot_nb_positive Line | Count | Source | 7333 | FUNCNAME(PyObject *self) \ | 7334 | { \ | 7335 | PyObject* stack[1] = {self}; \ | 7336 | return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ | 7337 | } |
typeobject.c:slot_nb_absolute Line | Count | Source | 7333 | FUNCNAME(PyObject *self) \ | 7334 | { \ | 7335 | PyObject* stack[1] = {self}; \ | 7336 | return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ | 7337 | } |
typeobject.c:slot_nb_invert Line | Count | Source | 7333 | FUNCNAME(PyObject *self) \ | 7334 | { \ | 7335 | PyObject* stack[1] = {self}; \ | 7336 | return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ | 7337 | } |
Line | Count | Source | 7333 | FUNCNAME(PyObject *self) \ | 7334 | { \ | 7335 | PyObject* stack[1] = {self}; \ | 7336 | return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ | 7337 | } |
typeobject.c:slot_nb_float Line | Count | Source | 7333 | FUNCNAME(PyObject *self) \ | 7334 | { \ | 7335 | PyObject* stack[1] = {self}; \ | 7336 | return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ | 7337 | } |
|
7338 | |
7339 | #define SLOT1(FUNCNAME, DUNDER, ARG1TYPE) \ |
7340 | static PyObject * \ |
7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
7342 | { \ |
7343 | PyObject* stack[2] = {self, arg1}; \ |
7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ |
7345 | } typeobject.c:slot_nb_inplace_add Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_subtract Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_multiply Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_remainder Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_lshift Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_rshift Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_and Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_xor Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_or Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_floor_divide Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_true_divide Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_nb_inplace_matrix_multiply Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
typeobject.c:slot_mp_subscript Line | Count | Source | 7341 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ | 7342 | { \ | 7343 | PyObject* stack[2] = {self, arg1}; \ | 7344 | return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ | 7345 | } |
|
7346 | |
7347 | /* Boolean helper for SLOT1BINFULL(). |
7348 | right.__class__ is a nontrivial subclass of left.__class__. */ |
7349 | static int |
7350 | method_is_overloaded(PyObject *left, PyObject *right, PyObject *name) |
7351 | { |
7352 | PyObject *a, *b; |
7353 | int ok; |
7354 | |
7355 | if (_PyObject_LookupAttr((PyObject *)(Py_TYPE(right)), name, &b) < 0) { Branch (7355:9): [True: 0, False: 10]
|
7356 | return -1; |
7357 | } |
7358 | if (b == NULL) { Branch (7358:9): [True: 0, False: 10]
|
7359 | /* If right doesn't have it, it's not overloaded */ |
7360 | return 0; |
7361 | } |
7362 | |
7363 | if (_PyObject_LookupAttr((PyObject *)(Py_TYPE(left)), name, &a) < 0) { Branch (7363:9): [True: 0, False: 10]
|
7364 | Py_DECREF(b); |
7365 | return -1; |
7366 | } |
7367 | if (a == NULL) { Branch (7367:9): [True: 0, False: 10]
|
7368 | Py_DECREF(b); |
7369 | /* If right has it but left doesn't, it's overloaded */ |
7370 | return 1; |
7371 | } |
7372 | |
7373 | ok = PyObject_RichCompareBool(a, b, Py_NE); |
7374 | Py_DECREF(a); |
7375 | Py_DECREF(b); |
7376 | return ok; |
7377 | } |
7378 | |
7379 | |
7380 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, DUNDER, RDUNDER) \ |
7381 | static PyObject * \ |
7382 | FUNCNAME(PyObject *self, PyObject *other) \ |
7383 | { \ |
7384 | PyObject* stack[2]; \ |
7385 | PyThreadState *tstate = _PyThreadState_GET(); \ |
7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 16.8k, False: 139k]
Branch (7386:20): [True: 3.08k, False: 19.9k]
Branch (7386:20): [True: 2.21k, False: 375]
Branch (7386:20): [True: 29, False: 20]
Branch (7386:20): [True: 48, False: 2.55k]
Branch (7386:20): [True: 117k, False: 22]
Branch (7386:20): [True: 9, False: 2]
Branch (7386:20): [True: 10, False: 2]
Branch (7386:20): [True: 21.3k, False: 4.89k]
Branch (7386:20): [True: 166, False: 58]
Branch (7386:20): [True: 1.24k, False: 5.03k]
Branch (7386:20): [True: 66, False: 1.67k]
Branch (7386:20): [True: 141k, False: 21.8k]
Branch (7386:20): [True: 16, False: 3]
|
7387 | Py_TYPE304k (other)->tp_as_number != NULL304k && \ Branch (7387:9): [True: 16.8k, False: 6]
Branch (7387:9): [True: 3.08k, False: 2]
Branch (7387:9): [True: 2.21k, False: 0]
Branch (7387:9): [True: 29, False: 0]
Branch (7387:9): [True: 48, False: 0]
Branch (7387:9): [True: 117k, False: 0]
Branch (7387:9): [True: 9, False: 0]
Branch (7387:9): [True: 10, False: 0]
Branch (7387:9): [True: 21.3k, False: 1]
Branch (7387:9): [True: 165, False: 1]
Branch (7387:9): [True: 1.24k, False: 4]
Branch (7387:9): [True: 66, False: 0]
Branch (7387:9): [True: 141k, False: 0]
Branch (7387:9): [True: 16, False: 0]
|
7388 | Py_TYPE304k (other)->tp_as_number->SLOTNAME == TESTFUNC304k ; \ Branch (7388:9): [True: 16.7k, False: 123]
Branch (7388:9): [True: 2.93k, False: 149]
Branch (7388:9): [True: 1.79k, False: 418]
Branch (7388:9): [True: 11, False: 18]
Branch (7388:9): [True: 9, False: 39]
Branch (7388:9): [True: 18, False: 117k]
Branch (7388:9): [True: 3, False: 6]
Branch (7388:9): [True: 4, False: 6]
Branch (7388:9): [True: 20.2k, False: 1.02k]
Branch (7388:9): [True: 80, False: 85]
Branch (7388:9): [True: 886, False: 359]
Branch (7388:9): [True: 35, False: 31]
Branch (7388:9): [True: 207, False: 141k]
Branch (7388:9): [True: 5, False: 11]
|
7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 156k, False: 2]
Branch (7389:9): [True: 23.0k, False: 0]
Branch (7389:9): [True: 2.58k, False: 0]
Branch (7389:9): [True: 49, False: 0]
Branch (7389:9): [True: 2.59k, False: 0]
Branch (7389:9): [True: 117k, False: 1]
Branch (7389:9): [True: 11, False: 0]
Branch (7389:9): [True: 11, False: 1]
Branch (7389:9): [True: 26.2k, False: 0]
Branch (7389:9): [True: 224, False: 0]
Branch (7389:9): [True: 6.28k, False: 0]
Branch (7389:9): [True: 1.74k, False: 0]
Branch (7389:9): [True: 163k, False: 0]
Branch (7389:9): [True: 19, False: 0]
|
7390 | Py_TYPE500k (self)->tp_as_number->SLOTNAME == TESTFUNC500k ) { \ Branch (7390:9): [True: 155k, False: 696]
Branch (7390:9): [True: 23.0k, False: 38]
Branch (7390:9): [True: 805, False: 1.78k]
Branch (7390:9): [True: 41, False: 8]
Branch (7390:9): [True: 2.59k, False: 7]
Branch (7390:9): [True: 117k, False: 14]
Branch (7390:9): [True: 8, False: 3]
Branch (7390:9): [True: 8, False: 3]
Branch (7390:9): [True: 5.91k, False: 20.2k]
Branch (7390:9): [True: 145, False: 79]
Branch (7390:9): [True: 5.41k, False: 873]
Branch (7390:9): [True: 1.71k, False: 25]
Branch (7390:9): [True: 163k, False: 179]
Branch (7390:9): [True: 14, False: 5]
|
7391 | PyObject *r; \ |
7392 | if (do_other && PyType_IsSubtype(19.0k Py_TYPE19.0k (other), Py_TYPE19.0k (self))) { \ Branch (7392:13): [True: 16.0k, False: 139k]
Branch (7392:25): [True: 0, False: 16.0k]
Branch (7392:13): [True: 2.89k, False: 20.1k]
Branch (7392:25): [True: 0, False: 2.89k]
Branch (7392:13): [True: 12, False: 793]
Branch (7392:25): [True: 0, False: 12]
Branch (7392:13): [True: 3, False: 38]
Branch (7392:25): [True: 1, False: 2]
Branch (7392:13): [True: 2, False: 2.59k]
Branch (7392:25): [True: 0, False: 2]
Branch (7392:13): [True: 3, False: 117k]
Branch (7392:25): [True: 0, False: 3]
Branch (7392:13): [True: 0, False: 8]
Branch (7392:25): [True: 0, False: 0]
Branch (7392:13): [True: 0, False: 8]
Branch (7392:25): [True: 0, False: 0]
Branch (7392:13): [True: 1, False: 5.91k]
Branch (7392:25): [True: 0, False: 1]
Branch (7392:13): [True: 1, False: 144]
Branch (7392:25): [True: 0, False: 1]
Branch (7392:13): [True: 13, False: 5.39k]
Branch (7392:25): [True: 7, False: 6]
Branch (7392:13): [True: 10, False: 1.70k]
Branch (7392:25): [True: 2, False: 8]
Branch (7392:13): [True: 28, False: 163k]
Branch (7392:25): [True: 0, False: 28]
Branch (7392:13): [True: 0, False: 14]
Branch (7392:25): [True: 0, False: 0]
|
7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ |
7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 1]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 7]
Branch (7394:17): [True: 0, False: 2]
Branch (7394:17): [True: 0, False: 0]
Branch (7394:17): [True: 0, False: 0]
|
7395 | return NULL; \ |
7396 | } \ |
7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 1, False: 0]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 1, False: 6]
Branch (7397:17): [True: 1, False: 1]
Branch (7397:17): [True: 0, False: 0]
Branch (7397:17): [True: 0, False: 0]
|
7398 | stack[0] = other; \ |
7399 | stack[1] = self; \ |
7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ |
7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 1, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 1, False: 0]
Branch (7401:21): [True: 1, False: 0]
Branch (7401:21): [True: 0, False: 0]
Branch (7401:21): [True: 0, False: 0]
|
7402 | return r; \ |
7403 | Py_DECREF0 (r); \ |
7404 | do_other = 0; \ |
7405 | } \ |
7406 | } \ |
7407 | stack[0] = self; \ |
7408 | stack[1] = other; \ |
7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ |
7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 155k, False: 42]
Branch (7410:13): [True: 23.0k, False: 23]
Branch (7410:13): [True: 794, False: 11]
Branch (7410:13): [True: 32, False: 8]
Branch (7410:13): [True: 2.58k, False: 5]
Branch (7410:13): [True: 117k, False: 7]
Branch (7410:13): [True: 2, False: 6]
Branch (7410:13): [True: 2, False: 6]
Branch (7410:13): [True: 5.91k, False: 7]
Branch (7410:13): [True: 138, False: 7]
Branch (7410:13): [True: 5.39k, False: 16]
Branch (7410:13): [True: 1.70k, False: 11]
Branch (7410:13): [True: 163k, False: 10]
Branch (7410:13): [True: 8, False: 6]
|
7411 | Py_IS_TYPE165 (other, Py_TYPE(self))) \ |
7412 | return r476k ; \ |
7413 | Py_DECREF131 (r); \ |
7414 | } \ |
7415 | if (24.1k do_other24.1k ) { \ Branch (7415:9): [True: 712, False: 19]
Branch (7415:9): [True: 45, False: 14]
Branch (7415:9): [True: 1.78k, False: 8]
Branch (7415:9): [True: 9, False: 5]
Branch (7415:9): [True: 8, False: 3]
Branch (7415:9): [True: 16, False: 4]
Branch (7415:9): [True: 3, False: 4]
Branch (7415:9): [True: 4, False: 4]
Branch (7415:9): [True: 20.2k, False: 4]
Branch (7415:9): [True: 80, False: 4]
Branch (7415:9): [True: 874, False: 13]
Branch (7415:9): [True: 26, False: 8]
Branch (7415:9): [True: 181, False: 6]
Branch (7415:9): [True: 5, False: 4]
|
7416 | stack[0] = other; \ |
7417 | stack[1] = self; \ |
7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ |
7419 | } \ |
7420 | Py_RETURN_NOTIMPLEMENTED100 ; \ |
7421 | } Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 16.8k, False: 139k]
| 7387 | Py_TYPE16.8k (other)->tp_as_number != NULL16.8k && \ Branch (7387:9): [True: 16.8k, False: 6]
| 7388 | Py_TYPE16.8k (other)->tp_as_number->SLOTNAME == TESTFUNC16.8k ; \ Branch (7388:9): [True: 16.7k, False: 123]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 156k, False: 2]
| 7390 | Py_TYPE156k (self)->tp_as_number->SLOTNAME == TESTFUNC156k ) { \ Branch (7390:9): [True: 155k, False: 696]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(16.0k Py_TYPE16.0k (other), Py_TYPE16.0k (self))) { \ Branch (7392:13): [True: 16.0k, False: 139k]
Branch (7392:25): [True: 0, False: 16.0k]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 155k, False: 42]
| 7411 | Py_IS_TYPE42 (other, Py_TYPE(self))) \ | 7412 | return r155k ; \ | 7413 | Py_DECREF33 (r); \ | 7414 | } \ | 7415 | if (731 do_other731 ) { \ Branch (7415:9): [True: 712, False: 19]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED19 0 ; \ | 7421 | } |
typeobject.c:slot_nb_subtract Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 3.08k, False: 19.9k]
| 7387 | Py_TYPE3.08k (other)->tp_as_number != NULL3.08k && \ Branch (7387:9): [True: 3.08k, False: 2]
| 7388 | Py_TYPE3.08k (other)->tp_as_number->SLOTNAME == TESTFUNC3.08k ; \ Branch (7388:9): [True: 2.93k, False: 149]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 23.0k, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 23.0k, False: 38]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(2.89k Py_TYPE2.89k (other), Py_TYPE2.89k (self))) { \ Branch (7392:13): [True: 2.89k, False: 20.1k]
Branch (7392:25): [True: 0, False: 2.89k]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 23.0k, False: 23]
| 7411 | Py_IS_TYPE23 (other, Py_TYPE(self))) \ | 7412 | return r23.0k ; \ | 7413 | Py_DECREF21 (r); \ | 7414 | } \ | 7415 | if (59 do_other59 ) { \ Branch (7415:9): [True: 45, False: 14]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED14 0 ; \ | 7421 | } |
typeobject.c:slot_nb_multiply Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 2.21k, False: 375]
| 7387 | Py_TYPE2.21k (other)->tp_as_number != NULL2.21k && \ Branch (7387:9): [True: 2.21k, False: 0]
| 7388 | Py_TYPE2.21k (other)->tp_as_number->SLOTNAME == TESTFUNC2.21k ; \ Branch (7388:9): [True: 1.79k, False: 418]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 2.58k, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 805, False: 1.78k]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(12 Py_TYPE12 (other), Py_TYPE12 (self))) { \ Branch (7392:13): [True: 12, False: 793]
Branch (7392:25): [True: 0, False: 12]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 794, False: 11]
| 7411 | Py_IS_TYPE11 (other, Py_TYPE(self))) \ | 7412 | return r796 ; \ | 7413 | Py_DECREF9 (r); \ | 7414 | } \ | 7415 | if (1.79k do_other1.79k ) { \ Branch (7415:9): [True: 1.78k, False: 8]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED8 0 ; \ | 7421 | } |
typeobject.c:slot_nb_remainder Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 29, False: 20]
| 7387 | Py_TYPE29 (other)->tp_as_number != NULL29 && \ Branch (7387:9): [True: 29, False: 0]
| 7388 | Py_TYPE29 (other)->tp_as_number->SLOTNAME == TESTFUNC29 ; \ Branch (7388:9): [True: 11, False: 18]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 49, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 41, False: 8]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(3 Py_TYPE3 (other), Py_TYPE3 (self))) { \ Branch (7392:13): [True: 3, False: 38]
Branch (7392:25): [True: 1, False: 2]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 1]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 1, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 1, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF0 (r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 32, False: 8]
| 7411 | Py_IS_TYPE8 (other, Py_TYPE(self))) \ | 7412 | return r34 ; \ | 7413 | Py_DECREF6 (r); \ | 7414 | } \ | 7415 | if (14 do_other14 ) { \ Branch (7415:9): [True: 9, False: 5]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED5 0 ; \ | 7421 | } |
typeobject.c:slot_nb_divmod Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 48, False: 2.55k]
| 7387 | Py_TYPE48 (other)->tp_as_number != NULL48 && \ Branch (7387:9): [True: 48, False: 0]
| 7388 | Py_TYPE48 (other)->tp_as_number->SLOTNAME == TESTFUNC48 ; \ Branch (7388:9): [True: 9, False: 39]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 2.59k, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 2.59k, False: 7]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(2 Py_TYPE2 (other), Py_TYPE2 (self))) { \ Branch (7392:13): [True: 2, False: 2.59k]
Branch (7392:25): [True: 0, False: 2]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 2.58k, False: 5]
| 7411 | Py_IS_TYPE5 (other, Py_TYPE(self))) \ | 7412 | return r2.58k ; \ | 7413 | Py_DECREF4 (r); \ | 7414 | } \ | 7415 | if (11 do_other11 ) { \ Branch (7415:9): [True: 8, False: 3]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED3 0 ; \ | 7421 | } |
typeobject.c:slot_nb_power_binary Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 117k, False: 22]
| 7387 | Py_TYPE117k (other)->tp_as_number != NULL117k && \ Branch (7387:9): [True: 117k, False: 0]
| 7388 | Py_TYPE117k (other)->tp_as_number->SLOTNAME == TESTFUNC117k ; \ Branch (7388:9): [True: 18, False: 117k]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 117k, False: 1]
| 7390 | Py_TYPE117k (self)->tp_as_number->SLOTNAME == TESTFUNC117k ) { \ Branch (7390:9): [True: 117k, False: 14]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(3 Py_TYPE3 (other), Py_TYPE3 (self))) { \ Branch (7392:13): [True: 3, False: 117k]
Branch (7392:25): [True: 0, False: 3]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 117k, False: 7]
| 7411 | Py_IS_TYPE7 (other, Py_TYPE(self))) \ | 7412 | return r117k ; \ | 7413 | Py_DECREF5 (r); \ | 7414 | } \ | 7415 | if (20 do_other20 ) { \ Branch (7415:9): [True: 16, False: 4]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED4 0 ; \ | 7421 | } |
typeobject.c:slot_nb_lshift Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 9, False: 2]
| 7387 | Py_TYPE9 (other)->tp_as_number != NULL9 && \ Branch (7387:9): [True: 9, False: 0]
| 7388 | Py_TYPE9 (other)->tp_as_number->SLOTNAME == TESTFUNC9 ; \ Branch (7388:9): [True: 3, False: 6]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 11, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 8, False: 3]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(0 Py_TYPE0 (other), Py_TYPE0 (self))) { \ Branch (7392:13): [True: 0, False: 8]
Branch (7392:25): [True: 0, False: 0]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 2, False: 6]
| 7411 | Py_IS_TYPE6 (other, Py_TYPE(self))) \ | 7412 | return r4 ; \ | 7413 | Py_DECREF4 (r); \ | 7414 | } \ | 7415 | if (7 do_other7 ) { \ Branch (7415:9): [True: 3, False: 4]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED4 0 ; \ | 7421 | } |
typeobject.c:slot_nb_rshift Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 10, False: 2]
| 7387 | Py_TYPE10 (other)->tp_as_number != NULL10 && \ Branch (7387:9): [True: 10, False: 0]
| 7388 | Py_TYPE10 (other)->tp_as_number->SLOTNAME == TESTFUNC10 ; \ Branch (7388:9): [True: 4, False: 6]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 11, False: 1]
| 7390 | Py_TYPE11 (self)->tp_as_number->SLOTNAME == TESTFUNC11 ) { \ Branch (7390:9): [True: 8, False: 3]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(0 Py_TYPE0 (other), Py_TYPE0 (self))) { \ Branch (7392:13): [True: 0, False: 8]
Branch (7392:25): [True: 0, False: 0]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 2, False: 6]
| 7411 | Py_IS_TYPE6 (other, Py_TYPE(self))) \ | 7412 | return r4 ; \ | 7413 | Py_DECREF4 (r); \ | 7414 | } \ | 7415 | if (8 do_other8 ) { \ Branch (7415:9): [True: 4, False: 4]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED4 0 ; \ | 7421 | } |
Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 21.3k, False: 4.89k]
| 7387 | Py_TYPE21.3k (other)->tp_as_number != NULL21.3k && \ Branch (7387:9): [True: 21.3k, False: 1]
| 7388 | Py_TYPE21.3k (other)->tp_as_number->SLOTNAME == TESTFUNC21.3k ; \ Branch (7388:9): [True: 20.2k, False: 1.02k]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 26.2k, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 5.91k, False: 20.2k]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(1 Py_TYPE1 (other), Py_TYPE1 (self))) { \ Branch (7392:13): [True: 1, False: 5.91k]
Branch (7392:25): [True: 0, False: 1]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 5.91k, False: 7]
| 7411 | Py_IS_TYPE7 (other, Py_TYPE(self))) \ | 7412 | return r5.91k ; \ | 7413 | Py_DECREF5 (r); \ | 7414 | } \ | 7415 | if (20.2k do_other20.2k ) { \ Branch (7415:9): [True: 20.2k, False: 4]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED4 0 ; \ | 7421 | } |
Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 166, False: 58]
| 7387 | Py_TYPE166 (other)->tp_as_number != NULL166 && \ Branch (7387:9): [True: 165, False: 1]
| 7388 | Py_TYPE165 (other)->tp_as_number->SLOTNAME == TESTFUNC165 ; \ Branch (7388:9): [True: 80, False: 85]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 224, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 145, False: 79]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(1 Py_TYPE1 (other), Py_TYPE1 (self))) { \ Branch (7392:13): [True: 1, False: 144]
Branch (7392:25): [True: 0, False: 1]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 138, False: 7]
| 7411 | Py_IS_TYPE7 (other, Py_TYPE(self))) \ | 7412 | return r140 ; \ | 7413 | Py_DECREF5 (r); \ | 7414 | } \ | 7415 | if (84 do_other84 ) { \ Branch (7415:9): [True: 80, False: 4]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED4 0 ; \ | 7421 | } |
Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 1.24k, False: 5.03k]
| 7387 | Py_TYPE1.24k (other)->tp_as_number != NULL1.24k && \ Branch (7387:9): [True: 1.24k, False: 4]
| 7388 | Py_TYPE1.24k (other)->tp_as_number->SLOTNAME == TESTFUNC1.24k ; \ Branch (7388:9): [True: 886, False: 359]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 6.28k, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 5.41k, False: 873]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(13 Py_TYPE13 (other), Py_TYPE13 (self))) { \ Branch (7392:13): [True: 13, False: 5.39k]
Branch (7392:25): [True: 7, False: 6]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 7]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 1, False: 6]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 1, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF0 (r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 5.39k, False: 16]
| 7411 | Py_IS_TYPE16 (other, Py_TYPE(self))) \ | 7412 | return r5.39k ; \ | 7413 | Py_DECREF14 (r); \ | 7414 | } \ | 7415 | if (887 do_other887 ) { \ Branch (7415:9): [True: 874, False: 13]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED13 0 ; \ | 7421 | } |
typeobject.c:slot_nb_floor_divide Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 66, False: 1.67k]
| 7387 | Py_TYPE66 (other)->tp_as_number != NULL66 && \ Branch (7387:9): [True: 66, False: 0]
| 7388 | Py_TYPE66 (other)->tp_as_number->SLOTNAME == TESTFUNC66 ; \ Branch (7388:9): [True: 35, False: 31]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 1.74k, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 1.71k, False: 25]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(10 Py_TYPE10 (other), Py_TYPE10 (self))) { \ Branch (7392:13): [True: 10, False: 1.70k]
Branch (7392:25): [True: 2, False: 8]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 2]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 1, False: 1]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 1, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF0 (r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 1.70k, False: 11]
| 7411 | Py_IS_TYPE11 (other, Py_TYPE(self))) \ | 7412 | return r1.70k ; \ | 7413 | Py_DECREF9 (r); \ | 7414 | } \ | 7415 | if (34 do_other34 ) { \ Branch (7415:9): [True: 26, False: 8]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED8 0 ; \ | 7421 | } |
typeobject.c:slot_nb_true_divide Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 141k, False: 21.8k]
| 7387 | Py_TYPE141k (other)->tp_as_number != NULL141k && \ Branch (7387:9): [True: 141k, False: 0]
| 7388 | Py_TYPE141k (other)->tp_as_number->SLOTNAME == TESTFUNC141k ; \ Branch (7388:9): [True: 207, False: 141k]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 163k, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 163k, False: 179]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(28 Py_TYPE28 (other), Py_TYPE28 (self))) { \ Branch (7392:13): [True: 28, False: 163k]
Branch (7392:25): [True: 0, False: 28]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 163k, False: 10]
| 7411 | Py_IS_TYPE10 (other, Py_TYPE(self))) \ | 7412 | return r163k ; \ | 7413 | Py_DECREF8 (r); \ | 7414 | } \ | 7415 | if (187 do_other187 ) { \ Branch (7415:9): [True: 181, False: 6]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED6 0 ; \ | 7421 | } |
typeobject.c:slot_nb_matrix_multiply Line | Count | Source | 7382 | FUNCNAME(PyObject *self, PyObject *other) \ | 7383 | { \ | 7384 | PyObject* stack[2]; \ | 7385 | PyThreadState *tstate = _PyThreadState_GET(); \ | 7386 | int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Branch (7386:20): [True: 16, False: 3]
| 7387 | Py_TYPE16 (other)->tp_as_number != NULL16 && \ Branch (7387:9): [True: 16, False: 0]
| 7388 | Py_TYPE16 (other)->tp_as_number->SLOTNAME == TESTFUNC16 ; \ Branch (7388:9): [True: 5, False: 11]
| 7389 | if (Py_TYPE(self)->tp_as_number != NULL && \ Branch (7389:9): [True: 19, False: 0]
| 7390 | Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ Branch (7390:9): [True: 14, False: 5]
| 7391 | PyObject *r; \ | 7392 | if (do_other && PyType_IsSubtype(0 Py_TYPE0 (other), Py_TYPE0 (self))) { \ Branch (7392:13): [True: 0, False: 14]
Branch (7392:25): [True: 0, False: 0]
| 7393 | int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ | 7394 | if (ok < 0) { \ Branch (7394:17): [True: 0, False: 0]
| 7395 | return NULL; \ | 7396 | } \ | 7397 | if (ok) { \ Branch (7397:17): [True: 0, False: 0]
| 7398 | stack[0] = other; \ | 7399 | stack[1] = self; \ | 7400 | r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7401 | if (r != Py_NotImplemented) \ Branch (7401:21): [True: 0, False: 0]
| 7402 | return r; \ | 7403 | Py_DECREF(r); \ | 7404 | do_other = 0; \ | 7405 | } \ | 7406 | } \ | 7407 | stack[0] = self; \ | 7408 | stack[1] = other; \ | 7409 | r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ | 7410 | if (r != Py_NotImplemented || \ Branch (7410:13): [True: 8, False: 6]
| 7411 | Py_IS_TYPE6 (other, Py_TYPE(self))) \ | 7412 | return r10 ; \ | 7413 | Py_DECREF4 (r); \ | 7414 | } \ | 7415 | if (9 do_other9 ) { \ Branch (7415:9): [True: 5, False: 4]
| 7416 | stack[0] = other; \ | 7417 | stack[1] = self; \ | 7418 | return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ | 7419 | } \ | 7420 | Py_RETURN_NOTIMPLEMENTED4 0 ; \ | 7421 | } |
|
7422 | |
7423 | #define SLOT1BIN(FUNCNAME, SLOTNAME, DUNDER, RDUNDER) \ |
7424 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, DUNDER, RDUNDER) |
7425 | |
7426 | static Py_ssize_t |
7427 | slot_sq_length(PyObject *self) |
7428 | { |
7429 | PyObject* stack[1] = {self}; |
7430 | PyObject *res = vectorcall_method(&_Py_ID(__len__), stack, 1); |
7431 | Py_ssize_t len; |
7432 | |
7433 | if (res == NULL) Branch (7433:9): [True: 11, False: 967k]
|
7434 | return -1; |
7435 | |
7436 | Py_SETREF(res, _PyNumber_Index(res)); |
7437 | if (res == NULL) Branch (7437:9): [True: 4, False: 967k]
|
7438 | return -1; |
7439 | |
7440 | assert(PyLong_Check(res)); |
7441 | if (Py_SIZE(res) < 0) { Branch (7441:9): [True: 5, False: 967k]
|
7442 | Py_DECREF(res); |
7443 | PyErr_SetString(PyExc_ValueError, |
7444 | "__len__() should return >= 0"); |
7445 | return -1; |
7446 | } |
7447 | |
7448 | len = PyNumber_AsSsize_t(res, PyExc_OverflowError); |
7449 | assert(len >= 0 || PyErr_ExceptionMatches(PyExc_OverflowError)); |
7450 | Py_DECREF(res); |
7451 | return len; |
7452 | } |
7453 | |
7454 | static PyObject * |
7455 | slot_sq_item(PyObject *self, Py_ssize_t i) |
7456 | { |
7457 | PyObject *ival = PyLong_FromSsize_t(i); |
7458 | if (ival == NULL) { Branch (7458:9): [True: 0, False: 246k]
|
7459 | return NULL; |
7460 | } |
7461 | PyObject *stack[2] = {self, ival}; |
7462 | PyObject *retval = vectorcall_method(&_Py_ID(__getitem__), stack, 2); |
7463 | Py_DECREF(ival); |
7464 | return retval; |
7465 | } |
7466 | |
7467 | static int |
7468 | slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
7469 | { |
7470 | PyObject *stack[3]; |
7471 | PyObject *res; |
7472 | PyObject *index_obj; |
7473 | |
7474 | index_obj = PyLong_FromSsize_t(index); |
7475 | if (index_obj == NULL) { Branch (7475:9): [True: 0, False: 863]
|
7476 | return -1; |
7477 | } |
7478 | |
7479 | stack[0] = self; |
7480 | stack[1] = index_obj; |
7481 | if (value == NULL) { Branch (7481:9): [True: 0, False: 863]
|
7482 | res = vectorcall_method(&_Py_ID(__delitem__), stack, 2); |
7483 | } |
7484 | else { |
7485 | stack[2] = value; |
7486 | res = vectorcall_method(&_Py_ID(__setitem__), stack, 3); |
7487 | } |
7488 | Py_DECREF(index_obj); |
7489 | |
7490 | if (res == NULL) { Branch (7490:9): [True: 15, False: 848]
|
7491 | return -1; |
7492 | } |
7493 | Py_DECREF(res); |
7494 | return 0; |
7495 | } |
7496 | |
7497 | static int |
7498 | slot_sq_contains(PyObject *self, PyObject *value) |
7499 | { |
7500 | PyThreadState *tstate = _PyThreadState_GET(); |
7501 | PyObject *func, *res; |
7502 | int result = -1, unbound; |
7503 | |
7504 | func = lookup_maybe_method(self, &_Py_ID(__contains__), &unbound); |
7505 | if (func == Py_None) { Branch (7505:9): [True: 1, False: 385k]
|
7506 | Py_DECREF(func); |
7507 | PyErr_Format(PyExc_TypeError, |
7508 | "'%.200s' object is not a container", |
7509 | Py_TYPE(self)->tp_name); |
7510 | return -1; |
7511 | } |
7512 | if (func != NULL) { Branch (7512:9): [True: 385k, False: 0]
|
7513 | PyObject *args[2] = {self, value}; |
7514 | res = vectorcall_unbound(tstate, unbound, func, args, 2); |
7515 | Py_DECREF(func); |
7516 | if (res != NULL) { Branch (7516:13): [True: 385k, False: 3]
|
7517 | result = PyObject_IsTrue(res); |
7518 | Py_DECREF(res); |
7519 | } |
7520 | } |
7521 | else if (! PyErr_Occurred()) { Branch (7521:14): [True: 0, False: 0]
|
7522 | /* Possible results: -1 and 1 */ |
7523 | result = (int)_PySequence_IterSearch(self, value, |
7524 | PY_ITERSEARCH_CONTAINS); |
7525 | } |
7526 | return result; |
7527 | } |
7528 | |
7529 | #define slot_mp_length slot_sq_length |
7530 | |
7531 | SLOT1(slot_mp_subscript, __getitem__, PyObject *) |
7532 | |
7533 | static int |
7534 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
7535 | { |
7536 | PyObject *stack[3]; |
7537 | PyObject *res; |
7538 | |
7539 | stack[0] = self; |
7540 | stack[1] = key; |
7541 | if (value == NULL) { Branch (7541:9): [True: 49.0k, False: 936k]
|
7542 | res = vectorcall_method(&_Py_ID(__delitem__), stack, 2); |
7543 | } |
7544 | else { |
7545 | stack[2] = value; |
7546 | res = vectorcall_method(&_Py_ID(__setitem__), stack, 3); |
7547 | } |
7548 | |
7549 | if (res == NULL) Branch (7549:9): [True: 251, False: 985k]
|
7550 | return -1; |
7551 | Py_DECREF(res); |
7552 | return 0; |
7553 | } |
7554 | |
7555 | SLOT1BIN(slot_nb_add, nb_add, __add__, __radd__) |
7556 | SLOT1BIN(slot_nb_subtract, nb_subtract, __sub__, __rsub__) |
7557 | SLOT1BIN(slot_nb_multiply, nb_multiply, __mul__, __rmul__) |
7558 | SLOT1BIN(slot_nb_matrix_multiply, nb_matrix_multiply, __matmul__, __rmatmul__) |
7559 | SLOT1BIN(slot_nb_remainder, nb_remainder, __mod__, __rmod__) |
7560 | SLOT1BIN(slot_nb_divmod, nb_divmod, __divmod__, __rdivmod__) |
7561 | |
7562 | static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
7563 | |
7564 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, nb_power, __pow__, __rpow__) |
7565 | |
7566 | static PyObject * |
7567 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
7568 | { |
7569 | if (modulus == Py_None) Branch (7569:9): [True: 117k, False: 3]
|
7570 | return slot_nb_power_binary(self, other); |
7571 | /* Three-arg power doesn't use __rpow__. But ternary_op |
7572 | can call this when the second argument's type uses |
7573 | slot_nb_power, so check before calling self.__pow__. */ |
7574 | if (Py_TYPE(self)->tp_as_number != NULL && Branch (7574:9): [True: 3, False: 0]
|
7575 | Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { Branch (7575:9): [True: 2, False: 1]
|
7576 | PyObject* stack[3] = {self, other, modulus}; |
7577 | return vectorcall_method(&_Py_ID(__pow__), stack, 3); |
7578 | } |
7579 | Py_RETURN_NOTIMPLEMENTED1 ; |
7580 | } |
7581 | |
7582 | SLOT0(slot_nb_negative, __neg__) |
7583 | SLOT0(slot_nb_positive, __pos__) |
7584 | SLOT0(slot_nb_absolute, __abs__) |
7585 | |
7586 | static int |
7587 | slot_nb_bool(PyObject *self) |
7588 | { |
7589 | PyObject *func, *value; |
7590 | int result, unbound; |
7591 | int using_len = 0; |
7592 | |
7593 | func = lookup_maybe_method(self, &_Py_ID(__bool__), &unbound); |
7594 | if (func == NULL) { Branch (7594:9): [True: 0, False: 165k]
|
7595 | if (PyErr_Occurred()) { Branch (7595:13): [True: 0, False: 0]
|
7596 | return -1; |
7597 | } |
7598 | |
7599 | func = lookup_maybe_method(self, &_Py_ID(__len__), &unbound); |
7600 | if (func == NULL) { Branch (7600:13): [True: 0, False: 0]
|
7601 | if (PyErr_Occurred()) { Branch (7601:17): [True: 0, False: 0]
|
7602 | return -1; |
7603 | } |
7604 | return 1; |
7605 | } |
7606 | using_len = 1; |
7607 | } |
7608 | |
7609 | value = call_unbound_noarg(unbound, func, self); |
7610 | if (value == NULL) { Branch (7610:9): [True: 85, False: 165k]
|
7611 | goto error; |
7612 | } |
7613 | |
7614 | if (using_len) { Branch (7614:9): [True: 0, False: 165k]
|
7615 | /* bool type enforced by slot_nb_len */ |
7616 | result = PyObject_IsTrue(value); |
7617 | } |
7618 | else if (PyBool_Check(value)) { |
7619 | result = PyObject_IsTrue(value); |
7620 | } |
7621 | else { |
7622 | PyErr_Format(PyExc_TypeError, |
7623 | "__bool__ should return " |
7624 | "bool, returned %s", |
7625 | Py_TYPE(value)->tp_name); |
7626 | result = -1; |
7627 | } |
7628 | |
7629 | Py_DECREF(value); |
7630 | Py_DECREF(func); |
7631 | return result; |
7632 | |
7633 | error: |
7634 | Py_DECREF(func); |
7635 | return -1; |
7636 | } |
7637 | |
7638 | |
7639 | static PyObject * |
7640 | slot_nb_index(PyObject *self) |
7641 | { |
7642 | PyObject *stack[1] = {self}; |
7643 | return vectorcall_method(&_Py_ID(__index__), stack, 1); |
7644 | } |
7645 | |
7646 | |
7647 | SLOT0(slot_nb_invert, __invert__) |
7648 | SLOT1BIN(slot_nb_lshift, nb_lshift, __lshift__, __rlshift__) |
7649 | SLOT1BIN(slot_nb_rshift, nb_rshift, __rshift__, __rrshift__) |
7650 | SLOT1BIN(slot_nb_and, nb_and, __and__, __rand__) |
7651 | SLOT1BIN(slot_nb_xor, nb_xor, __xor__, __rxor__) |
7652 | SLOT1BIN(slot_nb_or, nb_or, __or__, __ror__) |
7653 | |
7654 | SLOT0(slot_nb_int, __int__) |
7655 | SLOT0(slot_nb_float, __float__) |
7656 | SLOT1(slot_nb_inplace_add, __iadd__, PyObject *) |
7657 | SLOT1(slot_nb_inplace_subtract, __isub__, PyObject *) |
7658 | SLOT1(slot_nb_inplace_multiply, __imul__, PyObject *) |
7659 | SLOT1(slot_nb_inplace_matrix_multiply, __imatmul__, PyObject *) |
7660 | SLOT1(slot_nb_inplace_remainder, __imod__, PyObject *) |
7661 | /* Can't use SLOT1 here, because nb_inplace_power is ternary */ |
7662 | static PyObject * |
7663 | slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) |
7664 | { |
7665 | PyObject *stack[2] = {self, arg1}; |
7666 | return vectorcall_method(&_Py_ID(__ipow__), stack, 2); |
7667 | } |
7668 | SLOT1(slot_nb_inplace_lshift, __ilshift__, PyObject *) |
7669 | SLOT1(slot_nb_inplace_rshift, __irshift__, PyObject *) |
7670 | SLOT1(slot_nb_inplace_and, __iand__, PyObject *) |
7671 | SLOT1(slot_nb_inplace_xor, __ixor__, PyObject *) |
7672 | SLOT1(slot_nb_inplace_or, __ior__, PyObject *) |
7673 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
7674 | __floordiv__, __rfloordiv__) |
7675 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, __truediv__, __rtruediv__) |
7676 | SLOT1(slot_nb_inplace_floor_divide, __ifloordiv__, PyObject *) |
7677 | SLOT1(slot_nb_inplace_true_divide, __itruediv__, PyObject *) |
7678 | |
7679 | static PyObject * |
7680 | slot_tp_repr(PyObject *self) |
7681 | { |
7682 | PyObject *func, *res; |
7683 | int unbound; |
7684 | |
7685 | func = lookup_maybe_method(self, &_Py_ID(__repr__), &unbound); |
7686 | if (func != NULL) { Branch (7686:9): [True: 23.7k, False: 0]
|
7687 | res = call_unbound_noarg(unbound, func, self); |
7688 | Py_DECREF(func); |
7689 | return res; |
7690 | } |
7691 | PyErr_Clear(); |
7692 | return PyUnicode_FromFormat("<%s object at %p>", |
7693 | Py_TYPE(self)->tp_name, self); |
7694 | } |
7695 | |
7696 | SLOT0(slot_tp_str, __str__) |
7697 | |
7698 | static Py_hash_t |
7699 | slot_tp_hash(PyObject *self) |
7700 | { |
7701 | PyObject *func, *res; |
7702 | Py_ssize_t h; |
7703 | int unbound; |
7704 | |
7705 | func = lookup_maybe_method(self, &_Py_ID(__hash__), &unbound); |
7706 | |
7707 | if (func == Py_None) { Branch (7707:9): [True: 0, False: 1.31M]
|
7708 | Py_DECREF(func); |
7709 | func = NULL; |
7710 | } |
7711 | |
7712 | if (func == NULL) { Branch (7712:9): [True: 0, False: 1.31M]
|
7713 | return PyObject_HashNotImplemented(self); |
7714 | } |
7715 | |
7716 | res = call_unbound_noarg(unbound, func, self); |
7717 | Py_DECREF(func); |
7718 | if (res == NULL) Branch (7718:9): [True: 452, False: 1.31M]
|
7719 | return -1; |
7720 | |
7721 | if (!PyLong_Check(res)) { Branch (7721:9): [True: 0, False: 1.31M]
|
7722 | PyErr_SetString(PyExc_TypeError, |
7723 | "__hash__ method should return an integer"); |
7724 | return -1; |
7725 | } |
7726 | /* Transform the PyLong `res` to a Py_hash_t `h`. For an existing |
7727 | hashable Python object x, hash(x) will always lie within the range of |
7728 | Py_hash_t. Therefore our transformation must preserve values that |
7729 | already lie within this range, to ensure that if x.__hash__() returns |
7730 | hash(y) then hash(x) == hash(y). */ |
7731 | h = PyLong_AsSsize_t(res); |
7732 | if (h == -1 && PyErr_Occurred()1 ) { Branch (7732:9): [True: 1, False: 1.31M]
Branch (7732:20): [True: 1, False: 0]
|
7733 | /* res was not within the range of a Py_hash_t, so we're free to |
7734 | use any sufficiently bit-mixing transformation; |
7735 | long.__hash__ will do nicely. */ |
7736 | PyErr_Clear(); |
7737 | h = PyLong_Type.tp_hash(res); |
7738 | } |
7739 | /* -1 is reserved for errors. */ |
7740 | if (h == -1) Branch (7740:9): [True: 0, False: 1.31M]
|
7741 | h = -2; |
7742 | Py_DECREF(res); |
7743 | return h; |
7744 | } |
7745 | |
7746 | static PyObject * |
7747 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
7748 | { |
7749 | PyThreadState *tstate = _PyThreadState_GET(); |
7750 | int unbound; |
7751 | |
7752 | PyObject *meth = lookup_method(self, &_Py_ID(__call__), &unbound); |
7753 | if (meth == NULL) { Branch (7753:9): [True: 0, False: 1.72M]
|
7754 | return NULL; |
7755 | } |
7756 | |
7757 | PyObject *res; |
7758 | if (unbound) { Branch (7758:9): [True: 1.72M, False: 1.96k]
|
7759 | res = _PyObject_Call_Prepend(tstate, meth, self, args, kwds); |
7760 | } |
7761 | else { |
7762 | res = _PyObject_Call(tstate, meth, args, kwds); |
7763 | } |
7764 | |
7765 | Py_DECREF(meth); |
7766 | return res; |
7767 | } |
7768 | |
7769 | /* There are two slot dispatch functions for tp_getattro. |
7770 | |
7771 | - slot_tp_getattro() is used when __getattribute__ is overridden |
7772 | but no __getattr__ hook is present; |
7773 | |
7774 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
7775 | |
7776 | The code in update_one_slot() always installs slot_tp_getattr_hook(); this |
7777 | detects the absence of __getattr__ and then installs the simpler slot if |
7778 | necessary. */ |
7779 | |
7780 | static PyObject * |
7781 | slot_tp_getattro(PyObject *self, PyObject *name) |
7782 | { |
7783 | PyObject *stack[2] = {self, name}; |
7784 | return vectorcall_method(&_Py_ID(__getattribute__), stack, 2); |
7785 | } |
7786 | |
7787 | static inline PyObject * |
7788 | call_attribute(PyObject *self, PyObject *attr, PyObject *name) |
7789 | { |
7790 | PyObject *res, *descr = NULL; |
7791 | |
7792 | if (_PyType_HasFeature(Py_TYPE(attr), Py_TPFLAGS_METHOD_DESCRIPTOR)) { Branch (7792:9): [True: 524k, False: 3]
|
7793 | PyObject *args[] = { self, name }; |
7794 | res = PyObject_Vectorcall(attr, args, 2, NULL); |
7795 | return res; |
7796 | } |
7797 | |
7798 | descrgetfunc f = Py_TYPE(attr)->tp_descr_get; |
7799 | |
7800 | if (f != NULL) { Branch (7800:9): [True: 3, False: 0]
|
7801 | descr = f(attr, self, (PyObject *)(Py_TYPE(self))); |
7802 | if (descr == NULL) Branch (7802:13): [True: 0, False: 3]
|
7803 | return NULL; |
7804 | else |
7805 | attr = descr; |
7806 | } |
7807 | res = PyObject_CallOneArg(attr, name); |
7808 | Py_XDECREF(descr); |
7809 | return res; |
7810 | } |
7811 | |
7812 | static PyObject * |
7813 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
7814 | { |
7815 | PyTypeObject *tp = Py_TYPE(self); |
7816 | PyObject *getattr, *getattribute, *res; |
7817 | |
7818 | /* speed hack: we could use lookup_maybe, but that would resolve the |
7819 | method fully for each attribute lookup for classes with |
7820 | __getattr__, even when the attribute is present. So we use |
7821 | _PyType_Lookup and create the method only when needed, with |
7822 | call_attribute. */ |
7823 | getattr = _PyType_Lookup(tp, &_Py_ID(__getattr__)); |
7824 | if (getattr == NULL) { Branch (7824:9): [True: 26, False: 7.33M]
|
7825 | /* No __getattr__ hook: use a simpler dispatcher */ |
7826 | tp->tp_getattro = slot_tp_getattro; |
7827 | return slot_tp_getattro(self, name); |
7828 | } |
7829 | Py_INCREF(getattr); |
7830 | /* speed hack: we could use lookup_maybe, but that would resolve the |
7831 | method fully for each attribute lookup for classes with |
7832 | __getattr__, even when self has the default __getattribute__ |
7833 | method. So we use _PyType_Lookup and create the method only when |
7834 | needed, with call_attribute. */ |
7835 | getattribute = _PyType_Lookup(tp, &_Py_ID(__getattribute__)); |
7836 | if (getattribute == NULL || Branch (7836:9): [True: 0, False: 7.33M]
|
7837 | (Py_IS_TYPE(getattribute, &PyWrapperDescr_Type) && |
7838 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == Branch (7838:10): [True: 6.89M, False: 430k]
|
7839 | (void *)PyObject_GenericGetAttr)) |
7840 | res = PyObject_GenericGetAttr(self, name); |
7841 | else { |
7842 | Py_INCREF(getattribute); |
7843 | res = call_attribute(self, getattribute, name); |
7844 | Py_DECREF(getattribute); |
7845 | } |
7846 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)82.7k ) { Branch (7846:9): [True: 82.7k, False: 7.25M]
Branch (7846:24): [True: 82.7k, False: 0]
|
7847 | PyErr_Clear(); |
7848 | res = call_attribute(self, getattr, name); |
7849 | } |
7850 | Py_DECREF(getattr); |
7851 | return res; |
7852 | } |
7853 | |
7854 | static int |
7855 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
7856 | { |
7857 | PyObject *stack[3]; |
7858 | PyObject *res; |
7859 | |
7860 | stack[0] = self; |
7861 | stack[1] = name; |
7862 | if (value == NULL) { Branch (7862:9): [True: 75.8k, False: 432k]
|
7863 | res = vectorcall_method(&_Py_ID(__delattr__), stack, 2); |
7864 | } |
7865 | else { |
7866 | stack[2] = value; |
7867 | res = vectorcall_method(&_Py_ID(__setattr__), stack, 3); |
7868 | } |
7869 | if (res == NULL) Branch (7869:9): [True: 329, False: 508k]
|
7870 | return -1; |
7871 | Py_DECREF(res); |
7872 | return 0; |
7873 | } |
7874 | |
7875 | static PyObject *name_op[] = { |
7876 | &_Py_ID(__lt__), |
7877 | &_Py_ID(__le__), |
7878 | &_Py_ID(__eq__), |
7879 | &_Py_ID(__ne__), |
7880 | &_Py_ID(__gt__), |
7881 | &_Py_ID(__ge__), |
7882 | }; |
7883 | |
7884 | static PyObject * |
7885 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
7886 | { |
7887 | PyThreadState *tstate = _PyThreadState_GET(); |
7888 | |
7889 | int unbound; |
7890 | PyObject *func = lookup_maybe_method(self, name_op[op], &unbound); |
7891 | if (func == NULL) { Branch (7891:9): [True: 2, False: 9.65M]
|
7892 | PyErr_Clear(); |
7893 | Py_RETURN_NOTIMPLEMENTED; |
7894 | } |
7895 | |
7896 | PyObject *stack[2] = {self, other}; |
7897 | PyObject *res = vectorcall_unbound(tstate, unbound, func, stack, 2); |
7898 | Py_DECREF(func); |
7899 | return res; |
7900 | } |
7901 | |
7902 | static PyObject * |
7903 | slot_tp_iter(PyObject *self) |
7904 | { |
7905 | int unbound; |
7906 | PyObject *func, *res; |
7907 | |
7908 | func = lookup_maybe_method(self, &_Py_ID(__iter__), &unbound); |
7909 | if (func == Py_None) { Branch (7909:9): [True: 28, False: 85.4k]
|
7910 | Py_DECREF(func); |
7911 | PyErr_Format(PyExc_TypeError, |
7912 | "'%.200s' object is not iterable", |
7913 | Py_TYPE(self)->tp_name); |
7914 | return NULL; |
7915 | } |
7916 | |
7917 | if (func != NULL) { Branch (7917:9): [True: 85.4k, False: 0]
|
7918 | res = call_unbound_noarg(unbound, func, self); |
7919 | Py_DECREF(func); |
7920 | return res; |
7921 | } |
7922 | |
7923 | PyErr_Clear(); |
7924 | func = lookup_maybe_method(self, &_Py_ID(__getitem__), &unbound); |
7925 | if (func == NULL) { Branch (7925:9): [True: 0, False: 0]
|
7926 | PyErr_Format(PyExc_TypeError, |
7927 | "'%.200s' object is not iterable", |
7928 | Py_TYPE(self)->tp_name); |
7929 | return NULL; |
7930 | } |
7931 | Py_DECREF(func); |
7932 | return PySeqIter_New(self); |
7933 | } |
7934 | |
7935 | static PyObject * |
7936 | slot_tp_iternext(PyObject *self) |
7937 | { |
7938 | PyObject *stack[1] = {self}; |
7939 | return vectorcall_method(&_Py_ID(__next__), stack, 1); |
7940 | } |
7941 | |
7942 | static PyObject * |
7943 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
7944 | { |
7945 | PyTypeObject *tp = Py_TYPE(self); |
7946 | PyObject *get; |
7947 | |
7948 | get = _PyType_Lookup(tp, &_Py_ID(__get__)); |
7949 | if (get == NULL) { Branch (7949:9): [True: 0, False: 51.6k]
|
7950 | /* Avoid further slowdowns */ |
7951 | if (tp->tp_descr_get == slot_tp_descr_get) Branch (7951:13): [True: 0, False: 0]
|
7952 | tp->tp_descr_get = NULL; |
7953 | Py_INCREF(self); |
7954 | return self; |
7955 | } |
7956 | if (obj == NULL) Branch (7956:9): [True: 34.3k, False: 17.2k]
|
7957 | obj = Py_None; |
7958 | if (type == NULL) Branch (7958:9): [True: 0, False: 51.6k]
|
7959 | type = Py_None; |
7960 | return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); |
7961 | } |
7962 | |
7963 | static int |
7964 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
7965 | { |
7966 | PyObject* stack[3]; |
7967 | PyObject *res; |
7968 | |
7969 | stack[0] = self; |
7970 | stack[1] = target; |
7971 | if (value == NULL) { Branch (7971:9): [True: 15, False: 16]
|
7972 | res = vectorcall_method(&_Py_ID(__delete__), stack, 2); |
7973 | } |
7974 | else { |
7975 | stack[2] = value; |
7976 | res = vectorcall_method(&_Py_ID(__set__), stack, 3); |
7977 | } |
7978 | if (res == NULL) Branch (7978:9): [True: 14, False: 17]
|
7979 | return -1; |
7980 | Py_DECREF(res); |
7981 | return 0; |
7982 | } |
7983 | |
7984 | static int |
7985 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
7986 | { |
7987 | PyThreadState *tstate = _PyThreadState_GET(); |
7988 | |
7989 | int unbound; |
7990 | PyObject *meth = lookup_method(self, &_Py_ID(__init__), &unbound); |
7991 | if (meth == NULL) { Branch (7991:9): [True: 1, False: 9.44M]
|
7992 | return -1; |
7993 | } |
7994 | |
7995 | PyObject *res; |
7996 | if (unbound) { Branch (7996:9): [True: 9.44M, False: 2]
|
7997 | res = _PyObject_Call_Prepend(tstate, meth, self, args, kwds); |
7998 | } |
7999 | else { |
8000 | res = _PyObject_Call(tstate, meth, args, kwds); |
8001 | } |
8002 | Py_DECREF(meth); |
8003 | if (res == NULL) Branch (8003:9): [True: 8.21k, False: 9.43M]
|
8004 | return -1; |
8005 | if (res != Py_None) { Branch (8005:9): [True: 1, False: 9.43M]
|
8006 | PyErr_Format(PyExc_TypeError, |
8007 | "__init__() should return None, not '%.200s'", |
8008 | Py_TYPE(res)->tp_name); |
8009 | Py_DECREF(res); |
8010 | return -1; |
8011 | } |
8012 | Py_DECREF(res); |
8013 | return 0; |
8014 | } |
8015 | |
8016 | static PyObject * |
8017 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
8018 | { |
8019 | PyThreadState *tstate = _PyThreadState_GET(); |
8020 | PyObject *func, *result; |
8021 | |
8022 | func = PyObject_GetAttr((PyObject *)type, &_Py_ID(__new__)); |
8023 | if (func == NULL) { Branch (8023:9): [True: 0, False: 3.05M]
|
8024 | return NULL; |
8025 | } |
8026 | |
8027 | result = _PyObject_Call_Prepend(tstate, func, (PyObject *)type, args, kwds); |
8028 | Py_DECREF(func); |
8029 | return result; |
8030 | } |
8031 | |
8032 | static void |
8033 | slot_tp_finalize(PyObject *self) |
8034 | { |
8035 | int unbound; |
8036 | PyObject *del, *res; |
8037 | PyObject *error_type, *error_value, *error_traceback; |
8038 | |
8039 | /* Save the current exception, if any. */ |
8040 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
8041 | |
8042 | /* Execute __del__ method, if any. */ |
8043 | del = lookup_maybe_method(self, &_Py_ID(__del__), &unbound); |
8044 | if (del != NULL) { Branch (8044:9): [True: 75.0k, False: 0]
|
8045 | res = call_unbound_noarg(unbound, del, self); |
8046 | if (res == NULL) Branch (8046:13): [True: 7, False: 75.0k]
|
8047 | PyErr_WriteUnraisable(del); |
8048 | else |
8049 | Py_DECREF(res); |
8050 | Py_DECREF(del); |
8051 | } |
8052 | |
8053 | /* Restore the saved exception. */ |
8054 | PyErr_Restore(error_type, error_value, error_traceback); |
8055 | } |
8056 | |
8057 | static PyObject * |
8058 | slot_am_await(PyObject *self) |
8059 | { |
8060 | int unbound; |
8061 | PyObject *func, *res; |
8062 | |
8063 | func = lookup_maybe_method(self, &_Py_ID(__await__), &unbound); |
8064 | if (func != NULL) { Branch (8064:9): [True: 221, False: 0]
|
8065 | res = call_unbound_noarg(unbound, func, self); |
8066 | Py_DECREF(func); |
8067 | return res; |
8068 | } |
8069 | PyErr_Format(PyExc_AttributeError, |
8070 | "object %.50s does not have __await__ method", |
8071 | Py_TYPE(self)->tp_name); |
8072 | return NULL; |
8073 | } |
8074 | |
8075 | static PyObject * |
8076 | slot_am_aiter(PyObject *self) |
8077 | { |
8078 | int unbound; |
8079 | PyObject *func, *res; |
8080 | |
8081 | func = lookup_maybe_method(self, &_Py_ID(__aiter__), &unbound); |
8082 | if (func != NULL) { Branch (8082:9): [True: 31, False: 0]
|
8083 | res = call_unbound_noarg(unbound, func, self); |
8084 | Py_DECREF(func); |
8085 | return res; |
8086 | } |
8087 | PyErr_Format(PyExc_AttributeError, |
8088 | "object %.50s does not have __aiter__ method", |
8089 | Py_TYPE(self)->tp_name); |
8090 | return NULL; |
8091 | } |
8092 | |
8093 | static PyObject * |
8094 | slot_am_anext(PyObject *self) |
8095 | { |
8096 | int unbound; |
8097 | PyObject *func, *res; |
8098 | |
8099 | func = lookup_maybe_method(self, &_Py_ID(__anext__), &unbound); |
8100 | if (func != NULL) { Branch (8100:9): [True: 365, False: 0]
|
8101 | res = call_unbound_noarg(unbound, func, self); |
8102 | Py_DECREF(func); |
8103 | return res; |
8104 | } |
8105 | PyErr_Format(PyExc_AttributeError, |
8106 | "object %.50s does not have __anext__ method", |
8107 | Py_TYPE(self)->tp_name); |
8108 | return NULL; |
8109 | } |
8110 | |
8111 | /* |
8112 | Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions. |
8113 | |
8114 | The table is ordered by offsets relative to the 'PyHeapTypeObject' structure, |
8115 | which incorporates the additional structures used for numbers, sequences and |
8116 | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
8117 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots |
8118 | (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with |
8119 | an all-zero entry. (This table is further initialized in |
8120 | _PyTypes_InitSlotDefs().) |
8121 | */ |
8122 | |
8123 | typedef struct wrapperbase slotdef; |
8124 | |
8125 | #undef TPSLOT |
8126 | #undef FLSLOT |
8127 | #undef AMSLOT |
8128 | #undef ETSLOT |
8129 | #undef SQSLOT |
8130 | #undef MPSLOT |
8131 | #undef NBSLOT |
8132 | #undef UNSLOT |
8133 | #undef IBSLOT |
8134 | #undef BINSLOT |
8135 | #undef RBINSLOT |
8136 | |
8137 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
8138 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
8139 | PyDoc_STR(DOC)} |
8140 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
8141 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
8142 | PyDoc_STR(DOC), FLAGS} |
8143 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
8144 | {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
8145 | PyDoc_STR(DOC)} |
8146 | #define AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
8147 | ETSLOT(NAME, as_async.SLOT, FUNCTION, WRAPPER, DOC) |
8148 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
8149 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
8150 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
8151 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
8152 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
8153 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
8154 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
8155 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
8156 | NAME "($self, /)\n--\n\n" DOC) |
8157 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
8158 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
8159 | NAME "($self, value, /)\n--\n\nReturn self" DOC "value.") |
8160 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
8161 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
8162 | NAME "($self, value, /)\n--\n\nReturn self" DOC "value.") |
8163 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
8164 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
8165 | NAME "($self, value, /)\n--\n\nReturn value" DOC "self.") |
8166 | #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
8167 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
8168 | NAME "($self, value, /)\n--\n\n" DOC) |
8169 | #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ |
8170 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
8171 | NAME "($self, value, /)\n--\n\n" DOC) |
8172 | |
8173 | static slotdef slotdefs[] = { |
8174 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
8175 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
8176 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
8177 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
8178 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
8179 | "__repr__($self, /)\n--\n\nReturn repr(self)."), |
8180 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
8181 | "__hash__($self, /)\n--\n\nReturn hash(self)."), |
8182 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)(void(*)(void))wrap_call, |
8183 | "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function.", |
8184 | PyWrapperFlag_KEYWORDS), |
8185 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
8186 | "__str__($self, /)\n--\n\nReturn str(self)."), |
8187 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
8188 | wrap_binaryfunc, |
8189 | "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)."), |
8190 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
8191 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
8192 | "__setattr__($self, name, value, /)\n--\n\nImplement setattr(self, name, value)."), |
8193 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
8194 | "__delattr__($self, name, /)\n--\n\nImplement delattr(self, name)."), |
8195 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
8196 | "__lt__($self, value, /)\n--\n\nReturn self<value."), |
8197 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
8198 | "__le__($self, value, /)\n--\n\nReturn self<=value."), |
8199 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
8200 | "__eq__($self, value, /)\n--\n\nReturn self==value."), |
8201 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
8202 | "__ne__($self, value, /)\n--\n\nReturn self!=value."), |
8203 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
8204 | "__gt__($self, value, /)\n--\n\nReturn self>value."), |
8205 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
8206 | "__ge__($self, value, /)\n--\n\nReturn self>=value."), |
8207 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
8208 | "__iter__($self, /)\n--\n\nImplement iter(self)."), |
8209 | TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
8210 | "__next__($self, /)\n--\n\nImplement next(self)."), |
8211 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
8212 | "__get__($self, instance, owner=None, /)\n--\n\nReturn an attribute of instance, which is of type owner."), |
8213 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
8214 | "__set__($self, instance, value, /)\n--\n\nSet an attribute of instance to value."), |
8215 | TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
8216 | wrap_descr_delete, |
8217 | "__delete__($self, instance, /)\n--\n\nDelete an attribute of instance."), |
8218 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)(void(*)(void))wrap_init, |
8219 | "__init__($self, /, *args, **kwargs)\n--\n\n" |
8220 | "Initialize self. See help(type(self)) for accurate signature.", |
8221 | PyWrapperFlag_KEYWORDS), |
8222 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, |
8223 | "__new__(type, /, *args, **kwargs)\n--\n\n" |
8224 | "Create and return new object. See help(type) for accurate signature."), |
8225 | TPSLOT("__del__", tp_finalize, slot_tp_finalize, (wrapperfunc)wrap_del, ""), |
8226 | |
8227 | AMSLOT("__await__", am_await, slot_am_await, wrap_unaryfunc, |
8228 | "__await__($self, /)\n--\n\nReturn an iterator to be used in await expression."), |
8229 | AMSLOT("__aiter__", am_aiter, slot_am_aiter, wrap_unaryfunc, |
8230 | "__aiter__($self, /)\n--\n\nReturn an awaitable, that resolves in asynchronous iterator."), |
8231 | AMSLOT("__anext__", am_anext, slot_am_anext, wrap_unaryfunc, |
8232 | "__anext__($self, /)\n--\n\nReturn a value or raise StopAsyncIteration."), |
8233 | |
8234 | BINSLOT("__add__", nb_add, slot_nb_add, |
8235 | "+"), |
8236 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
8237 | "+"), |
8238 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
8239 | "-"), |
8240 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
8241 | "-"), |
8242 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
8243 | "*"), |
8244 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
8245 | "*"), |
8246 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
8247 | "%"), |
8248 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
8249 | "%"), |
8250 | BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
8251 | "Return divmod(self, value)."), |
8252 | RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
8253 | "Return divmod(value, self)."), |
8254 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
8255 | "__pow__($self, value, mod=None, /)\n--\n\nReturn pow(self, value, mod)."), |
8256 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
8257 | "__rpow__($self, value, mod=None, /)\n--\n\nReturn pow(value, self, mod)."), |
8258 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-self"), |
8259 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+self"), |
8260 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
8261 | "abs(self)"), |
8262 | UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
8263 | "True if self else False"), |
8264 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~self"), |
8265 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
8266 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
8267 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
8268 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
8269 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
8270 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
8271 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
8272 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
8273 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
8274 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
8275 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
8276 | "int(self)"), |
8277 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
8278 | "float(self)"), |
8279 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
8280 | wrap_binaryfunc, "+="), |
8281 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
8282 | wrap_binaryfunc, "-="), |
8283 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
8284 | wrap_binaryfunc, "*="), |
8285 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
8286 | wrap_binaryfunc, "%="), |
8287 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
8288 | wrap_ternaryfunc, "**="), |
8289 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
8290 | wrap_binaryfunc, "<<="), |
8291 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
8292 | wrap_binaryfunc, ">>="), |
8293 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
8294 | wrap_binaryfunc, "&="), |
8295 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
8296 | wrap_binaryfunc, "^="), |
8297 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
8298 | wrap_binaryfunc, "|="), |
8299 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
8300 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
8301 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
8302 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
8303 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
8304 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="), |
8305 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
8306 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/="), |
8307 | NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
8308 | "__index__($self, /)\n--\n\n" |
8309 | "Return self converted to an integer, if self is suitable " |
8310 | "for use as an index into a list."), |
8311 | BINSLOT("__matmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
8312 | "@"), |
8313 | RBINSLOT("__rmatmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
8314 | "@"), |
8315 | IBSLOT("__imatmul__", nb_inplace_matrix_multiply, slot_nb_inplace_matrix_multiply, |
8316 | wrap_binaryfunc, "@="), |
8317 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
8318 | "__len__($self, /)\n--\n\nReturn len(self)."), |
8319 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
8320 | wrap_binaryfunc, |
8321 | "__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
8322 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
8323 | wrap_objobjargproc, |
8324 | "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
8325 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
8326 | wrap_delitem, |
8327 | "__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
8328 | |
8329 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
8330 | "__len__($self, /)\n--\n\nReturn len(self)."), |
8331 | /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
8332 | The logic in abstract.c always falls back to nb_add/nb_multiply in |
8333 | this case. Defining both the nb_* and the sq_* slots to call the |
8334 | user-defined methods has unexpected side-effects, as shown by |
8335 | test_descr.notimplemented() */ |
8336 | SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
8337 | "__add__($self, value, /)\n--\n\nReturn self+value."), |
8338 | SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, |
8339 | "__mul__($self, value, /)\n--\n\nReturn self*value."), |
8340 | SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, |
8341 | "__rmul__($self, value, /)\n--\n\nReturn value*self."), |
8342 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
8343 | "__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
8344 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
8345 | "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
8346 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
8347 | "__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
8348 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
8349 | "__contains__($self, key, /)\n--\n\nReturn key in self."), |
8350 | SQSLOT("__iadd__", sq_inplace_concat, NULL, |
8351 | wrap_binaryfunc, |
8352 | "__iadd__($self, value, /)\n--\n\nImplement self+=value."), |
8353 | SQSLOT("__imul__", sq_inplace_repeat, NULL, |
8354 | wrap_indexargfunc, |
8355 | "__imul__($self, value, /)\n--\n\nImplement self*=value."), |
8356 | |
8357 | {NULL} |
8358 | }; |
8359 | |
8360 | /* Given a type pointer and an offset gotten from a slotdef entry, return a |
8361 | pointer to the actual slot. This is not quite the same as simply adding |
8362 | the offset to the type pointer, since it takes care to indirect through the |
8363 | proper indirection pointer (as_buffer, etc.); it returns NULL if the |
8364 | indirection pointer is NULL. */ |
8365 | static void ** |
8366 | slotptr(PyTypeObject *type, int ioffset) |
8367 | { |
8368 | char *ptr; |
8369 | long offset = ioffset; |
8370 | |
8371 | /* Note: this depends on the order of the members of PyHeapTypeObject! */ |
8372 | assert(offset >= 0); |
8373 | assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); |
8374 | if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { Branch (8374:9): [True: 2.01M, False: 16.0M]
|
8375 | ptr = (char *)type->tp_as_sequence; |
8376 | offset -= offsetof(PyHeapTypeObject, as_sequence); |
8377 | } |
8378 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { Branch (8378:14): [True: 785k, False: 15.2M]
|
8379 | ptr = (char *)type->tp_as_mapping; |
8380 | offset -= offsetof(PyHeapTypeObject, as_mapping); |
8381 | } |
8382 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { Branch (8382:14): [True: 9.06M, False: 6.15M]
|
8383 | ptr = (char *)type->tp_as_number; |
8384 | offset -= offsetof(PyHeapTypeObject, as_number); |
8385 | } |
8386 | else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_async)) { Branch (8386:14): [True: 615k, False: 5.54M]
|
8387 | ptr = (char *)type->tp_as_async; |
8388 | offset -= offsetof(PyHeapTypeObject, as_async); |
8389 | } |
8390 | else { |
8391 | ptr = (char *)type; |
8392 | } |
8393 | if (ptr != NULL) Branch (8393:9): [True: 16.7M, False: 1.32M]
|
8394 | ptr += offset; |
8395 | return (void **)ptr; |
8396 | } |
8397 | |
8398 | /* Length of array of slotdef pointers used to store slots with the |
8399 | same __name__. There should be at most MAX_EQUIV-1 slotdef entries with |
8400 | the same __name__, for any __name__. Since that's a static property, it is |
8401 | appropriate to declare fixed-size arrays for this. */ |
8402 | #define MAX_EQUIV 10 |
8403 | |
8404 | /* Return a slot pointer for a given name, but ONLY if the attribute has |
8405 | exactly one slot function. The name must be an interned string. */ |
8406 | static void ** |
8407 | resolve_slotdups(PyTypeObject *type, PyObject *name) |
8408 | { |
8409 | /* XXX Maybe this could be optimized more -- but is it worth it? */ |
8410 | |
8411 | /* pname and ptrs act as a little cache */ |
8412 | static PyObject *pname; |
8413 | static slotdef *ptrs[MAX_EQUIV]; |
8414 | slotdef *p, **pp; |
8415 | void **res, **ptr; |
8416 | |
8417 | if (pname != name) { Branch (8417:9): [True: 1.50M, False: 5.86k]
|
8418 | /* Collect all slotdefs that match name into ptrs. */ |
8419 | pname = name; |
8420 | pp = ptrs; |
8421 | for (p = slotdefs; p->name_strobj; p++138M ) { Branch (8421:28): [True: 138M, False: 1.50M]
|
8422 | if (p->name_strobj == name) Branch (8422:17): [True: 2.10M, False: 136M]
|
8423 | *pp++ = p; |
8424 | } |
8425 | *pp = NULL; |
8426 | } |
8427 | |
8428 | /* Look in all slots of the type matching the name. If exactly one of these |
8429 | has a filled-in slot, return a pointer to that slot. |
8430 | Otherwise, return NULL. */ |
8431 | res = NULL; |
8432 | for (pp = ptrs; *pp; pp++2.06M ) { Branch (8432:21): [True: 2.10M, False: 1.46M]
|
8433 | ptr = slotptr(type, (*pp)->offset); |
8434 | if (ptr == NULL || *ptr == NULL) Branch (8434:13): [True: 0, False: 2.10M]
Branch (8434:28): [True: 647k, False: 1.46M]
|
8435 | continue; |
8436 | if (res != NULL) Branch (8436:13): [True: 39.7k, False: 1.42M]
|
8437 | return NULL; |
8438 | res = ptr; |
8439 | } |
8440 | return res; |
8441 | } |
8442 | |
8443 | |
8444 | /* Common code for update_slots_callback() and fixup_slot_dispatchers(). |
8445 | * |
8446 | * This is meant to set a "slot" like type->tp_repr or |
8447 | * type->tp_as_sequence->sq_concat by looking up special methods like |
8448 | * __repr__ or __add__. The opposite (adding special methods from slots) is |
8449 | * done by add_operators(), called from PyType_Ready(). Since update_one_slot() |
8450 | * calls PyType_Ready() if needed, the special methods are already in place. |
8451 | * |
8452 | * The special methods corresponding to each slot are defined in the "slotdef" |
8453 | * array. Note that one slot may correspond to multiple special methods and vice |
8454 | * versa. For example, tp_richcompare uses 6 methods __lt__, ..., __ge__ and |
8455 | * tp_as_number->nb_add uses __add__ and __radd__. In the other direction, |
8456 | * __add__ is used by the number and sequence protocols and __getitem__ by the |
8457 | * sequence and mapping protocols. This causes a lot of complications. |
8458 | * |
8459 | * In detail, update_one_slot() does the following: |
8460 | * |
8461 | * First of all, if the slot in question does not exist, return immediately. |
8462 | * This can happen for example if it's tp_as_number->nb_add but tp_as_number |
8463 | * is NULL. |
8464 | * |
8465 | * For the given slot, we loop over all the special methods with a name |
8466 | * corresponding to that slot (for example, for tp_descr_set, this would be |
8467 | * __set__ and __delete__) and we look up these names in the MRO of the type. |
8468 | * If we don't find any special method, the slot is set to NULL (regardless of |
8469 | * what was in the slot before). |
8470 | * |
8471 | * Suppose that we find exactly one special method. If it's a wrapper_descriptor |
8472 | * (i.e. a special method calling a slot, for example str.__repr__ which calls |
8473 | * the tp_repr for the 'str' class) with the correct name ("__repr__" for |
8474 | * tp_repr), for the right class, calling the right wrapper C function (like |
8475 | * wrap_unaryfunc for tp_repr), then the slot is set to the slot that the |
8476 | * wrapper_descriptor originally wrapped. For example, a class inheriting |
8477 | * from 'str' and not redefining __repr__ will have tp_repr set to the tp_repr |
8478 | * of 'str'. |
8479 | * In all other cases where the special method exists, the slot is set to a |
8480 | * wrapper calling the special method. There is one exception: if the special |
8481 | * method is a wrapper_descriptor with the correct name but the type has |
8482 | * precisely one slot set for that name and that slot is not the one that we |
8483 | * are updating, then NULL is put in the slot (this exception is the only place |
8484 | * in update_one_slot() where the *existing* slots matter). |
8485 | * |
8486 | * When there are multiple special methods for the same slot, the above is |
8487 | * applied for each special method. As long as the results agree, the common |
8488 | * resulting slot is applied. If the results disagree, then a wrapper for |
8489 | * the special methods is installed. This is always safe, but less efficient |
8490 | * because it uses method lookup instead of direct C calls. |
8491 | * |
8492 | * There are some further special cases for specific slots, like supporting |
8493 | * __hash__ = None for tp_hash and special code for tp_new. |
8494 | * |
8495 | * When done, return a pointer to the next slotdef with a different offset, |
8496 | * because that's convenient for fixup_slot_dispatchers(). This function never |
8497 | * sets an exception: if an internal error happens (unlikely), it's ignored. */ |
8498 | static slotdef * |
8499 | update_one_slot(PyTypeObject *type, slotdef *p) |
8500 | { |
8501 | PyObject *descr; |
8502 | PyWrapperDescrObject *d; |
8503 | void *generic = NULL, *specific = NULL; |
8504 | int use_generic = 0; |
8505 | int offset = p->offset; |
8506 | int error; |
8507 | void **ptr = slotptr(type, offset); |
8508 | |
8509 | if (ptr == NULL) { Branch (8509:9): [True: 0, False: 6.04M]
|
8510 | do { |
8511 | ++p; |
8512 | } while (p->offset == offset); Branch (8512:18): [True: 0, False: 0]
|
8513 | return p; |
8514 | } |
8515 | /* We may end up clearing live exceptions below, so make sure it's ours. */ |
8516 | assert(!PyErr_Occurred()); |
8517 | do { |
8518 | /* Use faster uncached lookup as we won't get any cache hits during type setup. */ |
8519 | descr = find_name_in_mro(type, p->name_strobj, &error); |
8520 | if (descr == NULL) { Branch (8520:13): [True: 6.33M, False: 2.37M]
|
8521 | if (error == -1) { Branch (8521:17): [True: 0, False: 6.33M]
|
8522 | /* It is unlikely but not impossible that there has been an exception |
8523 | during lookup. Since this function originally expected no errors, |
8524 | we ignore them here in order to keep up the interface. */ |
8525 | PyErr_Clear(); |
8526 | } |
8527 | if (ptr == (void**)&type->tp_iternext) { Branch (8527:17): [True: 84.1k, False: 6.25M]
|
8528 | specific = (void *)_PyObject_NextNotImplemented; |
8529 | } |
8530 | continue; |
8531 | } |
8532 | if (Py_IS_TYPE(descr, &PyWrapperDescr_Type) && |
8533 | ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj1.51M ) { Branch (8533:13): [True: 1.50M, False: 1.21k]
|
8534 | void **tptr = resolve_slotdups(type, p->name_strobj); |
8535 | if (tptr == NULL || tptr == ptr1.38M ) Branch (8535:17): [True: 127k, False: 1.38M]
Branch (8535:33): [True: 1.10M, False: 279k]
|
8536 | generic = p->function; |
8537 | d = (PyWrapperDescrObject *)descr; |
8538 | if ((specific == NULL || specific == d->d_wrapped573k ) && Branch (8538:18): [True: 935k, False: 573k]
Branch (8538:38): [True: 573k, False: 114]
|
8539 | d->d_base->wrapper == p->wrapper1.50M && Branch (8539:17): [True: 1.21M, False: 289k]
|
8540 | PyType_IsSubtype(type, 1.21M PyDescr_TYPE1.21M (d))) Branch (8540:17): [True: 1.21M, False: 1]
|
8541 | { |
8542 | specific = d->d_wrapped; |
8543 | } |
8544 | else { |
8545 | /* We cannot use the specific slot function because either |
8546 | - it is not unique: there are multiple methods for this |
8547 | slot and they conflict |
8548 | - the signature is wrong (as checked by the ->wrapper |
8549 | comparison above) |
8550 | - it's wrapping the wrong class |
8551 | */ |
8552 | use_generic = 1; |
8553 | } |
8554 | } |
8555 | else if (Py_IS_TYPE(descr, &PyCFunction_Type) && |
8556 | PyCFunction_GET_FUNCTION64.9k (descr) == Branch (8556:18): [True: 64.9k, False: 4]
|
8557 | _PyCFunction_CAST(tp_new_wrapper) && |
8558 | ptr == (void**)&type->tp_new64.9k ) Branch (8558:18): [True: 64.9k, False: 0]
|
8559 | { |
8560 | /* The __new__ wrapper is not a wrapper descriptor, |
8561 | so must be special-cased differently. |
8562 | If we don't do this, creating an instance will |
8563 | always use slot_tp_new which will look up |
8564 | __new__ in the MRO which will call tp_new_wrapper |
8565 | which will look through the base classes looking |
8566 | for a static base and call its tp_new (usually |
8567 | PyType_GenericNew), after performing various |
8568 | sanity checks and constructing a new argument |
8569 | list. Cut all that nonsense short -- this speeds |
8570 | up instance creation tremendously. */ |
8571 | specific = (void *)type->tp_new; |
8572 | /* XXX I'm not 100% sure that there isn't a hole |
8573 | in this reasoning that requires additional |
8574 | sanity checks. I'll buy the first person to |
8575 | point out a bug in this reasoning a beer. */ |
8576 | } |
8577 | else if (descr == Py_None && Branch (8577:18): [True: 4.43k, False: 793k]
|
8578 | ptr == (void**)&type->tp_hash4.43k ) { Branch (8578:18): [True: 4.35k, False: 81]
|
8579 | /* We specifically allow __hash__ to be set to None |
8580 | to prevent inheritance of the default |
8581 | implementation from object.__hash__ */ |
8582 | specific = (void *)PyObject_HashNotImplemented; |
8583 | } |
8584 | else { |
8585 | use_generic = 1; |
8586 | generic = p->function; |
8587 | } |
8588 | } while ((++p)->offset == offset); Branch (8588:14): [True: 2.66M, False: 6.04M]
|
8589 | if (specific && !use_generic799k ) Branch (8589:9): [True: 799k, False: 5.24M]
Branch (8589:21): [True: 741k, False: 58.4k]
|
8590 | *ptr = specific; |
8591 | else |
8592 | *ptr = generic; |
8593 | return p; |
8594 | } |
8595 | |
8596 | /* In the type, update the slots whose slotdefs are gathered in the pp array. |
8597 | This is a callback for update_subclasses(). */ |
8598 | static int |
8599 | update_slots_callback(PyTypeObject *type, void *data) |
8600 | { |
8601 | slotdef **pp = (slotdef **)data; |
8602 | for (; *pp; pp++408k ) { Branch (8602:12): [True: 408k, False: 359k]
|
8603 | update_one_slot(type, *pp); |
8604 | } |
8605 | return 0; |
8606 | } |
8607 | |
8608 | static int slotdefs_initialized = 0; |
8609 | /* Initialize the slotdefs table by adding interned string objects for the |
8610 | names. */ |
8611 | PyStatus |
8612 | _PyTypes_InitSlotDefs(void) |
8613 | { |
8614 | if (slotdefs_initialized) { Branch (8614:9): [True: 0, False: 107]
|
8615 | return _PyStatus_OK(); |
8616 | } |
8617 | |
8618 | for (slotdef *p = slotdefs; 107 p->name; p++9.84k ) { Branch (8618:33): [True: 9.84k, False: 107]
|
8619 | /* Slots must be ordered by their offset in the PyHeapTypeObject. */ |
8620 | assert(!p[1].name || p->offset <= p[1].offset); |
8621 | /* bpo-40521: Interned strings are shared by all subinterpreters */ |
8622 | p->name_strobj = PyUnicode_InternFromString(p->name); |
8623 | if (!p->name_strobj || !PyUnicode_CHECK_INTERNED(p->name_strobj)) { Branch (8623:13): [True: 0, False: 9.84k]
Branch (8623:32): [True: 0, False: 9.84k]
|
8624 | return _PyStatus_NO_MEMORY(); |
8625 | } |
8626 | } |
8627 | slotdefs_initialized = 1; |
8628 | return _PyStatus_OK(); |
8629 | } |
8630 | |
8631 | /* Undo _PyTypes_InitSlotDefs(), releasing the interned strings. */ |
8632 | static void clear_slotdefs(void) |
8633 | { |
8634 | for (slotdef *p = slotdefs; p->name; p++9.47k ) { Branch (8634:33): [True: 9.47k, False: 103]
|
8635 | Py_CLEAR(p->name_strobj); |
8636 | } |
8637 | slotdefs_initialized = 0; |
8638 | } |
8639 | |
8640 | /* Update the slots after assignment to a class (type) attribute. */ |
8641 | static int |
8642 | update_slot(PyTypeObject *type, PyObject *name) |
8643 | { |
8644 | slotdef *ptrs[MAX_EQUIV]; |
8645 | slotdef *p; |
8646 | slotdef **pp; |
8647 | int offset; |
8648 | |
8649 | assert(PyUnicode_CheckExact(name)); |
8650 | assert(PyUnicode_CHECK_INTERNED(name)); |
8651 | |
8652 | assert(slotdefs_initialized); |
8653 | pp = ptrs; |
8654 | for (p = slotdefs; p->name; p++39.3M ) { Branch (8654:24): [True: 39.3M, False: 428k]
|
8655 | assert(PyUnicode_CheckExact(p->name_strobj)); |
8656 | assert(PyUnicode_CheckExact(name)); |
8657 | /* bpo-40521: Using interned strings. */ |
8658 | if (p->name_strobj == name) { Branch (8658:13): [True: 396k, False: 38.9M]
|
8659 | *pp++ = p; |
8660 | } |
8661 | } |
8662 | *pp = NULL; |
8663 | for (pp = ptrs; *pp; pp++396k ) { Branch (8663:21): [True: 396k, False: 428k]
|
8664 | p = *pp; |
8665 | offset = p->offset; |
8666 | while (p > slotdefs && (p-1)->offset == offset551k ) Branch (8666:16): [True: 551k, False: 186]
Branch (8666:32): [True: 155k, False: 396k]
|
8667 | --p; |
8668 | *pp = p; |
8669 | } |
8670 | if (ptrs[0] == NULL) Branch (8670:9): [True: 78.6k, False: 349k]
|
8671 | return 0; /* Not an attribute that affects any slots */ |
8672 | return update_subclasses(type, name, |
8673 | update_slots_callback, (void *)ptrs); |
8674 | } |
8675 | |
8676 | /* Store the proper functions in the slot dispatches at class (type) |
8677 | definition time, based upon which operations the class overrides in its |
8678 | dict. */ |
8679 | static void |
8680 | fixup_slot_dispatchers(PyTypeObject *type) |
8681 | { |
8682 | assert(!PyErr_Occurred()); |
8683 | assert(slotdefs_initialized); |
8684 | for (slotdef *p = slotdefs; p->name; ) { Branch (8684:33): [True: 5.63M, False: 86.7k]
|
8685 | p = update_one_slot(type, p); |
8686 | } |
8687 | } |
8688 | |
8689 | static void |
8690 | update_all_slots(PyTypeObject* type) |
8691 | { |
8692 | slotdef *p; |
8693 | |
8694 | /* Clear the VALID_VERSION flag of 'type' and all its subclasses. */ |
8695 | PyType_Modified(type); |
8696 | |
8697 | assert(slotdefs_initialized); |
8698 | for (p = slotdefs; p->name; p++4.14k ) { Branch (8698:24): [True: 4.14k, False: 45]
|
8699 | /* update_slot returns int but can't actually fail */ |
8700 | update_slot(type, p->name_strobj); |
8701 | } |
8702 | } |
8703 | |
8704 | |
8705 | /* Call __set_name__ on all attributes (including descriptors) |
8706 | in a newly generated type */ |
8707 | static int |
8708 | type_new_set_names(PyTypeObject *type) |
8709 | { |
8710 | PyObject *names_to_set = PyDict_Copy(type->tp_dict); |
8711 | if (names_to_set == NULL) { Branch (8711:9): [True: 0, False: 86.7k]
|
8712 | return -1; |
8713 | } |
8714 | |
8715 | Py_ssize_t i = 0; |
8716 | PyObject *key, *value; |
8717 | while (PyDict_Next(names_to_set, &i, &key, &value)) { Branch (8717:12): [True: 599k, False: 86.7k]
|
8718 | PyObject *set_name = _PyObject_LookupSpecial(value, |
8719 | &_Py_ID(__set_name__)); |
8720 | if (set_name == NULL) { Branch (8720:13): [True: 589k, False: 10.1k]
|
8721 | if (PyErr_Occurred()) { Branch (8721:17): [True: 0, False: 589k]
|
8722 | goto error; |
8723 | } |
8724 | continue; |
8725 | } |
8726 | |
8727 | PyObject *res = PyObject_CallFunctionObjArgs(set_name, type, key, NULL); |
8728 | Py_DECREF(set_name); |
8729 | |
8730 | if (res == NULL) { Branch (8730:13): [True: 12, False: 10.1k]
|
8731 | _PyErr_FormatFromCause(PyExc_RuntimeError, |
8732 | "Error calling __set_name__ on '%.100s' instance %R " |
8733 | "in '%.100s'", |
8734 | Py_TYPE(value)->tp_name, key, type->tp_name); |
8735 | goto error; |
8736 | } |
8737 | Py_DECREF(res); |
8738 | } |
8739 | |
8740 | Py_DECREF(names_to_set); |
8741 | return 0; |
8742 | |
8743 | error: |
8744 | Py_DECREF(names_to_set); |
8745 | return -1; |
8746 | } |
8747 | |
8748 | |
8749 | /* Call __init_subclass__ on the parent of a newly generated type */ |
8750 | static int |
8751 | type_new_init_subclass(PyTypeObject *type, PyObject *kwds) |
8752 | { |
8753 | PyObject *args[2] = {(PyObject *)type, (PyObject *)type}; |
8754 | PyObject *super = _PyObject_FastCall((PyObject *)&PySuper_Type, args, 2); |
8755 | if (super == NULL) { Branch (8755:9): [True: 1, False: 86.7k]
|
8756 | return -1; |
8757 | } |
8758 | |
8759 | PyObject *func = PyObject_GetAttr(super, &_Py_ID(__init_subclass__)); |
8760 | Py_DECREF(super); |
8761 | if (func == NULL) { Branch (8761:9): [True: 0, False: 86.7k]
|
8762 | return -1; |
8763 | } |
8764 | |
8765 | PyObject *result = PyObject_VectorcallDict(func, NULL, 0, kwds); |
8766 | Py_DECREF(func); |
8767 | if (result == NULL) { Branch (8767:9): [True: 43, False: 86.6k]
|
8768 | return -1; |
8769 | } |
8770 | |
8771 | Py_DECREF(result); |
8772 | return 0; |
8773 | } |
8774 | |
8775 | |
8776 | /* recurse_down_subclasses() and update_subclasses() are mutually |
8777 | recursive functions to call a callback for all subclasses, |
8778 | but refraining from recursing into subclasses that define 'attr_name'. */ |
8779 | |
8780 | static int |
8781 | update_subclasses(PyTypeObject *type, PyObject *attr_name, |
8782 | update_callback callback, void *data) |
8783 | { |
8784 | if (callback(type, data) < 0) { Branch (8784:9): [True: 0, False: 359k]
|
8785 | return -1; |
8786 | } |
8787 | return recurse_down_subclasses(type, attr_name, callback, data); |
8788 | } |
8789 | |
8790 | static int |
8791 | recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name, |
8792 | update_callback callback, void *data) |
8793 | { |
8794 | // It is safe to use a borrowed reference because update_subclasses() is |
8795 | // only used with update_slots_callback() which doesn't modify |
8796 | // tp_subclasses. |
8797 | PyObject *subclasses = type->tp_subclasses; // borrowed ref |
8798 | if (subclasses == NULL) { Branch (8798:9): [True: 355k, False: 3.49k]
|
8799 | return 0; |
8800 | } |
8801 | assert(PyDict_CheckExact(subclasses)); |
8802 | |
8803 | Py_ssize_t i = 0; |
8804 | PyObject *ref; |
8805 | while (PyDict_Next(subclasses, &i, NULL, &ref)) { Branch (8805:12): [True: 9.79k, False: 3.49k]
|
8806 | assert(PyWeakref_CheckRef(ref)); |
8807 | PyObject *obj = PyWeakref_GET_OBJECT(ref); |
8808 | assert(obj != NULL); |
8809 | if (obj == Py_None) { Branch (8809:13): [True: 0, False: 9.79k]
|
8810 | continue; |
8811 | } |
8812 | PyTypeObject *subclass = _PyType_CAST(obj); |
8813 | |
8814 | /* Avoid recursing down into unaffected classes */ |
8815 | PyObject *dict = subclass->tp_dict; |
8816 | if (dict != NULL && PyDict_Check(dict)) { Branch (8816:13): [True: 9.79k, False: 0]
|
8817 | int r = PyDict_Contains(dict, attr_name); |
8818 | if (r < 0) { Branch (8818:17): [True: 0, False: 9.79k]
|
8819 | return -1; |
8820 | } |
8821 | if (r > 0) { Branch (8821:17): [True: 27, False: 9.77k]
|
8822 | continue; |
8823 | } |
8824 | } |
8825 | |
8826 | if (update_subclasses(subclass, attr_name, callback, data) < 0) { Branch (8826:13): [True: 0, False: 9.77k]
|
8827 | return -1; |
8828 | } |
8829 | } |
8830 | return 0; |
8831 | } |
8832 | |
8833 | /* This function is called by PyType_Ready() to populate the type's |
8834 | dictionary with method descriptors for function slots. For each |
8835 | function slot (like tp_repr) that's defined in the type, one or more |
8836 | corresponding descriptors are added in the type's tp_dict dictionary |
8837 | under the appropriate name (like __repr__). Some function slots |
8838 | cause more than one descriptor to be added (for example, the nb_add |
8839 | slot adds both __add__ and __radd__ descriptors) and some function |
8840 | slots compete for the same descriptor (for example both sq_item and |
8841 | mp_subscript generate a __getitem__ descriptor). |
8842 | |
8843 | In the latter case, the first slotdef entry encountered wins. Since |
8844 | slotdef entries are sorted by the offset of the slot in the |
8845 | PyHeapTypeObject, this gives us some control over disambiguating |
8846 | between competing slots: the members of PyHeapTypeObject are listed |
8847 | from most general to least general, so the most general slot is |
8848 | preferred. In particular, because as_mapping comes before as_sequence, |
8849 | for a type that defines both mp_subscript and sq_item, mp_subscript |
8850 | wins. |
8851 | |
8852 | This only adds new descriptors and doesn't overwrite entries in |
8853 | tp_dict that were previously defined. The descriptors contain a |
8854 | reference to the C function they must call, so that it's safe if they |
8855 | are copied into a subtype's __dict__ and the subtype has a different |
8856 | C function in its slot -- calling the method defined by the |
8857 | descriptor will call the C function that was used to create it, |
8858 | rather than the C function present in the slot when it is called. |
8859 | (This is important because a subtype may have a C function in the |
8860 | slot that calls the method from the dictionary, and we want to avoid |
8861 | infinite recursion here.) */ |
8862 | |
8863 | static int |
8864 | add_operators(PyTypeObject *type) |
8865 | { |
8866 | PyObject *dict = type->tp_dict; |
8867 | slotdef *p; |
8868 | PyObject *descr; |
8869 | void **ptr; |
8870 | |
8871 | assert(slotdefs_initialized); |
8872 | for (p = slotdefs; p->name; p++10.5M ) { Branch (8872:24): [True: 10.5M, False: 114k]
|
8873 | if (p->wrapper == NULL) Branch (8873:13): [True: 688k, False: 9.87M]
|
8874 | continue; |
8875 | ptr = slotptr(type, p->offset); |
8876 | if (!ptr || !*ptr8.55M ) Branch (8876:13): [True: 1.32M, False: 8.55M]
Branch (8876:21): [True: 8.35M, False: 195k]
|
8877 | continue; |
8878 | int r = PyDict_Contains(dict, p->name_strobj); |
8879 | if (r > 0) Branch (8879:13): [True: 2.70k, False: 192k]
|
8880 | continue; |
8881 | if (r < 0) { Branch (8881:13): [True: 0, False: 192k]
|
8882 | return -1; |
8883 | } |
8884 | if (*ptr == (void *)PyObject_HashNotImplemented) { Branch (8884:13): [True: 1.48k, False: 191k]
|
8885 | /* Classes may prevent the inheritance of the tp_hash |
8886 | slot by storing PyObject_HashNotImplemented in it. Make it |
8887 | visible as a None value for the __hash__ attribute. */ |
8888 | if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) Branch (8888:17): [True: 0, False: 1.48k]
|
8889 | return -1; |
8890 | } |
8891 | else { |
8892 | descr = PyDescr_NewWrapper(type, p, *ptr); |
8893 | if (descr == NULL) Branch (8893:17): [True: 0, False: 191k]
|
8894 | return -1; |
8895 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) { Branch (8895:17): [True: 0, False: 191k]
|
8896 | Py_DECREF(descr); |
8897 | return -1; |
8898 | } |
8899 | Py_DECREF(descr); |
8900 | } |
8901 | } |
8902 | return 0; |
8903 | } |
8904 | |
8905 | |
8906 | /* Cooperative 'super' */ |
8907 | |
8908 | typedef struct { |
8909 | PyObject_HEAD |
8910 | PyTypeObject *type; |
8911 | PyObject *obj; |
8912 | PyTypeObject *obj_type; |
8913 | } superobject; |
8914 | |
8915 | static PyMemberDef super_members[] = { |
8916 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
8917 | "the class invoking super()"}, |
8918 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
8919 | "the instance invoking super(); may be None"}, |
8920 | {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, |
8921 | "the type of the instance invoking super(); may be None"}, |
8922 | {0} |
8923 | }; |
8924 | |
8925 | static void |
8926 | super_dealloc(PyObject *self) |
8927 | { |
8928 | superobject *su = (superobject *)self; |
8929 | |
8930 | _PyObject_GC_UNTRACK(self); |
8931 | Py_XDECREF(su->obj); |
8932 | Py_XDECREF(su->type); |
8933 | Py_XDECREF(su->obj_type); |
8934 | Py_TYPE(self)->tp_free(self); |
8935 | } |
8936 | |
8937 | static PyObject * |
8938 | super_repr(PyObject *self) |
8939 | { |
8940 | superobject *su = (superobject *)self; |
8941 |
|
8942 | if (su->obj_type) Branch (8942:9): [True: 0, False: 0]
|
8943 | return PyUnicode_FromFormat( |
8944 | "<super: <class '%s'>, <%s object>>", |
8945 | su->type ? su->type->tp_name : "NULL", Branch (8945:13): [True: 0, False: 0]
|
8946 | su->obj_type->tp_name); |
8947 | else |
8948 | return PyUnicode_FromFormat( |
8949 | "<super: <class '%s'>, NULL>", |
8950 | su->type ? su->type->tp_name : "NULL"); Branch (8950:13): [True: 0, False: 0]
|
8951 | } |
8952 | |
8953 | static PyObject * |
8954 | super_getattro(PyObject *self, PyObject *name) |
8955 | { |
8956 | superobject *su = (superobject *)self; |
8957 | PyTypeObject *starttype; |
8958 | PyObject *mro; |
8959 | Py_ssize_t i, n; |
8960 | |
8961 | starttype = su->obj_type; |
8962 | if (starttype == NULL) Branch (8962:9): [True: 2, False: 3.53M]
|
8963 | goto skip; |
8964 | |
8965 | /* We want __class__ to return the class of the super object |
8966 | (i.e. super, or a subclass), not the class of su->obj. */ |
8967 | if (PyUnicode_Check(name) && |
8968 | PyUnicode_GET_LENGTH(name) == 9 && Branch (8968:9): [True: 82.5k, False: 3.45M]
|
8969 | _PyUnicode_Equal(name, &82.5k _Py_ID82.5k (__class__))) Branch (8969:9): [True: 1, False: 82.5k]
|
8970 | goto skip; |
8971 | |
8972 | mro = starttype->tp_mro; |
8973 | if (mro == NULL) Branch (8973:9): [True: 1, False: 3.53M]
|
8974 | goto skip; |
8975 | |
8976 | assert(PyTuple_Check(mro)); |
8977 | n = PyTuple_GET_SIZE(mro); |
8978 | |
8979 | /* No need to check the last one: it's gonna be skipped anyway. */ |
8980 | for (i = 0; i+1 < n; i++858k ) { Branch (8980:17): [True: 4.39M, False: 1]
|
8981 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) Branch (8981:13): [True: 3.53M, False: 858k]
|
8982 | break; |
8983 | } |
8984 | i++; /* skip su->type (if any) */ |
8985 | if (i >= n) Branch (8985:9): [True: 1, False: 3.53M]
|
8986 | goto skip; |
8987 | |
8988 | /* keep a strong reference to mro because starttype->tp_mro can be |
8989 | replaced during PyDict_GetItemWithError(dict, name) */ |
8990 | Py_INCREF(mro); |
8991 | do { |
8992 | PyObject *obj = PyTuple_GET_ITEM(mro, i); |
8993 | PyObject *dict = _PyType_CAST(obj)->tp_dict; |
8994 | assert(dict != NULL && PyDict_Check(dict)); |
8995 | |
8996 | PyObject *res = PyDict_GetItemWithError(dict, name); |
8997 | if (res != NULL) { Branch (8997:13): [True: 3.53M, False: 3.15M]
|
8998 | Py_INCREF(res); |
8999 | |
9000 | descrgetfunc f = Py_TYPE(res)->tp_descr_get; |
9001 | if (f != NULL) { Branch (9001:17): [True: 2.74M, False: 785k]
|
9002 | PyObject *res2; |
9003 | res2 = f(res, |
9004 | /* Only pass 'obj' param if this is instance-mode super |
9005 | (See SF ID #743627) */ |
9006 | (su->obj == (PyObject *)starttype) ? NULL : su->obj2.65M , Branch (9006:21): [True: 90.2k, False: 2.65M]
|
9007 | (PyObject *)starttype); |
9008 | Py_DECREF(res); |
9009 | res = res2; |
9010 | } |
9011 | |
9012 | Py_DECREF(mro); |
9013 | return res; |
9014 | } |
9015 | else if (PyErr_Occurred()) { Branch (9015:18): [True: 0, False: 3.15M]
|
9016 | Py_DECREF(mro); |
9017 | return NULL; |
9018 | } |
9019 | |
9020 | i++; |
9021 | } while (i < n); Branch (9021:14): [True: 3.15M, False: 86]
|
9022 | Py_DECREF(mro); |
9023 | |
9024 | skip: |
9025 | return PyObject_GenericGetAttr(self, name); |
9026 | } |
9027 | |
9028 | static PyTypeObject * |
9029 | supercheck(PyTypeObject *type, PyObject *obj) |
9030 | { |
9031 | /* Check that a super() call makes sense. Return a type object. |
9032 | |
9033 | obj can be a class, or an instance of one: |
9034 | |
9035 | - If it is a class, it must be a subclass of 'type'. This case is |
9036 | used for class methods; the return value is obj. |
9037 | |
9038 | - If it is an instance, it must be an instance of 'type'. This is |
9039 | the normal case; the return value is obj.__class__. |
9040 | |
9041 | But... when obj is an instance, we want to allow for the case where |
9042 | Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! |
9043 | This will allow using super() with a proxy for obj. |
9044 | */ |
9045 | |
9046 | /* Check for first bullet above (special case) */ |
9047 | if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)936k ) { Branch (9047:30): [True: 875k, False: 61.2k]
|
9048 | Py_INCREF(obj); |
9049 | return (PyTypeObject *)obj; |
9050 | } |
9051 | |
9052 | /* Normal case */ |
9053 | if (PyType_IsSubtype(Py_TYPE(obj), type)) { Branch (9053:9): [True: 2.65M, False: 7]
|
9054 | Py_INCREF(Py_TYPE(obj)); |
9055 | return Py_TYPE(obj); |
9056 | } |
9057 | else { |
9058 | /* Try the slow way */ |
9059 | PyObject *class_attr; |
9060 | |
9061 | if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &class_attr) < 0) { Branch (9061:13): [True: 0, False: 7]
|
9062 | return NULL; |
9063 | } |
9064 | if (class_attr != NULL && Branch (9064:13): [True: 7, False: 0]
|
9065 | PyType_Check(class_attr) && |
9066 | (PyTypeObject *)class_attr != Py_TYPE(obj)) Branch (9066:13): [True: 1, False: 6]
|
9067 | { |
9068 | int ok = PyType_IsSubtype( |
9069 | (PyTypeObject *)class_attr, type); |
9070 | if (ok) { Branch (9070:17): [True: 1, False: 0]
|
9071 | return (PyTypeObject *)class_attr; |
9072 | } |
9073 | } |
9074 | Py_XDECREF(class_attr); |
9075 | } |
9076 | |
9077 | PyErr_SetString(PyExc_TypeError, |
9078 | "super(type, obj): " |
9079 | "obj must be an instance or subtype of type"); |
9080 | return NULL; |
9081 | } |
9082 | |
9083 | static PyObject * |
9084 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
9085 | { |
9086 | superobject *su = (superobject *)self; |
9087 | superobject *newobj; |
9088 | |
9089 | if (obj == NULL || obj == Py_None || su->obj != NULL) { Branch (9089:9): [True: 0, False: 16]
Branch (9089:24): [True: 0, False: 16]
Branch (9089:42): [True: 0, False: 16]
|
9090 | /* Not binding to an object, or already bound */ |
9091 | Py_INCREF(self); |
9092 | return self; |
9093 | } |
9094 | if (!Py_IS_TYPE(su, &PySuper_Type)) Branch (9094:9): [True: 1, False: 15]
|
9095 | /* If su is an instance of a (strict) subclass of super, |
9096 | call its type */ |
9097 | return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), |
9098 | su->type, obj, NULL); |
9099 | else { |
9100 | /* Inline the common case */ |
9101 | PyTypeObject *obj_type = supercheck(su->type, obj); |
9102 | if (obj_type == NULL) Branch (9102:13): [True: 2, False: 13]
|
9103 | return NULL; |
9104 | newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, |
9105 | NULL, NULL); |
9106 | if (newobj == NULL) Branch (9106:13): [True: 0, False: 13]
|
9107 | return NULL; |
9108 | Py_INCREF(su->type); |
9109 | Py_INCREF(obj); |
9110 | newobj->type = su->type; |
9111 | newobj->obj = obj; |
9112 | newobj->obj_type = obj_type; |
9113 | return (PyObject *)newobj; |
9114 | } |
9115 | } |
9116 | |
9117 | static int |
9118 | super_init_without_args(_PyInterpreterFrame *cframe, PyCodeObject *co, |
9119 | PyTypeObject **type_p, PyObject **obj_p) |
9120 | { |
9121 | if (co->co_argcount == 0) { Branch (9121:9): [True: 1, False: 2.37M]
|
9122 | PyErr_SetString(PyExc_RuntimeError, |
9123 | "super(): no arguments"); |
9124 | return -1; |
9125 | } |
9126 | |
9127 | assert(cframe->f_code->co_nlocalsplus > 0); |
9128 | PyObject *firstarg = _PyFrame_GetLocalsArray(cframe)[0]; |
9129 | // The first argument might be a cell. |
9130 | if (firstarg != NULL && (_PyLocals_GetKind(co->co_localspluskinds, 0) & 2.37M CO_FAST_CELL2.37M )) { Branch (9130:9): [True: 2.37M, False: 1]
Branch (9130:29): [True: 1.09k, False: 2.37M]
|
9131 | // "firstarg" is a cell here unless (very unlikely) super() |
9132 | // was called from the C-API before the first MAKE_CELL op. |
9133 | if (_PyInterpreterFrame_LASTI(cframe) >= 0) { Branch (9133:13): [True: 1.09k, False: 0]
|
9134 | // MAKE_CELL and COPY_FREE_VARS have no quickened forms, so no need |
9135 | // to use _PyOpcode_Deopt here: |
9136 | assert(_Py_OPCODE(_PyCode_CODE(co)[0]) == MAKE_CELL || |
9137 | _Py_OPCODE(_PyCode_CODE(co)[0]) == COPY_FREE_VARS); |
9138 | assert(PyCell_Check(firstarg)); |
9139 | firstarg = PyCell_GET(firstarg); |
9140 | } |
9141 | } |
9142 | if (firstarg == NULL) { Branch (9142:9): [True: 1, False: 2.37M]
|
9143 | PyErr_SetString(PyExc_RuntimeError, |
9144 | "super(): arg[0] deleted"); |
9145 | return -1; |
9146 | } |
9147 | |
9148 | // Look for __class__ in the free vars. |
9149 | PyTypeObject *type = NULL; |
9150 | int i = co->co_nlocals + co->co_nplaincellvars; |
9151 | for (; i < co->co_nlocalsplus; i++7 ) { Branch (9151:12): [True: 2.37M, False: 0]
|
9152 | assert((_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_FREE) != 0); |
9153 | PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i); |
9154 | assert(PyUnicode_Check(name)); |
9155 | if (_PyUnicode_Equal(name, &_Py_ID(__class__))) { Branch (9155:13): [True: 2.37M, False: 7]
|
9156 | PyObject *cell = _PyFrame_GetLocalsArray(cframe)[i]; |
9157 | if (cell == NULL || !PyCell_Check(cell)) { Branch (9157:17): [True: 0, False: 2.37M]
Branch (9157:33): [True: 0, False: 2.37M]
|
9158 | PyErr_SetString(PyExc_RuntimeError, |
9159 | "super(): bad __class__ cell"); |
9160 | return -1; |
9161 | } |
9162 | type = (PyTypeObject *) PyCell_GET(cell); |
9163 | if (type == NULL) { Branch (9163:17): [True: 1, False: 2.37M]
|
9164 | PyErr_SetString(PyExc_RuntimeError, |
9165 | "super(): empty __class__ cell"); |
9166 | return -1; |
9167 | } |
9168 | if (!PyType_Check(type)) { Branch (9168:17): [True: 0, False: 2.37M]
|
9169 | PyErr_Format(PyExc_RuntimeError, |
9170 | "super(): __class__ is not a type (%s)", |
9171 | Py_TYPE(type)->tp_name); |
9172 | return -1; |
9173 | } |
9174 | break; |
9175 | } |
9176 | } |
9177 | if (type == NULL) { Branch (9177:9): [True: 0, False: 2.37M]
|
9178 | PyErr_SetString(PyExc_RuntimeError, |
9179 | "super(): __class__ cell not found"); |
9180 | return -1; |
9181 | } |
9182 | |
9183 | *type_p = type; |
9184 | *obj_p = firstarg; |
9185 | return 0; |
9186 | } |
9187 | |
9188 | static int super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj); |
9189 | |
9190 | static int |
9191 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
9192 | { |
9193 | PyTypeObject *type = NULL; |
9194 | PyObject *obj = NULL; |
9195 | |
9196 | if (!_PyArg_NoKeywords("super", kwds)) |
9197 | return -1; |
9198 | if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj)) Branch (9198:9): [True: 0, False: 1.00k]
|
9199 | return -1; |
9200 | if (super_init_impl(self, type, obj) < 0) { Branch (9200:9): [True: 0, False: 1.00k]
|
9201 | return -1; |
9202 | } |
9203 | return 0; |
9204 | } |
9205 | |
9206 | static inline int |
9207 | super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj) { |
9208 | superobject *su = (superobject *)self; |
9209 | PyTypeObject *obj_type = NULL; |
9210 | if (type == NULL) { Branch (9210:9): [True: 2.37M, False: 1.15M]
|
9211 | /* Call super(), without args -- fill in from __class__ |
9212 | and first local variable on the stack. */ |
9213 | PyThreadState *tstate = _PyThreadState_GET(); |
9214 | _PyInterpreterFrame *cframe = tstate->cframe->current_frame; |
9215 | if (cframe == NULL) { Branch (9215:13): [True: 0, False: 2.37M]
|
9216 | PyErr_SetString(PyExc_RuntimeError, |
9217 | "super(): no current frame"); |
9218 | return -1; |
9219 | } |
9220 | int res = super_init_without_args(cframe, cframe->f_code, &type, &obj); |
9221 | |
9222 | if (res < 0) { Branch (9222:13): [True: 3, False: 2.37M]
|
9223 | return -1; |
9224 | } |
9225 | } |
9226 | |
9227 | if (obj == Py_None) Branch (9227:9): [True: 0, False: 3.53M]
|
9228 | obj = NULL; |
9229 | if (obj != NULL) { Branch (9229:9): [True: 3.53M, False: 14]
|
9230 | obj_type = supercheck(type, obj); |
9231 | if (obj_type == NULL) Branch (9231:13): [True: 4, False: 3.53M]
|
9232 | return -1; |
9233 | Py_INCREF(obj); |
9234 | } |
9235 | Py_INCREF(type); |
9236 | Py_XSETREF(su->type, type); |
9237 | Py_XSETREF(su->obj, obj); |
9238 | Py_XSETREF(su->obj_type, obj_type); |
9239 | return 0; |
9240 | } |
9241 | |
9242 | PyDoc_STRVAR(super_doc, |
9243 | "super() -> same as super(__class__, <first argument>)\n" |
9244 | "super(type) -> unbound super object\n" |
9245 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
9246 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
9247 | "Typical use to call a cooperative superclass method:\n" |
9248 | "class C(B):\n" |
9249 | " def meth(self, arg):\n" |
9250 | " super().meth(arg)\n" |
9251 | "This works for class methods too:\n" |
9252 | "class C(B):\n" |
9253 | " @classmethod\n" |
9254 | " def cmeth(cls, arg):\n" |
9255 | " super().cmeth(arg)\n"); |
9256 | |
9257 | static int |
9258 | super_traverse(PyObject *self, visitproc visit, void *arg) |
9259 | { |
9260 | superobject *su = (superobject *)self; |
9261 | |
9262 | Py_VISIT(su->obj); |
9263 | Py_VISIT(su->type); |
9264 | Py_VISIT(su->obj_type); |
9265 | |
9266 | return 0; |
9267 | } |
9268 | |
9269 | static PyObject * |
9270 | super_vectorcall(PyObject *self, PyObject *const *args, |
9271 | size_t nargsf, PyObject *kwnames) |
9272 | { |
9273 | assert(PyType_Check(self)); |
9274 | if (!_PyArg_NoKwnames("super", kwnames)) { |
9275 | return NULL; |
9276 | } |
9277 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
9278 | if (!_PyArg_CheckPositional("super()", nargs, 0, 2)) { |
9279 | return NULL; |
9280 | } |
9281 | PyTypeObject *type = NULL; |
9282 | PyObject *obj = NULL; |
9283 | PyTypeObject *self_type = (PyTypeObject *)self; |
9284 | PyObject *su = self_type->tp_alloc(self_type, 0); |
9285 | if (su == NULL) { Branch (9285:9): [True: 0, False: 3.53M]
|
9286 | return NULL; |
9287 | } |
9288 | // 1 or 2 argument form super(). |
9289 | if (nargs != 0) { Branch (9289:9): [True: 1.15M, False: 2.37M]
|
9290 | PyObject *arg0 = args[0]; |
9291 | if (!PyType_Check(arg0)) { Branch (9291:13): [True: 1, False: 1.15M]
|
9292 | PyErr_Format(PyExc_TypeError, |
9293 | "super() argument 1 must be a type, not %.200s", Py_TYPE(arg0)->tp_name); |
9294 | goto fail; |
9295 | } |
9296 | type = (PyTypeObject *)arg0; |
9297 | } |
9298 | if (nargs == 2) { Branch (9298:9): [True: 1.15M, False: 2.37M]
|
9299 | obj = args[1]; |
9300 | } |
9301 | if (super_init_impl(su, type, obj) < 0) { Branch (9301:9): [True: 7, False: 3.53M]
|
9302 | goto fail; |
9303 | } |
9304 | return su; |
9305 | fail: |
9306 | Py_DECREF(su); |
9307 | return NULL; |
9308 | } |
9309 | |
9310 | PyTypeObject PySuper_Type = { |
9311 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
9312 | "super", /* tp_name */ |
9313 | sizeof(superobject), /* tp_basicsize */ |
9314 | 0, /* tp_itemsize */ |
9315 | /* methods */ |
9316 | super_dealloc, /* tp_dealloc */ |
9317 | 0, /* tp_vectorcall_offset */ |
9318 | 0, /* tp_getattr */ |
9319 | 0, /* tp_setattr */ |
9320 | 0, /* tp_as_async */ |
9321 | super_repr, /* tp_repr */ |
9322 | 0, /* tp_as_number */ |
9323 | 0, /* tp_as_sequence */ |
9324 | 0, /* tp_as_mapping */ |
9325 | 0, /* tp_hash */ |
9326 | 0, /* tp_call */ |
9327 | 0, /* tp_str */ |
9328 | super_getattro, /* tp_getattro */ |
9329 | 0, /* tp_setattro */ |
9330 | 0, /* tp_as_buffer */ |
9331 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
9332 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
9333 | super_doc, /* tp_doc */ |
9334 | super_traverse, /* tp_traverse */ |
9335 | 0, /* tp_clear */ |
9336 | 0, /* tp_richcompare */ |
9337 | 0, /* tp_weaklistoffset */ |
9338 | 0, /* tp_iter */ |
9339 | 0, /* tp_iternext */ |
9340 | 0, /* tp_methods */ |
9341 | super_members, /* tp_members */ |
9342 | 0, /* tp_getset */ |
9343 | 0, /* tp_base */ |
9344 | 0, /* tp_dict */ |
9345 | super_descr_get, /* tp_descr_get */ |
9346 | 0, /* tp_descr_set */ |
9347 | 0, /* tp_dictoffset */ |
9348 | super_init, /* tp_init */ |
9349 | PyType_GenericAlloc, /* tp_alloc */ |
9350 | PyType_GenericNew, /* tp_new */ |
9351 | PyObject_GC_Del, /* tp_free */ |
9352 | .tp_vectorcall = (vectorcallfunc)super_vectorcall, |
9353 | }; |