Line data Source code
1 : /* Type object implementation */
2 :
3 : #include "Python.h"
4 : #include "structmember.h"
5 :
6 : #include <ctype.h>
7 :
8 :
9 : /* Support type attribute cache */
10 :
11 : /* The cache can keep references to the names alive for longer than
12 : they normally would. This is why the maximum size is limited to
13 : MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
14 : strings are used as attribute names. */
15 : #define MCACHE_MAX_ATTR_SIZE 100
16 : #define MCACHE_SIZE_EXP 12
17 : #define MCACHE_HASH(version, name_hash) \
18 : (((unsigned int)(version) ^ (unsigned int)(name_hash)) \
19 : & ((1 << MCACHE_SIZE_EXP) - 1))
20 :
21 : #define MCACHE_HASH_METHOD(type, name) \
22 : MCACHE_HASH((type)->tp_version_tag, \
23 : ((PyStringObject *)(name))->ob_shash)
24 : #define MCACHE_CACHEABLE_NAME(name) \
25 : PyString_CheckExact(name) && \
26 : PyString_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
27 :
28 : struct method_cache_entry {
29 : unsigned int version;
30 : PyObject *name; /* reference to exactly a str or None */
31 : PyObject *value; /* borrowed */
32 : };
33 :
34 : static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
35 : static unsigned int next_version_tag = 0;
36 :
37 : #define MCACHE_STATS 0
38 :
39 : #if MCACHE_STATS
40 : static size_t method_cache_hits = 0;
41 : static size_t method_cache_misses = 0;
42 : static size_t method_cache_collisions = 0;
43 : #endif
44 :
45 : unsigned int
46 3 : PyType_ClearCache(void)
47 : {
48 : Py_ssize_t i;
49 3 : unsigned int cur_version_tag = next_version_tag - 1;
50 :
51 : #if MCACHE_STATS
52 : size_t total = method_cache_hits + method_cache_collisions + method_cache_misses;
53 : fprintf(stderr, "-- Method cache hits = %zd (%d%%)\n",
54 : method_cache_hits, (int) (100.0 * method_cache_hits / total));
55 : fprintf(stderr, "-- Method cache true misses = %zd (%d%%)\n",
56 : method_cache_misses, (int) (100.0 * method_cache_misses / total));
57 : fprintf(stderr, "-- Method cache collisions = %zd (%d%%)\n",
58 : method_cache_collisions, (int) (100.0 * method_cache_collisions / total));
59 : fprintf(stderr, "-- Method cache size = %zd KB\n",
60 : sizeof(method_cache) / 1024);
61 : #endif
62 :
63 12291 : for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
64 12288 : method_cache[i].version = 0;
65 12288 : Py_CLEAR(method_cache[i].name);
66 12288 : method_cache[i].value = NULL;
67 : }
68 3 : next_version_tag = 0;
69 : /* mark all version tags as invalid */
70 3 : PyType_Modified(&PyBaseObject_Type);
71 3 : return cur_version_tag;
72 : }
73 :
74 : void
75 3813 : PyType_Modified(PyTypeObject *type)
76 : {
77 : /* Invalidate any cached data for the specified type and all
78 : subclasses. This function is called after the base
79 : classes, mro, or attributes of the type are altered.
80 :
81 : Invariants:
82 :
83 : - Py_TPFLAGS_VALID_VERSION_TAG is never set if
84 : Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
85 : objects coming from non-recompiled extension modules)
86 :
87 : - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
88 : it must first be set on all super types.
89 :
90 : This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
91 : type (so it must first clear it on all subclasses). The
92 : tp_version_tag value is meaningless unless this flag is set.
93 : We don't assign new version tags eagerly, but only as
94 : needed.
95 : */
96 : PyObject *raw, *ref;
97 : Py_ssize_t i, n;
98 :
99 3813 : if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
100 5943 : return;
101 :
102 1683 : raw = type->tp_subclasses;
103 1683 : if (raw != NULL) {
104 174 : n = PyList_GET_SIZE(raw);
105 1395 : for (i = 0; i < n; i++) {
106 1221 : ref = PyList_GET_ITEM(raw, i);
107 1221 : ref = PyWeakref_GET_OBJECT(ref);
108 1221 : if (ref != Py_None) {
109 1221 : PyType_Modified((PyTypeObject *)ref);
110 : }
111 : }
112 : }
113 1683 : type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
114 : }
115 :
116 : static void
117 2592 : type_mro_modified(PyTypeObject *type, PyObject *bases) {
118 : /*
119 : Check that all base classes or elements of the mro of type are
120 : able to be cached. This function is called after the base
121 : classes or mro of the type are altered.
122 :
123 : Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
124 : inherits from an old-style class, either directly or if it
125 : appears in the MRO of a new-style class. No support either for
126 : custom MROs that include types that are not officially super
127 : types.
128 :
129 : Called from mro_internal, which will subsequently be called on
130 : each subclass when their mro is recursively updated.
131 : */
132 : Py_ssize_t i, n;
133 2592 : int clear = 0;
134 :
135 2592 : if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
136 2925 : return;
137 :
138 2259 : n = PyTuple_GET_SIZE(bases);
139 7422 : for (i = 0; i < n; i++) {
140 5184 : PyObject *b = PyTuple_GET_ITEM(bases, i);
141 : PyTypeObject *cls;
142 :
143 5184 : if (!PyType_Check(b) ) {
144 0 : clear = 1;
145 0 : break;
146 : }
147 :
148 5184 : cls = (PyTypeObject *)b;
149 :
150 10347 : if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
151 5163 : !PyType_IsSubtype(type, cls)) {
152 21 : clear = 1;
153 21 : break;
154 : }
155 : }
156 :
157 2259 : if (clear)
158 21 : type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
159 : Py_TPFLAGS_VALID_VERSION_TAG);
160 : }
161 :
162 : static int
163 78585 : assign_version_tag(PyTypeObject *type)
164 : {
165 : /* Ensure that the tp_version_tag is valid and set
166 : Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
167 : must first be done on all super classes. Return 0 if this
168 : cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
169 : */
170 : Py_ssize_t i, n;
171 : PyObject *bases;
172 :
173 78585 : if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
174 74517 : return 1;
175 4068 : if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
176 2370 : return 0;
177 1698 : if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
178 0 : return 0;
179 :
180 1698 : type->tp_version_tag = next_version_tag++;
181 : /* for stress-testing: next_version_tag &= 0xFF; */
182 :
183 1698 : if (type->tp_version_tag == 0) {
184 : /* wrap-around or just starting Python - clear the whole
185 : cache by filling names with references to Py_None.
186 : Values are also set to NULL for added protection, as they
187 : are borrowed reference */
188 24582 : for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
189 24576 : method_cache[i].value = NULL;
190 24576 : Py_INCREF(Py_None);
191 24576 : Py_XSETREF(method_cache[i].name, Py_None);
192 : }
193 : /* mark all version tags as invalid */
194 6 : PyType_Modified(&PyBaseObject_Type);
195 6 : return 1;
196 : }
197 1692 : bases = type->tp_bases;
198 1692 : n = PyTuple_GET_SIZE(bases);
199 3456 : for (i = 0; i < n; i++) {
200 1764 : PyObject *b = PyTuple_GET_ITEM(bases, i);
201 : assert(PyType_Check(b));
202 1764 : if (!assign_version_tag((PyTypeObject *)b))
203 0 : return 0;
204 : }
205 1692 : type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
206 1692 : return 1;
207 : }
208 :
209 :
210 : static PyMemberDef type_members[] = {
211 : {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY},
212 : {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY},
213 : {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
214 : {"__weakrefoffset__", T_LONG,
215 : offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
216 : {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
217 : {"__dictoffset__", T_LONG,
218 : offsetof(PyTypeObject, tp_dictoffset), READONLY},
219 : {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
220 : {0}
221 : };
222 :
223 : static PyObject *
224 0 : type_name(PyTypeObject *type, void *context)
225 : {
226 : const char *s;
227 :
228 0 : if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
229 0 : PyHeapTypeObject* et = (PyHeapTypeObject*)type;
230 :
231 0 : Py_INCREF(et->ht_name);
232 0 : return et->ht_name;
233 : }
234 : else {
235 0 : s = strrchr(type->tp_name, '.');
236 0 : if (s == NULL)
237 0 : s = type->tp_name;
238 : else
239 0 : s++;
240 0 : return PyString_FromString(s);
241 : }
242 : }
243 :
244 : static int
245 0 : type_set_name(PyTypeObject *type, PyObject *value, void *context)
246 : {
247 : PyHeapTypeObject* et;
248 : PyObject *tmp;
249 :
250 0 : if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
251 0 : PyErr_Format(PyExc_TypeError,
252 : "can't set %s.__name__", type->tp_name);
253 0 : return -1;
254 : }
255 0 : if (!value) {
256 0 : PyErr_Format(PyExc_TypeError,
257 : "can't delete %s.__name__", type->tp_name);
258 0 : return -1;
259 : }
260 0 : if (!PyString_Check(value)) {
261 0 : PyErr_Format(PyExc_TypeError,
262 : "can only assign string to %s.__name__, not '%s'",
263 0 : type->tp_name, Py_TYPE(value)->tp_name);
264 0 : return -1;
265 : }
266 0 : if (strlen(PyString_AS_STRING(value))
267 0 : != (size_t)PyString_GET_SIZE(value)) {
268 0 : PyErr_SetString(PyExc_ValueError,
269 : "type name must not contain null characters");
270 0 : return -1;
271 : }
272 :
273 0 : et = (PyHeapTypeObject*)type;
274 :
275 0 : Py_INCREF(value);
276 :
277 : /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name
278 : value. (Bug #16447.) */
279 0 : tmp = et->ht_name;
280 0 : et->ht_name = value;
281 :
282 0 : type->tp_name = PyString_AS_STRING(value);
283 0 : Py_DECREF(tmp);
284 :
285 0 : return 0;
286 : }
287 :
288 : static PyObject *
289 0 : type_module(PyTypeObject *type, void *context)
290 : {
291 : PyObject *mod;
292 : char *s;
293 :
294 0 : if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
295 0 : mod = PyDict_GetItemString(type->tp_dict, "__module__");
296 0 : if (!mod) {
297 0 : PyErr_Format(PyExc_AttributeError, "__module__");
298 0 : return 0;
299 : }
300 0 : Py_XINCREF(mod);
301 0 : return mod;
302 : }
303 : else {
304 0 : s = strrchr(type->tp_name, '.');
305 0 : if (s != NULL)
306 0 : return PyString_FromStringAndSize(
307 0 : type->tp_name, (Py_ssize_t)(s - type->tp_name));
308 0 : return PyString_FromString("__builtin__");
309 : }
310 : }
311 :
312 : static int
313 27 : type_set_module(PyTypeObject *type, PyObject *value, void *context)
314 : {
315 27 : if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
316 0 : PyErr_Format(PyExc_TypeError,
317 : "can't set %s.__module__", type->tp_name);
318 0 : return -1;
319 : }
320 27 : if (!value) {
321 0 : PyErr_Format(PyExc_TypeError,
322 : "can't delete %s.__module__", type->tp_name);
323 0 : return -1;
324 : }
325 :
326 27 : PyType_Modified(type);
327 :
328 27 : return PyDict_SetItemString(type->tp_dict, "__module__", value);
329 : }
330 :
331 : static PyObject *
332 78 : type_abstractmethods(PyTypeObject *type, void *context)
333 : {
334 78 : PyObject *mod = NULL;
335 : /* type itself has an __abstractmethods__ descriptor (this). Don't return
336 : that. */
337 78 : if (type != &PyType_Type)
338 78 : mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__");
339 78 : if (!mod) {
340 12 : PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
341 12 : return NULL;
342 : }
343 66 : Py_XINCREF(mod);
344 66 : return mod;
345 : }
346 :
347 : static int
348 60 : type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
349 : {
350 : /* __abstractmethods__ should only be set once on a type, in
351 : abc.ABCMeta.__new__, so this function doesn't do anything
352 : special to update subclasses.
353 : */
354 : int abstract, res;
355 60 : if (value != NULL) {
356 60 : abstract = PyObject_IsTrue(value);
357 60 : if (abstract < 0)
358 0 : return -1;
359 60 : res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);
360 : }
361 : else {
362 0 : abstract = 0;
363 0 : res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");
364 0 : if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
365 0 : PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
366 0 : return -1;
367 : }
368 : }
369 60 : if (res == 0) {
370 60 : PyType_Modified(type);
371 60 : if (abstract)
372 36 : type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
373 : else
374 24 : type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
375 : }
376 60 : return res;
377 : }
378 :
379 : static PyObject *
380 30 : type_get_bases(PyTypeObject *type, void *context)
381 : {
382 30 : Py_INCREF(type->tp_bases);
383 30 : return type->tp_bases;
384 : }
385 :
386 : static PyTypeObject *best_base(PyObject *);
387 : static int mro_internal(PyTypeObject *);
388 : static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
389 : static int add_subclass(PyTypeObject*, PyTypeObject*);
390 : static void remove_subclass(PyTypeObject *, PyTypeObject *);
391 : static void update_all_slots(PyTypeObject *);
392 :
393 : typedef int (*update_callback)(PyTypeObject *, void *);
394 : static int update_subclasses(PyTypeObject *type, PyObject *name,
395 : update_callback callback, void *data);
396 : static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
397 : update_callback callback, void *data);
398 :
399 : static int
400 0 : mro_subclasses(PyTypeObject *type, PyObject* temp)
401 : {
402 : PyTypeObject *subclass;
403 : PyObject *ref, *subclasses, *old_mro;
404 : Py_ssize_t i, n;
405 :
406 0 : subclasses = type->tp_subclasses;
407 0 : if (subclasses == NULL)
408 0 : return 0;
409 : assert(PyList_Check(subclasses));
410 0 : n = PyList_GET_SIZE(subclasses);
411 0 : for (i = 0; i < n; i++) {
412 0 : ref = PyList_GET_ITEM(subclasses, i);
413 : assert(PyWeakref_CheckRef(ref));
414 0 : subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
415 : assert(subclass != NULL);
416 0 : if ((PyObject *)subclass == Py_None)
417 0 : continue;
418 : assert(PyType_Check(subclass));
419 0 : old_mro = subclass->tp_mro;
420 0 : if (mro_internal(subclass) < 0) {
421 0 : subclass->tp_mro = old_mro;
422 0 : return -1;
423 : }
424 : else {
425 : PyObject* tuple;
426 0 : tuple = PyTuple_Pack(2, subclass, old_mro);
427 0 : Py_DECREF(old_mro);
428 0 : if (!tuple)
429 0 : return -1;
430 0 : if (PyList_Append(temp, tuple) < 0)
431 0 : return -1;
432 0 : Py_DECREF(tuple);
433 : }
434 0 : if (mro_subclasses(subclass, temp) < 0)
435 0 : return -1;
436 : }
437 0 : return 0;
438 : }
439 :
440 : static int
441 0 : type_set_bases(PyTypeObject *type, PyObject *value, void *context)
442 : {
443 : Py_ssize_t i;
444 0 : int r = 0;
445 : PyObject *ob, *temp;
446 : PyTypeObject *new_base, *old_base;
447 : PyObject *old_bases, *old_mro;
448 :
449 0 : if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
450 0 : PyErr_Format(PyExc_TypeError,
451 : "can't set %s.__bases__", type->tp_name);
452 0 : return -1;
453 : }
454 0 : if (!value) {
455 0 : PyErr_Format(PyExc_TypeError,
456 : "can't delete %s.__bases__", type->tp_name);
457 0 : return -1;
458 : }
459 0 : if (!PyTuple_Check(value)) {
460 0 : PyErr_Format(PyExc_TypeError,
461 : "can only assign tuple to %s.__bases__, not %s",
462 0 : type->tp_name, Py_TYPE(value)->tp_name);
463 0 : return -1;
464 : }
465 0 : if (PyTuple_GET_SIZE(value) == 0) {
466 0 : PyErr_Format(PyExc_TypeError,
467 : "can only assign non-empty tuple to %s.__bases__, not ()",
468 : type->tp_name);
469 0 : return -1;
470 : }
471 0 : for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
472 0 : ob = PyTuple_GET_ITEM(value, i);
473 0 : if (!PyClass_Check(ob) && !PyType_Check(ob)) {
474 0 : PyErr_Format(
475 : PyExc_TypeError,
476 : "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
477 0 : type->tp_name, Py_TYPE(ob)->tp_name);
478 0 : return -1;
479 : }
480 0 : if (PyType_Check(ob)) {
481 0 : if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
482 0 : PyErr_SetString(PyExc_TypeError,
483 : "a __bases__ item causes an inheritance cycle");
484 0 : return -1;
485 : }
486 : }
487 : }
488 :
489 0 : new_base = best_base(value);
490 :
491 0 : if (!new_base) {
492 0 : return -1;
493 : }
494 :
495 0 : if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
496 0 : return -1;
497 :
498 0 : Py_INCREF(new_base);
499 0 : Py_INCREF(value);
500 :
501 0 : old_bases = type->tp_bases;
502 0 : old_base = type->tp_base;
503 0 : old_mro = type->tp_mro;
504 :
505 0 : type->tp_bases = value;
506 0 : type->tp_base = new_base;
507 :
508 0 : if (mro_internal(type) < 0) {
509 0 : goto bail;
510 : }
511 :
512 0 : temp = PyList_New(0);
513 0 : if (!temp)
514 0 : goto bail;
515 :
516 0 : r = mro_subclasses(type, temp);
517 :
518 0 : if (r < 0) {
519 0 : for (i = 0; i < PyList_Size(temp); i++) {
520 : PyTypeObject* cls;
521 : PyObject* mro;
522 0 : PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
523 : "", 2, 2, &cls, &mro);
524 0 : Py_INCREF(mro);
525 0 : ob = cls->tp_mro;
526 0 : cls->tp_mro = mro;
527 0 : Py_DECREF(ob);
528 : }
529 0 : Py_DECREF(temp);
530 0 : goto bail;
531 : }
532 :
533 0 : Py_DECREF(temp);
534 :
535 : /* any base that was in __bases__ but now isn't, we
536 : need to remove |type| from its tp_subclasses.
537 : conversely, any class now in __bases__ that wasn't
538 : needs to have |type| added to its subclasses. */
539 :
540 : /* for now, sod that: just remove from all old_bases,
541 : add to all new_bases */
542 :
543 0 : for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
544 0 : ob = PyTuple_GET_ITEM(old_bases, i);
545 0 : if (PyType_Check(ob)) {
546 0 : remove_subclass(
547 : (PyTypeObject*)ob, type);
548 : }
549 : }
550 :
551 0 : for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
552 0 : ob = PyTuple_GET_ITEM(value, i);
553 0 : if (PyType_Check(ob)) {
554 0 : if (add_subclass((PyTypeObject*)ob, type) < 0)
555 0 : r = -1;
556 : }
557 : }
558 :
559 0 : update_all_slots(type);
560 :
561 0 : Py_DECREF(old_bases);
562 0 : Py_DECREF(old_base);
563 0 : Py_DECREF(old_mro);
564 :
565 0 : return r;
566 :
567 : bail:
568 0 : Py_DECREF(type->tp_bases);
569 0 : Py_DECREF(type->tp_base);
570 0 : if (type->tp_mro != old_mro) {
571 0 : Py_DECREF(type->tp_mro);
572 : }
573 :
574 0 : type->tp_bases = old_bases;
575 0 : type->tp_base = old_base;
576 0 : type->tp_mro = old_mro;
577 :
578 0 : return -1;
579 : }
580 :
581 : static PyObject *
582 12 : type_dict(PyTypeObject *type, void *context)
583 : {
584 12 : if (type->tp_dict == NULL) {
585 0 : Py_INCREF(Py_None);
586 0 : return Py_None;
587 : }
588 12 : return PyDictProxy_New(type->tp_dict);
589 : }
590 :
591 : static PyObject *
592 12 : type_get_doc(PyTypeObject *type, void *context)
593 : {
594 : PyObject *result;
595 12 : if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
596 12 : return PyString_FromString(type->tp_doc);
597 0 : result = PyDict_GetItemString(type->tp_dict, "__doc__");
598 0 : if (result == NULL) {
599 0 : result = Py_None;
600 0 : Py_INCREF(result);
601 : }
602 0 : else if (Py_TYPE(result)->tp_descr_get) {
603 0 : result = Py_TYPE(result)->tp_descr_get(result, NULL,
604 : (PyObject *)type);
605 : }
606 : else {
607 0 : Py_INCREF(result);
608 : }
609 0 : return result;
610 : }
611 :
612 : static PyObject *
613 7102 : type___instancecheck__(PyObject *type, PyObject *inst)
614 : {
615 7102 : switch (_PyObject_RealIsInstance(inst, type)) {
616 : case -1:
617 0 : return NULL;
618 : case 0:
619 7063 : Py_RETURN_FALSE;
620 : default:
621 39 : Py_RETURN_TRUE;
622 : }
623 : }
624 :
625 :
626 : static PyObject *
627 10031 : type___subclasscheck__(PyObject *type, PyObject *inst)
628 : {
629 10031 : switch (_PyObject_RealIsSubclass(inst, type)) {
630 : case -1:
631 0 : return NULL;
632 : case 0:
633 126 : Py_RETURN_FALSE;
634 : default:
635 9905 : Py_RETURN_TRUE;
636 : }
637 : }
638 :
639 :
640 : static PyGetSetDef type_getsets[] = {
641 : {"__name__", (getter)type_name, (setter)type_set_name, NULL},
642 : {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
643 : {"__module__", (getter)type_module, (setter)type_set_module, NULL},
644 : {"__abstractmethods__", (getter)type_abstractmethods,
645 : (setter)type_set_abstractmethods, NULL},
646 : {"__dict__", (getter)type_dict, NULL, NULL},
647 : {"__doc__", (getter)type_get_doc, NULL, NULL},
648 : {0}
649 : };
650 :
651 :
652 : static PyObject*
653 315 : type_richcompare(PyObject *v, PyObject *w, int op)
654 : {
655 : PyObject *result;
656 : Py_uintptr_t vv, ww;
657 : int c;
658 :
659 : /* Make sure both arguments are types. */
660 630 : if (!PyType_Check(v) || !PyType_Check(w) ||
661 : /* If there is a __cmp__ method defined, let it be called instead
662 : of our dumb function designed merely to warn. See bug
663 : #7491. */
664 630 : Py_TYPE(v)->tp_compare || Py_TYPE(w)->tp_compare) {
665 0 : result = Py_NotImplemented;
666 0 : goto out;
667 : }
668 :
669 : /* Py3K warning if comparison isn't == or != */
670 315 : if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&
671 0 : PyErr_WarnEx(PyExc_DeprecationWarning,
672 : "type inequality comparisons not supported "
673 : "in 3.x", 1) < 0) {
674 0 : return NULL;
675 : }
676 :
677 : /* Compare addresses */
678 315 : vv = (Py_uintptr_t)v;
679 315 : ww = (Py_uintptr_t)w;
680 315 : switch (op) {
681 0 : case Py_LT: c = vv < ww; break;
682 0 : case Py_LE: c = vv <= ww; break;
683 174 : case Py_EQ: c = vv == ww; break;
684 141 : case Py_NE: c = vv != ww; break;
685 0 : case Py_GT: c = vv > ww; break;
686 0 : case Py_GE: c = vv >= ww; break;
687 : default:
688 0 : result = Py_NotImplemented;
689 0 : goto out;
690 : }
691 315 : result = c ? Py_True : Py_False;
692 :
693 : /* incref and return */
694 : out:
695 315 : Py_INCREF(result);
696 315 : return result;
697 : }
698 :
699 : static PyObject *
700 0 : type_repr(PyTypeObject *type)
701 : {
702 : PyObject *mod, *name, *rtn;
703 : char *kind;
704 :
705 0 : mod = type_module(type, NULL);
706 0 : if (mod == NULL)
707 0 : PyErr_Clear();
708 0 : else if (!PyString_Check(mod)) {
709 0 : Py_DECREF(mod);
710 0 : mod = NULL;
711 : }
712 0 : name = type_name(type, NULL);
713 0 : if (name == NULL) {
714 0 : Py_XDECREF(mod);
715 0 : return NULL;
716 : }
717 :
718 0 : if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
719 0 : kind = "class";
720 : else
721 0 : kind = "type";
722 :
723 0 : if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
724 0 : rtn = PyString_FromFormat("<%s '%s.%s'>",
725 : kind,
726 0 : PyString_AS_STRING(mod),
727 0 : PyString_AS_STRING(name));
728 : }
729 : else
730 0 : rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
731 :
732 0 : Py_XDECREF(mod);
733 0 : Py_DECREF(name);
734 0 : return rtn;
735 : }
736 :
737 : static PyObject *
738 18528 : type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
739 : {
740 : PyObject *obj;
741 :
742 18528 : if (type->tp_new == NULL) {
743 0 : PyErr_Format(PyExc_TypeError,
744 : "cannot create '%.100s' instances",
745 : type->tp_name);
746 0 : return NULL;
747 : }
748 :
749 18528 : obj = type->tp_new(type, args, kwds);
750 18528 : if (obj != NULL) {
751 : /* Ugly exception: when the call was type(something),
752 : don't call tp_init on the result. */
753 21966 : if (type == &PyType_Type &&
754 6876 : PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
755 0 : (kwds == NULL ||
756 0 : (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
757 2703 : return obj;
758 : /* If the returned object is not an instance of type,
759 : it won't be initialized. */
760 15825 : if (!PyType_IsSubtype(obj->ob_type, type))
761 3 : return obj;
762 15822 : type = obj->ob_type;
763 31644 : if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
764 31644 : type->tp_init != NULL &&
765 15822 : type->tp_init(obj, args, kwds) < 0) {
766 78 : Py_DECREF(obj);
767 78 : obj = NULL;
768 : }
769 : }
770 15822 : return obj;
771 : }
772 :
773 : PyObject *
774 16494 : PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
775 : {
776 : PyObject *obj;
777 16494 : const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
778 : /* note that we need to add one, for the sentinel */
779 :
780 16494 : if (PyType_IS_GC(type))
781 15783 : obj = _PyObject_GC_Malloc(size);
782 : else
783 711 : obj = (PyObject *)PyObject_MALLOC(size);
784 :
785 16494 : if (obj == NULL)
786 0 : return PyErr_NoMemory();
787 :
788 16494 : memset(obj, '\0', size);
789 :
790 16494 : if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
791 4233 : Py_INCREF(type);
792 :
793 16494 : if (type->tp_itemsize == 0)
794 13290 : (void)PyObject_INIT(obj, type);
795 : else
796 3204 : (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
797 :
798 16494 : if (PyType_IS_GC(type))
799 15783 : _PyObject_GC_TRACK(obj);
800 16494 : return obj;
801 : }
802 :
803 : PyObject *
804 2037 : PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
805 : {
806 2037 : return type->tp_alloc(type, 0);
807 : }
808 :
809 : /* Helpers for subtyping */
810 :
811 : static int
812 0 : traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
813 : {
814 : Py_ssize_t i, n;
815 : PyMemberDef *mp;
816 :
817 0 : n = Py_SIZE(type);
818 0 : mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
819 0 : for (i = 0; i < n; i++, mp++) {
820 0 : if (mp->type == T_OBJECT_EX) {
821 0 : char *addr = (char *)self + mp->offset;
822 0 : PyObject *obj = *(PyObject **)addr;
823 0 : if (obj != NULL) {
824 0 : int err = visit(obj, arg);
825 0 : if (err)
826 0 : return err;
827 : }
828 : }
829 : }
830 0 : return 0;
831 : }
832 :
833 : static int
834 8949 : subtype_traverse(PyObject *self, visitproc visit, void *arg)
835 : {
836 : PyTypeObject *type, *base;
837 : traverseproc basetraverse;
838 :
839 : /* Find the nearest base with a different tp_traverse,
840 : and traverse slots while we're at it */
841 8949 : type = Py_TYPE(self);
842 8949 : base = type;
843 27021 : while ((basetraverse = base->tp_traverse) == subtype_traverse) {
844 9123 : if (Py_SIZE(base)) {
845 0 : int err = traverse_slots(base, self, visit, arg);
846 0 : if (err)
847 0 : return err;
848 : }
849 9123 : base = base->tp_base;
850 : assert(base);
851 : }
852 :
853 8949 : if (type->tp_dictoffset != base->tp_dictoffset) {
854 8565 : PyObject **dictptr = _PyObject_GetDictPtr(self);
855 8565 : if (dictptr && *dictptr)
856 8485 : Py_VISIT(*dictptr);
857 : }
858 :
859 8949 : if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
860 : /* For a heaptype, the instances count as references
861 : to the type. Traverse the type so the collector
862 : can find cycles involving this link. */
863 8949 : Py_VISIT(type);
864 :
865 8949 : if (basetraverse)
866 402 : return basetraverse(self, visit, arg);
867 8547 : return 0;
868 : }
869 :
870 : static void
871 0 : clear_slots(PyTypeObject *type, PyObject *self)
872 : {
873 : Py_ssize_t i, n;
874 : PyMemberDef *mp;
875 :
876 0 : n = Py_SIZE(type);
877 0 : mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
878 0 : for (i = 0; i < n; i++, mp++) {
879 0 : if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
880 0 : char *addr = (char *)self + mp->offset;
881 0 : PyObject *obj = *(PyObject **)addr;
882 0 : if (obj != NULL) {
883 0 : *(PyObject **)addr = NULL;
884 0 : Py_DECREF(obj);
885 : }
886 : }
887 : }
888 0 : }
889 :
890 : static int
891 24 : subtype_clear(PyObject *self)
892 : {
893 : PyTypeObject *type, *base;
894 : inquiry baseclear;
895 :
896 : /* Find the nearest base with a different tp_clear
897 : and clear slots while we're at it */
898 24 : type = Py_TYPE(self);
899 24 : base = type;
900 78 : while ((baseclear = base->tp_clear) == subtype_clear) {
901 30 : if (Py_SIZE(base))
902 0 : clear_slots(base, self);
903 30 : base = base->tp_base;
904 : assert(base);
905 : }
906 :
907 : /* Clear the instance dict (if any), to break cycles involving only
908 : __dict__ slots (as in the case 'self.__dict__ is self'). */
909 24 : if (type->tp_dictoffset != base->tp_dictoffset) {
910 24 : PyObject **dictptr = _PyObject_GetDictPtr(self);
911 24 : if (dictptr && *dictptr)
912 24 : Py_CLEAR(*dictptr);
913 : }
914 :
915 24 : if (baseclear)
916 0 : return baseclear(self);
917 24 : return 0;
918 : }
919 :
920 : static void
921 3309 : subtype_dealloc(PyObject *self)
922 : {
923 : PyTypeObject *type, *base;
924 : destructor basedealloc;
925 3309 : PyThreadState *tstate = PyThreadState_GET();
926 :
927 : /* Extract the type; we expect it to be a heap type */
928 3309 : type = Py_TYPE(self);
929 : assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
930 :
931 : /* Test whether the type has GC exactly once */
932 :
933 3309 : if (!PyType_IS_GC(type)) {
934 : /* It's really rare to find a dynamic type that doesn't have
935 : GC; it can only happen when deriving from 'object' and not
936 : adding any slots or instance variables. This allows
937 : certain simplifications: there's no need to call
938 : clear_slots(), or DECREF the dict, or clear weakrefs. */
939 :
940 : /* Maybe call finalizer; exit early if resurrected */
941 0 : if (type->tp_del) {
942 0 : type->tp_del(self);
943 0 : if (self->ob_refcnt > 0)
944 0 : return;
945 : }
946 :
947 : /* Find the nearest base with a different tp_dealloc */
948 0 : base = type;
949 0 : while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
950 : assert(Py_SIZE(base) == 0);
951 0 : base = base->tp_base;
952 : assert(base);
953 : }
954 :
955 : /* Extract the type again; tp_del may have changed it */
956 0 : type = Py_TYPE(self);
957 :
958 : /* Call the base tp_dealloc() */
959 : assert(basedealloc);
960 0 : basedealloc(self);
961 :
962 : /* Can't reference self beyond this point */
963 0 : Py_DECREF(type);
964 :
965 : /* Done */
966 0 : return;
967 : }
968 :
969 : /* We get here only if the type has GC */
970 :
971 : /* UnTrack and re-Track around the trashcan macro, alas */
972 : /* See explanation at end of function for full disclosure */
973 3309 : PyObject_GC_UnTrack(self);
974 3309 : ++_PyTrash_delete_nesting;
975 3309 : ++ tstate->trash_delete_nesting;
976 3309 : Py_TRASHCAN_SAFE_BEGIN(self);
977 3309 : --_PyTrash_delete_nesting;
978 3309 : -- tstate->trash_delete_nesting;
979 : /* DO NOT restore GC tracking at this point. weakref callbacks
980 : * (if any, and whether directly here or indirectly in something we
981 : * call) may trigger GC, and if self is tracked at that point, it
982 : * will look like trash to GC and GC will try to delete self again.
983 : */
984 :
985 : /* Find the nearest base with a different tp_dealloc */
986 3309 : base = type;
987 10326 : while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
988 3708 : base = base->tp_base;
989 : assert(base);
990 : }
991 :
992 : /* If we added a weaklist, we clear it. Do this *before* calling
993 : the finalizer (__del__), clearing slots, or clearing the instance
994 : dict. */
995 :
996 3309 : if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
997 894 : PyObject_ClearWeakRefs(self);
998 :
999 : /* Maybe call finalizer; exit early if resurrected */
1000 3309 : if (type->tp_del) {
1001 0 : _PyObject_GC_TRACK(self);
1002 0 : type->tp_del(self);
1003 0 : if (self->ob_refcnt > 0)
1004 0 : goto endlabel; /* resurrected */
1005 : else
1006 0 : _PyObject_GC_UNTRACK(self);
1007 : /* New weakrefs could be created during the finalizer call.
1008 : If this occurs, clear them out without calling their
1009 : finalizers since they might rely on part of the object
1010 : being finalized that has already been destroyed. */
1011 0 : if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
1012 : /* Modeled after GET_WEAKREFS_LISTPTR() */
1013 0 : PyWeakReference **list = (PyWeakReference **) \
1014 0 : PyObject_GET_WEAKREFS_LISTPTR(self);
1015 0 : while (*list)
1016 0 : _PyWeakref_ClearRef(*list);
1017 : }
1018 : }
1019 :
1020 : /* Clear slots up to the nearest base with a different tp_dealloc */
1021 3309 : base = type;
1022 10326 : while (base->tp_dealloc == subtype_dealloc) {
1023 3708 : if (Py_SIZE(base))
1024 0 : clear_slots(base, self);
1025 3708 : base = base->tp_base;
1026 : assert(base);
1027 : }
1028 :
1029 : /* If we added a dict, DECREF it */
1030 3309 : if (type->tp_dictoffset && !base->tp_dictoffset) {
1031 897 : PyObject **dictptr = _PyObject_GetDictPtr(self);
1032 897 : if (dictptr != NULL) {
1033 897 : PyObject *dict = *dictptr;
1034 897 : if (dict != NULL) {
1035 861 : Py_DECREF(dict);
1036 861 : *dictptr = NULL;
1037 : }
1038 : }
1039 : }
1040 :
1041 : /* Extract the type again; tp_del may have changed it */
1042 3309 : type = Py_TYPE(self);
1043 :
1044 : /* Call the base tp_dealloc(); first retrack self if
1045 : * basedealloc knows about gc.
1046 : */
1047 3309 : if (PyType_IS_GC(base))
1048 2415 : _PyObject_GC_TRACK(self);
1049 : assert(basedealloc);
1050 3309 : basedealloc(self);
1051 :
1052 : /* Can't reference self beyond this point */
1053 3309 : Py_DECREF(type);
1054 :
1055 : endlabel:
1056 3309 : ++_PyTrash_delete_nesting;
1057 3309 : ++ tstate->trash_delete_nesting;
1058 3309 : Py_TRASHCAN_SAFE_END(self);
1059 3309 : --_PyTrash_delete_nesting;
1060 3309 : -- tstate->trash_delete_nesting;
1061 :
1062 : /* Explanation of the weirdness around the trashcan macros:
1063 :
1064 : Q. What do the trashcan macros do?
1065 :
1066 : A. Read the comment titled "Trashcan mechanism" in object.h.
1067 : For one, this explains why there must be a call to GC-untrack
1068 : before the trashcan begin macro. Without understanding the
1069 : trashcan code, the answers to the following questions don't make
1070 : sense.
1071 :
1072 : Q. Why do we GC-untrack before the trashcan and then immediately
1073 : GC-track again afterward?
1074 :
1075 : A. In the case that the base class is GC-aware, the base class
1076 : probably GC-untracks the object. If it does that using the
1077 : UNTRACK macro, this will crash when the object is already
1078 : untracked. Because we don't know what the base class does, the
1079 : only safe thing is to make sure the object is tracked when we
1080 : call the base class dealloc. But... The trashcan begin macro
1081 : requires that the object is *untracked* before it is called. So
1082 : the dance becomes:
1083 :
1084 : GC untrack
1085 : trashcan begin
1086 : GC track
1087 :
1088 : Q. Why did the last question say "immediately GC-track again"?
1089 : It's nowhere near immediately.
1090 :
1091 : A. Because the code *used* to re-track immediately. Bad Idea.
1092 : self has a refcount of 0, and if gc ever gets its hands on it
1093 : (which can happen if any weakref callback gets invoked), it
1094 : looks like trash to gc too, and gc also tries to delete self
1095 : then. But we're already deleting self. Double deallocation is
1096 : a subtle disaster.
1097 :
1098 : Q. Why the bizarre (net-zero) manipulation of
1099 : _PyTrash_delete_nesting around the trashcan macros?
1100 :
1101 : A. Some base classes (e.g. list) also use the trashcan mechanism.
1102 : The following scenario used to be possible:
1103 :
1104 : - suppose the trashcan level is one below the trashcan limit
1105 :
1106 : - subtype_dealloc() is called
1107 :
1108 : - the trashcan limit is not yet reached, so the trashcan level
1109 : is incremented and the code between trashcan begin and end is
1110 : executed
1111 :
1112 : - this destroys much of the object's contents, including its
1113 : slots and __dict__
1114 :
1115 : - basedealloc() is called; this is really list_dealloc(), or
1116 : some other type which also uses the trashcan macros
1117 :
1118 : - the trashcan limit is now reached, so the object is put on the
1119 : trashcan's to-be-deleted-later list
1120 :
1121 : - basedealloc() returns
1122 :
1123 : - subtype_dealloc() decrefs the object's type
1124 :
1125 : - subtype_dealloc() returns
1126 :
1127 : - later, the trashcan code starts deleting the objects from its
1128 : to-be-deleted-later list
1129 :
1130 : - subtype_dealloc() is called *AGAIN* for the same object
1131 :
1132 : - at the very least (if the destroyed slots and __dict__ don't
1133 : cause problems) the object's type gets decref'ed a second
1134 : time, which is *BAD*!!!
1135 :
1136 : The remedy is to make sure that if the code between trashcan
1137 : begin and end in subtype_dealloc() is called, the code between
1138 : trashcan begin and end in basedealloc() will also be called.
1139 : This is done by decrementing the level after passing into the
1140 : trashcan block, and incrementing it just before leaving the
1141 : block.
1142 :
1143 : But now it's possible that a chain of objects consisting solely
1144 : of objects whose deallocator is subtype_dealloc() will defeat
1145 : the trashcan mechanism completely: the decremented level means
1146 : that the effective level never reaches the limit. Therefore, we
1147 : *increment* the level *before* entering the trashcan block, and
1148 : matchingly decrement it after leaving. This means the trashcan
1149 : code will trigger a little early, but that's no big deal.
1150 :
1151 : Q. Are there any live examples of code in need of all this
1152 : complexity?
1153 :
1154 : A. Yes. See SF bug 668433 for code that crashed (when Python was
1155 : compiled in debug mode) before the trashcan level manipulations
1156 : were added. For more discussion, see SF patches 581742, 575073
1157 : and bug 574207.
1158 : */
1159 : }
1160 :
1161 : static PyTypeObject *solid_base(PyTypeObject *type);
1162 :
1163 : /* type test with subclassing support */
1164 :
1165 : int
1166 114436 : PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
1167 : {
1168 : PyObject *mro;
1169 :
1170 114436 : if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1171 4965 : return b == a || b == &PyBaseObject_Type;
1172 :
1173 109471 : mro = a->tp_mro;
1174 109471 : if (mro != NULL) {
1175 : /* Deal with multiple inheritance without recursion
1176 : by walking the MRO tuple */
1177 : Py_ssize_t i, n;
1178 : assert(PyTuple_Check(mro));
1179 109291 : n = PyTuple_GET_SIZE(mro);
1180 261686 : for (i = 0; i < n; i++) {
1181 203306 : if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
1182 50911 : return 1;
1183 : }
1184 58380 : return 0;
1185 : }
1186 : else {
1187 : /* a is not completely initilized yet; follow tp_base */
1188 : do {
1189 546 : if (a == b)
1190 177 : return 1;
1191 369 : a = a->tp_base;
1192 369 : } while (a != NULL);
1193 3 : return b == &PyBaseObject_Type;
1194 : }
1195 : }
1196 :
1197 : /* Internal routines to do a method lookup in the type
1198 : without looking in the instance dictionary
1199 : (so we can't use PyObject_GetAttr) but still binding
1200 : it to the instance. The arguments are the object,
1201 : the method name as a C string, and the address of a
1202 : static variable used to cache the interned Python string.
1203 :
1204 : Two variants:
1205 :
1206 : - lookup_maybe() returns NULL without raising an exception
1207 : when the _PyType_Lookup() call fails;
1208 :
1209 : - lookup_method() always raises an exception upon errors.
1210 :
1211 : - _PyObject_LookupSpecial() exported for the benefit of other places.
1212 : */
1213 :
1214 : static PyObject *
1215 21670 : lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
1216 : {
1217 : PyObject *res;
1218 :
1219 21670 : if (*attrobj == NULL) {
1220 48 : *attrobj = PyString_InternFromString(attrstr);
1221 48 : if (*attrobj == NULL)
1222 0 : return NULL;
1223 : }
1224 21670 : res = _PyType_Lookup(Py_TYPE(self), *attrobj);
1225 21670 : if (res != NULL) {
1226 : descrgetfunc f;
1227 20755 : if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
1228 0 : Py_INCREF(res);
1229 : else
1230 20755 : res = f(res, self, (PyObject *)(Py_TYPE(self)));
1231 : }
1232 21670 : return res;
1233 : }
1234 :
1235 : static PyObject *
1236 3910 : lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
1237 : {
1238 3910 : PyObject *res = lookup_maybe(self, attrstr, attrobj);
1239 3910 : if (res == NULL && !PyErr_Occurred())
1240 762 : PyErr_SetObject(PyExc_AttributeError, *attrobj);
1241 3910 : return res;
1242 : }
1243 :
1244 : PyObject *
1245 17502 : _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)
1246 : {
1247 : assert(!PyInstance_Check(self));
1248 17502 : return lookup_maybe(self, attrstr, attrobj);
1249 : }
1250 :
1251 : /* A variation of PyObject_CallMethod that uses lookup_method()
1252 : instead of PyObject_GetAttrString(). This uses the same convention
1253 : as lookup_method to cache the interned name string object. */
1254 :
1255 : static PyObject *
1256 165 : call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1257 : {
1258 : va_list va;
1259 165 : PyObject *args, *func = 0, *retval;
1260 165 : va_start(va, format);
1261 :
1262 165 : func = lookup_maybe(o, name, nameobj);
1263 165 : if (func == NULL) {
1264 0 : va_end(va);
1265 0 : if (!PyErr_Occurred())
1266 0 : PyErr_SetObject(PyExc_AttributeError, *nameobj);
1267 0 : return NULL;
1268 : }
1269 :
1270 165 : if (format && *format)
1271 165 : args = Py_VaBuildValue(format, va);
1272 : else
1273 0 : args = PyTuple_New(0);
1274 :
1275 165 : va_end(va);
1276 :
1277 165 : if (args == NULL) {
1278 0 : Py_DECREF(func);
1279 0 : return NULL;
1280 : }
1281 :
1282 : assert(PyTuple_Check(args));
1283 165 : retval = PyObject_Call(func, args, NULL);
1284 :
1285 165 : Py_DECREF(args);
1286 165 : Py_DECREF(func);
1287 :
1288 165 : return retval;
1289 : }
1290 :
1291 : /* Clone of call_method() that returns NotImplemented when the lookup fails. */
1292 :
1293 : static PyObject *
1294 0 : call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1295 : {
1296 : va_list va;
1297 0 : PyObject *args, *func = 0, *retval;
1298 0 : va_start(va, format);
1299 :
1300 0 : func = lookup_maybe(o, name, nameobj);
1301 0 : if (func == NULL) {
1302 0 : va_end(va);
1303 0 : if (!PyErr_Occurred()) {
1304 0 : Py_INCREF(Py_NotImplemented);
1305 0 : return Py_NotImplemented;
1306 : }
1307 0 : return NULL;
1308 : }
1309 :
1310 0 : if (format && *format)
1311 0 : args = Py_VaBuildValue(format, va);
1312 : else
1313 0 : args = PyTuple_New(0);
1314 :
1315 0 : va_end(va);
1316 :
1317 0 : if (args == NULL) {
1318 0 : Py_DECREF(func);
1319 0 : return NULL;
1320 : }
1321 :
1322 : assert(PyTuple_Check(args));
1323 0 : retval = PyObject_Call(func, args, NULL);
1324 :
1325 0 : Py_DECREF(args);
1326 0 : Py_DECREF(func);
1327 :
1328 0 : return retval;
1329 : }
1330 :
1331 : static int
1332 0 : fill_classic_mro(PyObject *mro, PyObject *cls)
1333 : {
1334 : PyObject *bases, *base;
1335 : Py_ssize_t i, n;
1336 :
1337 : assert(PyList_Check(mro));
1338 : assert(PyClass_Check(cls));
1339 0 : i = PySequence_Contains(mro, cls);
1340 0 : if (i < 0)
1341 0 : return -1;
1342 0 : if (!i) {
1343 0 : if (PyList_Append(mro, cls) < 0)
1344 0 : return -1;
1345 : }
1346 0 : bases = ((PyClassObject *)cls)->cl_bases;
1347 : assert(bases && PyTuple_Check(bases));
1348 0 : n = PyTuple_GET_SIZE(bases);
1349 0 : for (i = 0; i < n; i++) {
1350 0 : base = PyTuple_GET_ITEM(bases, i);
1351 0 : if (fill_classic_mro(mro, base) < 0)
1352 0 : return -1;
1353 : }
1354 0 : return 0;
1355 : }
1356 :
1357 : static PyObject *
1358 0 : classic_mro(PyObject *cls)
1359 : {
1360 : PyObject *mro;
1361 :
1362 : assert(PyClass_Check(cls));
1363 0 : mro = PyList_New(0);
1364 0 : if (mro != NULL) {
1365 0 : if (fill_classic_mro(mro, cls) == 0)
1366 0 : return mro;
1367 0 : Py_DECREF(mro);
1368 : }
1369 0 : return NULL;
1370 : }
1371 :
1372 : /*
1373 : Method resolution order algorithm C3 described in
1374 : "A Monotonic Superclass Linearization for Dylan",
1375 : by Kim Barrett, Bob Cassel, Paul Haahr,
1376 : David A. Moon, Keith Playford, and P. Tucker Withington.
1377 : (OOPSLA 1996)
1378 :
1379 : Some notes about the rules implied by C3:
1380 :
1381 : No duplicate bases.
1382 : It isn't legal to repeat a class in a list of base classes.
1383 :
1384 : The next three properties are the 3 constraints in "C3".
1385 :
1386 : Local precedence order.
1387 : If A precedes B in C's MRO, then A will precede B in the MRO of all
1388 : subclasses of C.
1389 :
1390 : Monotonicity.
1391 : The MRO of a class must be an extension without reordering of the
1392 : MRO of each of its superclasses.
1393 :
1394 : Extended Precedence Graph (EPG).
1395 : Linearization is consistent if there is a path in the EPG from
1396 : each class to all its successors in the linearization. See
1397 : the paper for definition of EPG.
1398 : */
1399 :
1400 : static int
1401 6699 : tail_contains(PyObject *list, int whence, PyObject *o) {
1402 : Py_ssize_t j, size;
1403 6699 : size = PyList_GET_SIZE(list);
1404 :
1405 10644 : for (j = whence+1; j < size; j++) {
1406 4014 : if (PyList_GET_ITEM(list, j) == o)
1407 69 : return 1;
1408 : }
1409 6630 : return 0;
1410 : }
1411 :
1412 : static PyObject *
1413 0 : class_name(PyObject *cls)
1414 : {
1415 0 : PyObject *name = PyObject_GetAttrString(cls, "__name__");
1416 0 : if (name == NULL) {
1417 0 : PyErr_Clear();
1418 0 : Py_XDECREF(name);
1419 0 : name = PyObject_Repr(cls);
1420 : }
1421 0 : if (name == NULL)
1422 0 : return NULL;
1423 0 : if (!PyString_Check(name)) {
1424 0 : Py_DECREF(name);
1425 0 : return NULL;
1426 : }
1427 0 : return name;
1428 : }
1429 :
1430 : static int
1431 1296 : check_duplicates(PyObject *list)
1432 : {
1433 : Py_ssize_t i, j, n;
1434 : /* Let's use a quadratic time algorithm,
1435 : assuming that the bases lists is short.
1436 : */
1437 1296 : n = PyList_GET_SIZE(list);
1438 2634 : for (i = 0; i < n; i++) {
1439 1338 : PyObject *o = PyList_GET_ITEM(list, i);
1440 1392 : for (j = i + 1; j < n; j++) {
1441 54 : if (PyList_GET_ITEM(list, j) == o) {
1442 0 : o = class_name(o);
1443 0 : PyErr_Format(PyExc_TypeError,
1444 : "duplicate base class %s",
1445 : o ? PyString_AS_STRING(o) : "?");
1446 0 : Py_XDECREF(o);
1447 0 : return -1;
1448 : }
1449 : }
1450 : }
1451 1296 : return 0;
1452 : }
1453 :
1454 : /* Raise a TypeError for an MRO order disagreement.
1455 :
1456 : It's hard to produce a good error message. In the absence of better
1457 : insight into error reporting, report the classes that were candidates
1458 : to be put next into the MRO. There is some conflict between the
1459 : order in which they should be put in the MRO, but it's hard to
1460 : diagnose what constraint can't be satisfied.
1461 : */
1462 :
1463 : static void
1464 0 : set_mro_error(PyObject *to_merge, int *remain)
1465 : {
1466 : Py_ssize_t i, n, off, to_merge_size;
1467 : char buf[1000];
1468 : PyObject *k, *v;
1469 0 : PyObject *set = PyDict_New();
1470 0 : if (!set) return;
1471 :
1472 0 : to_merge_size = PyList_GET_SIZE(to_merge);
1473 0 : for (i = 0; i < to_merge_size; i++) {
1474 0 : PyObject *L = PyList_GET_ITEM(to_merge, i);
1475 0 : if (remain[i] < PyList_GET_SIZE(L)) {
1476 0 : PyObject *c = PyList_GET_ITEM(L, remain[i]);
1477 0 : if (PyDict_SetItem(set, c, Py_None) < 0) {
1478 0 : Py_DECREF(set);
1479 0 : return;
1480 : }
1481 : }
1482 : }
1483 0 : n = PyDict_Size(set);
1484 :
1485 0 : off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1486 : consistent method resolution\norder (MRO) for bases");
1487 0 : i = 0;
1488 0 : while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
1489 0 : PyObject *name = class_name(k);
1490 0 : off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1491 : name ? PyString_AS_STRING(name) : "?");
1492 0 : Py_XDECREF(name);
1493 0 : if (--n && (size_t)(off+1) < sizeof(buf)) {
1494 0 : buf[off++] = ',';
1495 0 : buf[off] = '\0';
1496 : }
1497 : }
1498 0 : PyErr_SetString(PyExc_TypeError, buf);
1499 0 : Py_DECREF(set);
1500 : }
1501 :
1502 : static int
1503 1296 : pmerge(PyObject *acc, PyObject* to_merge) {
1504 : Py_ssize_t i, j, to_merge_size, empty_cnt;
1505 : int *remain;
1506 : int ok;
1507 :
1508 1296 : to_merge_size = PyList_GET_SIZE(to_merge);
1509 :
1510 : /* remain stores an index into each sublist of to_merge.
1511 : remain[i] is the index of the next base in to_merge[i]
1512 : that is not included in acc.
1513 : */
1514 1296 : remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1515 1296 : if (remain == NULL)
1516 0 : return -1;
1517 3930 : for (i = 0; i < to_merge_size; i++)
1518 2634 : remain[i] = 0;
1519 :
1520 : again:
1521 4464 : empty_cnt = 0;
1522 7167 : for (i = 0; i < to_merge_size; i++) {
1523 : PyObject *candidate;
1524 :
1525 5871 : PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1526 :
1527 5871 : if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1528 2634 : empty_cnt++;
1529 2634 : continue;
1530 : }
1531 :
1532 : /* Choose next candidate for MRO.
1533 :
1534 : The input sequences alone can determine the choice.
1535 : If not, choose the class which appears in the MRO
1536 : of the earliest direct superclass of the new class.
1537 : */
1538 :
1539 3237 : candidate = PyList_GET_ITEM(cur_list, remain[i]);
1540 9867 : for (j = 0; j < to_merge_size; j++) {
1541 6699 : PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1542 6699 : if (tail_contains(j_lst, remain[j], candidate)) {
1543 69 : goto skip; /* continue outer loop */
1544 : }
1545 : }
1546 3168 : ok = PyList_Append(acc, candidate);
1547 3168 : if (ok < 0) {
1548 0 : PyMem_Free(remain);
1549 0 : return -1;
1550 : }
1551 9711 : for (j = 0; j < to_merge_size; j++) {
1552 6543 : PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1553 11262 : if (remain[j] < PyList_GET_SIZE(j_lst) &&
1554 4719 : PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1555 4584 : remain[j]++;
1556 : }
1557 : }
1558 3168 : goto again;
1559 : skip: ;
1560 : }
1561 :
1562 1296 : if (empty_cnt == to_merge_size) {
1563 1296 : PyMem_FREE(remain);
1564 1296 : return 0;
1565 : }
1566 0 : set_mro_error(to_merge, remain);
1567 0 : PyMem_FREE(remain);
1568 0 : return -1;
1569 : }
1570 :
1571 : static PyObject *
1572 1296 : mro_implementation(PyTypeObject *type)
1573 : {
1574 : Py_ssize_t i, n;
1575 : int ok;
1576 : PyObject *bases, *result;
1577 : PyObject *to_merge, *bases_aslist;
1578 :
1579 1296 : if (type->tp_dict == NULL) {
1580 0 : if (PyType_Ready(type) < 0)
1581 0 : return NULL;
1582 : }
1583 :
1584 : /* Find a superclass linearization that honors the constraints
1585 : of the explicit lists of bases and the constraints implied by
1586 : each base class.
1587 :
1588 : to_merge is a list of lists, where each list is a superclass
1589 : linearization implied by a base class. The last element of
1590 : to_merge is the declared list of bases.
1591 : */
1592 :
1593 1296 : bases = type->tp_bases;
1594 1296 : n = PyTuple_GET_SIZE(bases);
1595 :
1596 1296 : to_merge = PyList_New(n+1);
1597 1296 : if (to_merge == NULL)
1598 0 : return NULL;
1599 :
1600 2634 : for (i = 0; i < n; i++) {
1601 1338 : PyObject *base = PyTuple_GET_ITEM(bases, i);
1602 : PyObject *parentMRO;
1603 1338 : if (PyType_Check(base))
1604 1338 : parentMRO = PySequence_List(
1605 : ((PyTypeObject*)base)->tp_mro);
1606 : else
1607 0 : parentMRO = classic_mro(base);
1608 1338 : if (parentMRO == NULL) {
1609 0 : Py_DECREF(to_merge);
1610 0 : return NULL;
1611 : }
1612 :
1613 1338 : PyList_SET_ITEM(to_merge, i, parentMRO);
1614 : }
1615 :
1616 1296 : bases_aslist = PySequence_List(bases);
1617 1296 : if (bases_aslist == NULL) {
1618 0 : Py_DECREF(to_merge);
1619 0 : return NULL;
1620 : }
1621 : /* This is just a basic sanity check. */
1622 1296 : if (check_duplicates(bases_aslist) < 0) {
1623 0 : Py_DECREF(to_merge);
1624 0 : Py_DECREF(bases_aslist);
1625 0 : return NULL;
1626 : }
1627 1296 : PyList_SET_ITEM(to_merge, n, bases_aslist);
1628 :
1629 1296 : result = Py_BuildValue("[O]", (PyObject *)type);
1630 1296 : if (result == NULL) {
1631 0 : Py_DECREF(to_merge);
1632 0 : return NULL;
1633 : }
1634 :
1635 1296 : ok = pmerge(result, to_merge);
1636 1296 : Py_DECREF(to_merge);
1637 1296 : if (ok < 0) {
1638 0 : Py_DECREF(result);
1639 0 : return NULL;
1640 : }
1641 :
1642 1296 : return result;
1643 : }
1644 :
1645 : static PyObject *
1646 63 : mro_external(PyObject *self)
1647 : {
1648 63 : PyTypeObject *type = (PyTypeObject *)self;
1649 :
1650 63 : return mro_implementation(type);
1651 : }
1652 :
1653 : static int
1654 1296 : mro_internal(PyTypeObject *type)
1655 : {
1656 : PyObject *mro, *result, *tuple;
1657 1296 : int checkit = 0;
1658 :
1659 1296 : if (Py_TYPE(type) == &PyType_Type) {
1660 1233 : result = mro_implementation(type);
1661 : }
1662 : else {
1663 : static PyObject *mro_str;
1664 63 : checkit = 1;
1665 63 : mro = lookup_method((PyObject *)type, "mro", &mro_str);
1666 63 : if (mro == NULL)
1667 0 : return -1;
1668 63 : result = PyObject_CallObject(mro, NULL);
1669 63 : Py_DECREF(mro);
1670 : }
1671 1296 : if (result == NULL)
1672 0 : return -1;
1673 1296 : tuple = PySequence_Tuple(result);
1674 1296 : Py_DECREF(result);
1675 1296 : if (tuple == NULL)
1676 0 : return -1;
1677 1296 : if (checkit) {
1678 : Py_ssize_t i, len;
1679 : PyObject *cls;
1680 : PyTypeObject *solid;
1681 :
1682 63 : solid = solid_base(type);
1683 :
1684 63 : len = PyTuple_GET_SIZE(tuple);
1685 :
1686 324 : for (i = 0; i < len; i++) {
1687 : PyTypeObject *t;
1688 261 : cls = PyTuple_GET_ITEM(tuple, i);
1689 261 : if (PyClass_Check(cls))
1690 0 : continue;
1691 261 : else if (!PyType_Check(cls)) {
1692 0 : PyErr_Format(PyExc_TypeError,
1693 : "mro() returned a non-class ('%.500s')",
1694 0 : Py_TYPE(cls)->tp_name);
1695 0 : Py_DECREF(tuple);
1696 0 : return -1;
1697 : }
1698 261 : t = (PyTypeObject*)cls;
1699 261 : if (!PyType_IsSubtype(solid, solid_base(t))) {
1700 0 : PyErr_Format(PyExc_TypeError,
1701 : "mro() returned base with unsuitable layout ('%.500s')",
1702 : t->tp_name);
1703 0 : Py_DECREF(tuple);
1704 0 : return -1;
1705 : }
1706 : }
1707 : }
1708 1296 : type->tp_mro = tuple;
1709 :
1710 1296 : type_mro_modified(type, type->tp_mro);
1711 : /* corner case: the old-style super class might have been hidden
1712 : from the custom MRO */
1713 1296 : type_mro_modified(type, type->tp_bases);
1714 :
1715 1296 : PyType_Modified(type);
1716 :
1717 1296 : return 0;
1718 : }
1719 :
1720 :
1721 : /* Calculate the best base amongst multiple base classes.
1722 : This is the first one that's on the path to the "solid base". */
1723 :
1724 : static PyTypeObject *
1725 789 : best_base(PyObject *bases)
1726 : {
1727 : Py_ssize_t i, n;
1728 : PyTypeObject *base, *winner, *candidate, *base_i;
1729 : PyObject *base_proto;
1730 :
1731 : assert(PyTuple_Check(bases));
1732 789 : n = PyTuple_GET_SIZE(bases);
1733 : assert(n > 0);
1734 789 : base = NULL;
1735 789 : winner = NULL;
1736 1623 : for (i = 0; i < n; i++) {
1737 834 : base_proto = PyTuple_GET_ITEM(bases, i);
1738 834 : if (PyClass_Check(base_proto))
1739 0 : continue;
1740 834 : if (!PyType_Check(base_proto)) {
1741 0 : PyErr_SetString(
1742 : PyExc_TypeError,
1743 : "bases must be types");
1744 0 : return NULL;
1745 : }
1746 834 : base_i = (PyTypeObject *)base_proto;
1747 834 : if (base_i->tp_dict == NULL) {
1748 0 : if (PyType_Ready(base_i) < 0)
1749 0 : return NULL;
1750 : }
1751 834 : if (!PyType_HasFeature(base_i, Py_TPFLAGS_BASETYPE)) {
1752 0 : PyErr_Format(PyExc_TypeError,
1753 : "type '%.100s' is not an acceptable base type",
1754 : base_i->tp_name);
1755 0 : return NULL;
1756 : }
1757 834 : candidate = solid_base(base_i);
1758 834 : if (winner == NULL) {
1759 789 : winner = candidate;
1760 789 : base = base_i;
1761 : }
1762 45 : else if (PyType_IsSubtype(winner, candidate))
1763 : ;
1764 3 : else if (PyType_IsSubtype(candidate, winner)) {
1765 3 : winner = candidate;
1766 3 : base = base_i;
1767 : }
1768 : else {
1769 0 : PyErr_SetString(
1770 : PyExc_TypeError,
1771 : "multiple bases have "
1772 : "instance lay-out conflict");
1773 0 : return NULL;
1774 : }
1775 : }
1776 789 : if (base == NULL)
1777 0 : PyErr_SetString(PyExc_TypeError,
1778 : "a new-style class can't have only classic bases");
1779 789 : return base;
1780 : }
1781 :
1782 : static int
1783 2952 : extra_ivars(PyTypeObject *type, PyTypeObject *base)
1784 : {
1785 2952 : size_t t_size = type->tp_basicsize;
1786 2952 : size_t b_size = base->tp_basicsize;
1787 :
1788 : assert(t_size >= b_size); /* Else type smaller than base! */
1789 2952 : if (type->tp_itemsize || base->tp_itemsize) {
1790 : /* If itemsize is involved, stricter rules */
1791 54 : return t_size != b_size ||
1792 6 : type->tp_itemsize != base->tp_itemsize;
1793 : }
1794 4233 : if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1795 2658 : type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
1796 1329 : type->tp_flags & Py_TPFLAGS_HEAPTYPE)
1797 1254 : t_size -= sizeof(PyObject *);
1798 4302 : if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1799 2631 : type->tp_dictoffset + sizeof(PyObject *) == t_size &&
1800 1233 : type->tp_flags & Py_TPFLAGS_HEAPTYPE)
1801 1233 : t_size -= sizeof(PyObject *);
1802 :
1803 2904 : return t_size != b_size;
1804 : }
1805 :
1806 : static PyTypeObject *
1807 2952 : solid_base(PyTypeObject *type)
1808 : {
1809 : PyTypeObject *base;
1810 :
1811 2952 : if (type->tp_base)
1812 1794 : base = solid_base(type->tp_base);
1813 : else
1814 1158 : base = &PyBaseObject_Type;
1815 2952 : if (extra_ivars(type, base))
1816 300 : return type;
1817 : else
1818 2652 : return base;
1819 : }
1820 :
1821 : static void object_dealloc(PyObject *);
1822 : static int object_init(PyObject *, PyObject *, PyObject *);
1823 : static int update_slot(PyTypeObject *, PyObject *);
1824 : static void fixup_slot_dispatchers(PyTypeObject *);
1825 :
1826 : /*
1827 : * Helpers for __dict__ descriptor. We don't want to expose the dicts
1828 : * inherited from various builtin types. The builtin base usually provides
1829 : * its own __dict__ descriptor, so we use that when we can.
1830 : */
1831 : static PyTypeObject *
1832 706 : get_builtin_base_with_dict(PyTypeObject *type)
1833 : {
1834 3805 : while (type->tp_base != NULL) {
1835 4786 : if (type->tp_dictoffset != 0 &&
1836 2393 : !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1837 0 : return type;
1838 2393 : type = type->tp_base;
1839 : }
1840 706 : return NULL;
1841 : }
1842 :
1843 : static PyObject *
1844 0 : get_dict_descriptor(PyTypeObject *type)
1845 : {
1846 : static PyObject *dict_str;
1847 : PyObject *descr;
1848 :
1849 0 : if (dict_str == NULL) {
1850 0 : dict_str = PyString_InternFromString("__dict__");
1851 0 : if (dict_str == NULL)
1852 0 : return NULL;
1853 : }
1854 0 : descr = _PyType_Lookup(type, dict_str);
1855 0 : if (descr == NULL || !PyDescr_IsData(descr))
1856 0 : return NULL;
1857 :
1858 0 : return descr;
1859 : }
1860 :
1861 : static void
1862 0 : raise_dict_descr_error(PyObject *obj)
1863 : {
1864 0 : PyErr_Format(PyExc_TypeError,
1865 : "this __dict__ descriptor does not support "
1866 0 : "'%.200s' objects", obj->ob_type->tp_name);
1867 0 : }
1868 :
1869 : static PyObject *
1870 706 : subtype_dict(PyObject *obj, void *context)
1871 : {
1872 : PyObject **dictptr;
1873 : PyObject *dict;
1874 : PyTypeObject *base;
1875 :
1876 706 : base = get_builtin_base_with_dict(obj->ob_type);
1877 706 : if (base != NULL) {
1878 : descrgetfunc func;
1879 0 : PyObject *descr = get_dict_descriptor(base);
1880 0 : if (descr == NULL) {
1881 0 : raise_dict_descr_error(obj);
1882 0 : return NULL;
1883 : }
1884 0 : func = descr->ob_type->tp_descr_get;
1885 0 : if (func == NULL) {
1886 0 : raise_dict_descr_error(obj);
1887 0 : return NULL;
1888 : }
1889 0 : return func(descr, obj, (PyObject *)(obj->ob_type));
1890 : }
1891 :
1892 706 : dictptr = _PyObject_GetDictPtr(obj);
1893 706 : if (dictptr == NULL) {
1894 0 : PyErr_SetString(PyExc_AttributeError,
1895 : "This object has no __dict__");
1896 0 : return NULL;
1897 : }
1898 706 : dict = *dictptr;
1899 706 : if (dict == NULL)
1900 155 : *dictptr = dict = PyDict_New();
1901 706 : Py_XINCREF(dict);
1902 706 : return dict;
1903 : }
1904 :
1905 : static int
1906 0 : subtype_setdict(PyObject *obj, PyObject *value, void *context)
1907 : {
1908 : PyObject **dictptr;
1909 : PyObject *dict;
1910 : PyTypeObject *base;
1911 :
1912 0 : base = get_builtin_base_with_dict(obj->ob_type);
1913 0 : if (base != NULL) {
1914 : descrsetfunc func;
1915 0 : PyObject *descr = get_dict_descriptor(base);
1916 0 : if (descr == NULL) {
1917 0 : raise_dict_descr_error(obj);
1918 0 : return -1;
1919 : }
1920 0 : func = descr->ob_type->tp_descr_set;
1921 0 : if (func == NULL) {
1922 0 : raise_dict_descr_error(obj);
1923 0 : return -1;
1924 : }
1925 0 : return func(descr, obj, value);
1926 : }
1927 :
1928 0 : dictptr = _PyObject_GetDictPtr(obj);
1929 0 : if (dictptr == NULL) {
1930 0 : PyErr_SetString(PyExc_AttributeError,
1931 : "This object has no __dict__");
1932 0 : return -1;
1933 : }
1934 0 : if (value != NULL && !PyDict_Check(value)) {
1935 0 : PyErr_Format(PyExc_TypeError,
1936 : "__dict__ must be set to a dictionary, "
1937 0 : "not a '%.200s'", Py_TYPE(value)->tp_name);
1938 0 : return -1;
1939 : }
1940 0 : dict = *dictptr;
1941 0 : Py_XINCREF(value);
1942 0 : *dictptr = value;
1943 0 : Py_XDECREF(dict);
1944 0 : return 0;
1945 : }
1946 :
1947 : static PyObject *
1948 0 : subtype_getweakref(PyObject *obj, void *context)
1949 : {
1950 : PyObject **weaklistptr;
1951 : PyObject *result;
1952 :
1953 0 : if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
1954 0 : PyErr_SetString(PyExc_AttributeError,
1955 : "This object has no __weakref__");
1956 0 : return NULL;
1957 : }
1958 : assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
1959 : assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
1960 : (size_t)(Py_TYPE(obj)->tp_basicsize));
1961 0 : weaklistptr = (PyObject **)
1962 0 : ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
1963 0 : if (*weaklistptr == NULL)
1964 0 : result = Py_None;
1965 : else
1966 0 : result = *weaklistptr;
1967 0 : Py_INCREF(result);
1968 0 : return result;
1969 : }
1970 :
1971 : /* Three variants on the subtype_getsets list. */
1972 :
1973 : static PyGetSetDef subtype_getsets_full[] = {
1974 : {"__dict__", subtype_dict, subtype_setdict,
1975 : PyDoc_STR("dictionary for instance variables (if defined)")},
1976 : {"__weakref__", subtype_getweakref, NULL,
1977 : PyDoc_STR("list of weak references to the object (if defined)")},
1978 : {0}
1979 : };
1980 :
1981 : static PyGetSetDef subtype_getsets_dict_only[] = {
1982 : {"__dict__", subtype_dict, subtype_setdict,
1983 : PyDoc_STR("dictionary for instance variables (if defined)")},
1984 : {0}
1985 : };
1986 :
1987 : static PyGetSetDef subtype_getsets_weakref_only[] = {
1988 : {"__weakref__", subtype_getweakref, NULL,
1989 : PyDoc_STR("list of weak references to the object (if defined)")},
1990 : {0}
1991 : };
1992 :
1993 : static int
1994 3 : valid_identifier(PyObject *s)
1995 : {
1996 : unsigned char *p;
1997 : Py_ssize_t i, n;
1998 :
1999 3 : if (!PyString_Check(s)) {
2000 0 : PyErr_Format(PyExc_TypeError,
2001 : "__slots__ items must be strings, not '%.200s'",
2002 0 : Py_TYPE(s)->tp_name);
2003 0 : return 0;
2004 : }
2005 3 : p = (unsigned char *) PyString_AS_STRING(s);
2006 3 : n = PyString_GET_SIZE(s);
2007 : /* We must reject an empty name. As a hack, we bump the
2008 : length to 1 so that the loop will balk on the trailing \0. */
2009 3 : if (n == 0)
2010 0 : n = 1;
2011 12 : for (i = 0; i < n; i++, p++) {
2012 9 : if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
2013 0 : PyErr_SetString(PyExc_TypeError,
2014 : "__slots__ must be identifiers");
2015 0 : return 0;
2016 : }
2017 : }
2018 3 : return 1;
2019 : }
2020 :
2021 : #ifdef Py_USING_UNICODE
2022 : /* Replace Unicode objects in slots. */
2023 :
2024 : static PyObject *
2025 36 : _unicode_to_string(PyObject *slots, Py_ssize_t nslots)
2026 : {
2027 36 : PyObject *tmp = NULL;
2028 : PyObject *slot_name, *new_name;
2029 : Py_ssize_t i;
2030 :
2031 39 : for (i = 0; i < nslots; i++) {
2032 3 : if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
2033 0 : if (tmp == NULL) {
2034 0 : tmp = PySequence_List(slots);
2035 0 : if (tmp == NULL)
2036 0 : return NULL;
2037 : }
2038 0 : new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
2039 : NULL);
2040 0 : if (new_name == NULL) {
2041 0 : Py_DECREF(tmp);
2042 0 : return NULL;
2043 : }
2044 0 : Py_INCREF(new_name);
2045 0 : PyList_SET_ITEM(tmp, i, new_name);
2046 0 : Py_DECREF(slot_name);
2047 : }
2048 : }
2049 36 : if (tmp != NULL) {
2050 0 : slots = PyList_AsTuple(tmp);
2051 0 : Py_DECREF(tmp);
2052 : }
2053 36 : return slots;
2054 : }
2055 : #endif
2056 :
2057 : /* Forward */
2058 : static int
2059 : object_init(PyObject *self, PyObject *args, PyObject *kwds);
2060 :
2061 : static int
2062 789 : type_init(PyObject *cls, PyObject *args, PyObject *kwds)
2063 : {
2064 : int res;
2065 :
2066 : assert(args != NULL && PyTuple_Check(args));
2067 : assert(kwds == NULL || PyDict_Check(kwds));
2068 :
2069 789 : if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
2070 0 : PyErr_SetString(PyExc_TypeError,
2071 : "type.__init__() takes no keyword arguments");
2072 0 : return -1;
2073 : }
2074 :
2075 1578 : if (args != NULL && PyTuple_Check(args) &&
2076 1578 : (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
2077 0 : PyErr_SetString(PyExc_TypeError,
2078 : "type.__init__() takes 1 or 3 arguments");
2079 0 : return -1;
2080 : }
2081 :
2082 : /* Call object.__init__(self) now. */
2083 : /* XXX Could call super(type, cls).__init__() but what's the point? */
2084 789 : args = PyTuple_GetSlice(args, 0, 0);
2085 789 : res = object_init(cls, args, NULL);
2086 789 : Py_DECREF(args);
2087 789 : return res;
2088 : }
2089 :
2090 : static PyObject *
2091 3501 : type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
2092 : {
2093 : PyObject *name, *bases, *dict;
2094 : static char *kwlist[] = {"name", "bases", "dict", 0};
2095 : PyObject *slots, *tmp, *newslots;
2096 : PyTypeObject *type, *base, *tmptype, *winner;
2097 : PyHeapTypeObject *et;
2098 : PyMemberDef *mp;
2099 : Py_ssize_t i, nbases, nslots, slotoffset;
2100 : int j, may_add_dict, may_add_weak, add_dict, add_weak;
2101 :
2102 : assert(args != NULL && PyTuple_Check(args));
2103 : assert(kwds == NULL || PyDict_Check(kwds));
2104 :
2105 : /* Special case: type(x) should return x->ob_type */
2106 : {
2107 3501 : const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
2108 3501 : const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
2109 :
2110 3501 : if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
2111 2703 : PyObject *x = PyTuple_GET_ITEM(args, 0);
2112 2703 : Py_INCREF(Py_TYPE(x));
2113 2703 : return (PyObject *) Py_TYPE(x);
2114 : }
2115 :
2116 : /* SF bug 475327 -- if that didn't trigger, we need 3
2117 : arguments. but PyArg_ParseTupleAndKeywords below may give
2118 : a msg saying type() needs exactly 3. */
2119 798 : if (nargs + nkwds != 3) {
2120 0 : PyErr_SetString(PyExc_TypeError,
2121 : "type() takes 1 or 3 arguments");
2122 0 : return NULL;
2123 : }
2124 : }
2125 :
2126 : /* Check arguments: (name, bases, dict) */
2127 798 : if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
2128 : &name,
2129 : &PyTuple_Type, &bases,
2130 : &PyDict_Type, &dict))
2131 0 : return NULL;
2132 :
2133 : /* Determine the proper metatype to deal with this,
2134 : and check for metatype conflicts while we're at it.
2135 : Note that if some other metatype wins to contract,
2136 : it's possible that its instances are not types. */
2137 798 : nbases = PyTuple_GET_SIZE(bases);
2138 798 : winner = metatype;
2139 1590 : for (i = 0; i < nbases; i++) {
2140 792 : tmp = PyTuple_GET_ITEM(bases, i);
2141 792 : tmptype = tmp->ob_type;
2142 792 : if (tmptype == &PyClass_Type)
2143 0 : continue; /* Special case classic classes */
2144 792 : if (PyType_IsSubtype(winner, tmptype))
2145 783 : continue;
2146 9 : if (PyType_IsSubtype(tmptype, winner)) {
2147 9 : winner = tmptype;
2148 9 : continue;
2149 : }
2150 0 : PyErr_SetString(PyExc_TypeError,
2151 : "metaclass conflict: "
2152 : "the metaclass of a derived class "
2153 : "must be a (non-strict) subclass "
2154 : "of the metaclasses of all its bases");
2155 0 : return NULL;
2156 : }
2157 798 : if (winner != metatype) {
2158 9 : if (winner->tp_new != type_new) /* Pass it to the winner */
2159 9 : return winner->tp_new(winner, args, kwds);
2160 0 : metatype = winner;
2161 : }
2162 :
2163 : /* Adjust for empty tuple bases */
2164 789 : if (nbases == 0) {
2165 60 : bases = PyTuple_Pack(1, &PyBaseObject_Type);
2166 60 : if (bases == NULL)
2167 0 : return NULL;
2168 60 : nbases = 1;
2169 : }
2170 : else
2171 729 : Py_INCREF(bases);
2172 :
2173 : /* XXX From here until type is allocated, "return NULL" leaks bases! */
2174 :
2175 : /* Calculate best base, and check that all bases are type objects */
2176 789 : base = best_base(bases);
2177 789 : if (base == NULL) {
2178 0 : Py_DECREF(bases);
2179 0 : return NULL;
2180 : }
2181 :
2182 : /* Check for a __slots__ sequence variable in dict, and count it */
2183 789 : slots = PyDict_GetItemString(dict, "__slots__");
2184 789 : nslots = 0;
2185 789 : add_dict = 0;
2186 789 : add_weak = 0;
2187 789 : may_add_dict = base->tp_dictoffset == 0;
2188 789 : may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
2189 789 : if (slots == NULL) {
2190 753 : if (may_add_dict) {
2191 249 : add_dict++;
2192 : }
2193 753 : if (may_add_weak) {
2194 315 : add_weak++;
2195 : }
2196 : }
2197 : else {
2198 : /* Have slots */
2199 :
2200 : /* Make it into a tuple */
2201 36 : if (PyString_Check(slots) || PyUnicode_Check(slots))
2202 0 : slots = PyTuple_Pack(1, slots);
2203 : else
2204 36 : slots = PySequence_Tuple(slots);
2205 36 : if (slots == NULL) {
2206 0 : Py_DECREF(bases);
2207 0 : return NULL;
2208 : }
2209 : assert(PyTuple_Check(slots));
2210 :
2211 : /* Are slots allowed? */
2212 36 : nslots = PyTuple_GET_SIZE(slots);
2213 36 : if (nslots > 0 && base->tp_itemsize != 0) {
2214 0 : PyErr_Format(PyExc_TypeError,
2215 : "nonempty __slots__ "
2216 : "not supported for subtype of '%s'",
2217 : base->tp_name);
2218 : bad_slots:
2219 0 : Py_DECREF(bases);
2220 0 : Py_DECREF(slots);
2221 0 : return NULL;
2222 : }
2223 :
2224 : #ifdef Py_USING_UNICODE
2225 36 : tmp = _unicode_to_string(slots, nslots);
2226 36 : if (tmp == NULL)
2227 0 : goto bad_slots;
2228 36 : if (tmp != slots) {
2229 0 : Py_DECREF(slots);
2230 0 : slots = tmp;
2231 : }
2232 : #endif
2233 : /* Check for valid slot names and two special cases */
2234 39 : for (i = 0; i < nslots; i++) {
2235 3 : PyObject *tmp = PyTuple_GET_ITEM(slots, i);
2236 : char *s;
2237 3 : if (!valid_identifier(tmp))
2238 0 : goto bad_slots;
2239 : assert(PyString_Check(tmp));
2240 3 : s = PyString_AS_STRING(tmp);
2241 3 : if (strcmp(s, "__dict__") == 0) {
2242 0 : if (!may_add_dict || add_dict) {
2243 0 : PyErr_SetString(PyExc_TypeError,
2244 : "__dict__ slot disallowed: "
2245 : "we already got one");
2246 0 : goto bad_slots;
2247 : }
2248 0 : add_dict++;
2249 : }
2250 3 : if (strcmp(s, "__weakref__") == 0) {
2251 0 : if (!may_add_weak || add_weak) {
2252 0 : PyErr_SetString(PyExc_TypeError,
2253 : "__weakref__ slot disallowed: "
2254 : "either we already got one, "
2255 : "or __itemsize__ != 0");
2256 0 : goto bad_slots;
2257 : }
2258 0 : add_weak++;
2259 : }
2260 : }
2261 :
2262 : /* Copy slots into a list, mangle names and sort them.
2263 : Sorted names are needed for __class__ assignment.
2264 : Convert them back to tuple at the end.
2265 : */
2266 36 : newslots = PyList_New(nslots - add_dict - add_weak);
2267 36 : if (newslots == NULL)
2268 0 : goto bad_slots;
2269 39 : for (i = j = 0; i < nslots; i++) {
2270 : char *s;
2271 3 : tmp = PyTuple_GET_ITEM(slots, i);
2272 3 : s = PyString_AS_STRING(tmp);
2273 3 : if ((add_dict && strcmp(s, "__dict__") == 0) ||
2274 0 : (add_weak && strcmp(s, "__weakref__") == 0))
2275 0 : continue;
2276 3 : tmp =_Py_Mangle(name, tmp);
2277 3 : if (!tmp) {
2278 0 : Py_DECREF(newslots);
2279 0 : goto bad_slots;
2280 : }
2281 3 : PyList_SET_ITEM(newslots, j, tmp);
2282 3 : j++;
2283 : }
2284 : assert(j == nslots - add_dict - add_weak);
2285 36 : nslots = j;
2286 36 : Py_DECREF(slots);
2287 36 : if (PyList_Sort(newslots) == -1) {
2288 0 : Py_DECREF(bases);
2289 0 : Py_DECREF(newslots);
2290 0 : return NULL;
2291 : }
2292 36 : slots = PyList_AsTuple(newslots);
2293 36 : Py_DECREF(newslots);
2294 36 : if (slots == NULL) {
2295 0 : Py_DECREF(bases);
2296 0 : return NULL;
2297 : }
2298 :
2299 : /* Secondary bases may provide weakrefs or dict */
2300 36 : if (nbases > 1 &&
2301 6 : ((may_add_dict && !add_dict) ||
2302 0 : (may_add_weak && !add_weak))) {
2303 12 : for (i = 0; i < nbases; i++) {
2304 12 : tmp = PyTuple_GET_ITEM(bases, i);
2305 12 : if (tmp == (PyObject *)base)
2306 6 : continue; /* Skip primary base */
2307 6 : if (PyClass_Check(tmp)) {
2308 : /* Classic base class provides both */
2309 0 : if (may_add_dict && !add_dict)
2310 0 : add_dict++;
2311 0 : if (may_add_weak && !add_weak)
2312 0 : add_weak++;
2313 0 : break;
2314 : }
2315 : assert(PyType_Check(tmp));
2316 6 : tmptype = (PyTypeObject *)tmp;
2317 12 : if (may_add_dict && !add_dict &&
2318 6 : tmptype->tp_dictoffset != 0)
2319 6 : add_dict++;
2320 6 : if (may_add_weak && !add_weak &&
2321 0 : tmptype->tp_weaklistoffset != 0)
2322 0 : add_weak++;
2323 6 : if (may_add_dict && !add_dict)
2324 0 : continue;
2325 6 : if (may_add_weak && !add_weak)
2326 0 : continue;
2327 : /* Nothing more to check */
2328 6 : break;
2329 : }
2330 : }
2331 : }
2332 :
2333 : /* XXX From here until type is safely allocated,
2334 : "return NULL" may leak slots! */
2335 :
2336 : /* Allocate the type object */
2337 789 : type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
2338 789 : if (type == NULL) {
2339 0 : Py_XDECREF(slots);
2340 0 : Py_DECREF(bases);
2341 0 : return NULL;
2342 : }
2343 :
2344 : /* Keep name and slots alive in the extended type object */
2345 789 : et = (PyHeapTypeObject *)type;
2346 789 : Py_INCREF(name);
2347 789 : et->ht_name = name;
2348 789 : et->ht_slots = slots;
2349 :
2350 : /* Initialize tp_flags */
2351 789 : type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2352 : Py_TPFLAGS_BASETYPE;
2353 789 : if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2354 552 : type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2355 789 : if (base->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)
2356 0 : type->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
2357 :
2358 : /* It's a new-style number unless it specifically inherits any
2359 : old-style numeric behavior */
2360 1155 : if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
2361 366 : (base->tp_as_number == NULL))
2362 789 : type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
2363 :
2364 : /* Initialize essential fields */
2365 789 : type->tp_as_number = &et->as_number;
2366 789 : type->tp_as_sequence = &et->as_sequence;
2367 789 : type->tp_as_mapping = &et->as_mapping;
2368 789 : type->tp_as_buffer = &et->as_buffer;
2369 789 : type->tp_name = PyString_AS_STRING(name);
2370 789 : if (!type->tp_name) {
2371 0 : Py_DECREF(bases);
2372 0 : Py_DECREF(type);
2373 0 : return NULL;
2374 : }
2375 789 : if (strlen(type->tp_name) != (size_t)PyString_GET_SIZE(name)) {
2376 0 : PyErr_SetString(PyExc_ValueError,
2377 : "type name must not contain null characters");
2378 0 : Py_DECREF(bases);
2379 0 : Py_DECREF(type);
2380 0 : return NULL;
2381 : }
2382 :
2383 : /* Set tp_base and tp_bases */
2384 789 : type->tp_bases = bases;
2385 789 : Py_INCREF(base);
2386 789 : type->tp_base = base;
2387 :
2388 : /* Initialize tp_dict from passed-in dict */
2389 789 : type->tp_dict = dict = PyDict_Copy(dict);
2390 789 : if (dict == NULL) {
2391 0 : Py_DECREF(type);
2392 0 : return NULL;
2393 : }
2394 :
2395 : /* Set __module__ in the dict */
2396 789 : if (PyDict_GetItemString(dict, "__module__") == NULL) {
2397 333 : tmp = PyEval_GetGlobals();
2398 333 : if (tmp != NULL) {
2399 333 : tmp = PyDict_GetItemString(tmp, "__name__");
2400 333 : if (tmp != NULL) {
2401 333 : if (PyDict_SetItemString(dict, "__module__",
2402 : tmp) < 0) {
2403 0 : Py_DECREF(type);
2404 0 : return NULL;
2405 : }
2406 : }
2407 : }
2408 : }
2409 :
2410 : /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
2411 : and is a string. The __doc__ accessor will first look for tp_doc;
2412 : if that fails, it will still look into __dict__.
2413 : */
2414 : {
2415 789 : PyObject *doc = PyDict_GetItemString(dict, "__doc__");
2416 789 : if (doc != NULL && PyString_Check(doc)) {
2417 276 : const size_t n = (size_t)PyString_GET_SIZE(doc);
2418 276 : char *tp_doc = (char *)PyObject_MALLOC(n+1);
2419 276 : if (tp_doc == NULL) {
2420 0 : Py_DECREF(type);
2421 0 : return NULL;
2422 : }
2423 276 : memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
2424 276 : type->tp_doc = tp_doc;
2425 : }
2426 : }
2427 :
2428 : /* Special-case __new__: if it's a plain function,
2429 : make it a static function */
2430 789 : tmp = PyDict_GetItemString(dict, "__new__");
2431 789 : if (tmp != NULL && PyFunction_Check(tmp)) {
2432 36 : tmp = PyStaticMethod_New(tmp);
2433 36 : if (tmp == NULL) {
2434 0 : Py_DECREF(type);
2435 0 : return NULL;
2436 : }
2437 36 : if (PyDict_SetItemString(dict, "__new__", tmp) < 0) {
2438 0 : Py_DECREF(tmp);
2439 0 : Py_DECREF(type);
2440 0 : return NULL;
2441 : }
2442 36 : Py_DECREF(tmp);
2443 : }
2444 :
2445 : /* Add descriptors for custom slots from __slots__, or for __dict__ */
2446 789 : mp = PyHeapType_GET_MEMBERS(et);
2447 789 : slotoffset = base->tp_basicsize;
2448 789 : if (slots != NULL) {
2449 39 : for (i = 0; i < nslots; i++, mp++) {
2450 3 : mp->name = PyString_AS_STRING(
2451 : PyTuple_GET_ITEM(slots, i));
2452 3 : mp->type = T_OBJECT_EX;
2453 3 : mp->offset = slotoffset;
2454 :
2455 : /* __dict__ and __weakref__ are already filtered out */
2456 : assert(strcmp(mp->name, "__dict__") != 0);
2457 : assert(strcmp(mp->name, "__weakref__") != 0);
2458 :
2459 3 : slotoffset += sizeof(PyObject *);
2460 : }
2461 : }
2462 789 : if (add_dict) {
2463 255 : if (base->tp_itemsize)
2464 9 : type->tp_dictoffset = -(long)sizeof(PyObject *);
2465 : else
2466 246 : type->tp_dictoffset = slotoffset;
2467 255 : slotoffset += sizeof(PyObject *);
2468 : }
2469 789 : if (add_weak) {
2470 : assert(!base->tp_itemsize);
2471 315 : type->tp_weaklistoffset = slotoffset;
2472 315 : slotoffset += sizeof(PyObject *);
2473 : }
2474 789 : type->tp_basicsize = slotoffset;
2475 789 : type->tp_itemsize = base->tp_itemsize;
2476 789 : type->tp_members = PyHeapType_GET_MEMBERS(et);
2477 :
2478 789 : if (type->tp_weaklistoffset && type->tp_dictoffset)
2479 246 : type->tp_getset = subtype_getsets_full;
2480 543 : else if (type->tp_weaklistoffset && !type->tp_dictoffset)
2481 69 : type->tp_getset = subtype_getsets_weakref_only;
2482 474 : else if (!type->tp_weaklistoffset && type->tp_dictoffset)
2483 9 : type->tp_getset = subtype_getsets_dict_only;
2484 : else
2485 465 : type->tp_getset = NULL;
2486 :
2487 : /* Special case some slots */
2488 789 : if (type->tp_dictoffset != 0 || nslots > 0) {
2489 258 : if (base->tp_getattr == NULL && base->tp_getattro == NULL)
2490 0 : type->tp_getattro = PyObject_GenericGetAttr;
2491 258 : if (base->tp_setattr == NULL && base->tp_setattro == NULL)
2492 0 : type->tp_setattro = PyObject_GenericSetAttr;
2493 : }
2494 789 : type->tp_dealloc = subtype_dealloc;
2495 :
2496 : /* Enable GC unless there are really no instance variables possible */
2497 789 : if (!(type->tp_basicsize == sizeof(PyObject) &&
2498 0 : type->tp_itemsize == 0))
2499 789 : type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2500 :
2501 : /* Always override allocation strategy to use regular heap */
2502 789 : type->tp_alloc = PyType_GenericAlloc;
2503 789 : if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
2504 789 : type->tp_free = PyObject_GC_Del;
2505 789 : type->tp_traverse = subtype_traverse;
2506 789 : type->tp_clear = subtype_clear;
2507 : }
2508 : else
2509 0 : type->tp_free = PyObject_Del;
2510 :
2511 : /* Initialize the rest */
2512 789 : if (PyType_Ready(type) < 0) {
2513 0 : Py_DECREF(type);
2514 0 : return NULL;
2515 : }
2516 :
2517 : /* Put the proper slots in place */
2518 789 : fixup_slot_dispatchers(type);
2519 :
2520 789 : return (PyObject *)type;
2521 : }
2522 :
2523 : /* Internal API to look for a name through the MRO.
2524 : This returns a borrowed reference, and doesn't set an exception! */
2525 : PyObject *
2526 223131 : _PyType_Lookup(PyTypeObject *type, PyObject *name)
2527 : {
2528 : Py_ssize_t i, n;
2529 : PyObject *mro, *res, *base, *dict;
2530 : unsigned int h;
2531 :
2532 446262 : if (MCACHE_CACHEABLE_NAME(name) &&
2533 223131 : PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
2534 : /* fast path */
2535 219129 : h = MCACHE_HASH_METHOD(type, name);
2536 366474 : if (method_cache[h].version == type->tp_version_tag &&
2537 147345 : method_cache[h].name == name) {
2538 : #if MCACHE_STATS
2539 : method_cache_hits++;
2540 : #endif
2541 146310 : return method_cache[h].value;
2542 : }
2543 : }
2544 :
2545 : /* Look in tp_dict of types in MRO */
2546 76821 : mro = type->tp_mro;
2547 :
2548 76821 : if (mro == NULL) {
2549 0 : if ((type->tp_flags & Py_TPFLAGS_READYING) == 0 &&
2550 0 : PyType_Ready(type) < 0) {
2551 : /* It's not ideal to clear the error condition,
2552 : but this function is documented as not setting
2553 : an exception, and I don't want to change that.
2554 : When PyType_Ready() can't proceed, it won't
2555 : set the "ready" flag, so future attempts to ready
2556 : the same type will call it again -- hopefully
2557 : in a context that propagates the exception out.
2558 : */
2559 0 : PyErr_Clear();
2560 0 : return NULL;
2561 : }
2562 0 : mro = type->tp_mro;
2563 0 : if (mro == NULL) {
2564 0 : return NULL;
2565 : }
2566 : }
2567 :
2568 76821 : res = NULL;
2569 : assert(PyTuple_Check(mro));
2570 76821 : n = PyTuple_GET_SIZE(mro);
2571 323509 : for (i = 0; i < n; i++) {
2572 258640 : base = PyTuple_GET_ITEM(mro, i);
2573 258640 : if (PyClass_Check(base))
2574 0 : dict = ((PyClassObject *)base)->cl_dict;
2575 : else {
2576 : assert(PyType_Check(base));
2577 258640 : dict = ((PyTypeObject *)base)->tp_dict;
2578 : }
2579 : assert(dict && PyDict_Check(dict));
2580 258640 : res = PyDict_GetItem(dict, name);
2581 258640 : if (res != NULL)
2582 11952 : break;
2583 : }
2584 :
2585 76821 : if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
2586 74451 : h = MCACHE_HASH_METHOD(type, name);
2587 74451 : method_cache[h].version = type->tp_version_tag;
2588 74451 : method_cache[h].value = res; /* borrowed */
2589 74451 : Py_INCREF(name);
2590 : assert(((PyStringObject *)(name))->ob_shash != -1);
2591 : #if MCACHE_STATS
2592 : if (method_cache[h].name != Py_None && method_cache[h].name != name)
2593 : method_cache_collisions++;
2594 : else
2595 : method_cache_misses++;
2596 : #endif
2597 74451 : Py_DECREF(method_cache[h].name);
2598 74451 : method_cache[h].name = name;
2599 : }
2600 76821 : return res;
2601 : }
2602 :
2603 : /* This is similar to PyObject_GenericGetAttr(),
2604 : but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2605 : static PyObject *
2606 8472 : type_getattro(PyTypeObject *type, PyObject *name)
2607 : {
2608 8472 : PyTypeObject *metatype = Py_TYPE(type);
2609 : PyObject *meta_attribute, *attribute;
2610 : descrgetfunc meta_get;
2611 :
2612 8472 : if (!PyString_Check(name)) {
2613 0 : PyErr_Format(PyExc_TypeError,
2614 : "attribute name must be string, not '%.200s'",
2615 0 : name->ob_type->tp_name);
2616 0 : return NULL;
2617 : }
2618 :
2619 : /* Initialize this type (we'll assume the metatype is initialized) */
2620 8472 : if (type->tp_dict == NULL) {
2621 9 : if (PyType_Ready(type) < 0)
2622 0 : return NULL;
2623 : }
2624 :
2625 : /* No readable descriptor found yet */
2626 8472 : meta_get = NULL;
2627 :
2628 : /* Look for the attribute in the metatype */
2629 8472 : meta_attribute = _PyType_Lookup(metatype, name);
2630 :
2631 8472 : if (meta_attribute != NULL) {
2632 5727 : meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
2633 :
2634 5727 : if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2635 : /* Data descriptors implement tp_descr_set to intercept
2636 : * writes. Assume the attribute is not overridden in
2637 : * type's tp_dict (and bases): call the descriptor now.
2638 : */
2639 612 : return meta_get(meta_attribute, (PyObject *)type,
2640 : (PyObject *)metatype);
2641 : }
2642 5115 : Py_INCREF(meta_attribute);
2643 : }
2644 :
2645 : /* No data descriptor found on metatype. Look in tp_dict of this
2646 : * type and its bases */
2647 7860 : attribute = _PyType_Lookup(type, name);
2648 7860 : if (attribute != NULL) {
2649 : /* Implement descriptor functionality, if any */
2650 7713 : descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
2651 :
2652 7713 : Py_XDECREF(meta_attribute);
2653 :
2654 7713 : if (local_get != NULL) {
2655 : /* NULL 2nd argument indicates the descriptor was
2656 : * found on the target object itself (or a base) */
2657 2634 : return local_get(attribute, (PyObject *)NULL,
2658 : (PyObject *)type);
2659 : }
2660 :
2661 5079 : Py_INCREF(attribute);
2662 5079 : return attribute;
2663 : }
2664 :
2665 : /* No attribute found in local __dict__ (or bases): use the
2666 : * descriptor from the metatype, if any */
2667 147 : if (meta_get != NULL) {
2668 : PyObject *res;
2669 129 : res = meta_get(meta_attribute, (PyObject *)type,
2670 : (PyObject *)metatype);
2671 129 : Py_DECREF(meta_attribute);
2672 129 : return res;
2673 : }
2674 :
2675 : /* If an ordinary attribute was found on the metatype, return it now */
2676 18 : if (meta_attribute != NULL) {
2677 0 : return meta_attribute;
2678 : }
2679 :
2680 : /* Give up */
2681 18 : PyErr_Format(PyExc_AttributeError,
2682 : "type object '%.50s' has no attribute '%.400s'",
2683 18 : type->tp_name, PyString_AS_STRING(name));
2684 18 : return NULL;
2685 : }
2686 :
2687 : static int
2688 1200 : type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2689 : {
2690 1200 : if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2691 0 : PyErr_Format(
2692 : PyExc_TypeError,
2693 : "can't set attributes of built-in/extension type '%s'",
2694 : type->tp_name);
2695 0 : return -1;
2696 : }
2697 1200 : if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2698 0 : return -1;
2699 1200 : return update_slot(type, name);
2700 : }
2701 :
2702 : static void
2703 0 : type_dealloc(PyTypeObject *type)
2704 : {
2705 : PyHeapTypeObject *et;
2706 :
2707 : /* Assert this is a heap-allocated type object */
2708 : assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2709 0 : _PyObject_GC_UNTRACK(type);
2710 0 : PyObject_ClearWeakRefs((PyObject *)type);
2711 0 : et = (PyHeapTypeObject *)type;
2712 0 : Py_XDECREF(type->tp_base);
2713 0 : Py_XDECREF(type->tp_dict);
2714 0 : Py_XDECREF(type->tp_bases);
2715 0 : Py_XDECREF(type->tp_mro);
2716 0 : Py_XDECREF(type->tp_cache);
2717 0 : Py_XDECREF(type->tp_subclasses);
2718 : /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2719 : * of most other objects. It's okay to cast it to char *.
2720 : */
2721 0 : PyObject_Free((char *)type->tp_doc);
2722 0 : Py_XDECREF(et->ht_name);
2723 0 : Py_XDECREF(et->ht_slots);
2724 0 : Py_TYPE(type)->tp_free((PyObject *)type);
2725 0 : }
2726 :
2727 : static PyObject *
2728 63 : type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2729 : {
2730 : PyObject *list, *raw, *ref;
2731 : Py_ssize_t i, n;
2732 :
2733 63 : list = PyList_New(0);
2734 63 : if (list == NULL)
2735 0 : return NULL;
2736 63 : raw = type->tp_subclasses;
2737 63 : if (raw == NULL)
2738 63 : return list;
2739 : assert(PyList_Check(raw));
2740 0 : n = PyList_GET_SIZE(raw);
2741 0 : for (i = 0; i < n; i++) {
2742 0 : ref = PyList_GET_ITEM(raw, i);
2743 : assert(PyWeakref_CheckRef(ref));
2744 0 : ref = PyWeakref_GET_OBJECT(ref);
2745 0 : if (ref != Py_None) {
2746 0 : if (PyList_Append(list, ref) < 0) {
2747 0 : Py_DECREF(list);
2748 0 : return NULL;
2749 : }
2750 : }
2751 : }
2752 0 : return list;
2753 : }
2754 :
2755 : static PyMethodDef type_methods[] = {
2756 : {"mro", (PyCFunction)mro_external, METH_NOARGS,
2757 : PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2758 : {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2759 : PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2760 : {"__instancecheck__", type___instancecheck__, METH_O,
2761 : PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")},
2762 : {"__subclasscheck__", type___subclasscheck__, METH_O,
2763 : PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")},
2764 : {0}
2765 : };
2766 :
2767 : PyDoc_STRVAR(type_doc,
2768 : "type(object) -> the object's type\n"
2769 : "type(name, bases, dict) -> a new type");
2770 :
2771 : static int
2772 4462 : type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2773 : {
2774 : /* Because of type_is_gc(), the collector only calls this
2775 : for heaptypes. */
2776 : assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2777 :
2778 4462 : Py_VISIT(type->tp_dict);
2779 4462 : Py_VISIT(type->tp_cache);
2780 4462 : Py_VISIT(type->tp_mro);
2781 4462 : Py_VISIT(type->tp_bases);
2782 4462 : Py_VISIT(type->tp_base);
2783 :
2784 : /* There's no need to visit type->tp_subclasses or
2785 : ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2786 : in cycles; tp_subclasses is a list of weak references,
2787 : and slots is a tuple of strings. */
2788 :
2789 4462 : return 0;
2790 : }
2791 :
2792 : static int
2793 0 : type_clear(PyTypeObject *type)
2794 : {
2795 : /* Because of type_is_gc(), the collector only calls this
2796 : for heaptypes. */
2797 : assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2798 :
2799 : /* We need to invalidate the method cache carefully before clearing
2800 : the dict, so that other objects caught in a reference cycle
2801 : don't start calling destroyed methods.
2802 :
2803 : Otherwise, the only field we need to clear is tp_mro, which is
2804 : part of a hard cycle (its first element is the class itself) that
2805 : won't be broken otherwise (it's a tuple and tuples don't have a
2806 : tp_clear handler). None of the other fields need to be
2807 : cleared, and here's why:
2808 :
2809 : tp_cache:
2810 : Not used; if it were, it would be a dict.
2811 :
2812 : tp_bases, tp_base:
2813 : If these are involved in a cycle, there must be at least
2814 : one other, mutable object in the cycle, e.g. a base
2815 : class's dict; the cycle will be broken that way.
2816 :
2817 : tp_subclasses:
2818 : A list of weak references can't be part of a cycle; and
2819 : lists have their own tp_clear.
2820 :
2821 : slots (in PyHeapTypeObject):
2822 : A tuple of strings can't be part of a cycle.
2823 : */
2824 :
2825 0 : PyType_Modified(type);
2826 0 : if (type->tp_dict)
2827 0 : PyDict_Clear(type->tp_dict);
2828 0 : Py_CLEAR(type->tp_mro);
2829 :
2830 0 : return 0;
2831 : }
2832 :
2833 : static int
2834 102580 : type_is_gc(PyTypeObject *type)
2835 : {
2836 102580 : return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2837 : }
2838 :
2839 : PyTypeObject PyType_Type = {
2840 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
2841 : "type", /* tp_name */
2842 : sizeof(PyHeapTypeObject), /* tp_basicsize */
2843 : sizeof(PyMemberDef), /* tp_itemsize */
2844 : (destructor)type_dealloc, /* tp_dealloc */
2845 : 0, /* tp_print */
2846 : 0, /* tp_getattr */
2847 : 0, /* tp_setattr */
2848 : 0, /* tp_compare */
2849 : (reprfunc)type_repr, /* tp_repr */
2850 : 0, /* tp_as_number */
2851 : 0, /* tp_as_sequence */
2852 : 0, /* tp_as_mapping */
2853 : (hashfunc)_Py_HashPointer, /* tp_hash */
2854 : (ternaryfunc)type_call, /* tp_call */
2855 : 0, /* tp_str */
2856 : (getattrofunc)type_getattro, /* tp_getattro */
2857 : (setattrofunc)type_setattro, /* tp_setattro */
2858 : 0, /* tp_as_buffer */
2859 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2860 : Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
2861 : type_doc, /* tp_doc */
2862 : (traverseproc)type_traverse, /* tp_traverse */
2863 : (inquiry)type_clear, /* tp_clear */
2864 : type_richcompare, /* tp_richcompare */
2865 : offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
2866 : 0, /* tp_iter */
2867 : 0, /* tp_iternext */
2868 : type_methods, /* tp_methods */
2869 : type_members, /* tp_members */
2870 : type_getsets, /* tp_getset */
2871 : 0, /* tp_base */
2872 : 0, /* tp_dict */
2873 : 0, /* tp_descr_get */
2874 : 0, /* tp_descr_set */
2875 : offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2876 : type_init, /* tp_init */
2877 : 0, /* tp_alloc */
2878 : type_new, /* tp_new */
2879 : PyObject_GC_Del, /* tp_free */
2880 : (inquiry)type_is_gc, /* tp_is_gc */
2881 : };
2882 :
2883 :
2884 : /* The base type of all types (eventually)... except itself. */
2885 :
2886 : /* You may wonder why object.__new__() only complains about arguments
2887 : when object.__init__() is not overridden, and vice versa.
2888 :
2889 : Consider the use cases:
2890 :
2891 : 1. When neither is overridden, we want to hear complaints about
2892 : excess (i.e., any) arguments, since their presence could
2893 : indicate there's a bug.
2894 :
2895 : 2. When defining an Immutable type, we are likely to override only
2896 : __new__(), since __init__() is called too late to initialize an
2897 : Immutable object. Since __new__() defines the signature for the
2898 : type, it would be a pain to have to override __init__() just to
2899 : stop it from complaining about excess arguments.
2900 :
2901 : 3. When defining a Mutable type, we are likely to override only
2902 : __init__(). So here the converse reasoning applies: we don't
2903 : want to have to override __new__() just to stop it from
2904 : complaining.
2905 :
2906 : 4. When __init__() is overridden, and the subclass __init__() calls
2907 : object.__init__(), the latter should complain about excess
2908 : arguments; ditto for __new__().
2909 :
2910 : Use cases 2 and 3 make it unattractive to unconditionally check for
2911 : excess arguments. The best solution that addresses all four use
2912 : cases is as follows: __init__() complains about excess arguments
2913 : unless __new__() is overridden and __init__() is not overridden
2914 : (IOW, if __init__() is overridden or __new__() is not overridden);
2915 : symmetrically, __new__() complains about excess arguments unless
2916 : __init__() is overridden and __new__() is not overridden
2917 : (IOW, if __new__() is overridden or __init__() is not overridden).
2918 :
2919 : However, for backwards compatibility, this breaks too much code.
2920 : Therefore, in 2.6, we'll *warn* about excess arguments when both
2921 : methods are overridden; for all other cases we'll use the above
2922 : rules.
2923 :
2924 : */
2925 :
2926 : /* Forward */
2927 : static PyObject *
2928 : object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2929 :
2930 : static int
2931 11643 : excess_args(PyObject *args, PyObject *kwds)
2932 : {
2933 22179 : return PyTuple_GET_SIZE(args) ||
2934 186 : (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
2935 : }
2936 :
2937 : static int
2938 9885 : object_init(PyObject *self, PyObject *args, PyObject *kwds)
2939 : {
2940 9885 : int err = 0;
2941 9885 : if (excess_args(args, kwds)) {
2942 9090 : PyTypeObject *type = Py_TYPE(self);
2943 9090 : if (type->tp_init != object_init &&
2944 0 : type->tp_new != object_new)
2945 : {
2946 0 : err = PyErr_WarnEx(PyExc_DeprecationWarning,
2947 : "object.__init__() takes no parameters",
2948 : 1);
2949 : }
2950 18180 : else if (type->tp_init != object_init ||
2951 9090 : type->tp_new == object_new)
2952 : {
2953 0 : PyErr_SetString(PyExc_TypeError,
2954 : "object.__init__() takes no parameters");
2955 0 : err = -1;
2956 : }
2957 : }
2958 9885 : return err;
2959 : }
2960 :
2961 : static PyObject *
2962 1758 : object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2963 : {
2964 1758 : int err = 0;
2965 1758 : if (excess_args(args, kwds)) {
2966 1446 : if (type->tp_new != object_new &&
2967 0 : type->tp_init != object_init)
2968 : {
2969 0 : err = PyErr_WarnEx(PyExc_DeprecationWarning,
2970 : "object() takes no parameters",
2971 : 1);
2972 : }
2973 2892 : else if (type->tp_new != object_new ||
2974 1446 : type->tp_init == object_init)
2975 : {
2976 0 : PyErr_SetString(PyExc_TypeError,
2977 : "object() takes no parameters");
2978 0 : err = -1;
2979 : }
2980 : }
2981 1758 : if (err < 0)
2982 0 : return NULL;
2983 :
2984 1758 : if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
2985 : static PyObject *comma = NULL;
2986 0 : PyObject *abstract_methods = NULL;
2987 : PyObject *builtins;
2988 : PyObject *sorted;
2989 0 : PyObject *sorted_methods = NULL;
2990 0 : PyObject *joined = NULL;
2991 : const char *joined_str;
2992 :
2993 : /* Compute ", ".join(sorted(type.__abstractmethods__))
2994 : into joined. */
2995 0 : abstract_methods = type_abstractmethods(type, NULL);
2996 0 : if (abstract_methods == NULL)
2997 0 : goto error;
2998 0 : builtins = PyEval_GetBuiltins();
2999 0 : if (builtins == NULL)
3000 0 : goto error;
3001 0 : sorted = PyDict_GetItemString(builtins, "sorted");
3002 0 : if (sorted == NULL)
3003 0 : goto error;
3004 0 : sorted_methods = PyObject_CallFunctionObjArgs(sorted,
3005 : abstract_methods,
3006 : NULL);
3007 0 : if (sorted_methods == NULL)
3008 0 : goto error;
3009 0 : if (comma == NULL) {
3010 0 : comma = PyString_InternFromString(", ");
3011 0 : if (comma == NULL)
3012 0 : goto error;
3013 : }
3014 0 : joined = PyObject_CallMethod(comma, "join",
3015 : "O", sorted_methods);
3016 0 : if (joined == NULL)
3017 0 : goto error;
3018 0 : joined_str = PyString_AsString(joined);
3019 0 : if (joined_str == NULL)
3020 0 : goto error;
3021 :
3022 0 : PyErr_Format(PyExc_TypeError,
3023 : "Can't instantiate abstract class %s "
3024 : "with abstract methods %s",
3025 : type->tp_name,
3026 : joined_str);
3027 : error:
3028 0 : Py_XDECREF(joined);
3029 0 : Py_XDECREF(sorted_methods);
3030 0 : Py_XDECREF(abstract_methods);
3031 0 : return NULL;
3032 : }
3033 1758 : return type->tp_alloc(type, 0);
3034 : }
3035 :
3036 : static void
3037 936 : object_dealloc(PyObject *self)
3038 : {
3039 936 : Py_TYPE(self)->tp_free(self);
3040 936 : }
3041 :
3042 : static PyObject *
3043 0 : object_repr(PyObject *self)
3044 : {
3045 : PyTypeObject *type;
3046 : PyObject *mod, *name, *rtn;
3047 :
3048 0 : type = Py_TYPE(self);
3049 0 : mod = type_module(type, NULL);
3050 0 : if (mod == NULL)
3051 0 : PyErr_Clear();
3052 0 : else if (!PyString_Check(mod)) {
3053 0 : Py_DECREF(mod);
3054 0 : mod = NULL;
3055 : }
3056 0 : name = type_name(type, NULL);
3057 0 : if (name == NULL) {
3058 0 : Py_XDECREF(mod);
3059 0 : return NULL;
3060 : }
3061 0 : if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
3062 0 : rtn = PyString_FromFormat("<%s.%s object at %p>",
3063 0 : PyString_AS_STRING(mod),
3064 0 : PyString_AS_STRING(name),
3065 : self);
3066 : else
3067 0 : rtn = PyString_FromFormat("<%s object at %p>",
3068 : type->tp_name, self);
3069 0 : Py_XDECREF(mod);
3070 0 : Py_DECREF(name);
3071 0 : return rtn;
3072 : }
3073 :
3074 : static PyObject *
3075 0 : object_str(PyObject *self)
3076 : {
3077 : unaryfunc f;
3078 :
3079 0 : f = Py_TYPE(self)->tp_repr;
3080 0 : if (f == NULL)
3081 0 : f = object_repr;
3082 0 : return f(self);
3083 : }
3084 :
3085 : static PyObject *
3086 7519 : object_get_class(PyObject *self, void *closure)
3087 : {
3088 7519 : Py_INCREF(Py_TYPE(self));
3089 7519 : return (PyObject *)(Py_TYPE(self));
3090 : }
3091 :
3092 : static int
3093 0 : equiv_structs(PyTypeObject *a, PyTypeObject *b)
3094 : {
3095 0 : return a == b ||
3096 0 : (a != NULL &&
3097 0 : b != NULL &&
3098 0 : a->tp_basicsize == b->tp_basicsize &&
3099 0 : a->tp_itemsize == b->tp_itemsize &&
3100 0 : a->tp_dictoffset == b->tp_dictoffset &&
3101 0 : a->tp_weaklistoffset == b->tp_weaklistoffset &&
3102 0 : ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3103 0 : (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
3104 : }
3105 :
3106 : static int
3107 0 : same_slots_added(PyTypeObject *a, PyTypeObject *b)
3108 : {
3109 0 : PyTypeObject *base = a->tp_base;
3110 : Py_ssize_t size;
3111 : PyObject *slots_a, *slots_b;
3112 :
3113 : assert(base == b->tp_base);
3114 0 : size = base->tp_basicsize;
3115 0 : if (a->tp_dictoffset == size && b->tp_dictoffset == size)
3116 0 : size += sizeof(PyObject *);
3117 0 : if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
3118 0 : size += sizeof(PyObject *);
3119 :
3120 : /* Check slots compliance */
3121 0 : slots_a = ((PyHeapTypeObject *)a)->ht_slots;
3122 0 : slots_b = ((PyHeapTypeObject *)b)->ht_slots;
3123 0 : if (slots_a && slots_b) {
3124 0 : if (PyObject_Compare(slots_a, slots_b) != 0)
3125 0 : return 0;
3126 0 : size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
3127 : }
3128 0 : return size == a->tp_basicsize && size == b->tp_basicsize;
3129 : }
3130 :
3131 : static int
3132 0 : compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
3133 : {
3134 : PyTypeObject *newbase, *oldbase;
3135 :
3136 0 : if (newto->tp_dealloc != oldto->tp_dealloc ||
3137 0 : newto->tp_free != oldto->tp_free)
3138 : {
3139 0 : PyErr_Format(PyExc_TypeError,
3140 : "%s assignment: "
3141 : "'%s' deallocator differs from '%s'",
3142 : attr,
3143 : newto->tp_name,
3144 : oldto->tp_name);
3145 0 : return 0;
3146 : }
3147 0 : newbase = newto;
3148 0 : oldbase = oldto;
3149 0 : while (equiv_structs(newbase, newbase->tp_base))
3150 0 : newbase = newbase->tp_base;
3151 0 : while (equiv_structs(oldbase, oldbase->tp_base))
3152 0 : oldbase = oldbase->tp_base;
3153 0 : if (newbase != oldbase &&
3154 0 : (newbase->tp_base != oldbase->tp_base ||
3155 0 : !same_slots_added(newbase, oldbase))) {
3156 0 : PyErr_Format(PyExc_TypeError,
3157 : "%s assignment: "
3158 : "'%s' object layout differs from '%s'",
3159 : attr,
3160 : newto->tp_name,
3161 : oldto->tp_name);
3162 0 : return 0;
3163 : }
3164 :
3165 0 : return 1;
3166 : }
3167 :
3168 : static int
3169 0 : object_set_class(PyObject *self, PyObject *value, void *closure)
3170 : {
3171 0 : PyTypeObject *oldto = Py_TYPE(self);
3172 : PyTypeObject *newto;
3173 :
3174 0 : if (value == NULL) {
3175 0 : PyErr_SetString(PyExc_TypeError,
3176 : "can't delete __class__ attribute");
3177 0 : return -1;
3178 : }
3179 0 : if (!PyType_Check(value)) {
3180 0 : PyErr_Format(PyExc_TypeError,
3181 : "__class__ must be set to new-style class, not '%s' object",
3182 0 : Py_TYPE(value)->tp_name);
3183 0 : return -1;
3184 : }
3185 0 : newto = (PyTypeObject *)value;
3186 0 : if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
3187 0 : !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
3188 : {
3189 0 : PyErr_Format(PyExc_TypeError,
3190 : "__class__ assignment: only for heap types");
3191 0 : return -1;
3192 : }
3193 0 : if (compatible_for_assignment(newto, oldto, "__class__")) {
3194 0 : Py_INCREF(newto);
3195 0 : Py_TYPE(self) = newto;
3196 0 : Py_DECREF(oldto);
3197 0 : return 0;
3198 : }
3199 : else {
3200 0 : return -1;
3201 : }
3202 : }
3203 :
3204 : static PyGetSetDef object_getsets[] = {
3205 : {"__class__", object_get_class, object_set_class,
3206 : PyDoc_STR("the object's class")},
3207 : {0}
3208 : };
3209 :
3210 :
3211 : /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
3212 : We fall back to helpers in copy_reg for:
3213 : - pickle protocols < 2
3214 : - calculating the list of slot names (done only once per class)
3215 : - the __newobj__ function (which is used as a token but never called)
3216 : */
3217 :
3218 : static PyObject *
3219 0 : import_copyreg(void)
3220 : {
3221 : static PyObject *copyreg_str;
3222 :
3223 0 : if (!copyreg_str) {
3224 0 : copyreg_str = PyString_InternFromString("copy_reg");
3225 0 : if (copyreg_str == NULL)
3226 0 : return NULL;
3227 : }
3228 :
3229 0 : return PyImport_Import(copyreg_str);
3230 : }
3231 :
3232 : static PyObject *
3233 0 : slotnames(PyObject *cls)
3234 : {
3235 : PyObject *clsdict;
3236 : PyObject *copyreg;
3237 : PyObject *slotnames;
3238 :
3239 0 : if (!PyType_Check(cls)) {
3240 0 : Py_INCREF(Py_None);
3241 0 : return Py_None;
3242 : }
3243 :
3244 0 : clsdict = ((PyTypeObject *)cls)->tp_dict;
3245 0 : slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
3246 0 : if (slotnames != NULL && PyList_Check(slotnames)) {
3247 0 : Py_INCREF(slotnames);
3248 0 : return slotnames;
3249 : }
3250 :
3251 0 : copyreg = import_copyreg();
3252 0 : if (copyreg == NULL)
3253 0 : return NULL;
3254 :
3255 0 : slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
3256 0 : Py_DECREF(copyreg);
3257 0 : if (slotnames != NULL &&
3258 0 : slotnames != Py_None &&
3259 0 : !PyList_Check(slotnames))
3260 : {
3261 0 : PyErr_SetString(PyExc_TypeError,
3262 : "copy_reg._slotnames didn't return a list or None");
3263 0 : Py_DECREF(slotnames);
3264 0 : slotnames = NULL;
3265 : }
3266 :
3267 0 : return slotnames;
3268 : }
3269 :
3270 : static PyObject *
3271 0 : reduce_2(PyObject *obj)
3272 : {
3273 : PyObject *cls, *getnewargs;
3274 0 : PyObject *args = NULL, *args2 = NULL;
3275 0 : PyObject *getstate = NULL, *state = NULL, *names = NULL;
3276 0 : PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
3277 0 : PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
3278 : Py_ssize_t i, n;
3279 0 : int required_state = 0;
3280 :
3281 0 : cls = PyObject_GetAttrString(obj, "__class__");
3282 0 : if (cls == NULL)
3283 0 : return NULL;
3284 :
3285 0 : if (PyType_Check(cls) && ((PyTypeObject *)cls)->tp_new == NULL) {
3286 0 : PyErr_Format(PyExc_TypeError,
3287 : "can't pickle %.200s objects",
3288 : ((PyTypeObject *)cls)->tp_name);
3289 0 : goto end;
3290 : }
3291 :
3292 0 : getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
3293 0 : if (getnewargs != NULL) {
3294 0 : args = PyObject_CallObject(getnewargs, NULL);
3295 0 : Py_DECREF(getnewargs);
3296 0 : if (args == NULL)
3297 0 : goto end;
3298 0 : if (!PyTuple_Check(args)) {
3299 0 : PyErr_Format(PyExc_TypeError,
3300 : "__getnewargs__ should return a tuple, "
3301 0 : "not '%.200s'", Py_TYPE(args)->tp_name);
3302 0 : goto end;
3303 : }
3304 : }
3305 : else {
3306 0 : PyErr_Clear();
3307 0 : required_state = !PyList_Check(obj) && !PyDict_Check(obj);
3308 : }
3309 :
3310 0 : getstate = PyObject_GetAttrString(obj, "__getstate__");
3311 0 : if (getstate != NULL) {
3312 0 : state = PyObject_CallObject(getstate, NULL);
3313 0 : Py_DECREF(getstate);
3314 0 : if (state == NULL)
3315 0 : goto end;
3316 : }
3317 : else {
3318 0 : PyErr_Clear();
3319 :
3320 0 : if (required_state && obj->ob_type->tp_itemsize) {
3321 0 : PyErr_Format(PyExc_TypeError,
3322 : "can't pickle %.200s objects",
3323 0 : Py_TYPE(obj)->tp_name);
3324 0 : goto end;
3325 : }
3326 :
3327 0 : state = PyObject_GetAttrString(obj, "__dict__");
3328 0 : if (state == NULL) {
3329 0 : PyErr_Clear();
3330 0 : state = Py_None;
3331 0 : Py_INCREF(state);
3332 : }
3333 0 : names = slotnames(cls);
3334 0 : if (names == NULL)
3335 0 : goto end;
3336 : assert(names == Py_None || PyList_Check(names));
3337 :
3338 0 : if (names != Py_None) {
3339 0 : slots = PyDict_New();
3340 0 : if (slots == NULL)
3341 0 : goto end;
3342 0 : n = 0;
3343 : /* Can't pre-compute the list size; the list
3344 : is stored on the class so accessible to other
3345 : threads, which may be run by DECREF */
3346 0 : for (i = 0; i < PyList_GET_SIZE(names); i++) {
3347 : PyObject *name, *value;
3348 0 : name = PyList_GET_ITEM(names, i);
3349 0 : Py_INCREF(name);
3350 0 : value = PyObject_GetAttr(obj, name);
3351 0 : if (value == NULL) {
3352 0 : Py_DECREF(name);
3353 0 : PyErr_Clear();
3354 : }
3355 : else {
3356 0 : int err = PyDict_SetItem(slots, name,
3357 : value);
3358 0 : Py_DECREF(name);
3359 0 : Py_DECREF(value);
3360 0 : if (err)
3361 0 : goto end;
3362 0 : n++;
3363 : }
3364 : }
3365 0 : if (n) {
3366 0 : state = Py_BuildValue("(NO)", state, slots);
3367 0 : if (state == NULL)
3368 0 : goto end;
3369 : }
3370 : }
3371 : }
3372 :
3373 0 : if (!PyList_Check(obj)) {
3374 0 : listitems = Py_None;
3375 0 : Py_INCREF(listitems);
3376 : }
3377 : else {
3378 0 : listitems = PyObject_GetIter(obj);
3379 0 : if (listitems == NULL)
3380 0 : goto end;
3381 : }
3382 :
3383 0 : if (!PyDict_Check(obj)) {
3384 0 : dictitems = Py_None;
3385 0 : Py_INCREF(dictitems);
3386 : }
3387 : else {
3388 0 : dictitems = PyObject_CallMethod(obj, "iteritems", "");
3389 0 : if (dictitems == NULL)
3390 0 : goto end;
3391 : }
3392 :
3393 0 : copyreg = import_copyreg();
3394 0 : if (copyreg == NULL)
3395 0 : goto end;
3396 0 : newobj = PyObject_GetAttrString(copyreg, "__newobj__");
3397 0 : if (newobj == NULL)
3398 0 : goto end;
3399 :
3400 0 : n = args ? PyTuple_GET_SIZE(args) : 0;
3401 0 : args2 = PyTuple_New(n+1);
3402 0 : if (args2 == NULL)
3403 0 : goto end;
3404 0 : PyTuple_SET_ITEM(args2, 0, cls);
3405 0 : cls = NULL;
3406 0 : for (i = 0; i < n; i++) {
3407 0 : PyObject *v = PyTuple_GET_ITEM(args, i);
3408 0 : Py_INCREF(v);
3409 0 : PyTuple_SET_ITEM(args2, i+1, v);
3410 : }
3411 :
3412 0 : res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
3413 :
3414 : end:
3415 0 : Py_XDECREF(cls);
3416 0 : Py_XDECREF(args);
3417 0 : Py_XDECREF(args2);
3418 0 : Py_XDECREF(slots);
3419 0 : Py_XDECREF(state);
3420 0 : Py_XDECREF(names);
3421 0 : Py_XDECREF(listitems);
3422 0 : Py_XDECREF(dictitems);
3423 0 : Py_XDECREF(copyreg);
3424 0 : Py_XDECREF(newobj);
3425 0 : return res;
3426 : }
3427 :
3428 : /*
3429 : * There were two problems when object.__reduce__ and object.__reduce_ex__
3430 : * were implemented in the same function:
3431 : * - trying to pickle an object with a custom __reduce__ method that
3432 : * fell back to object.__reduce__ in certain circumstances led to
3433 : * infinite recursion at Python level and eventual RuntimeError.
3434 : * - Pickling objects that lied about their type by overwriting the
3435 : * __class__ descriptor could lead to infinite recursion at C level
3436 : * and eventual segfault.
3437 : *
3438 : * Because of backwards compatibility, the two methods still have to
3439 : * behave in the same way, even if this is not required by the pickle
3440 : * protocol. This common functionality was moved to the _common_reduce
3441 : * function.
3442 : */
3443 : static PyObject *
3444 0 : _common_reduce(PyObject *self, int proto)
3445 : {
3446 : PyObject *copyreg, *res;
3447 :
3448 0 : if (proto >= 2)
3449 0 : return reduce_2(self);
3450 :
3451 0 : copyreg = import_copyreg();
3452 0 : if (!copyreg)
3453 0 : return NULL;
3454 :
3455 0 : res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
3456 0 : Py_DECREF(copyreg);
3457 :
3458 0 : return res;
3459 : }
3460 :
3461 : static PyObject *
3462 0 : object_reduce(PyObject *self, PyObject *args)
3463 : {
3464 0 : int proto = 0;
3465 :
3466 0 : if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
3467 0 : return NULL;
3468 :
3469 0 : return _common_reduce(self, proto);
3470 : }
3471 :
3472 : static PyObject *
3473 0 : object_reduce_ex(PyObject *self, PyObject *args)
3474 : {
3475 : PyObject *reduce, *res;
3476 0 : int proto = 0;
3477 :
3478 0 : if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
3479 0 : return NULL;
3480 :
3481 0 : reduce = PyObject_GetAttrString(self, "__reduce__");
3482 0 : if (reduce == NULL)
3483 0 : PyErr_Clear();
3484 : else {
3485 : PyObject *cls, *clsreduce, *objreduce;
3486 : int override;
3487 0 : cls = PyObject_GetAttrString(self, "__class__");
3488 0 : if (cls == NULL) {
3489 0 : Py_DECREF(reduce);
3490 0 : return NULL;
3491 : }
3492 0 : clsreduce = PyObject_GetAttrString(cls, "__reduce__");
3493 0 : Py_DECREF(cls);
3494 0 : if (clsreduce == NULL) {
3495 0 : Py_DECREF(reduce);
3496 0 : return NULL;
3497 : }
3498 0 : objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
3499 : "__reduce__");
3500 0 : override = (clsreduce != objreduce);
3501 0 : Py_DECREF(clsreduce);
3502 0 : if (override) {
3503 0 : res = PyObject_CallObject(reduce, NULL);
3504 0 : Py_DECREF(reduce);
3505 0 : return res;
3506 : }
3507 : else
3508 0 : Py_DECREF(reduce);
3509 : }
3510 :
3511 0 : return _common_reduce(self, proto);
3512 : }
3513 :
3514 : static PyObject *
3515 24 : object_subclasshook(PyObject *cls, PyObject *args)
3516 : {
3517 24 : Py_INCREF(Py_NotImplemented);
3518 24 : return Py_NotImplemented;
3519 : }
3520 :
3521 : PyDoc_STRVAR(object_subclasshook_doc,
3522 : "Abstract classes can override this to customize issubclass().\n"
3523 : "\n"
3524 : "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
3525 : "It should return True, False or NotImplemented. If it returns\n"
3526 : "NotImplemented, the normal algorithm is used. Otherwise, it\n"
3527 : "overrides the normal algorithm (and the outcome is cached).\n");
3528 :
3529 : /*
3530 : from PEP 3101, this code implements:
3531 :
3532 : class object:
3533 : def __format__(self, format_spec):
3534 : if isinstance(format_spec, str):
3535 : return format(str(self), format_spec)
3536 : elif isinstance(format_spec, unicode):
3537 : return format(unicode(self), format_spec)
3538 : */
3539 : static PyObject *
3540 0 : object_format(PyObject *self, PyObject *args)
3541 : {
3542 : PyObject *format_spec;
3543 0 : PyObject *self_as_str = NULL;
3544 0 : PyObject *result = NULL;
3545 : Py_ssize_t format_len;
3546 :
3547 0 : if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
3548 0 : return NULL;
3549 : #ifdef Py_USING_UNICODE
3550 0 : if (PyUnicode_Check(format_spec)) {
3551 0 : format_len = PyUnicode_GET_SIZE(format_spec);
3552 0 : self_as_str = PyObject_Unicode(self);
3553 0 : } else if (PyString_Check(format_spec)) {
3554 : #else
3555 : if (PyString_Check(format_spec)) {
3556 : #endif
3557 0 : format_len = PyString_GET_SIZE(format_spec);
3558 0 : self_as_str = PyObject_Str(self);
3559 : } else {
3560 0 : PyErr_SetString(PyExc_TypeError,
3561 : "argument to __format__ must be unicode or str");
3562 0 : return NULL;
3563 : }
3564 :
3565 0 : if (self_as_str != NULL) {
3566 : /* Issue 7994: If we're converting to a string, we
3567 : should reject format specifications */
3568 0 : if (format_len > 0) {
3569 0 : if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,
3570 : "object.__format__ with a non-empty format "
3571 : "string is deprecated", 1) < 0) {
3572 0 : goto done;
3573 : }
3574 : /* Eventually this will become an error:
3575 : PyErr_Format(PyExc_TypeError,
3576 : "non-empty format string passed to object.__format__");
3577 : goto done;
3578 : */
3579 : }
3580 0 : result = PyObject_Format(self_as_str, format_spec);
3581 : }
3582 :
3583 : done:
3584 0 : Py_XDECREF(self_as_str);
3585 :
3586 0 : return result;
3587 : }
3588 :
3589 : static PyObject *
3590 0 : object_sizeof(PyObject *self, PyObject *args)
3591 : {
3592 : Py_ssize_t res, isize;
3593 :
3594 0 : res = 0;
3595 0 : isize = self->ob_type->tp_itemsize;
3596 0 : if (isize > 0)
3597 0 : res = Py_SIZE(self) * isize;
3598 0 : res += self->ob_type->tp_basicsize;
3599 :
3600 0 : return PyInt_FromSsize_t(res);
3601 : }
3602 :
3603 : static PyMethodDef object_methods[] = {
3604 : {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
3605 : PyDoc_STR("helper for pickle")},
3606 : {"__reduce__", object_reduce, METH_VARARGS,
3607 : PyDoc_STR("helper for pickle")},
3608 : {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
3609 : object_subclasshook_doc},
3610 : {"__format__", object_format, METH_VARARGS,
3611 : PyDoc_STR("default object formatter")},
3612 : {"__sizeof__", object_sizeof, METH_NOARGS,
3613 : PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")},
3614 : {0}
3615 : };
3616 :
3617 :
3618 : PyTypeObject PyBaseObject_Type = {
3619 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
3620 : "object", /* tp_name */
3621 : sizeof(PyObject), /* tp_basicsize */
3622 : 0, /* tp_itemsize */
3623 : object_dealloc, /* tp_dealloc */
3624 : 0, /* tp_print */
3625 : 0, /* tp_getattr */
3626 : 0, /* tp_setattr */
3627 : 0, /* tp_compare */
3628 : object_repr, /* tp_repr */
3629 : 0, /* tp_as_number */
3630 : 0, /* tp_as_sequence */
3631 : 0, /* tp_as_mapping */
3632 : (hashfunc)_Py_HashPointer, /* tp_hash */
3633 : 0, /* tp_call */
3634 : object_str, /* tp_str */
3635 : PyObject_GenericGetAttr, /* tp_getattro */
3636 : PyObject_GenericSetAttr, /* tp_setattro */
3637 : 0, /* tp_as_buffer */
3638 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3639 : PyDoc_STR("The most base type"), /* tp_doc */
3640 : 0, /* tp_traverse */
3641 : 0, /* tp_clear */
3642 : 0, /* tp_richcompare */
3643 : 0, /* tp_weaklistoffset */
3644 : 0, /* tp_iter */
3645 : 0, /* tp_iternext */
3646 : object_methods, /* tp_methods */
3647 : 0, /* tp_members */
3648 : object_getsets, /* tp_getset */
3649 : 0, /* tp_base */
3650 : 0, /* tp_dict */
3651 : 0, /* tp_descr_get */
3652 : 0, /* tp_descr_set */
3653 : 0, /* tp_dictoffset */
3654 : object_init, /* tp_init */
3655 : PyType_GenericAlloc, /* tp_alloc */
3656 : object_new, /* tp_new */
3657 : PyObject_Del, /* tp_free */
3658 : };
3659 :
3660 :
3661 : /* Initialize the __dict__ in a type object */
3662 :
3663 : static int
3664 219 : add_methods(PyTypeObject *type, PyMethodDef *meth)
3665 : {
3666 219 : PyObject *dict = type->tp_dict;
3667 :
3668 1821 : for (; meth->ml_name != NULL; meth++) {
3669 : PyObject *descr;
3670 : int err;
3671 1617 : if (PyDict_GetItemString(dict, meth->ml_name) &&
3672 15 : !(meth->ml_flags & METH_COEXIST))
3673 0 : continue;
3674 1602 : if (meth->ml_flags & METH_CLASS) {
3675 21 : if (meth->ml_flags & METH_STATIC) {
3676 0 : PyErr_SetString(PyExc_ValueError,
3677 : "method cannot be both class and static");
3678 0 : return -1;
3679 : }
3680 21 : descr = PyDescr_NewClassMethod(type, meth);
3681 : }
3682 1581 : else if (meth->ml_flags & METH_STATIC) {
3683 0 : PyObject *cfunc = PyCFunction_New(meth, NULL);
3684 0 : if (cfunc == NULL)
3685 0 : return -1;
3686 0 : descr = PyStaticMethod_New(cfunc);
3687 0 : Py_DECREF(cfunc);
3688 : }
3689 : else {
3690 1581 : descr = PyDescr_NewMethod(type, meth);
3691 : }
3692 1602 : if (descr == NULL)
3693 0 : return -1;
3694 1602 : err = PyDict_SetItemString(dict, meth->ml_name, descr);
3695 1602 : Py_DECREF(descr);
3696 1602 : if (err < 0)
3697 0 : return -1;
3698 : }
3699 219 : return 0;
3700 : }
3701 :
3702 : static int
3703 933 : add_members(PyTypeObject *type, PyMemberDef *memb)
3704 : {
3705 933 : PyObject *dict = type->tp_dict;
3706 :
3707 1563 : for (; memb->name != NULL; memb++) {
3708 : PyObject *descr;
3709 630 : if (PyDict_GetItemString(dict, memb->name))
3710 0 : continue;
3711 630 : descr = PyDescr_NewMember(type, memb);
3712 630 : if (descr == NULL)
3713 0 : return -1;
3714 630 : if (PyDict_SetItemString(dict, memb->name, descr) < 0) {
3715 0 : Py_DECREF(descr);
3716 0 : return -1;
3717 : }
3718 630 : Py_DECREF(descr);
3719 : }
3720 933 : return 0;
3721 : }
3722 :
3723 : static int
3724 429 : add_getset(PyTypeObject *type, PyGetSetDef *gsp)
3725 : {
3726 429 : PyObject *dict = type->tp_dict;
3727 :
3728 1278 : for (; gsp->name != NULL; gsp++) {
3729 : PyObject *descr;
3730 849 : if (PyDict_GetItemString(dict, gsp->name))
3731 0 : continue;
3732 849 : descr = PyDescr_NewGetSet(type, gsp);
3733 :
3734 849 : if (descr == NULL)
3735 0 : return -1;
3736 849 : if (PyDict_SetItemString(dict, gsp->name, descr) < 0) {
3737 0 : Py_DECREF(descr);
3738 0 : return -1;
3739 : }
3740 849 : Py_DECREF(descr);
3741 : }
3742 429 : return 0;
3743 : }
3744 :
3745 : #define BUFFER_FLAGS (Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER)
3746 :
3747 : static void
3748 1293 : inherit_special(PyTypeObject *type, PyTypeObject *base)
3749 : {
3750 : Py_ssize_t oldsize, newsize;
3751 :
3752 : /* Special flag magic */
3753 1293 : if (!type->tp_as_buffer && base->tp_as_buffer) {
3754 0 : type->tp_flags &= ~BUFFER_FLAGS;
3755 0 : type->tp_flags |=
3756 0 : base->tp_flags & BUFFER_FLAGS;
3757 : }
3758 1293 : if (!type->tp_as_sequence && base->tp_as_sequence) {
3759 147 : type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
3760 147 : type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
3761 : }
3762 2586 : if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
3763 1293 : (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
3764 18 : if ((!type->tp_as_number && base->tp_as_number) ||
3765 18 : (!type->tp_as_sequence && base->tp_as_sequence)) {
3766 0 : type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
3767 0 : if (!type->tp_as_number && !type->tp_as_sequence) {
3768 0 : type->tp_flags |= base->tp_flags &
3769 : Py_TPFLAGS_HAVE_INPLACEOPS;
3770 : }
3771 : }
3772 : /* Wow */
3773 : }
3774 1293 : if (!type->tp_as_number && base->tp_as_number) {
3775 0 : type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
3776 0 : type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
3777 : }
3778 :
3779 : /* Copying basicsize is connected to the GC flags */
3780 1293 : oldsize = base->tp_basicsize;
3781 1293 : newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
3782 1419 : if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3783 138 : (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3784 24 : (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
3785 24 : (!type->tp_traverse && !type->tp_clear)) {
3786 12 : type->tp_flags |= Py_TPFLAGS_HAVE_GC;
3787 12 : if (type->tp_traverse == NULL)
3788 12 : type->tp_traverse = base->tp_traverse;
3789 12 : if (type->tp_clear == NULL)
3790 12 : type->tp_clear = base->tp_clear;
3791 : }
3792 1293 : if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3793 : /* The condition below could use some explanation.
3794 : It appears that tp_new is not inherited for static types
3795 : whose base class is 'object'; this seems to be a precaution
3796 : so that old extension types don't suddenly become
3797 : callable (object.__new__ wouldn't insure the invariants
3798 : that the extension type's own factory function ensures).
3799 : Heap types, of course, are under our control, so they do
3800 : inherit tp_new; static extension types that specify some
3801 : other built-in type as the default are considered
3802 : new-style-aware so they also inherit object.__new__. */
3803 1824 : if (base != &PyBaseObject_Type ||
3804 540 : (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3805 978 : if (type->tp_new == NULL)
3806 804 : type->tp_new = base->tp_new;
3807 : }
3808 : }
3809 1293 : type->tp_basicsize = newsize;
3810 :
3811 : /* Copy other non-function slots */
3812 :
3813 : #undef COPYVAL
3814 : #define COPYVAL(SLOT) \
3815 : if (type->SLOT == 0) type->SLOT = base->SLOT
3816 :
3817 1293 : COPYVAL(tp_itemsize);
3818 1293 : if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
3819 1284 : COPYVAL(tp_weaklistoffset);
3820 : }
3821 1293 : if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3822 1284 : COPYVAL(tp_dictoffset);
3823 : }
3824 :
3825 : /* Setup fast subclass flags */
3826 1293 : if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
3827 228 : type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
3828 1065 : else if (PyType_IsSubtype(base, &PyType_Type))
3829 6 : type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
3830 1059 : else if (PyType_IsSubtype(base, &PyInt_Type))
3831 3 : type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS;
3832 1056 : else if (PyType_IsSubtype(base, &PyLong_Type))
3833 0 : type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
3834 1056 : else if (PyType_IsSubtype(base, &PyString_Type))
3835 0 : type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS;
3836 : #ifdef Py_USING_UNICODE
3837 1056 : else if (PyType_IsSubtype(base, &PyUnicode_Type))
3838 0 : type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
3839 : #endif
3840 1056 : else if (PyType_IsSubtype(base, &PyTuple_Type))
3841 36 : type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
3842 1020 : else if (PyType_IsSubtype(base, &PyList_Type))
3843 0 : type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
3844 1020 : else if (PyType_IsSubtype(base, &PyDict_Type))
3845 9 : type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
3846 1293 : }
3847 :
3848 : static int
3849 0 : overrides_name(PyTypeObject *type, char *name)
3850 : {
3851 0 : PyObject *dict = type->tp_dict;
3852 :
3853 : assert(dict != NULL);
3854 0 : if (PyDict_GetItemString(dict, name) != NULL) {
3855 0 : return 1;
3856 : }
3857 0 : return 0;
3858 : }
3859 :
3860 : #define OVERRIDES_HASH(x) overrides_name(x, "__hash__")
3861 : #define OVERRIDES_EQ(x) overrides_name(x, "__eq__")
3862 :
3863 : static void
3864 3168 : inherit_slots(PyTypeObject *type, PyTypeObject *base)
3865 : {
3866 : PyTypeObject *basebase;
3867 :
3868 : #undef SLOTDEFINED
3869 : #undef COPYSLOT
3870 : #undef COPYNUM
3871 : #undef COPYSEQ
3872 : #undef COPYMAP
3873 : #undef COPYBUF
3874 :
3875 : #define SLOTDEFINED(SLOT) \
3876 : (base->SLOT != 0 && \
3877 : (basebase == NULL || base->SLOT != basebase->SLOT))
3878 :
3879 : #define COPYSLOT(SLOT) \
3880 : if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3881 :
3882 : #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3883 : #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3884 : #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3885 : #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3886 :
3887 : /* This won't inherit indirect slots (from tp_as_number etc.)
3888 : if type doesn't provide the space. */
3889 :
3890 3168 : if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3891 1029 : basebase = base->tp_base;
3892 1029 : if (basebase->tp_as_number == NULL)
3893 489 : basebase = NULL;
3894 1029 : COPYNUM(nb_add);
3895 1029 : COPYNUM(nb_subtract);
3896 1029 : COPYNUM(nb_multiply);
3897 1029 : COPYNUM(nb_divide);
3898 1029 : COPYNUM(nb_remainder);
3899 1029 : COPYNUM(nb_divmod);
3900 1029 : COPYNUM(nb_power);
3901 1029 : COPYNUM(nb_negative);
3902 1029 : COPYNUM(nb_positive);
3903 1029 : COPYNUM(nb_absolute);
3904 1029 : COPYNUM(nb_nonzero);
3905 1029 : COPYNUM(nb_invert);
3906 1029 : COPYNUM(nb_lshift);
3907 1029 : COPYNUM(nb_rshift);
3908 1029 : COPYNUM(nb_and);
3909 1029 : COPYNUM(nb_xor);
3910 1029 : COPYNUM(nb_or);
3911 1029 : COPYNUM(nb_coerce);
3912 1029 : COPYNUM(nb_int);
3913 1029 : COPYNUM(nb_long);
3914 1029 : COPYNUM(nb_float);
3915 1029 : COPYNUM(nb_oct);
3916 1029 : COPYNUM(nb_hex);
3917 1029 : COPYNUM(nb_inplace_add);
3918 1029 : COPYNUM(nb_inplace_subtract);
3919 1029 : COPYNUM(nb_inplace_multiply);
3920 1029 : COPYNUM(nb_inplace_divide);
3921 1029 : COPYNUM(nb_inplace_remainder);
3922 1029 : COPYNUM(nb_inplace_power);
3923 1029 : COPYNUM(nb_inplace_lshift);
3924 1029 : COPYNUM(nb_inplace_rshift);
3925 1029 : COPYNUM(nb_inplace_and);
3926 1029 : COPYNUM(nb_inplace_xor);
3927 1029 : COPYNUM(nb_inplace_or);
3928 1029 : if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3929 1029 : COPYNUM(nb_true_divide);
3930 1029 : COPYNUM(nb_floor_divide);
3931 1029 : COPYNUM(nb_inplace_true_divide);
3932 1029 : COPYNUM(nb_inplace_floor_divide);
3933 : }
3934 1029 : if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
3935 1029 : COPYNUM(nb_index);
3936 : }
3937 : }
3938 :
3939 3168 : if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3940 1302 : basebase = base->tp_base;
3941 1302 : if (basebase->tp_as_sequence == NULL)
3942 591 : basebase = NULL;
3943 1302 : COPYSEQ(sq_length);
3944 1302 : COPYSEQ(sq_concat);
3945 1302 : COPYSEQ(sq_repeat);
3946 1302 : COPYSEQ(sq_item);
3947 1302 : COPYSEQ(sq_slice);
3948 1302 : COPYSEQ(sq_ass_item);
3949 1302 : COPYSEQ(sq_ass_slice);
3950 1302 : COPYSEQ(sq_contains);
3951 1302 : COPYSEQ(sq_inplace_concat);
3952 1302 : COPYSEQ(sq_inplace_repeat);
3953 : }
3954 :
3955 3168 : if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3956 1068 : basebase = base->tp_base;
3957 1068 : if (basebase->tp_as_mapping == NULL)
3958 522 : basebase = NULL;
3959 1068 : COPYMAP(mp_length);
3960 1068 : COPYMAP(mp_subscript);
3961 1068 : COPYMAP(mp_ass_subscript);
3962 : }
3963 :
3964 3168 : if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3965 1026 : basebase = base->tp_base;
3966 1026 : if (basebase->tp_as_buffer == NULL)
3967 486 : basebase = NULL;
3968 1026 : COPYBUF(bf_getreadbuffer);
3969 1026 : COPYBUF(bf_getwritebuffer);
3970 1026 : COPYBUF(bf_getsegcount);
3971 1026 : COPYBUF(bf_getcharbuffer);
3972 1026 : COPYBUF(bf_getbuffer);
3973 1026 : COPYBUF(bf_releasebuffer);
3974 : }
3975 :
3976 3168 : basebase = base->tp_base;
3977 :
3978 3168 : COPYSLOT(tp_dealloc);
3979 3168 : COPYSLOT(tp_print);
3980 3168 : if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3981 1047 : type->tp_getattr = base->tp_getattr;
3982 1047 : type->tp_getattro = base->tp_getattro;
3983 : }
3984 3168 : if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3985 1254 : type->tp_setattr = base->tp_setattr;
3986 1254 : type->tp_setattro = base->tp_setattro;
3987 : }
3988 : /* tp_compare see tp_richcompare */
3989 3168 : COPYSLOT(tp_repr);
3990 : /* tp_hash see tp_richcompare */
3991 3168 : COPYSLOT(tp_call);
3992 3168 : COPYSLOT(tp_str);
3993 3168 : if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3994 6261 : if (type->tp_compare == NULL &&
3995 5274 : type->tp_richcompare == NULL &&
3996 2172 : type->tp_hash == NULL)
3997 : {
3998 1176 : type->tp_compare = base->tp_compare;
3999 1176 : type->tp_richcompare = base->tp_richcompare;
4000 1176 : type->tp_hash = base->tp_hash;
4001 : /* Check for changes to inherited methods in Py3k*/
4002 1176 : if (Py_Py3kWarningFlag) {
4003 0 : if (base->tp_hash &&
4004 0 : (base->tp_hash != PyObject_HashNotImplemented) &&
4005 0 : !OVERRIDES_HASH(type)) {
4006 0 : if (OVERRIDES_EQ(type)) {
4007 0 : if (PyErr_WarnPy3k("Overriding "
4008 : "__eq__ blocks inheritance "
4009 : "of __hash__ in 3.x",
4010 0 : 1) < 0)
4011 : /* XXX This isn't right. If the warning is turned
4012 : into an exception, we should be communicating
4013 : the error back to the caller, but figuring out
4014 : how to clean up in that case is tricky. See
4015 : issue 8627 for more. */
4016 0 : PyErr_Clear();
4017 : }
4018 : }
4019 : }
4020 : }
4021 : }
4022 : else {
4023 9 : COPYSLOT(tp_compare);
4024 : }
4025 3168 : if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
4026 3159 : COPYSLOT(tp_iter);
4027 3159 : COPYSLOT(tp_iternext);
4028 : }
4029 3168 : if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
4030 3159 : COPYSLOT(tp_descr_get);
4031 3159 : COPYSLOT(tp_descr_set);
4032 3159 : COPYSLOT(tp_dictoffset);
4033 3159 : COPYSLOT(tp_init);
4034 3159 : COPYSLOT(tp_alloc);
4035 3159 : COPYSLOT(tp_is_gc);
4036 6318 : if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
4037 3159 : (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
4038 : /* They agree about gc. */
4039 1971 : COPYSLOT(tp_free);
4040 : }
4041 2376 : else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
4042 1281 : type->tp_free == NULL &&
4043 93 : base->tp_free == _PyObject_Del) {
4044 : /* A bit of magic to plug in the correct default
4045 : * tp_free function when a derived class adds gc,
4046 : * didn't define tp_free, and the base uses the
4047 : * default non-gc tp_free.
4048 : */
4049 93 : type->tp_free = PyObject_GC_Del;
4050 : }
4051 : /* else they didn't agree about gc, and there isn't something
4052 : * obvious to be done -- the type is on its own.
4053 : */
4054 : }
4055 3168 : }
4056 :
4057 : static int add_operators(PyTypeObject *);
4058 :
4059 : int
4060 1308 : PyType_Ready(PyTypeObject *type)
4061 : {
4062 : PyObject *dict, *bases;
4063 : PyTypeObject *base;
4064 : Py_ssize_t i, n;
4065 :
4066 1308 : if (type->tp_flags & Py_TPFLAGS_READY) {
4067 : assert(type->tp_dict != NULL);
4068 12 : return 0;
4069 : }
4070 : assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
4071 :
4072 1296 : type->tp_flags |= Py_TPFLAGS_READYING;
4073 :
4074 : #ifdef Py_TRACE_REFS
4075 : /* PyType_Ready is the closest thing we have to a choke point
4076 : * for type objects, so is the best place I can think of to try
4077 : * to get type objects into the doubly-linked list of all objects.
4078 : * Still, not all type objects go thru PyType_Ready.
4079 : */
4080 : _Py_AddToAllObjects((PyObject *)type, 0);
4081 : #endif
4082 :
4083 1296 : if (type->tp_name == NULL) {
4084 0 : PyErr_Format(PyExc_SystemError,
4085 : "Type does not define the tp_name field.");
4086 0 : goto error;
4087 : }
4088 :
4089 : /* Initialize tp_base (defaults to BaseObject unless that's us) */
4090 1296 : base = type->tp_base;
4091 1296 : if (base == NULL && type != &PyBaseObject_Type) {
4092 312 : base = type->tp_base = &PyBaseObject_Type;
4093 312 : Py_INCREF(base);
4094 : }
4095 :
4096 : /* Now the only way base can still be NULL is if type is
4097 : * &PyBaseObject_Type.
4098 : */
4099 :
4100 : /* Initialize the base class */
4101 1296 : if (base && base->tp_dict == NULL) {
4102 9 : if (PyType_Ready(base) < 0)
4103 0 : goto error;
4104 : }
4105 :
4106 : /* Initialize ob_type if NULL. This means extensions that want to be
4107 : compilable separately on Windows can call PyType_Ready() instead of
4108 : initializing the ob_type field of their type objects. */
4109 : /* The test for base != NULL is really unnecessary, since base is only
4110 : NULL when type is &PyBaseObject_Type, and we know its ob_type is
4111 : not NULL (it's initialized to &PyType_Type). But coverity doesn't
4112 : know that. */
4113 1296 : if (Py_TYPE(type) == NULL && base != NULL)
4114 300 : Py_TYPE(type) = Py_TYPE(base);
4115 :
4116 : /* Initialize tp_bases */
4117 1296 : bases = type->tp_bases;
4118 1296 : if (bases == NULL) {
4119 507 : if (base == NULL)
4120 3 : bases = PyTuple_New(0);
4121 : else
4122 504 : bases = PyTuple_Pack(1, base);
4123 507 : if (bases == NULL)
4124 0 : goto error;
4125 507 : type->tp_bases = bases;
4126 : }
4127 :
4128 : /* Initialize tp_dict */
4129 1296 : dict = type->tp_dict;
4130 1296 : if (dict == NULL) {
4131 507 : dict = PyDict_New();
4132 507 : if (dict == NULL)
4133 0 : goto error;
4134 507 : type->tp_dict = dict;
4135 : }
4136 :
4137 : /* Add type-specific descriptors to tp_dict */
4138 1296 : if (add_operators(type) < 0)
4139 0 : goto error;
4140 1296 : if (type->tp_methods != NULL) {
4141 219 : if (add_methods(type, type->tp_methods) < 0)
4142 0 : goto error;
4143 : }
4144 1296 : if (type->tp_members != NULL) {
4145 933 : if (add_members(type, type->tp_members) < 0)
4146 0 : goto error;
4147 : }
4148 1296 : if (type->tp_getset != NULL) {
4149 429 : if (add_getset(type, type->tp_getset) < 0)
4150 0 : goto error;
4151 : }
4152 :
4153 : /* Calculate method resolution order */
4154 1296 : if (mro_internal(type) < 0) {
4155 0 : goto error;
4156 : }
4157 :
4158 : /* Inherit special flags from dominant base */
4159 1296 : if (type->tp_base != NULL)
4160 1293 : inherit_special(type, type->tp_base);
4161 :
4162 : /* Initialize tp_dict properly */
4163 1296 : bases = type->tp_mro;
4164 : assert(bases != NULL);
4165 : assert(PyTuple_Check(bases));
4166 1296 : n = PyTuple_GET_SIZE(bases);
4167 4464 : for (i = 1; i < n; i++) {
4168 3168 : PyObject *b = PyTuple_GET_ITEM(bases, i);
4169 3168 : if (PyType_Check(b))
4170 3168 : inherit_slots(type, (PyTypeObject *)b);
4171 : }
4172 :
4173 : /* All bases of statically allocated type should be statically allocated */
4174 1296 : if (Py_Py3kWarningFlag && !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
4175 0 : for (i = 0; i < n; i++) {
4176 0 : PyObject *b = PyTuple_GET_ITEM(bases, i);
4177 0 : if (PyType_Check(b) &&
4178 0 : (((PyTypeObject *)b)->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
4179 : char buf[300];
4180 0 : PyOS_snprintf(buf, sizeof(buf),
4181 : "type '%.100s' is not dynamically allocated but "
4182 : "its base type '%.100s' is dynamically allocated",
4183 : type->tp_name, ((PyTypeObject *)b)->tp_name);
4184 0 : if (PyErr_WarnPy3k(buf, 1) < 0)
4185 0 : goto error;
4186 0 : break;
4187 : }
4188 : }
4189 :
4190 : /* Sanity check for tp_free. */
4191 2379 : if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
4192 2166 : (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
4193 : /* This base class needs to call tp_free, but doesn't have
4194 : * one, or its tp_free is for non-gc'ed objects.
4195 : */
4196 0 : PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
4197 : "gc and is a base type but has inappropriate "
4198 : "tp_free slot",
4199 : type->tp_name);
4200 0 : goto error;
4201 : }
4202 :
4203 : /* if the type dictionary doesn't contain a __doc__, set it from
4204 : the tp_doc slot.
4205 : */
4206 1296 : if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
4207 996 : if (type->tp_doc != NULL) {
4208 414 : PyObject *doc = PyString_FromString(type->tp_doc);
4209 414 : if (doc == NULL)
4210 0 : goto error;
4211 414 : PyDict_SetItemString(type->tp_dict, "__doc__", doc);
4212 414 : Py_DECREF(doc);
4213 : } else {
4214 582 : PyDict_SetItemString(type->tp_dict,
4215 : "__doc__", Py_None);
4216 : }
4217 : }
4218 :
4219 : /* Some more special stuff */
4220 1296 : base = type->tp_base;
4221 1296 : if (base != NULL) {
4222 1293 : if (type->tp_as_number == NULL)
4223 462 : type->tp_as_number = base->tp_as_number;
4224 1293 : if (type->tp_as_sequence == NULL)
4225 420 : type->tp_as_sequence = base->tp_as_sequence;
4226 1293 : if (type->tp_as_mapping == NULL)
4227 444 : type->tp_as_mapping = base->tp_as_mapping;
4228 1293 : if (type->tp_as_buffer == NULL)
4229 489 : type->tp_as_buffer = base->tp_as_buffer;
4230 : }
4231 :
4232 : /* Link into each base class's list of subclasses */
4233 1296 : bases = type->tp_bases;
4234 1296 : n = PyTuple_GET_SIZE(bases);
4235 2634 : for (i = 0; i < n; i++) {
4236 1338 : PyObject *b = PyTuple_GET_ITEM(bases, i);
4237 2676 : if (PyType_Check(b) &&
4238 1338 : add_subclass((PyTypeObject *)b, type) < 0)
4239 0 : goto error;
4240 : }
4241 :
4242 : /* All done -- set the ready flag */
4243 : assert(type->tp_dict != NULL);
4244 1296 : type->tp_flags =
4245 1296 : (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
4246 1296 : return 0;
4247 :
4248 : error:
4249 0 : type->tp_flags &= ~Py_TPFLAGS_READYING;
4250 0 : return -1;
4251 : }
4252 :
4253 : static int
4254 1338 : add_subclass(PyTypeObject *base, PyTypeObject *type)
4255 : {
4256 : Py_ssize_t i;
4257 : int result;
4258 : PyObject *list, *ref, *newobj;
4259 :
4260 1338 : list = base->tp_subclasses;
4261 1338 : if (list == NULL) {
4262 213 : base->tp_subclasses = list = PyList_New(0);
4263 213 : if (list == NULL)
4264 0 : return -1;
4265 : }
4266 : assert(PyList_Check(list));
4267 1338 : newobj = PyWeakref_NewRef((PyObject *)type, NULL);
4268 1338 : i = PyList_GET_SIZE(list);
4269 55869 : while (--i >= 0) {
4270 53193 : ref = PyList_GET_ITEM(list, i);
4271 : assert(PyWeakref_CheckRef(ref));
4272 53193 : if (PyWeakref_GET_OBJECT(ref) == Py_None)
4273 0 : return PyList_SetItem(list, i, newobj);
4274 : }
4275 1338 : result = PyList_Append(list, newobj);
4276 1338 : Py_DECREF(newobj);
4277 1338 : return result;
4278 : }
4279 :
4280 : static void
4281 0 : remove_subclass(PyTypeObject *base, PyTypeObject *type)
4282 : {
4283 : Py_ssize_t i;
4284 : PyObject *list, *ref;
4285 :
4286 0 : list = base->tp_subclasses;
4287 0 : if (list == NULL) {
4288 0 : return;
4289 : }
4290 : assert(PyList_Check(list));
4291 0 : i = PyList_GET_SIZE(list);
4292 0 : while (--i >= 0) {
4293 0 : ref = PyList_GET_ITEM(list, i);
4294 : assert(PyWeakref_CheckRef(ref));
4295 0 : if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
4296 : /* this can't fail, right? */
4297 0 : PySequence_DelItem(list, i);
4298 0 : return;
4299 : }
4300 : }
4301 : }
4302 :
4303 : static int
4304 0 : check_num_args(PyObject *ob, int n)
4305 : {
4306 0 : if (!PyTuple_CheckExact(ob)) {
4307 0 : PyErr_SetString(PyExc_SystemError,
4308 : "PyArg_UnpackTuple() argument list is not a tuple");
4309 0 : return 0;
4310 : }
4311 0 : if (n == PyTuple_GET_SIZE(ob))
4312 0 : return 1;
4313 0 : PyErr_Format(
4314 : PyExc_TypeError,
4315 : "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
4316 0 : return 0;
4317 : }
4318 :
4319 : /* Generic wrappers for overloadable 'operators' such as __getitem__ */
4320 :
4321 : /* There's a wrapper *function* for each distinct function typedef used
4322 : for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
4323 : wrapper *table* for each distinct operation (e.g. __len__, __add__).
4324 : Most tables have only one entry; the tables for binary operators have two
4325 : entries, one regular and one with reversed arguments. */
4326 :
4327 : static PyObject *
4328 0 : wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
4329 : {
4330 0 : lenfunc func = (lenfunc)wrapped;
4331 : Py_ssize_t res;
4332 :
4333 0 : if (!check_num_args(args, 0))
4334 0 : return NULL;
4335 0 : res = (*func)(self);
4336 0 : if (res == -1 && PyErr_Occurred())
4337 0 : return NULL;
4338 0 : return PyInt_FromLong((long)res);
4339 : }
4340 :
4341 : static PyObject *
4342 0 : wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
4343 : {
4344 0 : inquiry func = (inquiry)wrapped;
4345 : int res;
4346 :
4347 0 : if (!check_num_args(args, 0))
4348 0 : return NULL;
4349 0 : res = (*func)(self);
4350 0 : if (res == -1 && PyErr_Occurred())
4351 0 : return NULL;
4352 0 : return PyBool_FromLong((long)res);
4353 : }
4354 :
4355 : static PyObject *
4356 0 : wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
4357 : {
4358 0 : binaryfunc func = (binaryfunc)wrapped;
4359 : PyObject *other;
4360 :
4361 0 : if (!check_num_args(args, 1))
4362 0 : return NULL;
4363 0 : other = PyTuple_GET_ITEM(args, 0);
4364 0 : return (*func)(self, other);
4365 : }
4366 :
4367 : static PyObject *
4368 0 : wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
4369 : {
4370 0 : binaryfunc func = (binaryfunc)wrapped;
4371 : PyObject *other;
4372 :
4373 0 : if (!check_num_args(args, 1))
4374 0 : return NULL;
4375 0 : other = PyTuple_GET_ITEM(args, 0);
4376 0 : if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
4377 0 : !PyType_IsSubtype(other->ob_type, self->ob_type)) {
4378 0 : Py_INCREF(Py_NotImplemented);
4379 0 : return Py_NotImplemented;
4380 : }
4381 0 : return (*func)(self, other);
4382 : }
4383 :
4384 : static PyObject *
4385 0 : wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4386 : {
4387 0 : binaryfunc func = (binaryfunc)wrapped;
4388 : PyObject *other;
4389 :
4390 0 : if (!check_num_args(args, 1))
4391 0 : return NULL;
4392 0 : other = PyTuple_GET_ITEM(args, 0);
4393 0 : if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
4394 0 : !PyType_IsSubtype(other->ob_type, self->ob_type)) {
4395 0 : Py_INCREF(Py_NotImplemented);
4396 0 : return Py_NotImplemented;
4397 : }
4398 0 : return (*func)(other, self);
4399 : }
4400 :
4401 : static PyObject *
4402 0 : wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
4403 : {
4404 0 : coercion func = (coercion)wrapped;
4405 : PyObject *other, *res;
4406 : int ok;
4407 :
4408 0 : if (!check_num_args(args, 1))
4409 0 : return NULL;
4410 0 : other = PyTuple_GET_ITEM(args, 0);
4411 0 : ok = func(&self, &other);
4412 0 : if (ok < 0)
4413 0 : return NULL;
4414 0 : if (ok > 0) {
4415 0 : Py_INCREF(Py_NotImplemented);
4416 0 : return Py_NotImplemented;
4417 : }
4418 0 : res = PyTuple_New(2);
4419 0 : if (res == NULL) {
4420 0 : Py_DECREF(self);
4421 0 : Py_DECREF(other);
4422 0 : return NULL;
4423 : }
4424 0 : PyTuple_SET_ITEM(res, 0, self);
4425 0 : PyTuple_SET_ITEM(res, 1, other);
4426 0 : return res;
4427 : }
4428 :
4429 : static PyObject *
4430 0 : wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
4431 : {
4432 0 : ternaryfunc func = (ternaryfunc)wrapped;
4433 : PyObject *other;
4434 0 : PyObject *third = Py_None;
4435 :
4436 : /* Note: This wrapper only works for __pow__() */
4437 :
4438 0 : if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
4439 0 : return NULL;
4440 0 : return (*func)(self, other, third);
4441 : }
4442 :
4443 : static PyObject *
4444 0 : wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4445 : {
4446 0 : ternaryfunc func = (ternaryfunc)wrapped;
4447 : PyObject *other;
4448 0 : PyObject *third = Py_None;
4449 :
4450 : /* Note: This wrapper only works for __pow__() */
4451 :
4452 0 : if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
4453 0 : return NULL;
4454 0 : return (*func)(other, self, third);
4455 : }
4456 :
4457 : static PyObject *
4458 0 : wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
4459 : {
4460 0 : unaryfunc func = (unaryfunc)wrapped;
4461 :
4462 0 : if (!check_num_args(args, 0))
4463 0 : return NULL;
4464 0 : return (*func)(self);
4465 : }
4466 :
4467 : static PyObject *
4468 0 : wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
4469 : {
4470 0 : ssizeargfunc func = (ssizeargfunc)wrapped;
4471 : PyObject* o;
4472 : Py_ssize_t i;
4473 :
4474 0 : if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
4475 0 : return NULL;
4476 0 : i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
4477 0 : if (i == -1 && PyErr_Occurred())
4478 0 : return NULL;
4479 0 : return (*func)(self, i);
4480 : }
4481 :
4482 : static Py_ssize_t
4483 0 : getindex(PyObject *self, PyObject *arg)
4484 : {
4485 : Py_ssize_t i;
4486 :
4487 0 : i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
4488 0 : if (i == -1 && PyErr_Occurred())
4489 0 : return -1;
4490 0 : if (i < 0) {
4491 0 : PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
4492 0 : if (sq && sq->sq_length) {
4493 0 : Py_ssize_t n = (*sq->sq_length)(self);
4494 0 : if (n < 0)
4495 0 : return -1;
4496 0 : i += n;
4497 : }
4498 : }
4499 0 : return i;
4500 : }
4501 :
4502 : static PyObject *
4503 0 : wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
4504 : {
4505 0 : ssizeargfunc func = (ssizeargfunc)wrapped;
4506 : PyObject *arg;
4507 : Py_ssize_t i;
4508 :
4509 0 : if (PyTuple_GET_SIZE(args) == 1) {
4510 0 : arg = PyTuple_GET_ITEM(args, 0);
4511 0 : i = getindex(self, arg);
4512 0 : if (i == -1 && PyErr_Occurred())
4513 0 : return NULL;
4514 0 : return (*func)(self, i);
4515 : }
4516 0 : check_num_args(args, 1);
4517 : assert(PyErr_Occurred());
4518 0 : return NULL;
4519 : }
4520 :
4521 : static PyObject *
4522 0 : wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
4523 : {
4524 0 : ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
4525 : Py_ssize_t i, j;
4526 :
4527 0 : if (!PyArg_ParseTuple(args, "nn", &i, &j))
4528 0 : return NULL;
4529 0 : return (*func)(self, i, j);
4530 : }
4531 :
4532 : static PyObject *
4533 0 : wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
4534 : {
4535 0 : ssizeobjargproc func = (ssizeobjargproc)wrapped;
4536 : Py_ssize_t i;
4537 : int res;
4538 : PyObject *arg, *value;
4539 :
4540 0 : if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
4541 0 : return NULL;
4542 0 : i = getindex(self, arg);
4543 0 : if (i == -1 && PyErr_Occurred())
4544 0 : return NULL;
4545 0 : res = (*func)(self, i, value);
4546 0 : if (res == -1 && PyErr_Occurred())
4547 0 : return NULL;
4548 0 : Py_INCREF(Py_None);
4549 0 : return Py_None;
4550 : }
4551 :
4552 : static PyObject *
4553 0 : wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
4554 : {
4555 0 : ssizeobjargproc func = (ssizeobjargproc)wrapped;
4556 : Py_ssize_t i;
4557 : int res;
4558 : PyObject *arg;
4559 :
4560 0 : if (!check_num_args(args, 1))
4561 0 : return NULL;
4562 0 : arg = PyTuple_GET_ITEM(args, 0);
4563 0 : i = getindex(self, arg);
4564 0 : if (i == -1 && PyErr_Occurred())
4565 0 : return NULL;
4566 0 : res = (*func)(self, i, NULL);
4567 0 : if (res == -1 && PyErr_Occurred())
4568 0 : return NULL;
4569 0 : Py_INCREF(Py_None);
4570 0 : return Py_None;
4571 : }
4572 :
4573 : static PyObject *
4574 0 : wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
4575 : {
4576 0 : ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
4577 : Py_ssize_t i, j;
4578 : int res;
4579 : PyObject *value;
4580 :
4581 0 : if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
4582 0 : return NULL;
4583 0 : res = (*func)(self, i, j, value);
4584 0 : if (res == -1 && PyErr_Occurred())
4585 0 : return NULL;
4586 0 : Py_INCREF(Py_None);
4587 0 : return Py_None;
4588 : }
4589 :
4590 : static PyObject *
4591 0 : wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
4592 : {
4593 0 : ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
4594 : Py_ssize_t i, j;
4595 : int res;
4596 :
4597 0 : if (!PyArg_ParseTuple(args, "nn", &i, &j))
4598 0 : return NULL;
4599 0 : res = (*func)(self, i, j, NULL);
4600 0 : if (res == -1 && PyErr_Occurred())
4601 0 : return NULL;
4602 0 : Py_INCREF(Py_None);
4603 0 : return Py_None;
4604 : }
4605 :
4606 : /* XXX objobjproc is a misnomer; should be objargpred */
4607 : static PyObject *
4608 0 : wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
4609 : {
4610 0 : objobjproc func = (objobjproc)wrapped;
4611 : int res;
4612 : PyObject *value;
4613 :
4614 0 : if (!check_num_args(args, 1))
4615 0 : return NULL;
4616 0 : value = PyTuple_GET_ITEM(args, 0);
4617 0 : res = (*func)(self, value);
4618 0 : if (res == -1 && PyErr_Occurred())
4619 0 : return NULL;
4620 : else
4621 0 : return PyBool_FromLong(res);
4622 : }
4623 :
4624 : static PyObject *
4625 0 : wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
4626 : {
4627 0 : objobjargproc func = (objobjargproc)wrapped;
4628 : int res;
4629 : PyObject *key, *value;
4630 :
4631 0 : if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
4632 0 : return NULL;
4633 0 : res = (*func)(self, key, value);
4634 0 : if (res == -1 && PyErr_Occurred())
4635 0 : return NULL;
4636 0 : Py_INCREF(Py_None);
4637 0 : return Py_None;
4638 : }
4639 :
4640 : static PyObject *
4641 0 : wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
4642 : {
4643 0 : objobjargproc func = (objobjargproc)wrapped;
4644 : int res;
4645 : PyObject *key;
4646 :
4647 0 : if (!check_num_args(args, 1))
4648 0 : return NULL;
4649 0 : key = PyTuple_GET_ITEM(args, 0);
4650 0 : res = (*func)(self, key, NULL);
4651 0 : if (res == -1 && PyErr_Occurred())
4652 0 : return NULL;
4653 0 : Py_INCREF(Py_None);
4654 0 : return Py_None;
4655 : }
4656 :
4657 : static PyObject *
4658 0 : wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
4659 : {
4660 0 : cmpfunc func = (cmpfunc)wrapped;
4661 : int res;
4662 : PyObject *other;
4663 :
4664 0 : if (!check_num_args(args, 1))
4665 0 : return NULL;
4666 0 : other = PyTuple_GET_ITEM(args, 0);
4667 0 : if (Py_TYPE(other)->tp_compare != func &&
4668 0 : !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
4669 0 : PyErr_Format(
4670 : PyExc_TypeError,
4671 : "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
4672 0 : Py_TYPE(self)->tp_name,
4673 0 : Py_TYPE(self)->tp_name,
4674 0 : Py_TYPE(other)->tp_name);
4675 0 : return NULL;
4676 : }
4677 0 : res = (*func)(self, other);
4678 0 : if (PyErr_Occurred())
4679 0 : return NULL;
4680 0 : return PyInt_FromLong((long)res);
4681 : }
4682 :
4683 : /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
4684 : This is called the Carlo Verre hack after its discoverer. */
4685 : static int
4686 0 : hackcheck(PyObject *self, setattrofunc func, char *what)
4687 : {
4688 0 : PyTypeObject *type = Py_TYPE(self);
4689 0 : while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
4690 0 : type = type->tp_base;
4691 : /* If type is NULL now, this is a really weird type.
4692 : In the spirit of backwards compatibility (?), just shut up. */
4693 0 : if (type && type->tp_setattro != func) {
4694 0 : PyErr_Format(PyExc_TypeError,
4695 : "can't apply this %s to %s object",
4696 : what,
4697 : type->tp_name);
4698 0 : return 0;
4699 : }
4700 0 : return 1;
4701 : }
4702 :
4703 : static PyObject *
4704 0 : wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
4705 : {
4706 0 : setattrofunc func = (setattrofunc)wrapped;
4707 : int res;
4708 : PyObject *name, *value;
4709 :
4710 0 : if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
4711 0 : return NULL;
4712 0 : if (!hackcheck(self, func, "__setattr__"))
4713 0 : return NULL;
4714 0 : res = (*func)(self, name, value);
4715 0 : if (res < 0)
4716 0 : return NULL;
4717 0 : Py_INCREF(Py_None);
4718 0 : return Py_None;
4719 : }
4720 :
4721 : static PyObject *
4722 0 : wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
4723 : {
4724 0 : setattrofunc func = (setattrofunc)wrapped;
4725 : int res;
4726 : PyObject *name;
4727 :
4728 0 : if (!check_num_args(args, 1))
4729 0 : return NULL;
4730 0 : name = PyTuple_GET_ITEM(args, 0);
4731 0 : if (!hackcheck(self, func, "__delattr__"))
4732 0 : return NULL;
4733 0 : res = (*func)(self, name, NULL);
4734 0 : if (res < 0)
4735 0 : return NULL;
4736 0 : Py_INCREF(Py_None);
4737 0 : return Py_None;
4738 : }
4739 :
4740 : static PyObject *
4741 0 : wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
4742 : {
4743 0 : hashfunc func = (hashfunc)wrapped;
4744 : long res;
4745 :
4746 0 : if (!check_num_args(args, 0))
4747 0 : return NULL;
4748 0 : res = (*func)(self);
4749 0 : if (res == -1 && PyErr_Occurred())
4750 0 : return NULL;
4751 0 : return PyInt_FromLong(res);
4752 : }
4753 :
4754 : static PyObject *
4755 0 : wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
4756 : {
4757 0 : ternaryfunc func = (ternaryfunc)wrapped;
4758 :
4759 0 : return (*func)(self, args, kwds);
4760 : }
4761 :
4762 : static PyObject *
4763 0 : wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
4764 : {
4765 0 : richcmpfunc func = (richcmpfunc)wrapped;
4766 : PyObject *other;
4767 :
4768 0 : if (!check_num_args(args, 1))
4769 0 : return NULL;
4770 0 : other = PyTuple_GET_ITEM(args, 0);
4771 0 : return (*func)(self, other, op);
4772 : }
4773 :
4774 : #undef RICHCMP_WRAPPER
4775 : #define RICHCMP_WRAPPER(NAME, OP) \
4776 : static PyObject * \
4777 : richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4778 : { \
4779 : return wrap_richcmpfunc(self, args, wrapped, OP); \
4780 : }
4781 :
4782 0 : RICHCMP_WRAPPER(lt, Py_LT)
4783 0 : RICHCMP_WRAPPER(le, Py_LE)
4784 0 : RICHCMP_WRAPPER(eq, Py_EQ)
4785 0 : RICHCMP_WRAPPER(ne, Py_NE)
4786 0 : RICHCMP_WRAPPER(gt, Py_GT)
4787 0 : RICHCMP_WRAPPER(ge, Py_GE)
4788 :
4789 : static PyObject *
4790 0 : wrap_next(PyObject *self, PyObject *args, void *wrapped)
4791 : {
4792 0 : unaryfunc func = (unaryfunc)wrapped;
4793 : PyObject *res;
4794 :
4795 0 : if (!check_num_args(args, 0))
4796 0 : return NULL;
4797 0 : res = (*func)(self);
4798 0 : if (res == NULL && !PyErr_Occurred())
4799 0 : PyErr_SetNone(PyExc_StopIteration);
4800 0 : return res;
4801 : }
4802 :
4803 : static PyObject *
4804 0 : wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
4805 : {
4806 0 : descrgetfunc func = (descrgetfunc)wrapped;
4807 : PyObject *obj;
4808 0 : PyObject *type = NULL;
4809 :
4810 0 : if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
4811 0 : return NULL;
4812 0 : if (obj == Py_None)
4813 0 : obj = NULL;
4814 0 : if (type == Py_None)
4815 0 : type = NULL;
4816 0 : if (type == NULL &&obj == NULL) {
4817 0 : PyErr_SetString(PyExc_TypeError,
4818 : "__get__(None, None) is invalid");
4819 0 : return NULL;
4820 : }
4821 0 : return (*func)(self, obj, type);
4822 : }
4823 :
4824 : static PyObject *
4825 0 : wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
4826 : {
4827 0 : descrsetfunc func = (descrsetfunc)wrapped;
4828 : PyObject *obj, *value;
4829 : int ret;
4830 :
4831 0 : if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
4832 0 : return NULL;
4833 0 : ret = (*func)(self, obj, value);
4834 0 : if (ret < 0)
4835 0 : return NULL;
4836 0 : Py_INCREF(Py_None);
4837 0 : return Py_None;
4838 : }
4839 :
4840 : static PyObject *
4841 0 : wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
4842 : {
4843 0 : descrsetfunc func = (descrsetfunc)wrapped;
4844 : PyObject *obj;
4845 : int ret;
4846 :
4847 0 : if (!check_num_args(args, 1))
4848 0 : return NULL;
4849 0 : obj = PyTuple_GET_ITEM(args, 0);
4850 0 : ret = (*func)(self, obj, NULL);
4851 0 : if (ret < 0)
4852 0 : return NULL;
4853 0 : Py_INCREF(Py_None);
4854 0 : return Py_None;
4855 : }
4856 :
4857 : static PyObject *
4858 3 : wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
4859 : {
4860 3 : initproc func = (initproc)wrapped;
4861 :
4862 3 : if (func(self, args, kwds) < 0)
4863 0 : return NULL;
4864 3 : Py_INCREF(Py_None);
4865 3 : return Py_None;
4866 : }
4867 :
4868 : static PyObject *
4869 2475 : tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
4870 : {
4871 : PyTypeObject *type, *subtype, *staticbase;
4872 : PyObject *arg0, *res;
4873 :
4874 2475 : if (self == NULL || !PyType_Check(self))
4875 0 : Py_FatalError("__new__() called with non-type 'self'");
4876 2475 : type = (PyTypeObject *)self;
4877 2475 : if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
4878 0 : PyErr_Format(PyExc_TypeError,
4879 : "%s.__new__(): not enough arguments",
4880 : type->tp_name);
4881 0 : return NULL;
4882 : }
4883 2475 : arg0 = PyTuple_GET_ITEM(args, 0);
4884 2475 : if (!PyType_Check(arg0)) {
4885 0 : PyErr_Format(PyExc_TypeError,
4886 : "%s.__new__(X): X is not a type object (%s)",
4887 : type->tp_name,
4888 0 : Py_TYPE(arg0)->tp_name);
4889 0 : return NULL;
4890 : }
4891 2475 : subtype = (PyTypeObject *)arg0;
4892 2475 : if (!PyType_IsSubtype(subtype, type)) {
4893 0 : PyErr_Format(PyExc_TypeError,
4894 : "%s.__new__(%s): %s is not a subtype of %s",
4895 : type->tp_name,
4896 : subtype->tp_name,
4897 : subtype->tp_name,
4898 : type->tp_name);
4899 0 : return NULL;
4900 : }
4901 :
4902 : /* Check that the use doesn't do something silly and unsafe like
4903 : object.__new__(dict). To do this, we check that the
4904 : most derived base that's not a heap type is this type. */
4905 2475 : staticbase = subtype;
4906 7425 : while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4907 2475 : staticbase = staticbase->tp_base;
4908 : /* If staticbase is NULL now, it is a really weird type.
4909 : In the spirit of backwards compatibility (?), just shut up. */
4910 2475 : if (staticbase && staticbase->tp_new != type->tp_new) {
4911 0 : PyErr_Format(PyExc_TypeError,
4912 : "%s.__new__(%s) is not safe, use %s.__new__()",
4913 : type->tp_name,
4914 : subtype->tp_name,
4915 : staticbase->tp_name);
4916 0 : return NULL;
4917 : }
4918 :
4919 2475 : args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4920 2475 : if (args == NULL)
4921 0 : return NULL;
4922 2475 : res = type->tp_new(subtype, args, kwds);
4923 2475 : Py_DECREF(args);
4924 2475 : return res;
4925 : }
4926 :
4927 : static struct PyMethodDef tp_new_methoddef[] = {
4928 : {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
4929 : PyDoc_STR("T.__new__(S, ...) -> "
4930 : "a new object with type S, a subtype of T")},
4931 : {0}
4932 : };
4933 :
4934 : static int
4935 393 : add_tp_new_wrapper(PyTypeObject *type)
4936 : {
4937 : PyObject *func;
4938 :
4939 393 : if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
4940 0 : return 0;
4941 393 : func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
4942 393 : if (func == NULL)
4943 0 : return -1;
4944 393 : if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
4945 0 : Py_DECREF(func);
4946 0 : return -1;
4947 : }
4948 393 : Py_DECREF(func);
4949 393 : return 0;
4950 : }
4951 :
4952 : /* Slot wrappers that call the corresponding __foo__ slot. See comments
4953 : below at override_slots() for more explanation. */
4954 :
4955 : #define SLOT0(FUNCNAME, OPSTR) \
4956 : static PyObject * \
4957 : FUNCNAME(PyObject *self) \
4958 : { \
4959 : static PyObject *cache_str; \
4960 : return call_method(self, OPSTR, &cache_str, "()"); \
4961 : }
4962 :
4963 : #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4964 : static PyObject * \
4965 : FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4966 : { \
4967 : static PyObject *cache_str; \
4968 : return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4969 : }
4970 :
4971 : /* Boolean helper for SLOT1BINFULL().
4972 : right.__class__ is a nontrivial subclass of left.__class__. */
4973 : static int
4974 0 : method_is_overloaded(PyObject *left, PyObject *right, char *name)
4975 : {
4976 : PyObject *a, *b;
4977 : int ok;
4978 :
4979 0 : b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
4980 0 : if (b == NULL) {
4981 0 : PyErr_Clear();
4982 : /* If right doesn't have it, it's not overloaded */
4983 0 : return 0;
4984 : }
4985 :
4986 0 : a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
4987 0 : if (a == NULL) {
4988 0 : PyErr_Clear();
4989 0 : Py_DECREF(b);
4990 : /* If right has it but left doesn't, it's overloaded */
4991 0 : return 1;
4992 : }
4993 :
4994 0 : ok = PyObject_RichCompareBool(a, b, Py_NE);
4995 0 : Py_DECREF(a);
4996 0 : Py_DECREF(b);
4997 0 : if (ok < 0) {
4998 0 : PyErr_Clear();
4999 0 : return 0;
5000 : }
5001 :
5002 0 : return ok;
5003 : }
5004 :
5005 :
5006 : #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
5007 : static PyObject * \
5008 : FUNCNAME(PyObject *self, PyObject *other) \
5009 : { \
5010 : static PyObject *cache_str, *rcache_str; \
5011 : int do_other = Py_TYPE(self) != Py_TYPE(other) && \
5012 : Py_TYPE(other)->tp_as_number != NULL && \
5013 : Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
5014 : if (Py_TYPE(self)->tp_as_number != NULL && \
5015 : Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
5016 : PyObject *r; \
5017 : if (do_other && \
5018 : PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
5019 : method_is_overloaded(self, other, ROPSTR)) { \
5020 : r = call_maybe( \
5021 : other, ROPSTR, &rcache_str, "(O)", self); \
5022 : if (r != Py_NotImplemented) \
5023 : return r; \
5024 : Py_DECREF(r); \
5025 : do_other = 0; \
5026 : } \
5027 : r = call_maybe( \
5028 : self, OPSTR, &cache_str, "(O)", other); \
5029 : if (r != Py_NotImplemented || \
5030 : Py_TYPE(other) == Py_TYPE(self)) \
5031 : return r; \
5032 : Py_DECREF(r); \
5033 : } \
5034 : if (do_other) { \
5035 : return call_maybe( \
5036 : other, ROPSTR, &rcache_str, "(O)", self); \
5037 : } \
5038 : Py_INCREF(Py_NotImplemented); \
5039 : return Py_NotImplemented; \
5040 : }
5041 :
5042 : #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
5043 : SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
5044 :
5045 : #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
5046 : static PyObject * \
5047 : FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
5048 : { \
5049 : static PyObject *cache_str; \
5050 : return call_method(self, OPSTR, &cache_str, \
5051 : "(" ARGCODES ")", arg1, arg2); \
5052 : }
5053 :
5054 : static Py_ssize_t
5055 0 : slot_sq_length(PyObject *self)
5056 : {
5057 : static PyObject *len_str;
5058 0 : PyObject *res = call_method(self, "__len__", &len_str, "()");
5059 : Py_ssize_t len;
5060 :
5061 0 : if (res == NULL)
5062 0 : return -1;
5063 0 : len = PyInt_AsSsize_t(res);
5064 0 : Py_DECREF(res);
5065 0 : if (len < 0) {
5066 0 : if (!PyErr_Occurred())
5067 0 : PyErr_SetString(PyExc_ValueError,
5068 : "__len__() should return >= 0");
5069 0 : return -1;
5070 : }
5071 0 : return len;
5072 : }
5073 :
5074 : /* Super-optimized version of slot_sq_item.
5075 : Other slots could do the same... */
5076 : static PyObject *
5077 0 : slot_sq_item(PyObject *self, Py_ssize_t i)
5078 : {
5079 : static PyObject *getitem_str;
5080 0 : PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
5081 : descrgetfunc f;
5082 :
5083 0 : if (getitem_str == NULL) {
5084 0 : getitem_str = PyString_InternFromString("__getitem__");
5085 0 : if (getitem_str == NULL)
5086 0 : return NULL;
5087 : }
5088 0 : func = _PyType_Lookup(Py_TYPE(self), getitem_str);
5089 0 : if (func != NULL) {
5090 0 : if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
5091 0 : Py_INCREF(func);
5092 : else {
5093 0 : func = f(func, self, (PyObject *)(Py_TYPE(self)));
5094 0 : if (func == NULL) {
5095 0 : return NULL;
5096 : }
5097 : }
5098 0 : ival = PyInt_FromSsize_t(i);
5099 0 : if (ival != NULL) {
5100 0 : args = PyTuple_New(1);
5101 0 : if (args != NULL) {
5102 0 : PyTuple_SET_ITEM(args, 0, ival);
5103 0 : retval = PyObject_Call(func, args, NULL);
5104 0 : Py_XDECREF(args);
5105 0 : Py_XDECREF(func);
5106 0 : return retval;
5107 : }
5108 : }
5109 : }
5110 : else {
5111 0 : PyErr_SetObject(PyExc_AttributeError, getitem_str);
5112 : }
5113 0 : Py_XDECREF(args);
5114 0 : Py_XDECREF(ival);
5115 0 : Py_XDECREF(func);
5116 0 : return NULL;
5117 : }
5118 :
5119 : static PyObject*
5120 0 : slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j)
5121 : {
5122 : static PyObject *getslice_str;
5123 :
5124 0 : if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; "
5125 0 : "use __getitem__", 1) < 0)
5126 0 : return NULL;
5127 0 : return call_method(self, "__getslice__", &getslice_str,
5128 : "nn", i, j);
5129 : }
5130 :
5131 : static int
5132 0 : slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
5133 : {
5134 : PyObject *res;
5135 : static PyObject *delitem_str, *setitem_str;
5136 :
5137 0 : if (value == NULL)
5138 0 : res = call_method(self, "__delitem__", &delitem_str,
5139 : "(n)", index);
5140 : else
5141 0 : res = call_method(self, "__setitem__", &setitem_str,
5142 : "(nO)", index, value);
5143 0 : if (res == NULL)
5144 0 : return -1;
5145 0 : Py_DECREF(res);
5146 0 : return 0;
5147 : }
5148 :
5149 : static int
5150 0 : slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
5151 : {
5152 : PyObject *res;
5153 : static PyObject *delslice_str, *setslice_str;
5154 :
5155 0 : if (value == NULL) {
5156 0 : if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; "
5157 0 : "use __delitem__", 1) < 0)
5158 0 : return -1;
5159 0 : res = call_method(self, "__delslice__", &delslice_str,
5160 : "(nn)", i, j);
5161 : }
5162 : else {
5163 0 : if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; "
5164 0 : "use __setitem__", 1) < 0)
5165 0 : return -1;
5166 0 : res = call_method(self, "__setslice__", &setslice_str,
5167 : "(nnO)", i, j, value);
5168 : }
5169 0 : if (res == NULL)
5170 0 : return -1;
5171 0 : Py_DECREF(res);
5172 0 : return 0;
5173 : }
5174 :
5175 : static int
5176 93 : slot_sq_contains(PyObject *self, PyObject *value)
5177 : {
5178 : PyObject *func, *res, *args;
5179 93 : int result = -1;
5180 :
5181 : static PyObject *contains_str;
5182 :
5183 93 : func = lookup_maybe(self, "__contains__", &contains_str);
5184 93 : if (func != NULL) {
5185 93 : args = PyTuple_Pack(1, value);
5186 93 : if (args == NULL)
5187 0 : res = NULL;
5188 : else {
5189 93 : res = PyObject_Call(func, args, NULL);
5190 93 : Py_DECREF(args);
5191 : }
5192 93 : Py_DECREF(func);
5193 93 : if (res != NULL) {
5194 93 : result = PyObject_IsTrue(res);
5195 93 : Py_DECREF(res);
5196 : }
5197 : }
5198 0 : else if (! PyErr_Occurred()) {
5199 : /* Possible results: -1 and 1 */
5200 0 : result = (int)_PySequence_IterSearch(self, value,
5201 : PY_ITERSEARCH_CONTAINS);
5202 : }
5203 93 : return result;
5204 : }
5205 :
5206 : #define slot_mp_length slot_sq_length
5207 :
5208 0 : SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
5209 :
5210 : static int
5211 0 : slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
5212 : {
5213 : PyObject *res;
5214 : static PyObject *delitem_str, *setitem_str;
5215 :
5216 0 : if (value == NULL)
5217 0 : res = call_method(self, "__delitem__", &delitem_str,
5218 : "(O)", key);
5219 : else
5220 0 : res = call_method(self, "__setitem__", &setitem_str,
5221 : "(OO)", key, value);
5222 0 : if (res == NULL)
5223 0 : return -1;
5224 0 : Py_DECREF(res);
5225 0 : return 0;
5226 : }
5227 :
5228 0 : SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
5229 0 : SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
5230 0 : SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
5231 0 : SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
5232 0 : SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
5233 0 : SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
5234 :
5235 : static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
5236 :
5237 0 : SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
5238 : nb_power, "__pow__", "__rpow__")
5239 :
5240 : static PyObject *
5241 0 : slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
5242 : {
5243 : static PyObject *pow_str;
5244 :
5245 0 : if (modulus == Py_None)
5246 0 : return slot_nb_power_binary(self, other);
5247 : /* Three-arg power doesn't use __rpow__. But ternary_op
5248 : can call this when the second argument's type uses
5249 : slot_nb_power, so check before calling self.__pow__. */
5250 0 : if (Py_TYPE(self)->tp_as_number != NULL &&
5251 0 : Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
5252 0 : return call_method(self, "__pow__", &pow_str,
5253 : "(OO)", other, modulus);
5254 : }
5255 0 : Py_INCREF(Py_NotImplemented);
5256 0 : return Py_NotImplemented;
5257 : }
5258 :
5259 0 : SLOT0(slot_nb_negative, "__neg__")
5260 0 : SLOT0(slot_nb_positive, "__pos__")
5261 0 : SLOT0(slot_nb_absolute, "__abs__")
5262 :
5263 : static int
5264 0 : slot_nb_nonzero(PyObject *self)
5265 : {
5266 : PyObject *func, *args;
5267 : static PyObject *nonzero_str, *len_str;
5268 0 : int result = -1;
5269 0 : int using_len = 0;
5270 :
5271 0 : func = lookup_maybe(self, "__nonzero__", &nonzero_str);
5272 0 : if (func == NULL) {
5273 0 : if (PyErr_Occurred())
5274 0 : return -1;
5275 0 : func = lookup_maybe(self, "__len__", &len_str);
5276 0 : if (func == NULL)
5277 0 : return PyErr_Occurred() ? -1 : 1;
5278 0 : using_len = 1;
5279 : }
5280 0 : args = PyTuple_New(0);
5281 0 : if (args != NULL) {
5282 0 : PyObject *temp = PyObject_Call(func, args, NULL);
5283 0 : Py_DECREF(args);
5284 0 : if (temp != NULL) {
5285 0 : if (PyInt_CheckExact(temp) || PyBool_Check(temp))
5286 0 : result = PyObject_IsTrue(temp);
5287 : else {
5288 0 : PyErr_Format(PyExc_TypeError,
5289 : "%s should return "
5290 : "bool or int, returned %s",
5291 : (using_len ? "__len__"
5292 : : "__nonzero__"),
5293 0 : temp->ob_type->tp_name);
5294 0 : result = -1;
5295 : }
5296 0 : Py_DECREF(temp);
5297 : }
5298 : }
5299 0 : Py_DECREF(func);
5300 0 : return result;
5301 : }
5302 :
5303 :
5304 : static PyObject *
5305 0 : slot_nb_index(PyObject *self)
5306 : {
5307 : static PyObject *index_str;
5308 0 : return call_method(self, "__index__", &index_str, "()");
5309 : }
5310 :
5311 :
5312 0 : SLOT0(slot_nb_invert, "__invert__")
5313 0 : SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
5314 0 : SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
5315 0 : SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
5316 0 : SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
5317 0 : SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
5318 :
5319 : static int
5320 0 : slot_nb_coerce(PyObject **a, PyObject **b)
5321 : {
5322 : static PyObject *coerce_str;
5323 0 : PyObject *self = *a, *other = *b;
5324 :
5325 0 : if (self->ob_type->tp_as_number != NULL &&
5326 0 : self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
5327 : PyObject *r;
5328 0 : r = call_maybe(
5329 : self, "__coerce__", &coerce_str, "(O)", other);
5330 0 : if (r == NULL)
5331 0 : return -1;
5332 0 : if (r == Py_NotImplemented) {
5333 0 : Py_DECREF(r);
5334 : }
5335 : else {
5336 0 : if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
5337 0 : PyErr_SetString(PyExc_TypeError,
5338 : "__coerce__ didn't return a 2-tuple");
5339 0 : Py_DECREF(r);
5340 0 : return -1;
5341 : }
5342 0 : *a = PyTuple_GET_ITEM(r, 0);
5343 0 : Py_INCREF(*a);
5344 0 : *b = PyTuple_GET_ITEM(r, 1);
5345 0 : Py_INCREF(*b);
5346 0 : Py_DECREF(r);
5347 0 : return 0;
5348 : }
5349 : }
5350 0 : if (other->ob_type->tp_as_number != NULL &&
5351 0 : other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
5352 : PyObject *r;
5353 0 : r = call_maybe(
5354 : other, "__coerce__", &coerce_str, "(O)", self);
5355 0 : if (r == NULL)
5356 0 : return -1;
5357 0 : if (r == Py_NotImplemented) {
5358 0 : Py_DECREF(r);
5359 0 : return 1;
5360 : }
5361 0 : if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
5362 0 : PyErr_SetString(PyExc_TypeError,
5363 : "__coerce__ didn't return a 2-tuple");
5364 0 : Py_DECREF(r);
5365 0 : return -1;
5366 : }
5367 0 : *a = PyTuple_GET_ITEM(r, 1);
5368 0 : Py_INCREF(*a);
5369 0 : *b = PyTuple_GET_ITEM(r, 0);
5370 0 : Py_INCREF(*b);
5371 0 : Py_DECREF(r);
5372 0 : return 0;
5373 : }
5374 0 : return 1;
5375 : }
5376 :
5377 0 : SLOT0(slot_nb_int, "__int__")
5378 0 : SLOT0(slot_nb_long, "__long__")
5379 0 : SLOT0(slot_nb_float, "__float__")
5380 0 : SLOT0(slot_nb_oct, "__oct__")
5381 0 : SLOT0(slot_nb_hex, "__hex__")
5382 0 : SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
5383 0 : SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
5384 0 : SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
5385 0 : SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
5386 0 : SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
5387 : /* Can't use SLOT1 here, because nb_inplace_power is ternary */
5388 : static PyObject *
5389 0 : slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
5390 : {
5391 : static PyObject *cache_str;
5392 0 : return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
5393 : }
5394 0 : SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
5395 0 : SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
5396 0 : SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
5397 0 : SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
5398 0 : SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
5399 0 : SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
5400 : "__floordiv__", "__rfloordiv__")
5401 0 : SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
5402 0 : SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
5403 0 : SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
5404 :
5405 : static int
5406 0 : half_compare(PyObject *self, PyObject *other)
5407 : {
5408 : PyObject *func, *args, *res;
5409 : static PyObject *cmp_str;
5410 : Py_ssize_t c;
5411 :
5412 0 : func = lookup_method(self, "__cmp__", &cmp_str);
5413 0 : if (func == NULL) {
5414 0 : PyErr_Clear();
5415 : }
5416 : else {
5417 0 : args = PyTuple_Pack(1, other);
5418 0 : if (args == NULL)
5419 0 : res = NULL;
5420 : else {
5421 0 : res = PyObject_Call(func, args, NULL);
5422 0 : Py_DECREF(args);
5423 : }
5424 0 : Py_DECREF(func);
5425 0 : if (res != Py_NotImplemented) {
5426 0 : if (res == NULL)
5427 0 : return -2;
5428 0 : c = PyInt_AsLong(res);
5429 0 : Py_DECREF(res);
5430 0 : if (c == -1 && PyErr_Occurred())
5431 0 : return -2;
5432 0 : return (c < 0) ? -1 : (c > 0) ? 1 : 0;
5433 : }
5434 0 : Py_DECREF(res);
5435 : }
5436 0 : return 2;
5437 : }
5438 :
5439 : /* This slot is published for the benefit of try_3way_compare in object.c */
5440 : int
5441 0 : _PyObject_SlotCompare(PyObject *self, PyObject *other)
5442 : {
5443 : int c;
5444 :
5445 0 : if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) {
5446 0 : c = half_compare(self, other);
5447 0 : if (c <= 1)
5448 0 : return c;
5449 : }
5450 0 : if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) {
5451 0 : c = half_compare(other, self);
5452 0 : if (c < -1)
5453 0 : return -2;
5454 0 : if (c <= 1)
5455 0 : return -c;
5456 : }
5457 0 : return (void *)self < (void *)other ? -1 :
5458 0 : (void *)self > (void *)other ? 1 : 0;
5459 : }
5460 :
5461 : static PyObject *
5462 0 : slot_tp_repr(PyObject *self)
5463 : {
5464 : PyObject *func, *res;
5465 : static PyObject *repr_str;
5466 :
5467 0 : func = lookup_method(self, "__repr__", &repr_str);
5468 0 : if (func != NULL) {
5469 0 : res = PyEval_CallObject(func, NULL);
5470 0 : Py_DECREF(func);
5471 0 : return res;
5472 : }
5473 0 : PyErr_Clear();
5474 0 : return PyString_FromFormat("<%s object at %p>",
5475 0 : Py_TYPE(self)->tp_name, self);
5476 : }
5477 :
5478 : static PyObject *
5479 0 : slot_tp_str(PyObject *self)
5480 : {
5481 : PyObject *func, *res;
5482 : static PyObject *str_str;
5483 :
5484 0 : func = lookup_method(self, "__str__", &str_str);
5485 0 : if (func != NULL) {
5486 0 : res = PyEval_CallObject(func, NULL);
5487 0 : Py_DECREF(func);
5488 0 : return res;
5489 : }
5490 : else {
5491 0 : PyErr_Clear();
5492 0 : return slot_tp_repr(self);
5493 : }
5494 : }
5495 :
5496 : static long
5497 823 : slot_tp_hash(PyObject *self)
5498 : {
5499 : PyObject *func;
5500 : static PyObject *hash_str, *eq_str, *cmp_str;
5501 : long h;
5502 :
5503 823 : func = lookup_method(self, "__hash__", &hash_str);
5504 :
5505 1646 : if (func != NULL && func != Py_None) {
5506 823 : PyObject *res = PyEval_CallObject(func, NULL);
5507 823 : Py_DECREF(func);
5508 823 : if (res == NULL)
5509 0 : return -1;
5510 823 : if (PyLong_Check(res))
5511 0 : h = PyLong_Type.tp_hash(res);
5512 : else
5513 823 : h = PyInt_AsLong(res);
5514 823 : Py_DECREF(res);
5515 : }
5516 : else {
5517 0 : Py_XDECREF(func); /* may be None */
5518 0 : PyErr_Clear();
5519 0 : func = lookup_method(self, "__eq__", &eq_str);
5520 0 : if (func == NULL) {
5521 0 : PyErr_Clear();
5522 0 : func = lookup_method(self, "__cmp__", &cmp_str);
5523 : }
5524 0 : if (func != NULL) {
5525 0 : Py_DECREF(func);
5526 0 : return PyObject_HashNotImplemented(self);
5527 : }
5528 0 : PyErr_Clear();
5529 0 : h = _Py_HashPointer((void *)self);
5530 : }
5531 823 : if (h == -1 && !PyErr_Occurred())
5532 0 : h = -2;
5533 823 : return h;
5534 : }
5535 :
5536 : static PyObject *
5537 0 : slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
5538 : {
5539 : static PyObject *call_str;
5540 0 : PyObject *meth = lookup_method(self, "__call__", &call_str);
5541 : PyObject *res;
5542 :
5543 0 : if (meth == NULL)
5544 0 : return NULL;
5545 :
5546 0 : res = PyObject_Call(meth, args, kwds);
5547 :
5548 0 : Py_DECREF(meth);
5549 0 : return res;
5550 : }
5551 :
5552 : /* There are two slot dispatch functions for tp_getattro.
5553 :
5554 : - slot_tp_getattro() is used when __getattribute__ is overridden
5555 : but no __getattr__ hook is present;
5556 :
5557 : - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
5558 :
5559 : The code in update_one_slot() always installs slot_tp_getattr_hook(); this
5560 : detects the absence of __getattr__ and then installs the simpler slot if
5561 : necessary. */
5562 :
5563 : static PyObject *
5564 0 : slot_tp_getattro(PyObject *self, PyObject *name)
5565 : {
5566 : static PyObject *getattribute_str = NULL;
5567 0 : return call_method(self, "__getattribute__", &getattribute_str,
5568 : "(O)", name);
5569 : }
5570 :
5571 : static PyObject *
5572 321 : call_attribute(PyObject *self, PyObject *attr, PyObject *name)
5573 : {
5574 321 : PyObject *res, *descr = NULL;
5575 321 : descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
5576 :
5577 321 : if (f != NULL) {
5578 321 : descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
5579 321 : if (descr == NULL)
5580 0 : return NULL;
5581 : else
5582 321 : attr = descr;
5583 : }
5584 321 : res = PyObject_CallFunctionObjArgs(attr, name, NULL);
5585 321 : Py_XDECREF(descr);
5586 321 : return res;
5587 : }
5588 :
5589 : static PyObject *
5590 960 : slot_tp_getattr_hook(PyObject *self, PyObject *name)
5591 : {
5592 960 : PyTypeObject *tp = Py_TYPE(self);
5593 : PyObject *getattr, *getattribute, *res;
5594 : static PyObject *getattribute_str = NULL;
5595 : static PyObject *getattr_str = NULL;
5596 :
5597 960 : if (getattr_str == NULL) {
5598 3 : getattr_str = PyString_InternFromString("__getattr__");
5599 3 : if (getattr_str == NULL)
5600 0 : return NULL;
5601 : }
5602 960 : if (getattribute_str == NULL) {
5603 3 : getattribute_str =
5604 3 : PyString_InternFromString("__getattribute__");
5605 3 : if (getattribute_str == NULL)
5606 0 : return NULL;
5607 : }
5608 : /* speed hack: we could use lookup_maybe, but that would resolve the
5609 : method fully for each attribute lookup for classes with
5610 : __getattr__, even when the attribute is present. So we use
5611 : _PyType_Lookup and create the method only when needed, with
5612 : call_attribute. */
5613 960 : getattr = _PyType_Lookup(tp, getattr_str);
5614 960 : if (getattr == NULL) {
5615 : /* No __getattr__ hook: use a simpler dispatcher */
5616 0 : tp->tp_getattro = slot_tp_getattro;
5617 0 : return slot_tp_getattro(self, name);
5618 : }
5619 960 : Py_INCREF(getattr);
5620 : /* speed hack: we could use lookup_maybe, but that would resolve the
5621 : method fully for each attribute lookup for classes with
5622 : __getattr__, even when self has the default __getattribute__
5623 : method. So we use _PyType_Lookup and create the method only when
5624 : needed, with call_attribute. */
5625 960 : getattribute = _PyType_Lookup(tp, getattribute_str);
5626 1920 : if (getattribute == NULL ||
5627 1920 : (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
5628 960 : ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
5629 : (void *)PyObject_GenericGetAttr))
5630 960 : res = PyObject_GenericGetAttr(self, name);
5631 : else {
5632 0 : Py_INCREF(getattribute);
5633 0 : res = call_attribute(self, getattribute, name);
5634 0 : Py_DECREF(getattribute);
5635 : }
5636 960 : if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
5637 321 : PyErr_Clear();
5638 321 : res = call_attribute(self, getattr, name);
5639 : }
5640 960 : Py_DECREF(getattr);
5641 960 : return res;
5642 : }
5643 :
5644 : static int
5645 165 : slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
5646 : {
5647 : PyObject *res;
5648 : static PyObject *delattr_str, *setattr_str;
5649 :
5650 165 : if (value == NULL)
5651 0 : res = call_method(self, "__delattr__", &delattr_str,
5652 : "(O)", name);
5653 : else
5654 165 : res = call_method(self, "__setattr__", &setattr_str,
5655 : "(OO)", name, value);
5656 165 : if (res == NULL)
5657 0 : return -1;
5658 165 : Py_DECREF(res);
5659 165 : return 0;
5660 : }
5661 :
5662 : static char *name_op[] = {
5663 : "__lt__",
5664 : "__le__",
5665 : "__eq__",
5666 : "__ne__",
5667 : "__gt__",
5668 : "__ge__",
5669 : };
5670 :
5671 : static PyObject *
5672 1203 : half_richcompare(PyObject *self, PyObject *other, int op)
5673 : {
5674 : PyObject *func, *args, *res;
5675 : static PyObject *op_str[6];
5676 :
5677 1203 : func = lookup_method(self, name_op[op], &op_str[op]);
5678 1203 : if (func == NULL) {
5679 762 : PyErr_Clear();
5680 762 : Py_INCREF(Py_NotImplemented);
5681 762 : return Py_NotImplemented;
5682 : }
5683 441 : args = PyTuple_Pack(1, other);
5684 441 : if (args == NULL)
5685 0 : res = NULL;
5686 : else {
5687 441 : res = PyObject_Call(func, args, NULL);
5688 441 : Py_DECREF(args);
5689 : }
5690 441 : Py_DECREF(func);
5691 441 : return res;
5692 : }
5693 :
5694 : static PyObject *
5695 822 : slot_tp_richcompare(PyObject *self, PyObject *other, int op)
5696 : {
5697 : PyObject *res;
5698 :
5699 822 : if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
5700 822 : res = half_richcompare(self, other, op);
5701 822 : if (res != Py_NotImplemented)
5702 441 : return res;
5703 381 : Py_DECREF(res);
5704 : }
5705 381 : if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
5706 381 : res = half_richcompare(other, self, _Py_SwappedOp[op]);
5707 381 : if (res != Py_NotImplemented) {
5708 0 : return res;
5709 : }
5710 381 : Py_DECREF(res);
5711 : }
5712 381 : Py_INCREF(Py_NotImplemented);
5713 381 : return Py_NotImplemented;
5714 : }
5715 :
5716 : static PyObject *
5717 63 : slot_tp_iter(PyObject *self)
5718 : {
5719 : PyObject *func, *res;
5720 : static PyObject *iter_str, *getitem_str;
5721 :
5722 63 : func = lookup_method(self, "__iter__", &iter_str);
5723 63 : if (func != NULL) {
5724 : PyObject *args;
5725 63 : args = res = PyTuple_New(0);
5726 63 : if (args != NULL) {
5727 63 : res = PyObject_Call(func, args, NULL);
5728 63 : Py_DECREF(args);
5729 : }
5730 63 : Py_DECREF(func);
5731 63 : return res;
5732 : }
5733 0 : PyErr_Clear();
5734 0 : func = lookup_method(self, "__getitem__", &getitem_str);
5735 0 : if (func == NULL) {
5736 0 : PyErr_Format(PyExc_TypeError,
5737 : "'%.200s' object is not iterable",
5738 0 : Py_TYPE(self)->tp_name);
5739 0 : return NULL;
5740 : }
5741 0 : Py_DECREF(func);
5742 0 : return PySeqIter_New(self);
5743 : }
5744 :
5745 : static PyObject *
5746 0 : slot_tp_iternext(PyObject *self)
5747 : {
5748 : static PyObject *next_str;
5749 0 : return call_method(self, "next", &next_str, "()");
5750 : }
5751 :
5752 : static PyObject *
5753 0 : slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5754 : {
5755 0 : PyTypeObject *tp = Py_TYPE(self);
5756 : PyObject *get;
5757 : static PyObject *get_str = NULL;
5758 :
5759 0 : if (get_str == NULL) {
5760 0 : get_str = PyString_InternFromString("__get__");
5761 0 : if (get_str == NULL)
5762 0 : return NULL;
5763 : }
5764 0 : get = _PyType_Lookup(tp, get_str);
5765 0 : if (get == NULL) {
5766 : /* Avoid further slowdowns */
5767 0 : if (tp->tp_descr_get == slot_tp_descr_get)
5768 0 : tp->tp_descr_get = NULL;
5769 0 : Py_INCREF(self);
5770 0 : return self;
5771 : }
5772 0 : if (obj == NULL)
5773 0 : obj = Py_None;
5774 0 : if (type == NULL)
5775 0 : type = Py_None;
5776 0 : return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
5777 : }
5778 :
5779 : static int
5780 0 : slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
5781 : {
5782 : PyObject *res;
5783 : static PyObject *del_str, *set_str;
5784 :
5785 0 : if (value == NULL)
5786 0 : res = call_method(self, "__delete__", &del_str,
5787 : "(O)", target);
5788 : else
5789 0 : res = call_method(self, "__set__", &set_str,
5790 : "(OO)", target, value);
5791 0 : if (res == NULL)
5792 0 : return -1;
5793 0 : Py_DECREF(res);
5794 0 : return 0;
5795 : }
5796 :
5797 : static int
5798 1758 : slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
5799 : {
5800 : static PyObject *init_str;
5801 1758 : PyObject *meth = lookup_method(self, "__init__", &init_str);
5802 : PyObject *res;
5803 :
5804 1758 : if (meth == NULL)
5805 0 : return -1;
5806 1758 : res = PyObject_Call(meth, args, kwds);
5807 1758 : Py_DECREF(meth);
5808 1758 : if (res == NULL)
5809 0 : return -1;
5810 1758 : if (res != Py_None) {
5811 0 : PyErr_Format(PyExc_TypeError,
5812 : "__init__() should return None, not '%.200s'",
5813 0 : Py_TYPE(res)->tp_name);
5814 0 : Py_DECREF(res);
5815 0 : return -1;
5816 : }
5817 1758 : Py_DECREF(res);
5818 1758 : return 0;
5819 : }
5820 :
5821 : static PyObject *
5822 2475 : slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5823 : {
5824 : static PyObject *new_str;
5825 : PyObject *func;
5826 : PyObject *newargs, *x;
5827 : Py_ssize_t i, n;
5828 :
5829 2475 : if (new_str == NULL) {
5830 3 : new_str = PyString_InternFromString("__new__");
5831 3 : if (new_str == NULL)
5832 0 : return NULL;
5833 : }
5834 2475 : func = PyObject_GetAttr((PyObject *)type, new_str);
5835 2475 : if (func == NULL)
5836 0 : return NULL;
5837 : assert(PyTuple_Check(args));
5838 2475 : n = PyTuple_GET_SIZE(args);
5839 2475 : newargs = PyTuple_New(n+1);
5840 2475 : if (newargs == NULL)
5841 0 : return NULL;
5842 2475 : Py_INCREF(type);
5843 2475 : PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
5844 9891 : for (i = 0; i < n; i++) {
5845 7416 : x = PyTuple_GET_ITEM(args, i);
5846 7416 : Py_INCREF(x);
5847 7416 : PyTuple_SET_ITEM(newargs, i+1, x);
5848 : }
5849 2475 : x = PyObject_Call(func, newargs, kwds);
5850 2475 : Py_DECREF(newargs);
5851 2475 : Py_DECREF(func);
5852 2475 : return x;
5853 : }
5854 :
5855 : static void
5856 0 : slot_tp_del(PyObject *self)
5857 : {
5858 : static PyObject *del_str = NULL;
5859 : PyObject *del, *res;
5860 : PyObject *error_type, *error_value, *error_traceback;
5861 :
5862 : /* Temporarily resurrect the object. */
5863 : assert(self->ob_refcnt == 0);
5864 0 : self->ob_refcnt = 1;
5865 :
5866 : /* Save the current exception, if any. */
5867 0 : PyErr_Fetch(&error_type, &error_value, &error_traceback);
5868 :
5869 : /* Execute __del__ method, if any. */
5870 0 : del = lookup_maybe(self, "__del__", &del_str);
5871 0 : if (del != NULL) {
5872 0 : res = PyEval_CallObject(del, NULL);
5873 0 : if (res == NULL)
5874 0 : PyErr_WriteUnraisable(del);
5875 : else
5876 0 : Py_DECREF(res);
5877 0 : Py_DECREF(del);
5878 : }
5879 :
5880 : /* Restore the saved exception. */
5881 0 : PyErr_Restore(error_type, error_value, error_traceback);
5882 :
5883 : /* Undo the temporary resurrection; can't use DECREF here, it would
5884 : * cause a recursive call.
5885 : */
5886 : assert(self->ob_refcnt > 0);
5887 0 : if (--self->ob_refcnt == 0)
5888 0 : return; /* this is the normal path out */
5889 :
5890 : /* __del__ resurrected it! Make it look like the original Py_DECREF
5891 : * never happened.
5892 : */
5893 : {
5894 0 : Py_ssize_t refcnt = self->ob_refcnt;
5895 0 : _Py_NewReference(self);
5896 0 : self->ob_refcnt = refcnt;
5897 : }
5898 : assert(!PyType_IS_GC(Py_TYPE(self)) ||
5899 : _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
5900 : /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5901 : * we need to undo that. */
5902 : _Py_DEC_REFTOTAL;
5903 : /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5904 : * chain, so no more to do there.
5905 : * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5906 : * _Py_NewReference bumped tp_allocs: both of those need to be
5907 : * undone.
5908 : */
5909 : #ifdef COUNT_ALLOCS
5910 : --Py_TYPE(self)->tp_frees;
5911 : --Py_TYPE(self)->tp_allocs;
5912 : #endif
5913 : }
5914 :
5915 :
5916 : /*
5917 : Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions.
5918 :
5919 : The table is ordered by offsets relative to the 'PyHeapTypeObject' structure,
5920 : which incorporates the additional structures used for numbers, sequences and
5921 : mappings. Note that multiple names may map to the same slot (e.g. __eq__,
5922 : __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots
5923 : (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with
5924 : an all-zero entry. (This table is further initialized in init_slotdefs().)
5925 : */
5926 :
5927 : typedef struct wrapperbase slotdef;
5928 :
5929 : #undef TPSLOT
5930 : #undef FLSLOT
5931 : #undef ETSLOT
5932 : #undef SQSLOT
5933 : #undef MPSLOT
5934 : #undef NBSLOT
5935 : #undef UNSLOT
5936 : #undef IBSLOT
5937 : #undef BINSLOT
5938 : #undef RBINSLOT
5939 :
5940 : #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5941 : {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5942 : PyDoc_STR(DOC)}
5943 : #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5944 : {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5945 : PyDoc_STR(DOC), FLAGS}
5946 : #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5947 : {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5948 : PyDoc_STR(DOC)}
5949 : #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5950 : ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5951 : #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5952 : ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5953 : #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5954 : ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5955 : #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5956 : ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5957 : "x." NAME "() <==> " DOC)
5958 : #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5959 : ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5960 : "x." NAME "(y) <==> x" DOC "y")
5961 : #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5962 : ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5963 : "x." NAME "(y) <==> x" DOC "y")
5964 : #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5965 : ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5966 : "x." NAME "(y) <==> y" DOC "x")
5967 : #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5968 : ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5969 : "x." NAME "(y) <==> " DOC)
5970 : #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5971 : ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5972 : "x." NAME "(y) <==> " DOC)
5973 :
5974 : static slotdef slotdefs[] = {
5975 : TPSLOT("__str__", tp_print, NULL, NULL, ""),
5976 : TPSLOT("__repr__", tp_print, NULL, NULL, ""),
5977 : TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5978 : TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5979 : TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5980 : TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5981 : TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5982 : "x.__cmp__(y) <==> cmp(x,y)"),
5983 : TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5984 : "x.__repr__() <==> repr(x)"),
5985 : TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5986 : "x.__hash__() <==> hash(x)"),
5987 : FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5988 : "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
5989 : TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5990 : "x.__str__() <==> str(x)"),
5991 : TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
5992 : wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5993 : TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5994 : TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5995 : "x.__setattr__('name', value) <==> x.name = value"),
5996 : TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5997 : "x.__delattr__('name') <==> del x.name"),
5998 : TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5999 : "x.__lt__(y) <==> x<y"),
6000 : TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
6001 : "x.__le__(y) <==> x<=y"),
6002 : TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
6003 : "x.__eq__(y) <==> x==y"),
6004 : TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
6005 : "x.__ne__(y) <==> x!=y"),
6006 : TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
6007 : "x.__gt__(y) <==> x>y"),
6008 : TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
6009 : "x.__ge__(y) <==> x>=y"),
6010 : TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
6011 : "x.__iter__() <==> iter(x)"),
6012 : TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
6013 : "x.next() -> the next value, or raise StopIteration"),
6014 : TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
6015 : "descr.__get__(obj[, type]) -> value"),
6016 : TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
6017 : "descr.__set__(obj, value)"),
6018 : TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
6019 : wrap_descr_delete, "descr.__delete__(obj)"),
6020 : FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
6021 : "x.__init__(...) initializes x; "
6022 : "see help(type(x)) for signature",
6023 : PyWrapperFlag_KEYWORDS),
6024 : TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
6025 : TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
6026 : BINSLOT("__add__", nb_add, slot_nb_add,
6027 : "+"),
6028 : RBINSLOT("__radd__", nb_add, slot_nb_add,
6029 : "+"),
6030 : BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
6031 : "-"),
6032 : RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
6033 : "-"),
6034 : BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
6035 : "*"),
6036 : RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
6037 : "*"),
6038 : BINSLOT("__div__", nb_divide, slot_nb_divide,
6039 : "/"),
6040 : RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
6041 : "/"),
6042 : BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
6043 : "%"),
6044 : RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
6045 : "%"),
6046 : BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
6047 : "divmod(x, y)"),
6048 : RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
6049 : "divmod(y, x)"),
6050 : NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
6051 : "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
6052 : NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
6053 : "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
6054 : UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
6055 : UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
6056 : UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
6057 : "abs(x)"),
6058 : UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
6059 : "x != 0"),
6060 : UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
6061 : BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
6062 : RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
6063 : BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
6064 : RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
6065 : BINSLOT("__and__", nb_and, slot_nb_and, "&"),
6066 : RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
6067 : BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
6068 : RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
6069 : BINSLOT("__or__", nb_or, slot_nb_or, "|"),
6070 : RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
6071 : NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
6072 : "x.__coerce__(y) <==> coerce(x, y)"),
6073 : UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
6074 : "int(x)"),
6075 : UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
6076 : "long(x)"),
6077 : UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
6078 : "float(x)"),
6079 : UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
6080 : "oct(x)"),
6081 : UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
6082 : "hex(x)"),
6083 : IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
6084 : wrap_binaryfunc, "+="),
6085 : IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
6086 : wrap_binaryfunc, "-="),
6087 : IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
6088 : wrap_binaryfunc, "*="),
6089 : IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
6090 : wrap_binaryfunc, "/="),
6091 : IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
6092 : wrap_binaryfunc, "%="),
6093 : IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
6094 : wrap_binaryfunc, "**="),
6095 : IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
6096 : wrap_binaryfunc, "<<="),
6097 : IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
6098 : wrap_binaryfunc, ">>="),
6099 : IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
6100 : wrap_binaryfunc, "&="),
6101 : IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
6102 : wrap_binaryfunc, "^="),
6103 : IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
6104 : wrap_binaryfunc, "|="),
6105 : BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
6106 : RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
6107 : BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
6108 : RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
6109 : IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
6110 : slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="),
6111 : IBSLOT("__itruediv__", nb_inplace_true_divide,
6112 : slot_nb_inplace_true_divide, wrap_binaryfunc, "/="),
6113 : NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
6114 : "x[y:z] <==> x[y.__index__():z.__index__()]"),
6115 : MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
6116 : "x.__len__() <==> len(x)"),
6117 : MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
6118 : wrap_binaryfunc,
6119 : "x.__getitem__(y) <==> x[y]"),
6120 : MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
6121 : wrap_objobjargproc,
6122 : "x.__setitem__(i, y) <==> x[i]=y"),
6123 : MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
6124 : wrap_delitem,
6125 : "x.__delitem__(y) <==> del x[y]"),
6126 : SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
6127 : "x.__len__() <==> len(x)"),
6128 : /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
6129 : The logic in abstract.c always falls back to nb_add/nb_multiply in
6130 : this case. Defining both the nb_* and the sq_* slots to call the
6131 : user-defined methods has unexpected side-effects, as shown by
6132 : test_descr.notimplemented() */
6133 : SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
6134 : "x.__add__(y) <==> x+y"),
6135 : SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
6136 : "x.__mul__(n) <==> x*n"),
6137 : SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
6138 : "x.__rmul__(n) <==> n*x"),
6139 : SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
6140 : "x.__getitem__(y) <==> x[y]"),
6141 : SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
6142 : "x.__getslice__(i, j) <==> x[i:j]\n\
6143 : \n\
6144 : Use of negative indices is not supported."),
6145 : SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
6146 : "x.__setitem__(i, y) <==> x[i]=y"),
6147 : SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
6148 : "x.__delitem__(y) <==> del x[y]"),
6149 : SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
6150 : wrap_ssizessizeobjargproc,
6151 : "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
6152 : \n\
6153 : Use of negative indices is not supported."),
6154 : SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
6155 : "x.__delslice__(i, j) <==> del x[i:j]\n\
6156 : \n\
6157 : Use of negative indices is not supported."),
6158 : SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
6159 : "x.__contains__(y) <==> y in x"),
6160 : SQSLOT("__iadd__", sq_inplace_concat, NULL,
6161 : wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
6162 : SQSLOT("__imul__", sq_inplace_repeat, NULL,
6163 : wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
6164 : {NULL}
6165 : };
6166 :
6167 : /* Given a type pointer and an offset gotten from a slotdef entry, return a
6168 : pointer to the actual slot. This is not quite the same as simply adding
6169 : the offset to the type pointer, since it takes care to indirect through the
6170 : proper indirection pointer (as_buffer, etc.); it returns NULL if the
6171 : indirection pointer is NULL. */
6172 : static void **
6173 187548 : slotptr(PyTypeObject *type, int ioffset)
6174 : {
6175 : char *ptr;
6176 187548 : long offset = ioffset;
6177 :
6178 : /* Note: this depends on the order of the members of PyHeapTypeObject! */
6179 : assert(offset >= 0);
6180 : assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
6181 187548 : if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
6182 25440 : ptr = (char *)type->tp_as_sequence;
6183 25440 : offset -= offsetof(PyHeapTypeObject, as_sequence);
6184 : }
6185 162108 : else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
6186 7881 : ptr = (char *)type->tp_as_mapping;
6187 7881 : offset -= offsetof(PyHeapTypeObject, as_mapping);
6188 : }
6189 154227 : else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
6190 99675 : ptr = (char *)type->tp_as_number;
6191 99675 : offset -= offsetof(PyHeapTypeObject, as_number);
6192 : }
6193 : else {
6194 54552 : ptr = (char *)type;
6195 : }
6196 187548 : if (ptr != NULL)
6197 155616 : ptr += offset;
6198 187548 : return (void **)ptr;
6199 : }
6200 :
6201 : /* Length of array of slotdef pointers used to store slots with the
6202 : same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
6203 : the same __name__, for any __name__. Since that's a static property, it is
6204 : appropriate to declare fixed-size arrays for this. */
6205 : #define MAX_EQUIV 10
6206 :
6207 : /* Return a slot pointer for a given name, but ONLY if the attribute has
6208 : exactly one slot function. The name must be an interned string. */
6209 : static void **
6210 8592 : resolve_slotdups(PyTypeObject *type, PyObject *name)
6211 : {
6212 : /* XXX Maybe this could be optimized more -- but is it worth it? */
6213 :
6214 : /* pname and ptrs act as a little cache */
6215 : static PyObject *pname;
6216 : static slotdef *ptrs[MAX_EQUIV];
6217 : slotdef *p, **pp;
6218 : void **res, **ptr;
6219 :
6220 8592 : if (pname != name) {
6221 : /* Collect all slotdefs that match name into ptrs. */
6222 8505 : pname = name;
6223 8505 : pp = ptrs;
6224 850500 : for (p = slotdefs; p->name_strobj; p++) {
6225 841995 : if (p->name_strobj == name)
6226 15504 : *pp++ = p;
6227 : }
6228 8505 : *pp = NULL;
6229 : }
6230 :
6231 : /* Look in all matching slots of the type; if exactly one of these has
6232 : a filled-in slot, return its value. Otherwise return NULL. */
6233 8592 : res = NULL;
6234 24084 : for (pp = ptrs; *pp; pp++) {
6235 15678 : ptr = slotptr(type, (*pp)->offset);
6236 15678 : if (ptr == NULL || *ptr == NULL)
6237 6900 : continue;
6238 8778 : if (res != NULL)
6239 186 : return NULL;
6240 8592 : res = ptr;
6241 : }
6242 8406 : return res;
6243 : }
6244 :
6245 : /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
6246 : does some incredibly complex thinking and then sticks something into the
6247 : slot. (It sees if the adjacent slotdefs for the same slot have conflicting
6248 : interests, and then stores a generic wrapper or a specific function into
6249 : the slot.) Return a pointer to the next slotdef with a different offset,
6250 : because that's convenient for fixup_slot_dispatchers(). */
6251 : static slotdef *
6252 55230 : update_one_slot(PyTypeObject *type, slotdef *p)
6253 : {
6254 : PyObject *descr;
6255 : PyWrapperDescrObject *d;
6256 55230 : void *generic = NULL, *specific = NULL;
6257 55230 : int use_generic = 0;
6258 55230 : int offset = p->offset;
6259 55230 : void **ptr = slotptr(type, offset);
6260 :
6261 55230 : if (ptr == NULL) {
6262 : do {
6263 0 : ++p;
6264 0 : } while (p->offset == offset);
6265 0 : return p;
6266 : }
6267 : do {
6268 78111 : descr = _PyType_Lookup(type, p->name_strobj);
6269 78111 : if (descr == NULL) {
6270 65676 : if (ptr == (void**)&type->tp_iternext) {
6271 774 : specific = _PyObject_NextNotImplemented;
6272 : }
6273 65676 : continue;
6274 : }
6275 21027 : if (Py_TYPE(descr) == &PyWrapperDescr_Type &&
6276 17184 : ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) {
6277 8592 : void **tptr = resolve_slotdups(type, p->name_strobj);
6278 8592 : if (tptr == NULL || tptr == ptr)
6279 5163 : generic = p->function;
6280 8592 : d = (PyWrapperDescrObject *)descr;
6281 13683 : if (d->d_base->wrapper == p->wrapper &&
6282 5091 : PyType_IsSubtype(type, d->d_type))
6283 : {
6284 5880 : if (specific == NULL ||
6285 789 : specific == d->d_wrapped)
6286 5091 : specific = d->d_wrapped;
6287 : else
6288 0 : use_generic = 1;
6289 : }
6290 : }
6291 4590 : else if (Py_TYPE(descr) == &PyCFunction_Type &&
6292 747 : PyCFunction_GET_FUNCTION(descr) ==
6293 747 : (PyCFunction)tp_new_wrapper &&
6294 747 : ptr == (void**)&type->tp_new)
6295 : {
6296 : /* The __new__ wrapper is not a wrapper descriptor,
6297 : so must be special-cased differently.
6298 : If we don't do this, creating an instance will
6299 : always use slot_tp_new which will look up
6300 : __new__ in the MRO which will call tp_new_wrapper
6301 : which will look through the base classes looking
6302 : for a static base and call its tp_new (usually
6303 : PyType_GenericNew), after performing various
6304 : sanity checks and constructing a new argument
6305 : list. Cut all that nonsense short -- this speeds
6306 : up instance creation tremendously. */
6307 747 : specific = (void *)type->tp_new;
6308 : /* XXX I'm not 100% sure that there isn't a hole
6309 : in this reasoning that requires additional
6310 : sanity checks. I'll buy the first person to
6311 : point out a bug in this reasoning a beer. */
6312 : }
6313 3123 : else if (descr == Py_None &&
6314 27 : ptr == (void**)&type->tp_hash) {
6315 : /* We specifically allow __hash__ to be set to None
6316 : to prevent inheritance of the default
6317 : implementation from object.__hash__ */
6318 27 : specific = PyObject_HashNotImplemented;
6319 : }
6320 : else {
6321 3069 : use_generic = 1;
6322 3069 : generic = p->function;
6323 : }
6324 78111 : } while ((++p)->offset == offset);
6325 55230 : if (specific && !use_generic)
6326 5553 : *ptr = specific;
6327 : else
6328 49677 : *ptr = generic;
6329 55230 : return p;
6330 : }
6331 :
6332 : /* In the type, update the slots whose slotdefs are gathered in the pp array.
6333 : This is a callback for update_subclasses(). */
6334 : static int
6335 0 : update_slots_callback(PyTypeObject *type, void *data)
6336 : {
6337 0 : slotdef **pp = (slotdef **)data;
6338 :
6339 0 : for (; *pp; pp++)
6340 0 : update_one_slot(type, *pp);
6341 0 : return 0;
6342 : }
6343 :
6344 : /* Initialize the slotdefs table by adding interned string objects for the
6345 : names and sorting the entries. */
6346 : static void
6347 3285 : init_slotdefs(void)
6348 : {
6349 : slotdef *p;
6350 : static int initialized = 0;
6351 :
6352 3285 : if (initialized)
6353 6567 : return;
6354 300 : for (p = slotdefs; p->name; p++) {
6355 : /* Slots must be ordered by their offset in the PyHeapTypeObject. */
6356 : assert(!p[1].name || p->offset <= p[1].offset);
6357 297 : p->name_strobj = PyString_InternFromString(p->name);
6358 297 : if (!p->name_strobj)
6359 0 : Py_FatalError("Out of memory interning slotdef names");
6360 : }
6361 3 : initialized = 1;
6362 : }
6363 :
6364 : /* Update the slots after assignment to a class (type) attribute. */
6365 : static int
6366 1200 : update_slot(PyTypeObject *type, PyObject *name)
6367 : {
6368 : slotdef *ptrs[MAX_EQUIV];
6369 : slotdef *p;
6370 : slotdef **pp;
6371 : int offset;
6372 :
6373 : /* Clear the VALID_VERSION flag of 'type' and all its
6374 : subclasses. This could possibly be unified with the
6375 : update_subclasses() recursion below, but carefully:
6376 : they each have their own conditions on which to stop
6377 : recursing into subclasses. */
6378 1200 : PyType_Modified(type);
6379 :
6380 1200 : init_slotdefs();
6381 1200 : pp = ptrs;
6382 120000 : for (p = slotdefs; p->name; p++) {
6383 : /* XXX assume name is interned! */
6384 118800 : if (p->name_strobj == name)
6385 0 : *pp++ = p;
6386 : }
6387 1200 : *pp = NULL;
6388 1200 : for (pp = ptrs; *pp; pp++) {
6389 0 : p = *pp;
6390 0 : offset = p->offset;
6391 0 : while (p > slotdefs && (p-1)->offset == offset)
6392 0 : --p;
6393 0 : *pp = p;
6394 : }
6395 1200 : if (ptrs[0] == NULL)
6396 1200 : return 0; /* Not an attribute that affects any slots */
6397 0 : return update_subclasses(type, name,
6398 : update_slots_callback, (void *)ptrs);
6399 : }
6400 :
6401 : /* Store the proper functions in the slot dispatches at class (type)
6402 : definition time, based upon which operations the class overrides in its
6403 : dict. */
6404 : static void
6405 789 : fixup_slot_dispatchers(PyTypeObject *type)
6406 : {
6407 : slotdef *p;
6408 :
6409 789 : init_slotdefs();
6410 56808 : for (p = slotdefs; p->name; )
6411 55230 : p = update_one_slot(type, p);
6412 789 : }
6413 :
6414 : static void
6415 0 : update_all_slots(PyTypeObject* type)
6416 : {
6417 : slotdef *p;
6418 :
6419 0 : init_slotdefs();
6420 0 : for (p = slotdefs; p->name; p++) {
6421 : /* update_slot returns int but can't actually fail */
6422 0 : update_slot(type, p->name_strobj);
6423 : }
6424 0 : }
6425 :
6426 : /* recurse_down_subclasses() and update_subclasses() are mutually
6427 : recursive functions to call a callback for all subclasses,
6428 : but refraining from recursing into subclasses that define 'name'. */
6429 :
6430 : static int
6431 0 : update_subclasses(PyTypeObject *type, PyObject *name,
6432 : update_callback callback, void *data)
6433 : {
6434 0 : if (callback(type, data) < 0)
6435 0 : return -1;
6436 0 : return recurse_down_subclasses(type, name, callback, data);
6437 : }
6438 :
6439 : static int
6440 0 : recurse_down_subclasses(PyTypeObject *type, PyObject *name,
6441 : update_callback callback, void *data)
6442 : {
6443 : PyTypeObject *subclass;
6444 : PyObject *ref, *subclasses, *dict;
6445 : Py_ssize_t i, n;
6446 :
6447 0 : subclasses = type->tp_subclasses;
6448 0 : if (subclasses == NULL)
6449 0 : return 0;
6450 : assert(PyList_Check(subclasses));
6451 0 : n = PyList_GET_SIZE(subclasses);
6452 0 : for (i = 0; i < n; i++) {
6453 0 : ref = PyList_GET_ITEM(subclasses, i);
6454 : assert(PyWeakref_CheckRef(ref));
6455 0 : subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
6456 : assert(subclass != NULL);
6457 0 : if ((PyObject *)subclass == Py_None)
6458 0 : continue;
6459 : assert(PyType_Check(subclass));
6460 : /* Avoid recursing down into unaffected classes */
6461 0 : dict = subclass->tp_dict;
6462 0 : if (dict != NULL && PyDict_Check(dict) &&
6463 0 : PyDict_GetItem(dict, name) != NULL)
6464 0 : continue;
6465 0 : if (update_subclasses(subclass, name, callback, data) < 0)
6466 0 : return -1;
6467 : }
6468 0 : return 0;
6469 : }
6470 :
6471 : /* This function is called by PyType_Ready() to populate the type's
6472 : dictionary with method descriptors for function slots. For each
6473 : function slot (like tp_repr) that's defined in the type, one or more
6474 : corresponding descriptors are added in the type's tp_dict dictionary
6475 : under the appropriate name (like __repr__). Some function slots
6476 : cause more than one descriptor to be added (for example, the nb_add
6477 : slot adds both __add__ and __radd__ descriptors) and some function
6478 : slots compete for the same descriptor (for example both sq_item and
6479 : mp_subscript generate a __getitem__ descriptor).
6480 :
6481 : In the latter case, the first slotdef entry encountered wins. Since
6482 : slotdef entries are sorted by the offset of the slot in the
6483 : PyHeapTypeObject, this gives us some control over disambiguating
6484 : between competing slots: the members of PyHeapTypeObject are listed
6485 : from most general to least general, so the most general slot is
6486 : preferred. In particular, because as_mapping comes before as_sequence,
6487 : for a type that defines both mp_subscript and sq_item, mp_subscript
6488 : wins.
6489 :
6490 : This only adds new descriptors and doesn't overwrite entries in
6491 : tp_dict that were previously defined. The descriptors contain a
6492 : reference to the C function they must call, so that it's safe if they
6493 : are copied into a subtype's __dict__ and the subtype has a different
6494 : C function in its slot -- calling the method defined by the
6495 : descriptor will call the C function that was used to create it,
6496 : rather than the C function present in the slot when it is called.
6497 : (This is important because a subtype may have a C function in the
6498 : slot that calls the method from the dictionary, and we want to avoid
6499 : infinite recursion here.) */
6500 :
6501 : static int
6502 1296 : add_operators(PyTypeObject *type)
6503 : {
6504 1296 : PyObject *dict = type->tp_dict;
6505 : slotdef *p;
6506 : PyObject *descr;
6507 : void **ptr;
6508 :
6509 1296 : init_slotdefs();
6510 129600 : for (p = slotdefs; p->name; p++) {
6511 128304 : if (p->wrapper == NULL)
6512 11664 : continue;
6513 116640 : ptr = slotptr(type, p->offset);
6514 116640 : if (!ptr || !*ptr)
6515 113202 : continue;
6516 3438 : if (PyDict_GetItem(dict, p->name_strobj))
6517 123 : continue;
6518 3315 : if (*ptr == PyObject_HashNotImplemented) {
6519 : /* Classes may prevent the inheritance of the tp_hash
6520 : slot by storing PyObject_HashNotImplemented in it. Make it
6521 : visible as a None value for the __hash__ attribute. */
6522 12 : if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
6523 0 : return -1;
6524 : }
6525 : else {
6526 3303 : descr = PyDescr_NewWrapper(type, p, *ptr);
6527 3303 : if (descr == NULL)
6528 0 : return -1;
6529 3303 : if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) {
6530 0 : Py_DECREF(descr);
6531 0 : return -1;
6532 : }
6533 3303 : Py_DECREF(descr);
6534 : }
6535 : }
6536 1296 : if (type->tp_new != NULL) {
6537 393 : if (add_tp_new_wrapper(type) < 0)
6538 0 : return -1;
6539 : }
6540 1296 : return 0;
6541 : }
6542 :
6543 :
6544 : /* Cooperative 'super' */
6545 :
6546 : typedef struct {
6547 : PyObject_HEAD
6548 : PyTypeObject *type;
6549 : PyObject *obj;
6550 : PyTypeObject *obj_type;
6551 : } superobject;
6552 :
6553 : static PyMemberDef super_members[] = {
6554 : {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
6555 : "the class invoking super()"},
6556 : {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
6557 : "the instance invoking super(); may be None"},
6558 : {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
6559 : "the type of the instance invoking super(); may be None"},
6560 : {0}
6561 : };
6562 :
6563 : static void
6564 72 : super_dealloc(PyObject *self)
6565 : {
6566 72 : superobject *su = (superobject *)self;
6567 :
6568 72 : _PyObject_GC_UNTRACK(self);
6569 72 : Py_XDECREF(su->obj);
6570 72 : Py_XDECREF(su->type);
6571 72 : Py_XDECREF(su->obj_type);
6572 72 : Py_TYPE(self)->tp_free(self);
6573 72 : }
6574 :
6575 : static PyObject *
6576 0 : super_repr(PyObject *self)
6577 : {
6578 0 : superobject *su = (superobject *)self;
6579 :
6580 0 : if (su->obj_type)
6581 0 : return PyString_FromFormat(
6582 : "<super: <class '%s'>, <%s object>>",
6583 0 : su->type ? su->type->tp_name : "NULL",
6584 0 : su->obj_type->tp_name);
6585 : else
6586 0 : return PyString_FromFormat(
6587 : "<super: <class '%s'>, NULL>",
6588 0 : su->type ? su->type->tp_name : "NULL");
6589 : }
6590 :
6591 : static PyObject *
6592 72 : super_getattro(PyObject *self, PyObject *name)
6593 : {
6594 72 : superobject *su = (superobject *)self;
6595 72 : int skip = su->obj_type == NULL;
6596 :
6597 72 : if (!skip) {
6598 : /* We want __class__ to return the class of the super object
6599 : (i.e. super, or a subclass), not the class of su->obj. */
6600 216 : skip = (PyString_Check(name) &&
6601 72 : PyString_GET_SIZE(name) == 9 &&
6602 0 : strcmp(PyString_AS_STRING(name), "__class__") == 0);
6603 : }
6604 :
6605 72 : if (!skip) {
6606 : PyObject *mro, *res, *tmp, *dict;
6607 : PyTypeObject *starttype;
6608 : descrgetfunc f;
6609 : Py_ssize_t i, n;
6610 :
6611 72 : starttype = su->obj_type;
6612 72 : mro = starttype->tp_mro;
6613 :
6614 72 : if (mro == NULL)
6615 0 : n = 0;
6616 : else {
6617 : assert(PyTuple_Check(mro));
6618 72 : n = PyTuple_GET_SIZE(mro);
6619 : }
6620 72 : for (i = 0; i < n; i++) {
6621 72 : if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
6622 72 : break;
6623 : }
6624 72 : i++;
6625 72 : res = NULL;
6626 72 : for (; i < n; i++) {
6627 72 : tmp = PyTuple_GET_ITEM(mro, i);
6628 72 : if (PyType_Check(tmp))
6629 72 : dict = ((PyTypeObject *)tmp)->tp_dict;
6630 0 : else if (PyClass_Check(tmp))
6631 0 : dict = ((PyClassObject *)tmp)->cl_dict;
6632 : else
6633 0 : continue;
6634 72 : res = PyDict_GetItem(dict, name);
6635 72 : if (res != NULL) {
6636 72 : Py_INCREF(res);
6637 72 : f = Py_TYPE(res)->tp_descr_get;
6638 72 : if (f != NULL) {
6639 12 : tmp = f(res,
6640 : /* Only pass 'obj' param if
6641 : this is instance-mode super
6642 : (See SF ID #743627)
6643 : */
6644 12 : (su->obj == (PyObject *)
6645 12 : su->obj_type
6646 : ? (PyObject *)NULL
6647 : : su->obj),
6648 : (PyObject *)starttype);
6649 12 : Py_DECREF(res);
6650 12 : res = tmp;
6651 : }
6652 72 : return res;
6653 : }
6654 : }
6655 : }
6656 0 : return PyObject_GenericGetAttr(self, name);
6657 : }
6658 :
6659 : static PyTypeObject *
6660 72 : supercheck(PyTypeObject *type, PyObject *obj)
6661 : {
6662 : /* Check that a super() call makes sense. Return a type object.
6663 :
6664 : obj can be a new-style class, or an instance of one:
6665 :
6666 : - If it is a class, it must be a subclass of 'type'. This case is
6667 : used for class methods; the return value is obj.
6668 :
6669 : - If it is an instance, it must be an instance of 'type'. This is
6670 : the normal case; the return value is obj.__class__.
6671 :
6672 : But... when obj is an instance, we want to allow for the case where
6673 : Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
6674 : This will allow using super() with a proxy for obj.
6675 : */
6676 :
6677 : /* Check for first bullet above (special case) */
6678 72 : if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
6679 60 : Py_INCREF(obj);
6680 60 : return (PyTypeObject *)obj;
6681 : }
6682 :
6683 : /* Normal case */
6684 12 : if (PyType_IsSubtype(Py_TYPE(obj), type)) {
6685 12 : Py_INCREF(Py_TYPE(obj));
6686 12 : return Py_TYPE(obj);
6687 : }
6688 : else {
6689 : /* Try the slow way */
6690 : static PyObject *class_str = NULL;
6691 : PyObject *class_attr;
6692 :
6693 0 : if (class_str == NULL) {
6694 0 : class_str = PyString_FromString("__class__");
6695 0 : if (class_str == NULL)
6696 0 : return NULL;
6697 : }
6698 :
6699 0 : class_attr = PyObject_GetAttr(obj, class_str);
6700 :
6701 0 : if (class_attr != NULL &&
6702 0 : PyType_Check(class_attr) &&
6703 0 : (PyTypeObject *)class_attr != Py_TYPE(obj))
6704 : {
6705 0 : int ok = PyType_IsSubtype(
6706 : (PyTypeObject *)class_attr, type);
6707 0 : if (ok)
6708 0 : return (PyTypeObject *)class_attr;
6709 : }
6710 :
6711 0 : if (class_attr == NULL)
6712 0 : PyErr_Clear();
6713 : else
6714 0 : Py_DECREF(class_attr);
6715 : }
6716 :
6717 0 : PyErr_SetString(PyExc_TypeError,
6718 : "super(type, obj): "
6719 : "obj must be an instance or subtype of type");
6720 0 : return NULL;
6721 : }
6722 :
6723 : static PyObject *
6724 0 : super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
6725 : {
6726 0 : superobject *su = (superobject *)self;
6727 : superobject *newobj;
6728 :
6729 0 : if (obj == NULL || obj == Py_None || su->obj != NULL) {
6730 : /* Not binding to an object, or already bound */
6731 0 : Py_INCREF(self);
6732 0 : return self;
6733 : }
6734 0 : if (Py_TYPE(su) != &PySuper_Type)
6735 : /* If su is an instance of a (strict) subclass of super,
6736 : call its type */
6737 0 : return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
6738 : su->type, obj, NULL);
6739 : else {
6740 : /* Inline the common case */
6741 0 : PyTypeObject *obj_type = supercheck(su->type, obj);
6742 0 : if (obj_type == NULL)
6743 0 : return NULL;
6744 0 : newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
6745 : NULL, NULL);
6746 0 : if (newobj == NULL)
6747 0 : return NULL;
6748 0 : Py_INCREF(su->type);
6749 0 : Py_INCREF(obj);
6750 0 : newobj->type = su->type;
6751 0 : newobj->obj = obj;
6752 0 : newobj->obj_type = obj_type;
6753 0 : return (PyObject *)newobj;
6754 : }
6755 : }
6756 :
6757 : static int
6758 72 : super_init(PyObject *self, PyObject *args, PyObject *kwds)
6759 : {
6760 72 : superobject *su = (superobject *)self;
6761 : PyTypeObject *type;
6762 72 : PyObject *obj = NULL;
6763 72 : PyTypeObject *obj_type = NULL;
6764 :
6765 72 : if (!_PyArg_NoKeywords("super", kwds))
6766 0 : return -1;
6767 72 : if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
6768 0 : return -1;
6769 72 : if (obj == Py_None)
6770 0 : obj = NULL;
6771 72 : if (obj != NULL) {
6772 72 : obj_type = supercheck(type, obj);
6773 72 : if (obj_type == NULL)
6774 0 : return -1;
6775 72 : Py_INCREF(obj);
6776 : }
6777 72 : Py_INCREF(type);
6778 72 : Py_XSETREF(su->type, type);
6779 72 : Py_XSETREF(su->obj, obj);
6780 72 : Py_XSETREF(su->obj_type, obj_type);
6781 72 : return 0;
6782 : }
6783 :
6784 : PyDoc_STRVAR(super_doc,
6785 : "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
6786 : "super(type) -> unbound super object\n"
6787 : "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
6788 : "Typical use to call a cooperative superclass method:\n"
6789 : "class C(B):\n"
6790 : " def meth(self, arg):\n"
6791 : " super(C, self).meth(arg)");
6792 :
6793 : static int
6794 0 : super_traverse(PyObject *self, visitproc visit, void *arg)
6795 : {
6796 0 : superobject *su = (superobject *)self;
6797 :
6798 0 : Py_VISIT(su->obj);
6799 0 : Py_VISIT(su->type);
6800 0 : Py_VISIT(su->obj_type);
6801 :
6802 0 : return 0;
6803 : }
6804 :
6805 : PyTypeObject PySuper_Type = {
6806 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
6807 : "super", /* tp_name */
6808 : sizeof(superobject), /* tp_basicsize */
6809 : 0, /* tp_itemsize */
6810 : /* methods */
6811 : super_dealloc, /* tp_dealloc */
6812 : 0, /* tp_print */
6813 : 0, /* tp_getattr */
6814 : 0, /* tp_setattr */
6815 : 0, /* tp_compare */
6816 : super_repr, /* tp_repr */
6817 : 0, /* tp_as_number */
6818 : 0, /* tp_as_sequence */
6819 : 0, /* tp_as_mapping */
6820 : 0, /* tp_hash */
6821 : 0, /* tp_call */
6822 : 0, /* tp_str */
6823 : super_getattro, /* tp_getattro */
6824 : 0, /* tp_setattro */
6825 : 0, /* tp_as_buffer */
6826 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
6827 : Py_TPFLAGS_BASETYPE, /* tp_flags */
6828 : super_doc, /* tp_doc */
6829 : super_traverse, /* tp_traverse */
6830 : 0, /* tp_clear */
6831 : 0, /* tp_richcompare */
6832 : 0, /* tp_weaklistoffset */
6833 : 0, /* tp_iter */
6834 : 0, /* tp_iternext */
6835 : 0, /* tp_methods */
6836 : super_members, /* tp_members */
6837 : 0, /* tp_getset */
6838 : 0, /* tp_base */
6839 : 0, /* tp_dict */
6840 : super_descr_get, /* tp_descr_get */
6841 : 0, /* tp_descr_set */
6842 : 0, /* tp_dictoffset */
6843 : super_init, /* tp_init */
6844 : PyType_GenericAlloc, /* tp_alloc */
6845 : PyType_GenericNew, /* tp_new */
6846 : PyObject_GC_Del, /* tp_free */
6847 : };
|