History

add hg and python

cinap_lenrek committed 15 years ago

Commit 4bb9e221 parent 987265c3706 files · +1228772 −-233Browse files

Showing the changes to sys/src/cmd/python/Objects/typeobject.c (1 of 3706 files). Show all

Unified · Split

sys/src/cmd/python/Objects/typeobject.c+5891−0
@@ -0,0 +1,5891 @@
1/* Type object implementation */
2
3#include "Python.h"
4#include "structmember.h"
5
6#include <ctype.h>
7
8static PyMemberDef type_members[] = {
9 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
10 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
11 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
12 {"__weakrefoffset__", T_LONG,
13 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
15 {"__dictoffset__", T_LONG,
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
17 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
18 {0}
19};
20
21static PyObject *
22type_name(PyTypeObject *type, void *context)
23{
24 const char *s;
25
26 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
27 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
28
29 Py_INCREF(et->ht_name);
30 return et->ht_name;
31 }
32 else {
33 s = strrchr(type->tp_name, '.');
34 if (s == NULL)
35 s = type->tp_name;
36 else
37 s++;
38 return PyString_FromString(s);
39 }
40}
41
42static int
43type_set_name(PyTypeObject *type, PyObject *value, void *context)
44{
45 PyHeapTypeObject* et;
46
47 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
48 PyErr_Format(PyExc_TypeError,
49 "can't set %s.__name__", type->tp_name);
50 return -1;
51 }
52 if (!value) {
53 PyErr_Format(PyExc_TypeError,
54 "can't delete %s.__name__", type->tp_name);
55 return -1;
56 }
57 if (!PyString_Check(value)) {
58 PyErr_Format(PyExc_TypeError,
59 "can only assign string to %s.__name__, not '%s'",
60 type->tp_name, value->ob_type->tp_name);
61 return -1;
62 }
63 if (strlen(PyString_AS_STRING(value))
64 != (size_t)PyString_GET_SIZE(value)) {
65 PyErr_Format(PyExc_ValueError,
66 "__name__ must not contain null bytes");
67 return -1;
68 }
69
70 et = (PyHeapTypeObject*)type;
71
72 Py_INCREF(value);
73
74 Py_DECREF(et->ht_name);
75 et->ht_name = value;
76
77 type->tp_name = PyString_AS_STRING(value);
78
79 return 0;
80}
81
82static PyObject *
83type_module(PyTypeObject *type, void *context)
84{
85 PyObject *mod;
86 char *s;
87
88 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
89 mod = PyDict_GetItemString(type->tp_dict, "__module__");
90 if (!mod) {
91 PyErr_Format(PyExc_AttributeError, "__module__");
92 return 0;
93 }
94 Py_XINCREF(mod);
95 return mod;
96 }
97 else {
98 s = strrchr(type->tp_name, '.');
99 if (s != NULL)
100 return PyString_FromStringAndSize(
101 type->tp_name, (Py_ssize_t)(s - type->tp_name));
102 return PyString_FromString("__builtin__");
103 }
104}
105
106static int
107type_set_module(PyTypeObject *type, PyObject *value, void *context)
108{
109 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
110 PyErr_Format(PyExc_TypeError,
111 "can't set %s.__module__", type->tp_name);
112 return -1;
113 }
114 if (!value) {
115 PyErr_Format(PyExc_TypeError,
116 "can't delete %s.__module__", type->tp_name);
117 return -1;
118 }
119
120 return PyDict_SetItemString(type->tp_dict, "__module__", value);
121}
122
123static PyObject *
124type_get_bases(PyTypeObject *type, void *context)
125{
126 Py_INCREF(type->tp_bases);
127 return type->tp_bases;
128}
129
130static PyTypeObject *best_base(PyObject *);
131static int mro_internal(PyTypeObject *);
132static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
133static int add_subclass(PyTypeObject*, PyTypeObject*);
134static void remove_subclass(PyTypeObject *, PyTypeObject *);
135static void update_all_slots(PyTypeObject *);
136
137typedef int (*update_callback)(PyTypeObject *, void *);
138static int update_subclasses(PyTypeObject *type, PyObject *name,
139 update_callback callback, void *data);
140static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
141 update_callback callback, void *data);
142
143static int
144mro_subclasses(PyTypeObject *type, PyObject* temp)
145{
146 PyTypeObject *subclass;
147 PyObject *ref, *subclasses, *old_mro;
148 Py_ssize_t i, n;
149
150 subclasses = type->tp_subclasses;
151 if (subclasses == NULL)
152 return 0;
153 assert(PyList_Check(subclasses));
154 n = PyList_GET_SIZE(subclasses);
155 for (i = 0; i < n; i++) {
156 ref = PyList_GET_ITEM(subclasses, i);
157 assert(PyWeakref_CheckRef(ref));
158 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
159 assert(subclass != NULL);
160 if ((PyObject *)subclass == Py_None)
161 continue;
162 assert(PyType_Check(subclass));
163 old_mro = subclass->tp_mro;
164 if (mro_internal(subclass) < 0) {
165 subclass->tp_mro = old_mro;
166 return -1;
167 }
168 else {
169 PyObject* tuple;
170 tuple = PyTuple_Pack(2, subclass, old_mro);
171 Py_DECREF(old_mro);
172 if (!tuple)
173 return -1;
174 if (PyList_Append(temp, tuple) < 0)
175 return -1;
176 Py_DECREF(tuple);
177 }
178 if (mro_subclasses(subclass, temp) < 0)
179 return -1;
180 }
181 return 0;
182}
183
184static int
185type_set_bases(PyTypeObject *type, PyObject *value, void *context)
186{
187 Py_ssize_t i;
188 int r = 0;
189 PyObject *ob, *temp;
190 PyTypeObject *new_base, *old_base;
191 PyObject *old_bases, *old_mro;
192
193 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
194 PyErr_Format(PyExc_TypeError,
195 "can't set %s.__bases__", type->tp_name);
196 return -1;
197 }
198 if (!value) {
199 PyErr_Format(PyExc_TypeError,
200 "can't delete %s.__bases__", type->tp_name);
201 return -1;
202 }
203 if (!PyTuple_Check(value)) {
204 PyErr_Format(PyExc_TypeError,
205 "can only assign tuple to %s.__bases__, not %s",
206 type->tp_name, value->ob_type->tp_name);
207 return -1;
208 }
209 if (PyTuple_GET_SIZE(value) == 0) {
210 PyErr_Format(PyExc_TypeError,
211 "can only assign non-empty tuple to %s.__bases__, not ()",
212 type->tp_name);
213 return -1;
214 }
215 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
216 ob = PyTuple_GET_ITEM(value, i);
217 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
218 PyErr_Format(
219 PyExc_TypeError,
220 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
221 type->tp_name, ob->ob_type->tp_name);
222 return -1;
223 }
224 if (PyType_Check(ob)) {
225 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
226 PyErr_SetString(PyExc_TypeError,
227 "a __bases__ item causes an inheritance cycle");
228 return -1;
229 }
230 }
231 }
232
233 new_base = best_base(value);
234
235 if (!new_base) {
236 return -1;
237 }
238
239 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
240 return -1;
241
242 Py_INCREF(new_base);
243 Py_INCREF(value);
244
245 old_bases = type->tp_bases;
246 old_base = type->tp_base;
247 old_mro = type->tp_mro;
248
249 type->tp_bases = value;
250 type->tp_base = new_base;
251
252 if (mro_internal(type) < 0) {
253 goto bail;
254 }
255
256 temp = PyList_New(0);
257 if (!temp)
258 goto bail;
259
260 r = mro_subclasses(type, temp);
261
262 if (r < 0) {
263 for (i = 0; i < PyList_Size(temp); i++) {
264 PyTypeObject* cls;
265 PyObject* mro;
266 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
267 "", 2, 2, &cls, &mro);
268 Py_DECREF(cls->tp_mro);
269 cls->tp_mro = mro;
270 Py_INCREF(cls->tp_mro);
271 }
272 Py_DECREF(temp);
273 goto bail;
274 }
275
276 Py_DECREF(temp);
277
278 /* any base that was in __bases__ but now isn't, we
279 need to remove |type| from its tp_subclasses.
280 conversely, any class now in __bases__ that wasn't
281 needs to have |type| added to its subclasses. */
282
283 /* for now, sod that: just remove from all old_bases,
284 add to all new_bases */
285
286 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
287 ob = PyTuple_GET_ITEM(old_bases, i);
288 if (PyType_Check(ob)) {
289 remove_subclass(
290 (PyTypeObject*)ob, type);
291 }
292 }
293
294 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
295 ob = PyTuple_GET_ITEM(value, i);
296 if (PyType_Check(ob)) {
297 if (add_subclass((PyTypeObject*)ob, type) < 0)
298 r = -1;
299 }
300 }
301
302 update_all_slots(type);
303
304 Py_DECREF(old_bases);
305 Py_DECREF(old_base);
306 Py_DECREF(old_mro);
307
308 return r;
309
310 bail:
311 Py_DECREF(type->tp_bases);
312 Py_DECREF(type->tp_base);
313 if (type->tp_mro != old_mro) {
314 Py_DECREF(type->tp_mro);
315 }
316
317 type->tp_bases = old_bases;
318 type->tp_base = old_base;
319 type->tp_mro = old_mro;
320
321 return -1;
322}
323
324static PyObject *
325type_dict(PyTypeObject *type, void *context)
326{
327 if (type->tp_dict == NULL) {
328 Py_INCREF(Py_None);
329 return Py_None;
330 }
331 return PyDictProxy_New(type->tp_dict);
332}
333
334static PyObject *
335type_get_doc(PyTypeObject *type, void *context)
336{
337 PyObject *result;
338 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
339 return PyString_FromString(type->tp_doc);
340 result = PyDict_GetItemString(type->tp_dict, "__doc__");
341 if (result == NULL) {
342 result = Py_None;
343 Py_INCREF(result);
344 }
345 else if (result->ob_type->tp_descr_get) {
346 result = result->ob_type->tp_descr_get(result, NULL,
347 (PyObject *)type);
348 }
349 else {
350 Py_INCREF(result);
351 }
352 return result;
353}
354
355static PyGetSetDef type_getsets[] = {
356 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
357 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
358 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
359 {"__dict__", (getter)type_dict, NULL, NULL},
360 {"__doc__", (getter)type_get_doc, NULL, NULL},
361 {0}
362};
363
364static int
365type_compare(PyObject *v, PyObject *w)
366{
367 /* This is called with type objects only. So we
368 can just compare the addresses. */
369 Py_uintptr_t vv = (Py_uintptr_t)v;
370 Py_uintptr_t ww = (Py_uintptr_t)w;
371 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
372}
373
374static PyObject *
375type_repr(PyTypeObject *type)
376{
377 PyObject *mod, *name, *rtn;
378 char *kind;
379
380 mod = type_module(type, NULL);
381 if (mod == NULL)
382 PyErr_Clear();
383 else if (!PyString_Check(mod)) {
384 Py_DECREF(mod);
385 mod = NULL;
386 }
387 name = type_name(type, NULL);
388 if (name == NULL)
389 return NULL;
390
391 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
392 kind = "class";
393 else
394 kind = "type";
395
396 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
397 rtn = PyString_FromFormat("<%s '%s.%s'>",
398 kind,
399 PyString_AS_STRING(mod),
400 PyString_AS_STRING(name));
401 }
402 else
403 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
404
405 Py_XDECREF(mod);
406 Py_DECREF(name);
407 return rtn;
408}
409
410static PyObject *
411type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
412{
413 PyObject *obj;
414
415 if (type->tp_new == NULL) {
416 PyErr_Format(PyExc_TypeError,
417 "cannot create '%.100s' instances",
418 type->tp_name);
419 return NULL;
420 }
421
422 obj = type->tp_new(type, args, kwds);
423 if (obj != NULL) {
424 /* Ugly exception: when the call was type(something),
425 don't call tp_init on the result. */
426 if (type == &PyType_Type &&
427 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
428 (kwds == NULL ||
429 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
430 return obj;
431 /* If the returned object is not an instance of type,
432 it won't be initialized. */
433 if (!PyType_IsSubtype(obj->ob_type, type))
434 return obj;
435 type = obj->ob_type;
436 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
437 type->tp_init != NULL &&
438 type->tp_init(obj, args, kwds) < 0) {
439 Py_DECREF(obj);
440 obj = NULL;
441 }
442 }
443 return obj;
444}
445
446PyObject *
447PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
448{
449 PyObject *obj;
450 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
451 /* note that we need to add one, for the sentinel */
452
453 if (PyType_IS_GC(type))
454 obj = _PyObject_GC_Malloc(size);
455 else
456 obj = (PyObject *)PyObject_MALLOC(size);
457
458 if (obj == NULL)
459 return PyErr_NoMemory();
460
461 memset(obj, '\0', size);
462
463 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
464 Py_INCREF(type);
465
466 if (type->tp_itemsize == 0)
467 PyObject_INIT(obj, type);
468 else
469 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
470
471 if (PyType_IS_GC(type))
472 _PyObject_GC_TRACK(obj);
473 return obj;
474}
475
476PyObject *
477PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
478{
479 return type->tp_alloc(type, 0);
480}
481
482/* Helpers for subtyping */
483
484static int
485traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
486{
487 Py_ssize_t i, n;
488 PyMemberDef *mp;
489
490 n = type->ob_size;
491 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
492 for (i = 0; i < n; i++, mp++) {
493 if (mp->type == T_OBJECT_EX) {
494 char *addr = (char *)self + mp->offset;
495 PyObject *obj = *(PyObject **)addr;
496 if (obj != NULL) {
497 int err = visit(obj, arg);
498 if (err)
499 return err;
500 }
501 }
502 }
503 return 0;
504}
505
506static int
507subtype_traverse(PyObject *self, visitproc visit, void *arg)
508{
509 PyTypeObject *type, *base;
510 traverseproc basetraverse;
511
512 /* Find the nearest base with a different tp_traverse,
513 and traverse slots while we're at it */
514 type = self->ob_type;
515 base = type;
516 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
517 if (base->ob_size) {
518 int err = traverse_slots(base, self, visit, arg);
519 if (err)
520 return err;
521 }
522 base = base->tp_base;
523 assert(base);
524 }
525
526 if (type->tp_dictoffset != base->tp_dictoffset) {
527 PyObject **dictptr = _PyObject_GetDictPtr(self);
528 if (dictptr && *dictptr)
529 Py_VISIT(*dictptr);
530 }
531
532 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
533 /* For a heaptype, the instances count as references
534 to the type. Traverse the type so the collector
535 can find cycles involving this link. */
536 Py_VISIT(type);
537
538 if (basetraverse)
539 return basetraverse(self, visit, arg);
540 return 0;
541}
542
543static void
544clear_slots(PyTypeObject *type, PyObject *self)
545{
546 Py_ssize_t i, n;
547 PyMemberDef *mp;
548
549 n = type->ob_size;
550 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
551 for (i = 0; i < n; i++, mp++) {
552 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
553 char *addr = (char *)self + mp->offset;
554 PyObject *obj = *(PyObject **)addr;
555 if (obj != NULL) {
556 *(PyObject **)addr = NULL;
557 Py_DECREF(obj);
558 }
559 }
560 }
561}
562
563static int
564subtype_clear(PyObject *self)
565{
566 PyTypeObject *type, *base;
567 inquiry baseclear;
568
569 /* Find the nearest base with a different tp_clear
570 and clear slots while we're at it */
571 type = self->ob_type;
572 base = type;
573 while ((baseclear = base->tp_clear) == subtype_clear) {
574 if (base->ob_size)
575 clear_slots(base, self);
576 base = base->tp_base;
577 assert(base);
578 }
579
580 /* There's no need to clear the instance dict (if any);
581 the collector will call its tp_clear handler. */
582
583 if (baseclear)
584 return baseclear(self);
585 return 0;
586}
587
588static void
589subtype_dealloc(PyObject *self)
590{
591 PyTypeObject *type, *base;
592 destructor basedealloc;
593
594 /* Extract the type; we expect it to be a heap type */
595 type = self->ob_type;
596 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
597
598 /* Test whether the type has GC exactly once */
599
600 if (!PyType_IS_GC(type)) {
601 /* It's really rare to find a dynamic type that doesn't have
602 GC; it can only happen when deriving from 'object' and not
603 adding any slots or instance variables. This allows
604 certain simplifications: there's no need to call
605 clear_slots(), or DECREF the dict, or clear weakrefs. */
606
607 /* Maybe call finalizer; exit early if resurrected */
608 if (type->tp_del) {
609 type->tp_del(self);
610 if (self->ob_refcnt > 0)
611 return;
612 }
613
614 /* Find the nearest base with a different tp_dealloc */
615 base = type;
616 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
617 assert(base->ob_size == 0);
618 base = base->tp_base;
619 assert(base);
620 }
621
622 /* Call the base tp_dealloc() */
623 assert(basedealloc);
624 basedealloc(self);
625
626 /* Can't reference self beyond this point */
627 Py_DECREF(type);
628
629 /* Done */
630 return;
631 }
632
633 /* We get here only if the type has GC */
634
635 /* UnTrack and re-Track around the trashcan macro, alas */
636 /* See explanation at end of function for full disclosure */
637 PyObject_GC_UnTrack(self);
638 ++_PyTrash_delete_nesting;
639 Py_TRASHCAN_SAFE_BEGIN(self);
640 --_PyTrash_delete_nesting;
641 /* DO NOT restore GC tracking at this point. weakref callbacks
642 * (if any, and whether directly here or indirectly in something we
643 * call) may trigger GC, and if self is tracked at that point, it
644 * will look like trash to GC and GC will try to delete self again.
645 */
646
647 /* Find the nearest base with a different tp_dealloc */
648 base = type;
649 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
650 base = base->tp_base;
651 assert(base);
652 }
653
654 /* If we added a weaklist, we clear it. Do this *before* calling
655 the finalizer (__del__), clearing slots, or clearing the instance
656 dict. */
657
658 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
659 PyObject_ClearWeakRefs(self);
660
661 /* Maybe call finalizer; exit early if resurrected */
662 if (type->tp_del) {
663 _PyObject_GC_TRACK(self);
664 type->tp_del(self);
665 if (self->ob_refcnt > 0)
666 goto endlabel; /* resurrected */
667 else
668 _PyObject_GC_UNTRACK(self);
669 /* New weakrefs could be created during the finalizer call.
670 If this occurs, clear them out without calling their
671 finalizers since they might rely on part of the object
672 being finalized that has already been destroyed. */
673 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
674 /* Modeled after GET_WEAKREFS_LISTPTR() */
675 PyWeakReference **list = (PyWeakReference **) \
676 PyObject_GET_WEAKREFS_LISTPTR(self);
677 while (*list)
678 _PyWeakref_ClearRef(*list);
679 }
680 }
681
682 /* Clear slots up to the nearest base with a different tp_dealloc */
683 base = type;
684 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
685 if (base->ob_size)
686 clear_slots(base, self);
687 base = base->tp_base;
688 assert(base);
689 }
690
691 /* If we added a dict, DECREF it */
692 if (type->tp_dictoffset && !base->tp_dictoffset) {
693 PyObject **dictptr = _PyObject_GetDictPtr(self);
694 if (dictptr != NULL) {
695 PyObject *dict = *dictptr;
696 if (dict != NULL) {
697 Py_DECREF(dict);
698 *dictptr = NULL;
699 }
700 }
701 }
702
703 /* Call the base tp_dealloc(); first retrack self if
704 * basedealloc knows about gc.
705 */
706 if (PyType_IS_GC(base))
707 _PyObject_GC_TRACK(self);
708 assert(basedealloc);
709 basedealloc(self);
710
711 /* Can't reference self beyond this point */
712 Py_DECREF(type);
713
714 endlabel:
715 ++_PyTrash_delete_nesting;
716 Py_TRASHCAN_SAFE_END(self);
717 --_PyTrash_delete_nesting;
718
719 /* Explanation of the weirdness around the trashcan macros:
720
721 Q. What do the trashcan macros do?
722
723 A. Read the comment titled "Trashcan mechanism" in object.h.
724 For one, this explains why there must be a call to GC-untrack
725 before the trashcan begin macro. Without understanding the
726 trashcan code, the answers to the following questions don't make
727 sense.
728
729 Q. Why do we GC-untrack before the trashcan and then immediately
730 GC-track again afterward?
731
732 A. In the case that the base class is GC-aware, the base class
733 probably GC-untracks the object. If it does that using the
734 UNTRACK macro, this will crash when the object is already
735 untracked. Because we don't know what the base class does, the
736 only safe thing is to make sure the object is tracked when we
737 call the base class dealloc. But... The trashcan begin macro
738 requires that the object is *untracked* before it is called. So
739 the dance becomes:
740
741 GC untrack
742 trashcan begin
743 GC track
744
745 Q. Why did the last question say "immediately GC-track again"?
746 It's nowhere near immediately.
747
748 A. Because the code *used* to re-track immediately. Bad Idea.
749 self has a refcount of 0, and if gc ever gets its hands on it
750 (which can happen if any weakref callback gets invoked), it
751 looks like trash to gc too, and gc also tries to delete self
752 then. But we're already deleting self. Double dealloction is
753 a subtle disaster.
754
755 Q. Why the bizarre (net-zero) manipulation of
756 _PyTrash_delete_nesting around the trashcan macros?
757
758 A. Some base classes (e.g. list) also use the trashcan mechanism.
759 The following scenario used to be possible:
760
761 - suppose the trashcan level is one below the trashcan limit
762
763 - subtype_dealloc() is called
764
765 - the trashcan limit is not yet reached, so the trashcan level
766 is incremented and the code between trashcan begin and end is
767 executed
768
769 - this destroys much of the object's contents, including its
770 slots and __dict__
771
772 - basedealloc() is called; this is really list_dealloc(), or
773 some other type which also uses the trashcan macros
774
775 - the trashcan limit is now reached, so the object is put on the
776 trashcan's to-be-deleted-later list
777
778 - basedealloc() returns
779
780 - subtype_dealloc() decrefs the object's type
781
782 - subtype_dealloc() returns
783
784 - later, the trashcan code starts deleting the objects from its
785 to-be-deleted-later list
786
787 - subtype_dealloc() is called *AGAIN* for the same object
788
789 - at the very least (if the destroyed slots and __dict__ don't
790 cause problems) the object's type gets decref'ed a second
791 time, which is *BAD*!!!
792
793 The remedy is to make sure that if the code between trashcan
794 begin and end in subtype_dealloc() is called, the code between
795 trashcan begin and end in basedealloc() will also be called.
796 This is done by decrementing the level after passing into the
797 trashcan block, and incrementing it just before leaving the
798 block.
799
800 But now it's possible that a chain of objects consisting solely
801 of objects whose deallocator is subtype_dealloc() will defeat
802 the trashcan mechanism completely: the decremented level means
803 that the effective level never reaches the limit. Therefore, we
804 *increment* the level *before* entering the trashcan block, and
805 matchingly decrement it after leaving. This means the trashcan
806 code will trigger a little early, but that's no big deal.
807
808 Q. Are there any live examples of code in need of all this
809 complexity?
810
811 A. Yes. See SF bug 668433 for code that crashed (when Python was
812 compiled in debug mode) before the trashcan level manipulations
813 were added. For more discussion, see SF patches 581742, 575073
814 and bug 574207.
815 */
816}
817
818static PyTypeObject *solid_base(PyTypeObject *type);
819
820/* type test with subclassing support */
821
822int
823PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
824{
825 PyObject *mro;
826
827 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
828 return b == a || b == &PyBaseObject_Type;
829
830 mro = a->tp_mro;
831 if (mro != NULL) {
832 /* Deal with multiple inheritance without recursion
833 by walking the MRO tuple */
834 Py_ssize_t i, n;
835 assert(PyTuple_Check(mro));
836 n = PyTuple_GET_SIZE(mro);
837 for (i = 0; i < n; i++) {
838 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
839 return 1;
840 }
841 return 0;
842 }
843 else {
844 /* a is not completely initilized yet; follow tp_base */
845 do {
846 if (a == b)
847 return 1;
848 a = a->tp_base;
849 } while (a != NULL);
850 return b == &PyBaseObject_Type;
851 }
852}
853
854/* Internal routines to do a method lookup in the type
855 without looking in the instance dictionary
856 (so we can't use PyObject_GetAttr) but still binding
857 it to the instance. The arguments are the object,
858 the method name as a C string, and the address of a
859 static variable used to cache the interned Python string.
860
861 Two variants:
862
863 - lookup_maybe() returns NULL without raising an exception
864 when the _PyType_Lookup() call fails;
865
866 - lookup_method() always raises an exception upon errors.
867*/
868
869static PyObject *
870lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
871{
872 PyObject *res;
873
874 if (*attrobj == NULL) {
875 *attrobj = PyString_InternFromString(attrstr);
876 if (*attrobj == NULL)
877 return NULL;
878 }
879 res = _PyType_Lookup(self->ob_type, *attrobj);
880 if (res != NULL) {
881 descrgetfunc f;
882 if ((f = res->ob_type->tp_descr_get) == NULL)
883 Py_INCREF(res);
884 else
885 res = f(res, self, (PyObject *)(self->ob_type));
886 }
887 return res;
888}
889
890static PyObject *
891lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
892{
893 PyObject *res = lookup_maybe(self, attrstr, attrobj);
894 if (res == NULL && !PyErr_Occurred())
895 PyErr_SetObject(PyExc_AttributeError, *attrobj);
896 return res;
897}
898
899/* A variation of PyObject_CallMethod that uses lookup_method()
900 instead of PyObject_GetAttrString(). This uses the same convention
901 as lookup_method to cache the interned name string object. */
902
903static PyObject *
904call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
905{
906 va_list va;
907 PyObject *args, *func = 0, *retval;
908 va_start(va, format);
909
910 func = lookup_maybe(o, name, nameobj);
911 if (func == NULL) {
912 va_end(va);
913 if (!PyErr_Occurred())
914 PyErr_SetObject(PyExc_AttributeError, *nameobj);
915 return NULL;
916 }
917
918 if (format && *format)
919 args = Py_VaBuildValue(format, va);
920 else
921 args = PyTuple_New(0);
922
923 va_end(va);
924
925 if (args == NULL)
926 return NULL;
927
928 assert(PyTuple_Check(args));
929 retval = PyObject_Call(func, args, NULL);
930
931 Py_DECREF(args);
932 Py_DECREF(func);
933
934 return retval;
935}
936
937/* Clone of call_method() that returns NotImplemented when the lookup fails. */
938
939static PyObject *
940call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
941{
942 va_list va;
943 PyObject *args, *func = 0, *retval;
944 va_start(va, format);
945
946 func = lookup_maybe(o, name, nameobj);
947 if (func == NULL) {
948 va_end(va);
949 if (!PyErr_Occurred()) {
950 Py_INCREF(Py_NotImplemented);
951 return Py_NotImplemented;
952 }
953 return NULL;
954 }
955
956 if (format && *format)
957 args = Py_VaBuildValue(format, va);
958 else
959 args = PyTuple_New(0);
960
961 va_end(va);
962
963 if (args == NULL)
964 return NULL;
965
966 assert(PyTuple_Check(args));
967 retval = PyObject_Call(func, args, NULL);
968
969 Py_DECREF(args);
970 Py_DECREF(func);
971
972 return retval;
973}
974
975static int
976fill_classic_mro(PyObject *mro, PyObject *cls)
977{
978 PyObject *bases, *base;
979 Py_ssize_t i, n;
980
981 assert(PyList_Check(mro));
982 assert(PyClass_Check(cls));
983 i = PySequence_Contains(mro, cls);
984 if (i < 0)
985 return -1;
986 if (!i) {
987 if (PyList_Append(mro, cls) < 0)
988 return -1;
989 }
990 bases = ((PyClassObject *)cls)->cl_bases;
991 assert(bases && PyTuple_Check(bases));
992 n = PyTuple_GET_SIZE(bases);
993 for (i = 0; i < n; i++) {
994 base = PyTuple_GET_ITEM(bases, i);
995 if (fill_classic_mro(mro, base) < 0)
996 return -1;
997 }
998 return 0;
999}
1000
1001static PyObject *
1002classic_mro(PyObject *cls)
1003{
1004 PyObject *mro;
1005
1006 assert(PyClass_Check(cls));
1007 mro = PyList_New(0);
1008 if (mro != NULL) {
1009 if (fill_classic_mro(mro, cls) == 0)
1010 return mro;
1011 Py_DECREF(mro);
1012 }
1013 return NULL;
1014}
1015
1016/*
1017 Method resolution order algorithm C3 described in
1018 "A Monotonic Superclass Linearization for Dylan",
1019 by Kim Barrett, Bob Cassel, Paul Haahr,
1020 David A. Moon, Keith Playford, and P. Tucker Withington.
1021 (OOPSLA 1996)
1022
1023 Some notes about the rules implied by C3:
1024
1025 No duplicate bases.
1026 It isn't legal to repeat a class in a list of base classes.
1027
1028 The next three properties are the 3 constraints in "C3".
1029
1030 Local precendece order.
1031 If A precedes B in C's MRO, then A will precede B in the MRO of all
1032 subclasses of C.
1033
1034 Monotonicity.
1035 The MRO of a class must be an extension without reordering of the
1036 MRO of each of its superclasses.
1037
1038 Extended Precedence Graph (EPG).
1039 Linearization is consistent if there is a path in the EPG from
1040 each class to all its successors in the linearization. See
1041 the paper for definition of EPG.
1042 */
1043
1044static int
1045tail_contains(PyObject *list, int whence, PyObject *o) {
1046 Py_ssize_t j, size;
1047 size = PyList_GET_SIZE(list);
1048
1049 for (j = whence+1; j < size; j++) {
1050 if (PyList_GET_ITEM(list, j) == o)
1051 return 1;
1052 }
1053 return 0;
1054}
1055
1056static PyObject *
1057class_name(PyObject *cls)
1058{
1059 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1060 if (name == NULL) {
1061 PyErr_Clear();
1062 Py_XDECREF(name);
1063 name = PyObject_Repr(cls);
1064 }
1065 if (name == NULL)
1066 return NULL;
1067 if (!PyString_Check(name)) {
1068 Py_DECREF(name);
1069 return NULL;
1070 }
1071 return name;
1072}
1073
1074static int
1075check_duplicates(PyObject *list)
1076{
1077 Py_ssize_t i, j, n;
1078 /* Let's use a quadratic time algorithm,
1079 assuming that the bases lists is short.
1080 */
1081 n = PyList_GET_SIZE(list);
1082 for (i = 0; i < n; i++) {
1083 PyObject *o = PyList_GET_ITEM(list, i);
1084 for (j = i + 1; j < n; j++) {
1085 if (PyList_GET_ITEM(list, j) == o) {
1086 o = class_name(o);
1087 PyErr_Format(PyExc_TypeError,
1088 "duplicate base class %s",
1089 o ? PyString_AS_STRING(o) : "?");
1090 Py_XDECREF(o);
1091 return -1;
1092 }
1093 }
1094 }
1095 return 0;
1096}
1097
1098/* Raise a TypeError for an MRO order disagreement.
1099
1100 It's hard to produce a good error message. In the absence of better
1101 insight into error reporting, report the classes that were candidates
1102 to be put next into the MRO. There is some conflict between the
1103 order in which they should be put in the MRO, but it's hard to
1104 diagnose what constraint can't be satisfied.
1105*/
1106
1107static void
1108set_mro_error(PyObject *to_merge, int *remain)
1109{
1110 Py_ssize_t i, n, off, to_merge_size;
1111 char buf[1000];
1112 PyObject *k, *v;
1113 PyObject *set = PyDict_New();
1114 if (!set) return;
1115
1116 to_merge_size = PyList_GET_SIZE(to_merge);
1117 for (i = 0; i < to_merge_size; i++) {
1118 PyObject *L = PyList_GET_ITEM(to_merge, i);
1119 if (remain[i] < PyList_GET_SIZE(L)) {
1120 PyObject *c = PyList_GET_ITEM(L, remain[i]);
1121 if (PyDict_SetItem(set, c, Py_None) < 0) {
1122 Py_DECREF(set);
1123 return;
1124 }
1125 }
1126 }
1127 n = PyDict_Size(set);
1128
1129 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1130consistent method resolution\norder (MRO) for bases");
1131 i = 0;
1132 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
1133 PyObject *name = class_name(k);
1134 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1135 name ? PyString_AS_STRING(name) : "?");
1136 Py_XDECREF(name);
1137 if (--n && (size_t)(off+1) < sizeof(buf)) {
1138 buf[off++] = ',';
1139 buf[off] = '\0';
1140 }
1141 }
1142 PyErr_SetString(PyExc_TypeError, buf);
1143 Py_DECREF(set);
1144}
1145
1146static int
1147pmerge(PyObject *acc, PyObject* to_merge) {
1148 Py_ssize_t i, j, to_merge_size, empty_cnt;
1149 int *remain;
1150 int ok;
1151
1152 to_merge_size = PyList_GET_SIZE(to_merge);
1153
1154 /* remain stores an index into each sublist of to_merge.
1155 remain[i] is the index of the next base in to_merge[i]
1156 that is not included in acc.
1157 */
1158 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1159 if (remain == NULL)
1160 return -1;
1161 for (i = 0; i < to_merge_size; i++)
1162 remain[i] = 0;
1163
1164 again:
1165 empty_cnt = 0;
1166 for (i = 0; i < to_merge_size; i++) {
1167 PyObject *candidate;
1168
1169 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1170
1171 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1172 empty_cnt++;
1173 continue;
1174 }
1175
1176 /* Choose next candidate for MRO.
1177
1178 The input sequences alone can determine the choice.
1179 If not, choose the class which appears in the MRO
1180 of the earliest direct superclass of the new class.
1181 */
1182
1183 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1184 for (j = 0; j < to_merge_size; j++) {
1185 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1186 if (tail_contains(j_lst, remain[j], candidate)) {
1187 goto skip; /* continue outer loop */
1188 }
1189 }
1190 ok = PyList_Append(acc, candidate);
1191 if (ok < 0) {
1192 PyMem_Free(remain);
1193 return -1;
1194 }
1195 for (j = 0; j < to_merge_size; j++) {
1196 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1197 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1198 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1199 remain[j]++;
1200 }
1201 }
1202 goto again;
1203 skip: ;
1204 }
1205
1206 if (empty_cnt == to_merge_size) {
1207 PyMem_FREE(remain);
1208 return 0;
1209 }
1210 set_mro_error(to_merge, remain);
1211 PyMem_FREE(remain);
1212 return -1;
1213}
1214
1215static PyObject *
1216mro_implementation(PyTypeObject *type)
1217{
1218 Py_ssize_t i, n;
1219 int ok;
1220 PyObject *bases, *result;
1221 PyObject *to_merge, *bases_aslist;
1222
1223 if(type->tp_dict == NULL) {
1224 if(PyType_Ready(type) < 0)
1225 return NULL;
1226 }
1227
1228 /* Find a superclass linearization that honors the constraints
1229 of the explicit lists of bases and the constraints implied by
1230 each base class.
1231
1232 to_merge is a list of lists, where each list is a superclass
1233 linearization implied by a base class. The last element of
1234 to_merge is the declared list of bases.
1235 */
1236
1237 bases = type->tp_bases;
1238 n = PyTuple_GET_SIZE(bases);
1239
1240 to_merge = PyList_New(n+1);
1241 if (to_merge == NULL)
1242 return NULL;
1243
1244 for (i = 0; i < n; i++) {
1245 PyObject *base = PyTuple_GET_ITEM(bases, i);
1246 PyObject *parentMRO;
1247 if (PyType_Check(base))
1248 parentMRO = PySequence_List(
1249 ((PyTypeObject*)base)->tp_mro);
1250 else
1251 parentMRO = classic_mro(base);
1252 if (parentMRO == NULL) {
1253 Py_DECREF(to_merge);
1254 return NULL;
1255 }
1256
1257 PyList_SET_ITEM(to_merge, i, parentMRO);
1258 }
1259
1260 bases_aslist = PySequence_List(bases);
1261 if (bases_aslist == NULL) {
1262 Py_DECREF(to_merge);
1263 return NULL;
1264 }
1265 /* This is just a basic sanity check. */
1266 if (check_duplicates(bases_aslist) < 0) {
1267 Py_DECREF(to_merge);
1268 Py_DECREF(bases_aslist);
1269 return NULL;
1270 }
1271 PyList_SET_ITEM(to_merge, n, bases_aslist);
1272
1273 result = Py_BuildValue("[O]", (PyObject *)type);
1274 if (result == NULL) {
1275 Py_DECREF(to_merge);
1276 return NULL;
1277 }
1278
1279 ok = pmerge(result, to_merge);
1280 Py_DECREF(to_merge);
1281 if (ok < 0) {
1282 Py_DECREF(result);
1283 return NULL;
1284 }
1285
1286 return result;
1287}
1288
1289static PyObject *
1290mro_external(PyObject *self)
1291{
1292 PyTypeObject *type = (PyTypeObject *)self;
1293
1294 return mro_implementation(type);
1295}
1296
1297static int
1298mro_internal(PyTypeObject *type)
1299{
1300 PyObject *mro, *result, *tuple;
1301 int checkit = 0;
1302
1303 if (type->ob_type == &PyType_Type) {
1304 result = mro_implementation(type);
1305 }
1306 else {
1307 static PyObject *mro_str;
1308 checkit = 1;
1309 mro = lookup_method((PyObject *)type, "mro", &mro_str);
1310 if (mro == NULL)
1311 return -1;
1312 result = PyObject_CallObject(mro, NULL);
1313 Py_DECREF(mro);
1314 }
1315 if (result == NULL)
1316 return -1;
1317 tuple = PySequence_Tuple(result);
1318 Py_DECREF(result);
1319 if (tuple == NULL)
1320 return -1;
1321 if (checkit) {
1322 Py_ssize_t i, len;
1323 PyObject *cls;
1324 PyTypeObject *solid;
1325
1326 solid = solid_base(type);
1327
1328 len = PyTuple_GET_SIZE(tuple);
1329
1330 for (i = 0; i < len; i++) {
1331 PyTypeObject *t;
1332 cls = PyTuple_GET_ITEM(tuple, i);
1333 if (PyClass_Check(cls))
1334 continue;
1335 else if (!PyType_Check(cls)) {
1336 PyErr_Format(PyExc_TypeError,
1337 "mro() returned a non-class ('%.500s')",
1338 cls->ob_type->tp_name);
1339 Py_DECREF(tuple);
1340 return -1;
1341 }
1342 t = (PyTypeObject*)cls;
1343 if (!PyType_IsSubtype(solid, solid_base(t))) {
1344 PyErr_Format(PyExc_TypeError,
1345 "mro() returned base with unsuitable layout ('%.500s')",
1346 t->tp_name);
1347 Py_DECREF(tuple);
1348 return -1;
1349 }
1350 }
1351 }
1352 type->tp_mro = tuple;
1353 return 0;
1354}
1355
1356
1357/* Calculate the best base amongst multiple base classes.
1358 This is the first one that's on the path to the "solid base". */
1359
1360static PyTypeObject *
1361best_base(PyObject *bases)
1362{
1363 Py_ssize_t i, n;
1364 PyTypeObject *base, *winner, *candidate, *base_i;
1365 PyObject *base_proto;
1366
1367 assert(PyTuple_Check(bases));
1368 n = PyTuple_GET_SIZE(bases);
1369 assert(n > 0);
1370 base = NULL;
1371 winner = NULL;
1372 for (i = 0; i < n; i++) {
1373 base_proto = PyTuple_GET_ITEM(bases, i);
1374 if (PyClass_Check(base_proto))
1375 continue;
1376 if (!PyType_Check(base_proto)) {
1377 PyErr_SetString(
1378 PyExc_TypeError,
1379 "bases must be types");
1380 return NULL;
1381 }
1382 base_i = (PyTypeObject *)base_proto;
1383 if (base_i->tp_dict == NULL) {
1384 if (PyType_Ready(base_i) < 0)
1385 return NULL;
1386 }
1387 candidate = solid_base(base_i);
1388 if (winner == NULL) {
1389 winner = candidate;
1390 base = base_i;
1391 }
1392 else if (PyType_IsSubtype(winner, candidate))
1393 ;
1394 else if (PyType_IsSubtype(candidate, winner)) {
1395 winner = candidate;
1396 base = base_i;
1397 }
1398 else {
1399 PyErr_SetString(
1400 PyExc_TypeError,
1401 "multiple bases have "
1402 "instance lay-out conflict");
1403 return NULL;
1404 }
1405 }
1406 if (base == NULL)
1407 PyErr_SetString(PyExc_TypeError,
1408 "a new-style class can't have only classic bases");
1409 return base;
1410}
1411
1412static int
1413extra_ivars(PyTypeObject *type, PyTypeObject *base)
1414{
1415 size_t t_size = type->tp_basicsize;
1416 size_t b_size = base->tp_basicsize;
1417
1418 assert(t_size >= b_size); /* Else type smaller than base! */
1419 if (type->tp_itemsize || base->tp_itemsize) {
1420 /* If itemsize is involved, stricter rules */
1421 return t_size != b_size ||
1422 type->tp_itemsize != base->tp_itemsize;
1423 }
1424 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1425 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1426 t_size -= sizeof(PyObject *);
1427 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1428 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1429 t_size -= sizeof(PyObject *);
1430
1431 return t_size != b_size;
1432}
1433
1434static PyTypeObject *
1435solid_base(PyTypeObject *type)
1436{
1437 PyTypeObject *base;
1438
1439 if (type->tp_base)
1440 base = solid_base(type->tp_base);
1441 else
1442 base = &PyBaseObject_Type;
1443 if (extra_ivars(type, base))
1444 return type;
1445 else
1446 return base;
1447}
1448
1449static void object_dealloc(PyObject *);
1450static int object_init(PyObject *, PyObject *, PyObject *);
1451static int update_slot(PyTypeObject *, PyObject *);
1452static void fixup_slot_dispatchers(PyTypeObject *);
1453
1454static PyObject *
1455subtype_dict(PyObject *obj, void *context)
1456{
1457 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1458 PyObject *dict;
1459
1460 if (dictptr == NULL) {
1461 PyErr_SetString(PyExc_AttributeError,
1462 "This object has no __dict__");
1463 return NULL;
1464 }
1465 dict = *dictptr;
1466 if (dict == NULL)
1467 *dictptr = dict = PyDict_New();
1468 Py_XINCREF(dict);
1469 return dict;
1470}
1471
1472static int
1473subtype_setdict(PyObject *obj, PyObject *value, void *context)
1474{
1475 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1476 PyObject *dict;
1477
1478 if (dictptr == NULL) {
1479 PyErr_SetString(PyExc_AttributeError,
1480 "This object has no __dict__");
1481 return -1;
1482 }
1483 if (value != NULL && !PyDict_Check(value)) {
1484 PyErr_Format(PyExc_TypeError,
1485 "__dict__ must be set to a dictionary, "
1486 "not a '%.200s'", value->ob_type->tp_name);
1487 return -1;
1488 }
1489 dict = *dictptr;
1490 Py_XINCREF(value);
1491 *dictptr = value;
1492 Py_XDECREF(dict);
1493 return 0;
1494}
1495
1496static PyObject *
1497subtype_getweakref(PyObject *obj, void *context)
1498{
1499 PyObject **weaklistptr;
1500 PyObject *result;
1501
1502 if (obj->ob_type->tp_weaklistoffset == 0) {
1503 PyErr_SetString(PyExc_AttributeError,
1504 "This object has no __weakref__");
1505 return NULL;
1506 }
1507 assert(obj->ob_type->tp_weaklistoffset > 0);
1508 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
1509 (size_t)(obj->ob_type->tp_basicsize));
1510 weaklistptr = (PyObject **)
1511 ((char *)obj + obj->ob_type->tp_weaklistoffset);
1512 if (*weaklistptr == NULL)
1513 result = Py_None;
1514 else
1515 result = *weaklistptr;
1516 Py_INCREF(result);
1517 return result;
1518}
1519
1520/* Three variants on the subtype_getsets list. */
1521
1522static PyGetSetDef subtype_getsets_full[] = {
1523 {"__dict__", subtype_dict, subtype_setdict,
1524 PyDoc_STR("dictionary for instance variables (if defined)")},
1525 {"__weakref__", subtype_getweakref, NULL,
1526 PyDoc_STR("list of weak references to the object (if defined)")},
1527 {0}
1528};
1529
1530static PyGetSetDef subtype_getsets_dict_only[] = {
1531 {"__dict__", subtype_dict, subtype_setdict,
1532 PyDoc_STR("dictionary for instance variables (if defined)")},
1533 {0}
1534};
1535
1536static PyGetSetDef subtype_getsets_weakref_only[] = {
1537 {"__weakref__", subtype_getweakref, NULL,
1538 PyDoc_STR("list of weak references to the object (if defined)")},
1539 {0}
1540};
1541
1542static int
1543valid_identifier(PyObject *s)
1544{
1545 unsigned char *p;
1546 Py_ssize_t i, n;
1547
1548 if (!PyString_Check(s)) {
1549 PyErr_Format(PyExc_TypeError,
1550 "__slots__ items must be strings, not '%.200s'",
1551 s->ob_type->tp_name);
1552 return 0;
1553 }
1554 p = (unsigned char *) PyString_AS_STRING(s);
1555 n = PyString_GET_SIZE(s);
1556 /* We must reject an empty name. As a hack, we bump the
1557 length to 1 so that the loop will balk on the trailing \0. */
1558 if (n == 0)
1559 n = 1;
1560 for (i = 0; i < n; i++, p++) {
1561 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1562 PyErr_SetString(PyExc_TypeError,
1563 "__slots__ must be identifiers");
1564 return 0;
1565 }
1566 }
1567 return 1;
1568}
1569
1570#ifdef Py_USING_UNICODE
1571/* Replace Unicode objects in slots. */
1572
1573static PyObject *
1574_unicode_to_string(PyObject *slots, Py_ssize_t nslots)
1575{
1576 PyObject *tmp = NULL;
1577 PyObject *slot_name, *new_name;
1578 Py_ssize_t i;
1579
1580 for (i = 0; i < nslots; i++) {
1581 if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
1582 if (tmp == NULL) {
1583 tmp = PySequence_List(slots);
1584 if (tmp == NULL)
1585 return NULL;
1586 }
1587 new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
1588 NULL);
1589 if (new_name == NULL) {
1590 Py_DECREF(tmp);
1591 return NULL;
1592 }
1593 Py_INCREF(new_name);
1594 PyList_SET_ITEM(tmp, i, new_name);
1595 Py_DECREF(slot_name);
1596 }
1597 }
1598 if (tmp != NULL) {
1599 slots = PyList_AsTuple(tmp);
1600 Py_DECREF(tmp);
1601 }
1602 return slots;
1603}
1604#endif
1605
1606static PyObject *
1607type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1608{
1609 PyObject *name, *bases, *dict;
1610 static char *kwlist[] = {"name", "bases", "dict", 0};
1611 PyObject *slots, *tmp, *newslots;
1612 PyTypeObject *type, *base, *tmptype, *winner;
1613 PyHeapTypeObject *et;
1614 PyMemberDef *mp;
1615 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
1616 int j, may_add_dict, may_add_weak;
1617
1618 assert(args != NULL && PyTuple_Check(args));
1619 assert(kwds == NULL || PyDict_Check(kwds));
1620
1621 /* Special case: type(x) should return x->ob_type */
1622 {
1623 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1624 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1625
1626 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1627 PyObject *x = PyTuple_GET_ITEM(args, 0);
1628 Py_INCREF(x->ob_type);
1629 return (PyObject *) x->ob_type;
1630 }
1631
1632 /* SF bug 475327 -- if that didn't trigger, we need 3
1633 arguments. but PyArg_ParseTupleAndKeywords below may give
1634 a msg saying type() needs exactly 3. */
1635 if (nargs + nkwds != 3) {
1636 PyErr_SetString(PyExc_TypeError,
1637 "type() takes 1 or 3 arguments");
1638 return NULL;
1639 }
1640 }
1641
1642 /* Check arguments: (name, bases, dict) */
1643 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1644 &name,
1645 &PyTuple_Type, &bases,
1646 &PyDict_Type, &dict))
1647 return NULL;
1648
1649 /* Determine the proper metatype to deal with this,
1650 and check for metatype conflicts while we're at it.
1651 Note that if some other metatype wins to contract,
1652 it's possible that its instances are not types. */
1653 nbases = PyTuple_GET_SIZE(bases);
1654 winner = metatype;
1655 for (i = 0; i < nbases; i++) {
1656 tmp = PyTuple_GET_ITEM(bases, i);
1657 tmptype = tmp->ob_type;
1658 if (tmptype == &PyClass_Type)
1659 continue; /* Special case classic classes */
1660 if (PyType_IsSubtype(winner, tmptype))
1661 continue;
1662 if (PyType_IsSubtype(tmptype, winner)) {
1663 winner = tmptype;
1664 continue;
1665 }
1666 PyErr_SetString(PyExc_TypeError,
1667 "metaclass conflict: "
1668 "the metaclass of a derived class "
1669 "must be a (non-strict) subclass "
1670 "of the metaclasses of all its bases");
1671 return NULL;
1672 }
1673 if (winner != metatype) {
1674 if (winner->tp_new != type_new) /* Pass it to the winner */
1675 return winner->tp_new(winner, args, kwds);
1676 metatype = winner;
1677 }
1678
1679 /* Adjust for empty tuple bases */
1680 if (nbases == 0) {
1681 bases = PyTuple_Pack(1, &PyBaseObject_Type);
1682 if (bases == NULL)
1683 return NULL;
1684 nbases = 1;
1685 }
1686 else
1687 Py_INCREF(bases);
1688
1689 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1690
1691 /* Calculate best base, and check that all bases are type objects */
1692 base = best_base(bases);
1693 if (base == NULL) {
1694 Py_DECREF(bases);
1695 return NULL;
1696 }
1697 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1698 PyErr_Format(PyExc_TypeError,
1699 "type '%.100s' is not an acceptable base type",
1700 base->tp_name);
1701 Py_DECREF(bases);
1702 return NULL;
1703 }
1704
1705 /* Check for a __slots__ sequence variable in dict, and count it */
1706 slots = PyDict_GetItemString(dict, "__slots__");
1707 nslots = 0;
1708 add_dict = 0;
1709 add_weak = 0;
1710 may_add_dict = base->tp_dictoffset == 0;
1711 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1712 if (slots == NULL) {
1713 if (may_add_dict) {
1714 add_dict++;
1715 }
1716 if (may_add_weak) {
1717 add_weak++;
1718 }
1719 }
1720 else {
1721 /* Have slots */
1722
1723 /* Make it into a tuple */
1724 if (PyString_Check(slots))
1725 slots = PyTuple_Pack(1, slots);
1726 else
1727 slots = PySequence_Tuple(slots);
1728 if (slots == NULL) {
1729 Py_DECREF(bases);
1730 return NULL;
1731 }
1732 assert(PyTuple_Check(slots));
1733
1734 /* Are slots allowed? */
1735 nslots = PyTuple_GET_SIZE(slots);
1736 if (nslots > 0 && base->tp_itemsize != 0) {
1737 PyErr_Format(PyExc_TypeError,
1738 "nonempty __slots__ "
1739 "not supported for subtype of '%s'",
1740 base->tp_name);
1741 bad_slots:
1742 Py_DECREF(bases);
1743 Py_DECREF(slots);
1744 return NULL;
1745 }
1746
1747#ifdef Py_USING_UNICODE
1748 tmp = _unicode_to_string(slots, nslots);
1749 if (tmp == NULL)
1750 goto bad_slots;
1751 if (tmp != slots) {
1752 Py_DECREF(slots);
1753 slots = tmp;
1754 }
1755#endif
1756 /* Check for valid slot names and two special cases */
1757 for (i = 0; i < nslots; i++) {
1758 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1759 char *s;
1760 if (!valid_identifier(tmp))
1761 goto bad_slots;
1762 assert(PyString_Check(tmp));
1763 s = PyString_AS_STRING(tmp);
1764 if (strcmp(s, "__dict__") == 0) {
1765 if (!may_add_dict || add_dict) {
1766 PyErr_SetString(PyExc_TypeError,
1767 "__dict__ slot disallowed: "
1768 "we already got one");
1769 goto bad_slots;
1770 }
1771 add_dict++;
1772 }
1773 if (strcmp(s, "__weakref__") == 0) {
1774 if (!may_add_weak || add_weak) {
1775 PyErr_SetString(PyExc_TypeError,
1776 "__weakref__ slot disallowed: "
1777 "either we already got one, "
1778 "or __itemsize__ != 0");
1779 goto bad_slots;
1780 }
1781 add_weak++;
1782 }
1783 }
1784
1785 /* Copy slots into yet another tuple, demangling names */
1786 newslots = PyTuple_New(nslots - add_dict - add_weak);
1787 if (newslots == NULL)
1788 goto bad_slots;
1789 for (i = j = 0; i < nslots; i++) {
1790 char *s;
1791 tmp = PyTuple_GET_ITEM(slots, i);
1792 s = PyString_AS_STRING(tmp);
1793 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1794 (add_weak && strcmp(s, "__weakref__") == 0))
1795 continue;
1796 tmp =_Py_Mangle(name, tmp);
1797 if (!tmp)
1798 goto bad_slots;
1799 PyTuple_SET_ITEM(newslots, j, tmp);
1800 j++;
1801 }
1802 assert(j == nslots - add_dict - add_weak);
1803 nslots = j;
1804 Py_DECREF(slots);
1805 slots = newslots;
1806
1807 /* Secondary bases may provide weakrefs or dict */
1808 if (nbases > 1 &&
1809 ((may_add_dict && !add_dict) ||
1810 (may_add_weak && !add_weak))) {
1811 for (i = 0; i < nbases; i++) {
1812 tmp = PyTuple_GET_ITEM(bases, i);
1813 if (tmp == (PyObject *)base)
1814 continue; /* Skip primary base */
1815 if (PyClass_Check(tmp)) {
1816 /* Classic base class provides both */
1817 if (may_add_dict && !add_dict)
1818 add_dict++;
1819 if (may_add_weak && !add_weak)
1820 add_weak++;
1821 break;
1822 }
1823 assert(PyType_Check(tmp));
1824 tmptype = (PyTypeObject *)tmp;
1825 if (may_add_dict && !add_dict &&
1826 tmptype->tp_dictoffset != 0)
1827 add_dict++;
1828 if (may_add_weak && !add_weak &&
1829 tmptype->tp_weaklistoffset != 0)
1830 add_weak++;
1831 if (may_add_dict && !add_dict)
1832 continue;
1833 if (may_add_weak && !add_weak)
1834 continue;
1835 /* Nothing more to check */
1836 break;
1837 }
1838 }
1839 }
1840
1841 /* XXX From here until type is safely allocated,
1842 "return NULL" may leak slots! */
1843
1844 /* Allocate the type object */
1845 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1846 if (type == NULL) {
1847 Py_XDECREF(slots);
1848 Py_DECREF(bases);
1849 return NULL;
1850 }
1851
1852 /* Keep name and slots alive in the extended type object */
1853 et = (PyHeapTypeObject *)type;
1854 Py_INCREF(name);
1855 et->ht_name = name;
1856 et->ht_slots = slots;
1857
1858 /* Initialize tp_flags */
1859 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1860 Py_TPFLAGS_BASETYPE;
1861 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1862 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1863
1864 /* It's a new-style number unless it specifically inherits any
1865 old-style numeric behavior */
1866 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1867 (base->tp_as_number == NULL))
1868 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1869
1870 /* Initialize essential fields */
1871 type->tp_as_number = &et->as_number;
1872 type->tp_as_sequence = &et->as_sequence;
1873 type->tp_as_mapping = &et->as_mapping;
1874 type->tp_as_buffer = &et->as_buffer;
1875 type->tp_name = PyString_AS_STRING(name);
1876
1877 /* Set tp_base and tp_bases */
1878 type->tp_bases = bases;
1879 Py_INCREF(base);
1880 type->tp_base = base;
1881
1882 /* Initialize tp_dict from passed-in dict */
1883 type->tp_dict = dict = PyDict_Copy(dict);
1884 if (dict == NULL) {
1885 Py_DECREF(type);
1886 return NULL;
1887 }
1888
1889 /* Set __module__ in the dict */
1890 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1891 tmp = PyEval_GetGlobals();
1892 if (tmp != NULL) {
1893 tmp = PyDict_GetItemString(tmp, "__name__");
1894 if (tmp != NULL) {
1895 if (PyDict_SetItemString(dict, "__module__",
1896 tmp) < 0)
1897 return NULL;
1898 }
1899 }
1900 }
1901
1902 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1903 and is a string. The __doc__ accessor will first look for tp_doc;
1904 if that fails, it will still look into __dict__.
1905 */
1906 {
1907 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1908 if (doc != NULL && PyString_Check(doc)) {
1909 const size_t n = (size_t)PyString_GET_SIZE(doc);
1910 char *tp_doc = (char *)PyObject_MALLOC(n+1);
1911 if (tp_doc == NULL) {
1912 Py_DECREF(type);
1913 return NULL;
1914 }
1915 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
1916 type->tp_doc = tp_doc;
1917 }
1918 }
1919
1920 /* Special-case __new__: if it's a plain function,
1921 make it a static function */
1922 tmp = PyDict_GetItemString(dict, "__new__");
1923 if (tmp != NULL && PyFunction_Check(tmp)) {
1924 tmp = PyStaticMethod_New(tmp);
1925 if (tmp == NULL) {
1926 Py_DECREF(type);
1927 return NULL;
1928 }
1929 PyDict_SetItemString(dict, "__new__", tmp);
1930 Py_DECREF(tmp);
1931 }
1932
1933 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1934 mp = PyHeapType_GET_MEMBERS(et);
1935 slotoffset = base->tp_basicsize;
1936 if (slots != NULL) {
1937 for (i = 0; i < nslots; i++, mp++) {
1938 mp->name = PyString_AS_STRING(
1939 PyTuple_GET_ITEM(slots, i));
1940 mp->type = T_OBJECT_EX;
1941 mp->offset = slotoffset;
1942
1943 /* __dict__ and __weakref__ are already filtered out */
1944 assert(strcmp(mp->name, "__dict__") != 0);
1945 assert(strcmp(mp->name, "__weakref__") != 0);
1946
1947 slotoffset += sizeof(PyObject *);
1948 }
1949 }
1950 if (add_dict) {
1951 if (base->tp_itemsize)
1952 type->tp_dictoffset = -(long)sizeof(PyObject *);
1953 else
1954 type->tp_dictoffset = slotoffset;
1955 slotoffset += sizeof(PyObject *);
1956 }
1957 if (add_weak) {
1958 assert(!base->tp_itemsize);
1959 type->tp_weaklistoffset = slotoffset;
1960 slotoffset += sizeof(PyObject *);
1961 }
1962 type->tp_basicsize = slotoffset;
1963 type->tp_itemsize = base->tp_itemsize;
1964 type->tp_members = PyHeapType_GET_MEMBERS(et);
1965
1966 if (type->tp_weaklistoffset && type->tp_dictoffset)
1967 type->tp_getset = subtype_getsets_full;
1968 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1969 type->tp_getset = subtype_getsets_weakref_only;
1970 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1971 type->tp_getset = subtype_getsets_dict_only;
1972 else
1973 type->tp_getset = NULL;
1974
1975 /* Special case some slots */
1976 if (type->tp_dictoffset != 0 || nslots > 0) {
1977 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1978 type->tp_getattro = PyObject_GenericGetAttr;
1979 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1980 type->tp_setattro = PyObject_GenericSetAttr;
1981 }
1982 type->tp_dealloc = subtype_dealloc;
1983
1984 /* Enable GC unless there are really no instance variables possible */
1985 if (!(type->tp_basicsize == sizeof(PyObject) &&
1986 type->tp_itemsize == 0))
1987 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1988
1989 /* Always override allocation strategy to use regular heap */
1990 type->tp_alloc = PyType_GenericAlloc;
1991 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1992 type->tp_free = PyObject_GC_Del;
1993 type->tp_traverse = subtype_traverse;
1994 type->tp_clear = subtype_clear;
1995 }
1996 else
1997 type->tp_free = PyObject_Del;
1998
1999 /* Initialize the rest */
2000 if (PyType_Ready(type) < 0) {
2001 Py_DECREF(type);
2002 return NULL;
2003 }
2004
2005 /* Put the proper slots in place */
2006 fixup_slot_dispatchers(type);
2007
2008 return (PyObject *)type;
2009}
2010
2011/* Internal API to look for a name through the MRO.
2012 This returns a borrowed reference, and doesn't set an exception! */
2013PyObject *
2014_PyType_Lookup(PyTypeObject *type, PyObject *name)
2015{
2016 Py_ssize_t i, n;
2017 PyObject *mro, *res, *base, *dict;
2018
2019 /* Look in tp_dict of types in MRO */
2020 mro = type->tp_mro;
2021
2022 /* If mro is NULL, the type is either not yet initialized
2023 by PyType_Ready(), or already cleared by type_clear().
2024 Either way the safest thing to do is to return NULL. */
2025 if (mro == NULL)
2026 return NULL;
2027
2028 assert(PyTuple_Check(mro));
2029 n = PyTuple_GET_SIZE(mro);
2030 for (i = 0; i < n; i++) {
2031 base = PyTuple_GET_ITEM(mro, i);
2032 if (PyClass_Check(base))
2033 dict = ((PyClassObject *)base)->cl_dict;
2034 else {
2035 assert(PyType_Check(base));
2036 dict = ((PyTypeObject *)base)->tp_dict;
2037 }
2038 assert(dict && PyDict_Check(dict));
2039 res = PyDict_GetItem(dict, name);
2040 if (res != NULL)
2041 return res;
2042 }
2043 return NULL;
2044}
2045
2046/* This is similar to PyObject_GenericGetAttr(),
2047 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2048static PyObject *
2049type_getattro(PyTypeObject *type, PyObject *name)
2050{
2051 PyTypeObject *metatype = type->ob_type;
2052 PyObject *meta_attribute, *attribute;
2053 descrgetfunc meta_get;
2054
2055 /* Initialize this type (we'll assume the metatype is initialized) */
2056 if (type->tp_dict == NULL) {
2057 if (PyType_Ready(type) < 0)
2058 return NULL;
2059 }
2060
2061 /* No readable descriptor found yet */
2062 meta_get = NULL;
2063
2064 /* Look for the attribute in the metatype */
2065 meta_attribute = _PyType_Lookup(metatype, name);
2066
2067 if (meta_attribute != NULL) {
2068 meta_get = meta_attribute->ob_type->tp_descr_get;
2069
2070 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2071 /* Data descriptors implement tp_descr_set to intercept
2072 * writes. Assume the attribute is not overridden in
2073 * type's tp_dict (and bases): call the descriptor now.
2074 */
2075 return meta_get(meta_attribute, (PyObject *)type,
2076 (PyObject *)metatype);
2077 }
2078 Py_INCREF(meta_attribute);
2079 }
2080
2081 /* No data descriptor found on metatype. Look in tp_dict of this
2082 * type and its bases */
2083 attribute = _PyType_Lookup(type, name);
2084 if (attribute != NULL) {
2085 /* Implement descriptor functionality, if any */
2086 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
2087
2088 Py_XDECREF(meta_attribute);
2089
2090 if (local_get != NULL) {
2091 /* NULL 2nd argument indicates the descriptor was
2092 * found on the target object itself (or a base) */
2093 return local_get(attribute, (PyObject *)NULL,
2094 (PyObject *)type);
2095 }
2096
2097 Py_INCREF(attribute);
2098 return attribute;
2099 }
2100
2101 /* No attribute found in local __dict__ (or bases): use the
2102 * descriptor from the metatype, if any */
2103 if (meta_get != NULL) {
2104 PyObject *res;
2105 res = meta_get(meta_attribute, (PyObject *)type,
2106 (PyObject *)metatype);
2107 Py_DECREF(meta_attribute);
2108 return res;
2109 }
2110
2111 /* If an ordinary attribute was found on the metatype, return it now */
2112 if (meta_attribute != NULL) {
2113 return meta_attribute;
2114 }
2115
2116 /* Give up */
2117 PyErr_Format(PyExc_AttributeError,
2118 "type object '%.50s' has no attribute '%.400s'",
2119 type->tp_name, PyString_AS_STRING(name));
2120 return NULL;
2121}
2122
2123static int
2124type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2125{
2126 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2127 PyErr_Format(
2128 PyExc_TypeError,
2129 "can't set attributes of built-in/extension type '%s'",
2130 type->tp_name);
2131 return -1;
2132 }
2133 /* XXX Example of how I expect this to be used...
2134 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2135 return -1;
2136 */
2137 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2138 return -1;
2139 return update_slot(type, name);
2140}
2141
2142static void
2143type_dealloc(PyTypeObject *type)
2144{
2145 PyHeapTypeObject *et;
2146
2147 /* Assert this is a heap-allocated type object */
2148 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2149 _PyObject_GC_UNTRACK(type);
2150 PyObject_ClearWeakRefs((PyObject *)type);
2151 et = (PyHeapTypeObject *)type;
2152 Py_XDECREF(type->tp_base);
2153 Py_XDECREF(type->tp_dict);
2154 Py_XDECREF(type->tp_bases);
2155 Py_XDECREF(type->tp_mro);
2156 Py_XDECREF(type->tp_cache);
2157 Py_XDECREF(type->tp_subclasses);
2158 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2159 * of most other objects. It's okay to cast it to char *.
2160 */
2161 PyObject_Free((char *)type->tp_doc);
2162 Py_XDECREF(et->ht_name);
2163 Py_XDECREF(et->ht_slots);
2164 type->ob_type->tp_free((PyObject *)type);
2165}
2166
2167static PyObject *
2168type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2169{
2170 PyObject *list, *raw, *ref;
2171 Py_ssize_t i, n;
2172
2173 list = PyList_New(0);
2174 if (list == NULL)
2175 return NULL;
2176 raw = type->tp_subclasses;
2177 if (raw == NULL)
2178 return list;
2179 assert(PyList_Check(raw));
2180 n = PyList_GET_SIZE(raw);
2181 for (i = 0; i < n; i++) {
2182 ref = PyList_GET_ITEM(raw, i);
2183 assert(PyWeakref_CheckRef(ref));
2184 ref = PyWeakref_GET_OBJECT(ref);
2185 if (ref != Py_None) {
2186 if (PyList_Append(list, ref) < 0) {
2187 Py_DECREF(list);
2188 return NULL;
2189 }
2190 }
2191 }
2192 return list;
2193}
2194
2195static PyMethodDef type_methods[] = {
2196 {"mro", (PyCFunction)mro_external, METH_NOARGS,
2197 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2198 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2199 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2200 {0}
2201};
2202
2203PyDoc_STRVAR(type_doc,
2204"type(object) -> the object's type\n"
2205"type(name, bases, dict) -> a new type");
2206
2207static int
2208type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2209{
2210 /* Because of type_is_gc(), the collector only calls this
2211 for heaptypes. */
2212 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2213
2214 Py_VISIT(type->tp_dict);
2215 Py_VISIT(type->tp_cache);
2216 Py_VISIT(type->tp_mro);
2217 Py_VISIT(type->tp_bases);
2218 Py_VISIT(type->tp_base);
2219
2220 /* There's no need to visit type->tp_subclasses or
2221 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2222 in cycles; tp_subclasses is a list of weak references,
2223 and slots is a tuple of strings. */
2224
2225 return 0;
2226}
2227
2228static int
2229type_clear(PyTypeObject *type)
2230{
2231 /* Because of type_is_gc(), the collector only calls this
2232 for heaptypes. */
2233 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2234
2235 /* The only field we need to clear is tp_mro, which is part of a
2236 hard cycle (its first element is the class itself) that won't
2237 be broken otherwise (it's a tuple and tuples don't have a
2238 tp_clear handler). None of the other fields need to be
2239 cleared, and here's why:
2240
2241 tp_dict:
2242 It is a dict, so the collector will call its tp_clear.
2243
2244 tp_cache:
2245 Not used; if it were, it would be a dict.
2246
2247 tp_bases, tp_base:
2248 If these are involved in a cycle, there must be at least
2249 one other, mutable object in the cycle, e.g. a base
2250 class's dict; the cycle will be broken that way.
2251
2252 tp_subclasses:
2253 A list of weak references can't be part of a cycle; and
2254 lists have their own tp_clear.
2255
2256 slots (in PyHeapTypeObject):
2257 A tuple of strings can't be part of a cycle.
2258 */
2259
2260 Py_CLEAR(type->tp_mro);
2261
2262 return 0;
2263}
2264
2265static int
2266type_is_gc(PyTypeObject *type)
2267{
2268 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2269}
2270
2271PyTypeObject PyType_Type = {
2272 PyObject_HEAD_INIT(&PyType_Type)
2273 0, /* ob_size */
2274 "type", /* tp_name */
2275 sizeof(PyHeapTypeObject), /* tp_basicsize */
2276 sizeof(PyMemberDef), /* tp_itemsize */
2277 (destructor)type_dealloc, /* tp_dealloc */
2278 0, /* tp_print */
2279 0, /* tp_getattr */
2280 0, /* tp_setattr */
2281 type_compare, /* tp_compare */
2282 (reprfunc)type_repr, /* tp_repr */
2283 0, /* tp_as_number */
2284 0, /* tp_as_sequence */
2285 0, /* tp_as_mapping */
2286 (hashfunc)_Py_HashPointer, /* tp_hash */
2287 (ternaryfunc)type_call, /* tp_call */
2288 0, /* tp_str */
2289 (getattrofunc)type_getattro, /* tp_getattro */
2290 (setattrofunc)type_setattro, /* tp_setattro */
2291 0, /* tp_as_buffer */
2292 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2293 Py_TPFLAGS_BASETYPE, /* tp_flags */
2294 type_doc, /* tp_doc */
2295 (traverseproc)type_traverse, /* tp_traverse */
2296 (inquiry)type_clear, /* tp_clear */
2297 0, /* tp_richcompare */
2298 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
2299 0, /* tp_iter */
2300 0, /* tp_iternext */
2301 type_methods, /* tp_methods */
2302 type_members, /* tp_members */
2303 type_getsets, /* tp_getset */
2304 0, /* tp_base */
2305 0, /* tp_dict */
2306 0, /* tp_descr_get */
2307 0, /* tp_descr_set */
2308 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2309 0, /* tp_init */
2310 0, /* tp_alloc */
2311 type_new, /* tp_new */
2312 PyObject_GC_Del, /* tp_free */
2313 (inquiry)type_is_gc, /* tp_is_gc */
2314};
2315
2316
2317/* The base type of all types (eventually)... except itself. */
2318
2319static int
2320object_init(PyObject *self, PyObject *args, PyObject *kwds)
2321{
2322 return 0;
2323}
2324
2325/* If we don't have a tp_new for a new-style class, new will use this one.
2326 Therefore this should take no arguments/keywords. However, this new may
2327 also be inherited by objects that define a tp_init but no tp_new. These
2328 objects WILL pass argumets to tp_new, because it gets the same args as
2329 tp_init. So only allow arguments if we aren't using the default init, in
2330 which case we expect init to handle argument parsing. */
2331static PyObject *
2332object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2333{
2334 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2335 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2336 PyErr_SetString(PyExc_TypeError,
2337 "default __new__ takes no parameters");
2338 return NULL;
2339 }
2340 return type->tp_alloc(type, 0);
2341}
2342
2343static void
2344object_dealloc(PyObject *self)
2345{
2346 self->ob_type->tp_free(self);
2347}
2348
2349static PyObject *
2350object_repr(PyObject *self)
2351{
2352 PyTypeObject *type;
2353 PyObject *mod, *name, *rtn;
2354
2355 type = self->ob_type;
2356 mod = type_module(type, NULL);
2357 if (mod == NULL)
2358 PyErr_Clear();
2359 else if (!PyString_Check(mod)) {
2360 Py_DECREF(mod);
2361 mod = NULL;
2362 }
2363 name = type_name(type, NULL);
2364 if (name == NULL)
2365 return NULL;
2366 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
2367 rtn = PyString_FromFormat("<%s.%s object at %p>",
2368 PyString_AS_STRING(mod),
2369 PyString_AS_STRING(name),
2370 self);
2371 else
2372 rtn = PyString_FromFormat("<%s object at %p>",
2373 type->tp_name, self);
2374 Py_XDECREF(mod);
2375 Py_DECREF(name);
2376 return rtn;
2377}
2378
2379static PyObject *
2380object_str(PyObject *self)
2381{
2382 unaryfunc f;
2383
2384 f = self->ob_type->tp_repr;
2385 if (f == NULL)
2386 f = object_repr;
2387 return f(self);
2388}
2389
2390static long
2391object_hash(PyObject *self)
2392{
2393 return _Py_HashPointer(self);
2394}
2395
2396static PyObject *
2397object_get_class(PyObject *self, void *closure)
2398{
2399 Py_INCREF(self->ob_type);
2400 return (PyObject *)(self->ob_type);
2401}
2402
2403static int
2404equiv_structs(PyTypeObject *a, PyTypeObject *b)
2405{
2406 return a == b ||
2407 (a != NULL &&
2408 b != NULL &&
2409 a->tp_basicsize == b->tp_basicsize &&
2410 a->tp_itemsize == b->tp_itemsize &&
2411 a->tp_dictoffset == b->tp_dictoffset &&
2412 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2413 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2414 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2415}
2416
2417static int
2418same_slots_added(PyTypeObject *a, PyTypeObject *b)
2419{
2420 PyTypeObject *base = a->tp_base;
2421 Py_ssize_t size;
2422
2423 if (base != b->tp_base)
2424 return 0;
2425 if (equiv_structs(a, base) && equiv_structs(b, base))
2426 return 1;
2427 size = base->tp_basicsize;
2428 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2429 size += sizeof(PyObject *);
2430 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2431 size += sizeof(PyObject *);
2432 return size == a->tp_basicsize && size == b->tp_basicsize;
2433}
2434
2435static int
2436compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
2437{
2438 PyTypeObject *newbase, *oldbase;
2439
2440 if (newto->tp_dealloc != oldto->tp_dealloc ||
2441 newto->tp_free != oldto->tp_free)
2442 {
2443 PyErr_Format(PyExc_TypeError,
2444 "%s assignment: "
2445 "'%s' deallocator differs from '%s'",
2446 attr,
2447 newto->tp_name,
2448 oldto->tp_name);
2449 return 0;
2450 }
2451 newbase = newto;
2452 oldbase = oldto;
2453 while (equiv_structs(newbase, newbase->tp_base))
2454 newbase = newbase->tp_base;
2455 while (equiv_structs(oldbase, oldbase->tp_base))
2456 oldbase = oldbase->tp_base;
2457 if (newbase != oldbase &&
2458 (newbase->tp_base != oldbase->tp_base ||
2459 !same_slots_added(newbase, oldbase))) {
2460 PyErr_Format(PyExc_TypeError,
2461 "%s assignment: "
2462 "'%s' object layout differs from '%s'",
2463 attr,
2464 newto->tp_name,
2465 oldto->tp_name);
2466 return 0;
2467 }
2468
2469 return 1;
2470}
2471
2472static int
2473object_set_class(PyObject *self, PyObject *value, void *closure)
2474{
2475 PyTypeObject *oldto = self->ob_type;
2476 PyTypeObject *newto;
2477
2478 if (value == NULL) {
2479 PyErr_SetString(PyExc_TypeError,
2480 "can't delete __class__ attribute");
2481 return -1;
2482 }
2483 if (!PyType_Check(value)) {
2484 PyErr_Format(PyExc_TypeError,
2485 "__class__ must be set to new-style class, not '%s' object",
2486 value->ob_type->tp_name);
2487 return -1;
2488 }
2489 newto = (PyTypeObject *)value;
2490 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2491 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
2492 {
2493 PyErr_Format(PyExc_TypeError,
2494 "__class__ assignment: only for heap types");
2495 return -1;
2496 }
2497 if (compatible_for_assignment(newto, oldto, "__class__")) {
2498 Py_INCREF(newto);
2499 self->ob_type = newto;
2500 Py_DECREF(oldto);
2501 return 0;
2502 }
2503 else {
2504 return -1;
2505 }
2506}
2507
2508static PyGetSetDef object_getsets[] = {
2509 {"__class__", object_get_class, object_set_class,
2510 PyDoc_STR("the object's class")},
2511 {0}
2512};
2513
2514
2515/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2516 We fall back to helpers in copy_reg for:
2517 - pickle protocols < 2
2518 - calculating the list of slot names (done only once per class)
2519 - the __newobj__ function (which is used as a token but never called)
2520*/
2521
2522static PyObject *
2523import_copy_reg(void)
2524{
2525 static PyObject *copy_reg_str;
2526
2527 if (!copy_reg_str) {
2528 copy_reg_str = PyString_InternFromString("copy_reg");
2529 if (copy_reg_str == NULL)
2530 return NULL;
2531 }
2532
2533 return PyImport_Import(copy_reg_str);
2534}
2535
2536static PyObject *
2537slotnames(PyObject *cls)
2538{
2539 PyObject *clsdict;
2540 PyObject *copy_reg;
2541 PyObject *slotnames;
2542
2543 if (!PyType_Check(cls)) {
2544 Py_INCREF(Py_None);
2545 return Py_None;
2546 }
2547
2548 clsdict = ((PyTypeObject *)cls)->tp_dict;
2549 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
2550 if (slotnames != NULL && PyList_Check(slotnames)) {
2551 Py_INCREF(slotnames);
2552 return slotnames;
2553 }
2554
2555 copy_reg = import_copy_reg();
2556 if (copy_reg == NULL)
2557 return NULL;
2558
2559 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2560 Py_DECREF(copy_reg);
2561 if (slotnames != NULL &&
2562 slotnames != Py_None &&
2563 !PyList_Check(slotnames))
2564 {
2565 PyErr_SetString(PyExc_TypeError,
2566 "copy_reg._slotnames didn't return a list or None");
2567 Py_DECREF(slotnames);
2568 slotnames = NULL;
2569 }
2570
2571 return slotnames;
2572}
2573
2574static PyObject *
2575reduce_2(PyObject *obj)
2576{
2577 PyObject *cls, *getnewargs;
2578 PyObject *args = NULL, *args2 = NULL;
2579 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2580 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2581 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
2582 Py_ssize_t i, n;
2583
2584 cls = PyObject_GetAttrString(obj, "__class__");
2585 if (cls == NULL)
2586 return NULL;
2587
2588 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2589 if (getnewargs != NULL) {
2590 args = PyObject_CallObject(getnewargs, NULL);
2591 Py_DECREF(getnewargs);
2592 if (args != NULL && !PyTuple_Check(args)) {
2593 PyErr_Format(PyExc_TypeError,
2594 "__getnewargs__ should return a tuple, "
2595 "not '%.200s'", args->ob_type->tp_name);
2596 goto end;
2597 }
2598 }
2599 else {
2600 PyErr_Clear();
2601 args = PyTuple_New(0);
2602 }
2603 if (args == NULL)
2604 goto end;
2605
2606 getstate = PyObject_GetAttrString(obj, "__getstate__");
2607 if (getstate != NULL) {
2608 state = PyObject_CallObject(getstate, NULL);
2609 Py_DECREF(getstate);
2610 if (state == NULL)
2611 goto end;
2612 }
2613 else {
2614 PyErr_Clear();
2615 state = PyObject_GetAttrString(obj, "__dict__");
2616 if (state == NULL) {
2617 PyErr_Clear();
2618 state = Py_None;
2619 Py_INCREF(state);
2620 }
2621 names = slotnames(cls);
2622 if (names == NULL)
2623 goto end;
2624 if (names != Py_None) {
2625 assert(PyList_Check(names));
2626 slots = PyDict_New();
2627 if (slots == NULL)
2628 goto end;
2629 n = 0;
2630 /* Can't pre-compute the list size; the list
2631 is stored on the class so accessible to other
2632 threads, which may be run by DECREF */
2633 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2634 PyObject *name, *value;
2635 name = PyList_GET_ITEM(names, i);
2636 value = PyObject_GetAttr(obj, name);
2637 if (value == NULL)
2638 PyErr_Clear();
2639 else {
2640 int err = PyDict_SetItem(slots, name,
2641 value);
2642 Py_DECREF(value);
2643 if (err)
2644 goto end;
2645 n++;
2646 }
2647 }
2648 if (n) {
2649 state = Py_BuildValue("(NO)", state, slots);
2650 if (state == NULL)
2651 goto end;
2652 }
2653 }
2654 }
2655
2656 if (!PyList_Check(obj)) {
2657 listitems = Py_None;
2658 Py_INCREF(listitems);
2659 }
2660 else {
2661 listitems = PyObject_GetIter(obj);
2662 if (listitems == NULL)
2663 goto end;
2664 }
2665
2666 if (!PyDict_Check(obj)) {
2667 dictitems = Py_None;
2668 Py_INCREF(dictitems);
2669 }
2670 else {
2671 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2672 if (dictitems == NULL)
2673 goto end;
2674 }
2675
2676 copy_reg = import_copy_reg();
2677 if (copy_reg == NULL)
2678 goto end;
2679 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2680 if (newobj == NULL)
2681 goto end;
2682
2683 n = PyTuple_GET_SIZE(args);
2684 args2 = PyTuple_New(n+1);
2685 if (args2 == NULL)
2686 goto end;
2687 PyTuple_SET_ITEM(args2, 0, cls);
2688 cls = NULL;
2689 for (i = 0; i < n; i++) {
2690 PyObject *v = PyTuple_GET_ITEM(args, i);
2691 Py_INCREF(v);
2692 PyTuple_SET_ITEM(args2, i+1, v);
2693 }
2694
2695 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
2696
2697 end:
2698 Py_XDECREF(cls);
2699 Py_XDECREF(args);
2700 Py_XDECREF(args2);
2701 Py_XDECREF(slots);
2702 Py_XDECREF(state);
2703 Py_XDECREF(names);
2704 Py_XDECREF(listitems);
2705 Py_XDECREF(dictitems);
2706 Py_XDECREF(copy_reg);
2707 Py_XDECREF(newobj);
2708 return res;
2709}
2710
2711/*
2712 * There were two problems when object.__reduce__ and object.__reduce_ex__
2713 * were implemented in the same function:
2714 * - trying to pickle an object with a custom __reduce__ method that
2715 * fell back to object.__reduce__ in certain circumstances led to
2716 * infinite recursion at Python level and eventual RuntimeError.
2717 * - Pickling objects that lied about their type by overwriting the
2718 * __class__ descriptor could lead to infinite recursion at C level
2719 * and eventual segfault.
2720 *
2721 * Because of backwards compatibility, the two methods still have to
2722 * behave in the same way, even if this is not required by the pickle
2723 * protocol. This common functionality was moved to the _common_reduce
2724 * function.
2725 */
2726static PyObject *
2727_common_reduce(PyObject *self, int proto)
2728{
2729 PyObject *copy_reg, *res;
2730
2731 if (proto >= 2)
2732 return reduce_2(self);
2733
2734 copy_reg = import_copy_reg();
2735 if (!copy_reg)
2736 return NULL;
2737
2738 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
2739 Py_DECREF(copy_reg);
2740
2741 return res;
2742}
2743
2744static PyObject *
2745object_reduce(PyObject *self, PyObject *args)
2746{
2747 int proto = 0;
2748
2749 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
2750 return NULL;
2751
2752 return _common_reduce(self, proto);
2753}
2754
2755static PyObject *
2756object_reduce_ex(PyObject *self, PyObject *args)
2757{
2758 PyObject *reduce, *res;
2759 int proto = 0;
2760
2761 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2762 return NULL;
2763
2764 reduce = PyObject_GetAttrString(self, "__reduce__");
2765 if (reduce == NULL)
2766 PyErr_Clear();
2767 else {
2768 PyObject *cls, *clsreduce, *objreduce;
2769 int override;
2770 cls = PyObject_GetAttrString(self, "__class__");
2771 if (cls == NULL) {
2772 Py_DECREF(reduce);
2773 return NULL;
2774 }
2775 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2776 Py_DECREF(cls);
2777 if (clsreduce == NULL) {
2778 Py_DECREF(reduce);
2779 return NULL;
2780 }
2781 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2782 "__reduce__");
2783 override = (clsreduce != objreduce);
2784 Py_DECREF(clsreduce);
2785 if (override) {
2786 res = PyObject_CallObject(reduce, NULL);
2787 Py_DECREF(reduce);
2788 return res;
2789 }
2790 else
2791 Py_DECREF(reduce);
2792 }
2793
2794 return _common_reduce(self, proto);
2795}
2796
2797static PyMethodDef object_methods[] = {
2798 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2799 PyDoc_STR("helper for pickle")},
2800 {"__reduce__", object_reduce, METH_VARARGS,
2801 PyDoc_STR("helper for pickle")},
2802 {0}
2803};
2804
2805
2806PyTypeObject PyBaseObject_Type = {
2807 PyObject_HEAD_INIT(&PyType_Type)
2808 0, /* ob_size */
2809 "object", /* tp_name */
2810 sizeof(PyObject), /* tp_basicsize */
2811 0, /* tp_itemsize */
2812 object_dealloc, /* tp_dealloc */
2813 0, /* tp_print */
2814 0, /* tp_getattr */
2815 0, /* tp_setattr */
2816 0, /* tp_compare */
2817 object_repr, /* tp_repr */
2818 0, /* tp_as_number */
2819 0, /* tp_as_sequence */
2820 0, /* tp_as_mapping */
2821 object_hash, /* tp_hash */
2822 0, /* tp_call */
2823 object_str, /* tp_str */
2824 PyObject_GenericGetAttr, /* tp_getattro */
2825 PyObject_GenericSetAttr, /* tp_setattro */
2826 0, /* tp_as_buffer */
2827 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2828 PyDoc_STR("The most base type"), /* tp_doc */
2829 0, /* tp_traverse */
2830 0, /* tp_clear */
2831 0, /* tp_richcompare */
2832 0, /* tp_weaklistoffset */
2833 0, /* tp_iter */
2834 0, /* tp_iternext */
2835 object_methods, /* tp_methods */
2836 0, /* tp_members */
2837 object_getsets, /* tp_getset */
2838 0, /* tp_base */
2839 0, /* tp_dict */
2840 0, /* tp_descr_get */
2841 0, /* tp_descr_set */
2842 0, /* tp_dictoffset */
2843 object_init, /* tp_init */
2844 PyType_GenericAlloc, /* tp_alloc */
2845 object_new, /* tp_new */
2846 PyObject_Del, /* tp_free */
2847};
2848
2849
2850/* Initialize the __dict__ in a type object */
2851
2852static int
2853add_methods(PyTypeObject *type, PyMethodDef *meth)
2854{
2855 PyObject *dict = type->tp_dict;
2856
2857 for (; meth->ml_name != NULL; meth++) {
2858 PyObject *descr;
2859 if (PyDict_GetItemString(dict, meth->ml_name) &&
2860 !(meth->ml_flags & METH_COEXIST))
2861 continue;
2862 if (meth->ml_flags & METH_CLASS) {
2863 if (meth->ml_flags & METH_STATIC) {
2864 PyErr_SetString(PyExc_ValueError,
2865 "method cannot be both class and static");
2866 return -1;
2867 }
2868 descr = PyDescr_NewClassMethod(type, meth);
2869 }
2870 else if (meth->ml_flags & METH_STATIC) {
2871 PyObject *cfunc = PyCFunction_New(meth, NULL);
2872 if (cfunc == NULL)
2873 return -1;
2874 descr = PyStaticMethod_New(cfunc);
2875 Py_DECREF(cfunc);
2876 }
2877 else {
2878 descr = PyDescr_NewMethod(type, meth);
2879 }
2880 if (descr == NULL)
2881 return -1;
2882 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
2883 return -1;
2884 Py_DECREF(descr);
2885 }
2886 return 0;
2887}
2888
2889static int
2890add_members(PyTypeObject *type, PyMemberDef *memb)
2891{
2892 PyObject *dict = type->tp_dict;
2893
2894 for (; memb->name != NULL; memb++) {
2895 PyObject *descr;
2896 if (PyDict_GetItemString(dict, memb->name))
2897 continue;
2898 descr = PyDescr_NewMember(type, memb);
2899 if (descr == NULL)
2900 return -1;
2901 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2902 return -1;
2903 Py_DECREF(descr);
2904 }
2905 return 0;
2906}
2907
2908static int
2909add_getset(PyTypeObject *type, PyGetSetDef *gsp)
2910{
2911 PyObject *dict = type->tp_dict;
2912
2913 for (; gsp->name != NULL; gsp++) {
2914 PyObject *descr;
2915 if (PyDict_GetItemString(dict, gsp->name))
2916 continue;
2917 descr = PyDescr_NewGetSet(type, gsp);
2918
2919 if (descr == NULL)
2920 return -1;
2921 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2922 return -1;
2923 Py_DECREF(descr);
2924 }
2925 return 0;
2926}
2927
2928static void
2929inherit_special(PyTypeObject *type, PyTypeObject *base)
2930{
2931 Py_ssize_t oldsize, newsize;
2932
2933 /* Special flag magic */
2934 if (!type->tp_as_buffer && base->tp_as_buffer) {
2935 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
2936 type->tp_flags |=
2937 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
2938 }
2939 if (!type->tp_as_sequence && base->tp_as_sequence) {
2940 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
2941 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
2942 }
2943 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
2944 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
2945 if ((!type->tp_as_number && base->tp_as_number) ||
2946 (!type->tp_as_sequence && base->tp_as_sequence)) {
2947 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
2948 if (!type->tp_as_number && !type->tp_as_sequence) {
2949 type->tp_flags |= base->tp_flags &
2950 Py_TPFLAGS_HAVE_INPLACEOPS;
2951 }
2952 }
2953 /* Wow */
2954 }
2955 if (!type->tp_as_number && base->tp_as_number) {
2956 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
2957 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
2958 }
2959
2960 /* Copying basicsize is connected to the GC flags */
2961 oldsize = base->tp_basicsize;
2962 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2963 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2964 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2965 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
2966 (!type->tp_traverse && !type->tp_clear)) {
2967 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2968 if (type->tp_traverse == NULL)
2969 type->tp_traverse = base->tp_traverse;
2970 if (type->tp_clear == NULL)
2971 type->tp_clear = base->tp_clear;
2972 }
2973 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2974 /* The condition below could use some explanation.
2975 It appears that tp_new is not inherited for static types
2976 whose base class is 'object'; this seems to be a precaution
2977 so that old extension types don't suddenly become
2978 callable (object.__new__ wouldn't insure the invariants
2979 that the extension type's own factory function ensures).
2980 Heap types, of course, are under our control, so they do
2981 inherit tp_new; static extension types that specify some
2982 other built-in type as the default are considered
2983 new-style-aware so they also inherit object.__new__. */
2984 if (base != &PyBaseObject_Type ||
2985 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2986 if (type->tp_new == NULL)
2987 type->tp_new = base->tp_new;
2988 }
2989 }
2990 type->tp_basicsize = newsize;
2991
2992 /* Copy other non-function slots */
2993
2994#undef COPYVAL
2995#define COPYVAL(SLOT) \
2996 if (type->SLOT == 0) type->SLOT = base->SLOT
2997
2998 COPYVAL(tp_itemsize);
2999 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
3000 COPYVAL(tp_weaklistoffset);
3001 }
3002 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3003 COPYVAL(tp_dictoffset);
3004 }
3005}
3006
3007static void
3008inherit_slots(PyTypeObject *type, PyTypeObject *base)
3009{
3010 PyTypeObject *basebase;
3011
3012#undef SLOTDEFINED
3013#undef COPYSLOT
3014#undef COPYNUM
3015#undef COPYSEQ
3016#undef COPYMAP
3017#undef COPYBUF
3018
3019#define SLOTDEFINED(SLOT) \
3020 (base->SLOT != 0 && \
3021 (basebase == NULL || base->SLOT != basebase->SLOT))
3022
3023#define COPYSLOT(SLOT) \
3024 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3025
3026#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3027#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3028#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3029#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3030
3031 /* This won't inherit indirect slots (from tp_as_number etc.)
3032 if type doesn't provide the space. */
3033
3034 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3035 basebase = base->tp_base;
3036 if (basebase->tp_as_number == NULL)
3037 basebase = NULL;
3038 COPYNUM(nb_add);
3039 COPYNUM(nb_subtract);
3040 COPYNUM(nb_multiply);
3041 COPYNUM(nb_divide);
3042 COPYNUM(nb_remainder);
3043 COPYNUM(nb_divmod);
3044 COPYNUM(nb_power);
3045 COPYNUM(nb_negative);
3046 COPYNUM(nb_positive);
3047 COPYNUM(nb_absolute);
3048 COPYNUM(nb_nonzero);
3049 COPYNUM(nb_invert);
3050 COPYNUM(nb_lshift);
3051 COPYNUM(nb_rshift);
3052 COPYNUM(nb_and);
3053 COPYNUM(nb_xor);
3054 COPYNUM(nb_or);
3055 COPYNUM(nb_coerce);
3056 COPYNUM(nb_int);
3057 COPYNUM(nb_long);
3058 COPYNUM(nb_float);
3059 COPYNUM(nb_oct);
3060 COPYNUM(nb_hex);
3061 COPYNUM(nb_inplace_add);
3062 COPYNUM(nb_inplace_subtract);
3063 COPYNUM(nb_inplace_multiply);
3064 COPYNUM(nb_inplace_divide);
3065 COPYNUM(nb_inplace_remainder);
3066 COPYNUM(nb_inplace_power);
3067 COPYNUM(nb_inplace_lshift);
3068 COPYNUM(nb_inplace_rshift);
3069 COPYNUM(nb_inplace_and);
3070 COPYNUM(nb_inplace_xor);
3071 COPYNUM(nb_inplace_or);
3072 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3073 COPYNUM(nb_true_divide);
3074 COPYNUM(nb_floor_divide);
3075 COPYNUM(nb_inplace_true_divide);
3076 COPYNUM(nb_inplace_floor_divide);
3077 }
3078 if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
3079 COPYNUM(nb_index);
3080 }
3081 }
3082
3083 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3084 basebase = base->tp_base;
3085 if (basebase->tp_as_sequence == NULL)
3086 basebase = NULL;
3087 COPYSEQ(sq_length);
3088 COPYSEQ(sq_concat);
3089 COPYSEQ(sq_repeat);
3090 COPYSEQ(sq_item);
3091 COPYSEQ(sq_slice);
3092 COPYSEQ(sq_ass_item);
3093 COPYSEQ(sq_ass_slice);
3094 COPYSEQ(sq_contains);
3095 COPYSEQ(sq_inplace_concat);
3096 COPYSEQ(sq_inplace_repeat);
3097 }
3098
3099 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3100 basebase = base->tp_base;
3101 if (basebase->tp_as_mapping == NULL)
3102 basebase = NULL;
3103 COPYMAP(mp_length);
3104 COPYMAP(mp_subscript);
3105 COPYMAP(mp_ass_subscript);
3106 }
3107
3108 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3109 basebase = base->tp_base;
3110 if (basebase->tp_as_buffer == NULL)
3111 basebase = NULL;
3112 COPYBUF(bf_getreadbuffer);
3113 COPYBUF(bf_getwritebuffer);
3114 COPYBUF(bf_getsegcount);
3115 COPYBUF(bf_getcharbuffer);
3116 }
3117
3118 basebase = base->tp_base;
3119
3120 COPYSLOT(tp_dealloc);
3121 COPYSLOT(tp_print);
3122 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3123 type->tp_getattr = base->tp_getattr;
3124 type->tp_getattro = base->tp_getattro;
3125 }
3126 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3127 type->tp_setattr = base->tp_setattr;
3128 type->tp_setattro = base->tp_setattro;
3129 }
3130 /* tp_compare see tp_richcompare */
3131 COPYSLOT(tp_repr);
3132 /* tp_hash see tp_richcompare */
3133 COPYSLOT(tp_call);
3134 COPYSLOT(tp_str);
3135 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3136 if (type->tp_compare == NULL &&
3137 type->tp_richcompare == NULL &&
3138 type->tp_hash == NULL)
3139 {
3140 type->tp_compare = base->tp_compare;
3141 type->tp_richcompare = base->tp_richcompare;
3142 type->tp_hash = base->tp_hash;
3143 }
3144 }
3145 else {
3146 COPYSLOT(tp_compare);
3147 }
3148 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3149 COPYSLOT(tp_iter);
3150 COPYSLOT(tp_iternext);
3151 }
3152 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3153 COPYSLOT(tp_descr_get);
3154 COPYSLOT(tp_descr_set);
3155 COPYSLOT(tp_dictoffset);
3156 COPYSLOT(tp_init);
3157 COPYSLOT(tp_alloc);
3158 COPYSLOT(tp_is_gc);
3159 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3160 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3161 /* They agree about gc. */
3162 COPYSLOT(tp_free);
3163 }
3164 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3165 type->tp_free == NULL &&
3166 base->tp_free == _PyObject_Del) {
3167 /* A bit of magic to plug in the correct default
3168 * tp_free function when a derived class adds gc,
3169 * didn't define tp_free, and the base uses the
3170 * default non-gc tp_free.
3171 */
3172 type->tp_free = PyObject_GC_Del;
3173 }
3174 /* else they didn't agree about gc, and there isn't something
3175 * obvious to be done -- the type is on its own.
3176 */
3177 }
3178}
3179
3180static int add_operators(PyTypeObject *);
3181
3182int
3183PyType_Ready(PyTypeObject *type)
3184{
3185 PyObject *dict, *bases;
3186 PyTypeObject *base;
3187 Py_ssize_t i, n;
3188
3189 if (type->tp_flags & Py_TPFLAGS_READY) {
3190 assert(type->tp_dict != NULL);
3191 return 0;
3192 }
3193 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
3194
3195 type->tp_flags |= Py_TPFLAGS_READYING;
3196
3197#ifdef Py_TRACE_REFS
3198 /* PyType_Ready is the closest thing we have to a choke point
3199 * for type objects, so is the best place I can think of to try
3200 * to get type objects into the doubly-linked list of all objects.
3201 * Still, not all type objects go thru PyType_Ready.
3202 */
3203 _Py_AddToAllObjects((PyObject *)type, 0);
3204#endif
3205
3206 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3207 base = type->tp_base;
3208 if (base == NULL && type != &PyBaseObject_Type) {
3209 base = type->tp_base = &PyBaseObject_Type;
3210 Py_INCREF(base);
3211 }
3212
3213 /* Now the only way base can still be NULL is if type is
3214 * &PyBaseObject_Type.
3215 */
3216
3217 /* Initialize the base class */
3218 if (base && base->tp_dict == NULL) {
3219 if (PyType_Ready(base) < 0)
3220 goto error;
3221 }
3222
3223 /* Initialize ob_type if NULL. This means extensions that want to be
3224 compilable separately on Windows can call PyType_Ready() instead of
3225 initializing the ob_type field of their type objects. */
3226 /* The test for base != NULL is really unnecessary, since base is only
3227 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3228 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3229 know that. */
3230 if (type->ob_type == NULL && base != NULL)
3231 type->ob_type = base->ob_type;
3232
3233 /* Initialize tp_bases */
3234 bases = type->tp_bases;
3235 if (bases == NULL) {
3236 if (base == NULL)
3237 bases = PyTuple_New(0);
3238 else
3239 bases = PyTuple_Pack(1, base);
3240 if (bases == NULL)
3241 goto error;
3242 type->tp_bases = bases;
3243 }
3244
3245 /* Initialize tp_dict */
3246 dict = type->tp_dict;
3247 if (dict == NULL) {
3248 dict = PyDict_New();
3249 if (dict == NULL)
3250 goto error;
3251 type->tp_dict = dict;
3252 }
3253
3254 /* Add type-specific descriptors to tp_dict */
3255 if (add_operators(type) < 0)
3256 goto error;
3257 if (type->tp_methods != NULL) {
3258 if (add_methods(type, type->tp_methods) < 0)
3259 goto error;
3260 }
3261 if (type->tp_members != NULL) {
3262 if (add_members(type, type->tp_members) < 0)
3263 goto error;
3264 }
3265 if (type->tp_getset != NULL) {
3266 if (add_getset(type, type->tp_getset) < 0)
3267 goto error;
3268 }
3269
3270 /* Calculate method resolution order */
3271 if (mro_internal(type) < 0) {
3272 goto error;
3273 }
3274
3275 /* Inherit special flags from dominant base */
3276 if (type->tp_base != NULL)
3277 inherit_special(type, type->tp_base);
3278
3279 /* Initialize tp_dict properly */
3280 bases = type->tp_mro;
3281 assert(bases != NULL);
3282 assert(PyTuple_Check(bases));
3283 n = PyTuple_GET_SIZE(bases);
3284 for (i = 1; i < n; i++) {
3285 PyObject *b = PyTuple_GET_ITEM(bases, i);
3286 if (PyType_Check(b))
3287 inherit_slots(type, (PyTypeObject *)b);
3288 }
3289
3290 /* Sanity check for tp_free. */
3291 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3292 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3293 /* This base class needs to call tp_free, but doesn't have
3294 * one, or its tp_free is for non-gc'ed objects.
3295 */
3296 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3297 "gc and is a base type but has inappropriate "
3298 "tp_free slot",
3299 type->tp_name);
3300 goto error;
3301 }
3302
3303 /* if the type dictionary doesn't contain a __doc__, set it from
3304 the tp_doc slot.
3305 */
3306 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3307 if (type->tp_doc != NULL) {
3308 PyObject *doc = PyString_FromString(type->tp_doc);
3309 if (doc == NULL)
3310 goto error;
3311 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3312 Py_DECREF(doc);
3313 } else {
3314 PyDict_SetItemString(type->tp_dict,
3315 "__doc__", Py_None);
3316 }
3317 }
3318
3319 /* Some more special stuff */
3320 base = type->tp_base;
3321 if (base != NULL) {
3322 if (type->tp_as_number == NULL)
3323 type->tp_as_number = base->tp_as_number;
3324 if (type->tp_as_sequence == NULL)
3325 type->tp_as_sequence = base->tp_as_sequence;
3326 if (type->tp_as_mapping == NULL)
3327 type->tp_as_mapping = base->tp_as_mapping;
3328 if (type->tp_as_buffer == NULL)
3329 type->tp_as_buffer = base->tp_as_buffer;
3330 }
3331
3332 /* Link into each base class's list of subclasses */
3333 bases = type->tp_bases;
3334 n = PyTuple_GET_SIZE(bases);
3335 for (i = 0; i < n; i++) {
3336 PyObject *b = PyTuple_GET_ITEM(bases, i);
3337 if (PyType_Check(b) &&
3338 add_subclass((PyTypeObject *)b, type) < 0)
3339 goto error;
3340 }
3341
3342 /* All done -- set the ready flag */
3343 assert(type->tp_dict != NULL);
3344 type->tp_flags =
3345 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
3346 return 0;
3347
3348 error:
3349 type->tp_flags &= ~Py_TPFLAGS_READYING;
3350 return -1;
3351}
3352
3353static int
3354add_subclass(PyTypeObject *base, PyTypeObject *type)
3355{
3356 Py_ssize_t i;
3357 int result;
3358 PyObject *list, *ref, *newobj;
3359
3360 list = base->tp_subclasses;
3361 if (list == NULL) {
3362 base->tp_subclasses = list = PyList_New(0);
3363 if (list == NULL)
3364 return -1;
3365 }
3366 assert(PyList_Check(list));
3367 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
3368 i = PyList_GET_SIZE(list);
3369 while (--i >= 0) {
3370 ref = PyList_GET_ITEM(list, i);
3371 assert(PyWeakref_CheckRef(ref));
3372 if (PyWeakref_GET_OBJECT(ref) == Py_None)
3373 return PyList_SetItem(list, i, newobj);
3374 }
3375 result = PyList_Append(list, newobj);
3376 Py_DECREF(newobj);
3377 return result;
3378}
3379
3380static void
3381remove_subclass(PyTypeObject *base, PyTypeObject *type)
3382{
3383 Py_ssize_t i;
3384 PyObject *list, *ref;
3385
3386 list = base->tp_subclasses;
3387 if (list == NULL) {
3388 return;
3389 }
3390 assert(PyList_Check(list));
3391 i = PyList_GET_SIZE(list);
3392 while (--i >= 0) {
3393 ref = PyList_GET_ITEM(list, i);
3394 assert(PyWeakref_CheckRef(ref));
3395 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3396 /* this can't fail, right? */
3397 PySequence_DelItem(list, i);
3398 return;
3399 }
3400 }
3401}
3402
3403static int
3404check_num_args(PyObject *ob, int n)
3405{
3406 if (!PyTuple_CheckExact(ob)) {
3407 PyErr_SetString(PyExc_SystemError,
3408 "PyArg_UnpackTuple() argument list is not a tuple");
3409 return 0;
3410 }
3411 if (n == PyTuple_GET_SIZE(ob))
3412 return 1;
3413 PyErr_Format(
3414 PyExc_TypeError,
3415 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
3416 return 0;
3417}
3418
3419/* Generic wrappers for overloadable 'operators' such as __getitem__ */
3420
3421/* There's a wrapper *function* for each distinct function typedef used
3422 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3423 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3424 Most tables have only one entry; the tables for binary operators have two
3425 entries, one regular and one with reversed arguments. */
3426
3427static PyObject *
3428wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
3429{
3430 lenfunc func = (lenfunc)wrapped;
3431 Py_ssize_t res;
3432
3433 if (!check_num_args(args, 0))
3434 return NULL;
3435 res = (*func)(self);
3436 if (res == -1 && PyErr_Occurred())
3437 return NULL;
3438 return PyInt_FromLong((long)res);
3439}
3440
3441static PyObject *
3442wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3443{
3444 inquiry func = (inquiry)wrapped;
3445 int res;
3446
3447 if (!check_num_args(args, 0))
3448 return NULL;
3449 res = (*func)(self);
3450 if (res == -1 && PyErr_Occurred())
3451 return NULL;
3452 return PyBool_FromLong((long)res);
3453}
3454
3455static PyObject *
3456wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3457{
3458 binaryfunc func = (binaryfunc)wrapped;
3459 PyObject *other;
3460
3461 if (!check_num_args(args, 1))
3462 return NULL;
3463 other = PyTuple_GET_ITEM(args, 0);
3464 return (*func)(self, other);
3465}
3466
3467static PyObject *
3468wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3469{
3470 binaryfunc func = (binaryfunc)wrapped;
3471 PyObject *other;
3472
3473 if (!check_num_args(args, 1))
3474 return NULL;
3475 other = PyTuple_GET_ITEM(args, 0);
3476 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3477 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3478 Py_INCREF(Py_NotImplemented);
3479 return Py_NotImplemented;
3480 }
3481 return (*func)(self, other);
3482}
3483
3484static PyObject *
3485wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3486{
3487 binaryfunc func = (binaryfunc)wrapped;
3488 PyObject *other;
3489
3490 if (!check_num_args(args, 1))
3491 return NULL;
3492 other = PyTuple_GET_ITEM(args, 0);
3493 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
3494 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3495 Py_INCREF(Py_NotImplemented);
3496 return Py_NotImplemented;
3497 }
3498 return (*func)(other, self);
3499}
3500
3501static PyObject *
3502wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3503{
3504 coercion func = (coercion)wrapped;
3505 PyObject *other, *res;
3506 int ok;
3507
3508 if (!check_num_args(args, 1))
3509 return NULL;
3510 other = PyTuple_GET_ITEM(args, 0);
3511 ok = func(&self, &other);
3512 if (ok < 0)
3513 return NULL;
3514 if (ok > 0) {
3515 Py_INCREF(Py_NotImplemented);
3516 return Py_NotImplemented;
3517 }
3518 res = PyTuple_New(2);
3519 if (res == NULL) {
3520 Py_DECREF(self);
3521 Py_DECREF(other);
3522 return NULL;
3523 }
3524 PyTuple_SET_ITEM(res, 0, self);
3525 PyTuple_SET_ITEM(res, 1, other);
3526 return res;
3527}
3528
3529static PyObject *
3530wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3531{
3532 ternaryfunc func = (ternaryfunc)wrapped;
3533 PyObject *other;
3534 PyObject *third = Py_None;
3535
3536 /* Note: This wrapper only works for __pow__() */
3537
3538 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3539 return NULL;
3540 return (*func)(self, other, third);
3541}
3542
3543static PyObject *
3544wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3545{
3546 ternaryfunc func = (ternaryfunc)wrapped;
3547 PyObject *other;
3548 PyObject *third = Py_None;
3549
3550 /* Note: This wrapper only works for __pow__() */
3551
3552 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
3553 return NULL;
3554 return (*func)(other, self, third);
3555}
3556
3557static PyObject *
3558wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3559{
3560 unaryfunc func = (unaryfunc)wrapped;
3561
3562 if (!check_num_args(args, 0))
3563 return NULL;
3564 return (*func)(self);
3565}
3566
3567static PyObject *
3568wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
3569{
3570 ssizeargfunc func = (ssizeargfunc)wrapped;
3571 PyObject* o;
3572 Py_ssize_t i;
3573
3574 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
3575 return NULL;
3576 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
3577 if (i == -1 && PyErr_Occurred())
3578 return NULL;
3579 return (*func)(self, i);
3580}
3581
3582static Py_ssize_t
3583getindex(PyObject *self, PyObject *arg)
3584{
3585 Py_ssize_t i;
3586
3587 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
3588 if (i == -1 && PyErr_Occurred())
3589 return -1;
3590 if (i < 0) {
3591 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3592 if (sq && sq->sq_length) {
3593 Py_ssize_t n = (*sq->sq_length)(self);
3594 if (n < 0)
3595 return -1;
3596 i += n;
3597 }
3598 }
3599 return i;
3600}
3601
3602static PyObject *
3603wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3604{
3605 ssizeargfunc func = (ssizeargfunc)wrapped;
3606 PyObject *arg;
3607 Py_ssize_t i;
3608
3609 if (PyTuple_GET_SIZE(args) == 1) {
3610 arg = PyTuple_GET_ITEM(args, 0);
3611 i = getindex(self, arg);
3612 if (i == -1 && PyErr_Occurred())
3613 return NULL;
3614 return (*func)(self, i);
3615 }
3616 check_num_args(args, 1);
3617 assert(PyErr_Occurred());
3618 return NULL;
3619}
3620
3621static PyObject *
3622wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
3623{
3624 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
3625 Py_ssize_t i, j;
3626
3627 if (!PyArg_ParseTuple(args, "nn", &i, &j))
3628 return NULL;
3629 return (*func)(self, i, j);
3630}
3631
3632static PyObject *
3633wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
3634{
3635 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3636 Py_ssize_t i;
3637 int res;
3638 PyObject *arg, *value;
3639
3640 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
3641 return NULL;
3642 i = getindex(self, arg);
3643 if (i == -1 && PyErr_Occurred())
3644 return NULL;
3645 res = (*func)(self, i, value);
3646 if (res == -1 && PyErr_Occurred())
3647 return NULL;
3648 Py_INCREF(Py_None);
3649 return Py_None;
3650}
3651
3652static PyObject *
3653wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
3654{
3655 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3656 Py_ssize_t i;
3657 int res;
3658 PyObject *arg;
3659
3660 if (!check_num_args(args, 1))
3661 return NULL;
3662 arg = PyTuple_GET_ITEM(args, 0);
3663 i = getindex(self, arg);
3664 if (i == -1 && PyErr_Occurred())
3665 return NULL;
3666 res = (*func)(self, i, NULL);
3667 if (res == -1 && PyErr_Occurred())
3668 return NULL;
3669 Py_INCREF(Py_None);
3670 return Py_None;
3671}
3672
3673static PyObject *
3674wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
3675{
3676 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3677 Py_ssize_t i, j;
3678 int res;
3679 PyObject *value;
3680
3681 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
3682 return NULL;
3683 res = (*func)(self, i, j, value);
3684 if (res == -1 && PyErr_Occurred())
3685 return NULL;
3686 Py_INCREF(Py_None);
3687 return Py_None;
3688}
3689
3690static PyObject *
3691wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3692{
3693 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3694 Py_ssize_t i, j;
3695 int res;
3696
3697 if (!PyArg_ParseTuple(args, "nn", &i, &j))
3698 return NULL;
3699 res = (*func)(self, i, j, NULL);
3700 if (res == -1 && PyErr_Occurred())
3701 return NULL;
3702 Py_INCREF(Py_None);
3703 return Py_None;
3704}
3705
3706/* XXX objobjproc is a misnomer; should be objargpred */
3707static PyObject *
3708wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3709{
3710 objobjproc func = (objobjproc)wrapped;
3711 int res;
3712 PyObject *value;
3713
3714 if (!check_num_args(args, 1))
3715 return NULL;
3716 value = PyTuple_GET_ITEM(args, 0);
3717 res = (*func)(self, value);
3718 if (res == -1 && PyErr_Occurred())
3719 return NULL;
3720 else
3721 return PyBool_FromLong(res);
3722}
3723
3724static PyObject *
3725wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3726{
3727 objobjargproc func = (objobjargproc)wrapped;
3728 int res;
3729 PyObject *key, *value;
3730
3731 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
3732 return NULL;
3733 res = (*func)(self, key, value);
3734 if (res == -1 && PyErr_Occurred())
3735 return NULL;
3736 Py_INCREF(Py_None);
3737 return Py_None;
3738}
3739
3740static PyObject *
3741wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3742{
3743 objobjargproc func = (objobjargproc)wrapped;
3744 int res;
3745 PyObject *key;
3746
3747 if (!check_num_args(args, 1))
3748 return NULL;
3749 key = PyTuple_GET_ITEM(args, 0);
3750 res = (*func)(self, key, NULL);
3751 if (res == -1 && PyErr_Occurred())
3752 return NULL;
3753 Py_INCREF(Py_None);
3754 return Py_None;
3755}
3756
3757static PyObject *
3758wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3759{
3760 cmpfunc func = (cmpfunc)wrapped;
3761 int res;
3762 PyObject *other;
3763
3764 if (!check_num_args(args, 1))
3765 return NULL;
3766 other = PyTuple_GET_ITEM(args, 0);
3767 if (other->ob_type->tp_compare != func &&
3768 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
3769 PyErr_Format(
3770 PyExc_TypeError,
3771 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3772 self->ob_type->tp_name,
3773 self->ob_type->tp_name,
3774 other->ob_type->tp_name);
3775 return NULL;
3776 }
3777 res = (*func)(self, other);
3778 if (PyErr_Occurred())
3779 return NULL;
3780 return PyInt_FromLong((long)res);
3781}
3782
3783/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
3784 This is called the Carlo Verre hack after its discoverer. */
3785static int
3786hackcheck(PyObject *self, setattrofunc func, char *what)
3787{
3788 PyTypeObject *type = self->ob_type;
3789 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3790 type = type->tp_base;
3791 /* If type is NULL now, this is a really weird type.
3792 In the same of backwards compatibility (?), just shut up. */
3793 if (type && type->tp_setattro != func) {
3794 PyErr_Format(PyExc_TypeError,
3795 "can't apply this %s to %s object",
3796 what,
3797 type->tp_name);
3798 return 0;
3799 }
3800 return 1;
3801}
3802
3803static PyObject *
3804wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
3805{
3806 setattrofunc func = (setattrofunc)wrapped;
3807 int res;
3808 PyObject *name, *value;
3809
3810 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
3811 return NULL;
3812 if (!hackcheck(self, func, "__setattr__"))
3813 return NULL;
3814 res = (*func)(self, name, value);
3815 if (res < 0)
3816 return NULL;
3817 Py_INCREF(Py_None);
3818 return Py_None;
3819}
3820
3821static PyObject *
3822wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
3823{
3824 setattrofunc func = (setattrofunc)wrapped;
3825 int res;
3826 PyObject *name;
3827
3828 if (!check_num_args(args, 1))
3829 return NULL;
3830 name = PyTuple_GET_ITEM(args, 0);
3831 if (!hackcheck(self, func, "__delattr__"))
3832 return NULL;
3833 res = (*func)(self, name, NULL);
3834 if (res < 0)
3835 return NULL;
3836 Py_INCREF(Py_None);
3837 return Py_None;
3838}
3839
3840static PyObject *
3841wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3842{
3843 hashfunc func = (hashfunc)wrapped;
3844 long res;
3845
3846 if (!check_num_args(args, 0))
3847 return NULL;
3848 res = (*func)(self);
3849 if (res == -1 && PyErr_Occurred())
3850 return NULL;
3851 return PyInt_FromLong(res);
3852}
3853
3854static PyObject *
3855wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3856{
3857 ternaryfunc func = (ternaryfunc)wrapped;
3858
3859 return (*func)(self, args, kwds);
3860}
3861
3862static PyObject *
3863wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3864{
3865 richcmpfunc func = (richcmpfunc)wrapped;
3866 PyObject *other;
3867
3868 if (!check_num_args(args, 1))
3869 return NULL;
3870 other = PyTuple_GET_ITEM(args, 0);
3871 return (*func)(self, other, op);
3872}
3873
3874#undef RICHCMP_WRAPPER
3875#define RICHCMP_WRAPPER(NAME, OP) \
3876static PyObject * \
3877richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3878{ \
3879 return wrap_richcmpfunc(self, args, wrapped, OP); \
3880}
3881
3882RICHCMP_WRAPPER(lt, Py_LT)
3883RICHCMP_WRAPPER(le, Py_LE)
3884RICHCMP_WRAPPER(eq, Py_EQ)
3885RICHCMP_WRAPPER(ne, Py_NE)
3886RICHCMP_WRAPPER(gt, Py_GT)
3887RICHCMP_WRAPPER(ge, Py_GE)
3888
3889static PyObject *
3890wrap_next(PyObject *self, PyObject *args, void *wrapped)
3891{
3892 unaryfunc func = (unaryfunc)wrapped;
3893 PyObject *res;
3894
3895 if (!check_num_args(args, 0))
3896 return NULL;
3897 res = (*func)(self);
3898 if (res == NULL && !PyErr_Occurred())
3899 PyErr_SetNone(PyExc_StopIteration);
3900 return res;
3901}
3902
3903static PyObject *
3904wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3905{
3906 descrgetfunc func = (descrgetfunc)wrapped;
3907 PyObject *obj;
3908 PyObject *type = NULL;
3909
3910 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
3911 return NULL;
3912 if (obj == Py_None)
3913 obj = NULL;
3914 if (type == Py_None)
3915 type = NULL;
3916 if (type == NULL &&obj == NULL) {
3917 PyErr_SetString(PyExc_TypeError,
3918 "__get__(None, None) is invalid");
3919 return NULL;
3920 }
3921 return (*func)(self, obj, type);
3922}
3923
3924static PyObject *
3925wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
3926{
3927 descrsetfunc func = (descrsetfunc)wrapped;
3928 PyObject *obj, *value;
3929 int ret;
3930
3931 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
3932 return NULL;
3933 ret = (*func)(self, obj, value);
3934 if (ret < 0)
3935 return NULL;
3936 Py_INCREF(Py_None);
3937 return Py_None;
3938}
3939
3940static PyObject *
3941wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3942{
3943 descrsetfunc func = (descrsetfunc)wrapped;
3944 PyObject *obj;
3945 int ret;
3946
3947 if (!check_num_args(args, 1))
3948 return NULL;
3949 obj = PyTuple_GET_ITEM(args, 0);
3950 ret = (*func)(self, obj, NULL);
3951 if (ret < 0)
3952 return NULL;
3953 Py_INCREF(Py_None);
3954 return Py_None;
3955}
3956
3957static PyObject *
3958wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
3959{
3960 initproc func = (initproc)wrapped;
3961
3962 if (func(self, args, kwds) < 0)
3963 return NULL;
3964 Py_INCREF(Py_None);
3965 return Py_None;
3966}
3967
3968static PyObject *
3969tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
3970{
3971 PyTypeObject *type, *subtype, *staticbase;
3972 PyObject *arg0, *res;
3973
3974 if (self == NULL || !PyType_Check(self))
3975 Py_FatalError("__new__() called with non-type 'self'");
3976 type = (PyTypeObject *)self;
3977 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
3978 PyErr_Format(PyExc_TypeError,
3979 "%s.__new__(): not enough arguments",
3980 type->tp_name);
3981 return NULL;
3982 }
3983 arg0 = PyTuple_GET_ITEM(args, 0);
3984 if (!PyType_Check(arg0)) {
3985 PyErr_Format(PyExc_TypeError,
3986 "%s.__new__(X): X is not a type object (%s)",
3987 type->tp_name,
3988 arg0->ob_type->tp_name);
3989 return NULL;
3990 }
3991 subtype = (PyTypeObject *)arg0;
3992 if (!PyType_IsSubtype(subtype, type)) {
3993 PyErr_Format(PyExc_TypeError,
3994 "%s.__new__(%s): %s is not a subtype of %s",
3995 type->tp_name,
3996 subtype->tp_name,
3997 subtype->tp_name,
3998 type->tp_name);
3999 return NULL;
4000 }
4001
4002 /* Check that the use doesn't do something silly and unsafe like
4003 object.__new__(dict). To do this, we check that the
4004 most derived base that's not a heap type is this type. */
4005 staticbase = subtype;
4006 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4007 staticbase = staticbase->tp_base;
4008 /* If staticbase is NULL now, it is a really weird type.
4009 In the same of backwards compatibility (?), just shut up. */
4010 if (staticbase && staticbase->tp_new != type->tp_new) {
4011 PyErr_Format(PyExc_TypeError,
4012 "%s.__new__(%s) is not safe, use %s.__new__()",
4013 type->tp_name,
4014 subtype->tp_name,
4015 staticbase == NULL ? "?" : staticbase->tp_name);
4016 return NULL;
4017 }
4018
4019 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4020 if (args == NULL)
4021 return NULL;
4022 res = type->tp_new(subtype, args, kwds);
4023 Py_DECREF(args);
4024 return res;
4025}
4026
4027static struct PyMethodDef tp_new_methoddef[] = {
4028 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
4029 PyDoc_STR("T.__new__(S, ...) -> "
4030 "a new object with type S, a subtype of T")},
4031 {0}
4032};
4033
4034static int
4035add_tp_new_wrapper(PyTypeObject *type)
4036{
4037 PyObject *func;
4038
4039 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
4040 return 0;
4041 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
4042 if (func == NULL)
4043 return -1;
4044 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
4045 Py_DECREF(func);
4046 return -1;
4047 }
4048 Py_DECREF(func);
4049 return 0;
4050}
4051
4052/* Slot wrappers that call the corresponding __foo__ slot. See comments
4053 below at override_slots() for more explanation. */
4054
4055#define SLOT0(FUNCNAME, OPSTR) \
4056static PyObject * \
4057FUNCNAME(PyObject *self) \
4058{ \
4059 static PyObject *cache_str; \
4060 return call_method(self, OPSTR, &cache_str, "()"); \
4061}
4062
4063#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4064static PyObject * \
4065FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4066{ \
4067 static PyObject *cache_str; \
4068 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4069}
4070
4071/* Boolean helper for SLOT1BINFULL().
4072 right.__class__ is a nontrivial subclass of left.__class__. */
4073static int
4074method_is_overloaded(PyObject *left, PyObject *right, char *name)
4075{
4076 PyObject *a, *b;
4077 int ok;
4078
4079 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
4080 if (b == NULL) {
4081 PyErr_Clear();
4082 /* If right doesn't have it, it's not overloaded */
4083 return 0;
4084 }
4085
4086 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
4087 if (a == NULL) {
4088 PyErr_Clear();
4089 Py_DECREF(b);
4090 /* If right has it but left doesn't, it's overloaded */
4091 return 1;
4092 }
4093
4094 ok = PyObject_RichCompareBool(a, b, Py_NE);
4095 Py_DECREF(a);
4096 Py_DECREF(b);
4097 if (ok < 0) {
4098 PyErr_Clear();
4099 return 0;
4100 }
4101
4102 return ok;
4103}
4104
4105
4106#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
4107static PyObject * \
4108FUNCNAME(PyObject *self, PyObject *other) \
4109{ \
4110 static PyObject *cache_str, *rcache_str; \
4111 int do_other = self->ob_type != other->ob_type && \
4112 other->ob_type->tp_as_number != NULL && \
4113 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
4114 if (self->ob_type->tp_as_number != NULL && \
4115 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4116 PyObject *r; \
4117 if (do_other && \
4118 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4119 method_is_overloaded(self, other, ROPSTR)) { \
4120 r = call_maybe( \
4121 other, ROPSTR, &rcache_str, "(O)", self); \
4122 if (r != Py_NotImplemented) \
4123 return r; \
4124 Py_DECREF(r); \
4125 do_other = 0; \
4126 } \
4127 r = call_maybe( \
4128 self, OPSTR, &cache_str, "(O)", other); \
4129 if (r != Py_NotImplemented || \
4130 other->ob_type == self->ob_type) \
4131 return r; \
4132 Py_DECREF(r); \
4133 } \
4134 if (do_other) { \
4135 return call_maybe( \
4136 other, ROPSTR, &rcache_str, "(O)", self); \
4137 } \
4138 Py_INCREF(Py_NotImplemented); \
4139 return Py_NotImplemented; \
4140}
4141
4142#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4143 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4144
4145#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4146static PyObject * \
4147FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4148{ \
4149 static PyObject *cache_str; \
4150 return call_method(self, OPSTR, &cache_str, \
4151 "(" ARGCODES ")", arg1, arg2); \
4152}
4153
4154static Py_ssize_t
4155slot_sq_length(PyObject *self)
4156{
4157 static PyObject *len_str;
4158 PyObject *res = call_method(self, "__len__", &len_str, "()");
4159 Py_ssize_t len;
4160
4161 if (res == NULL)
4162 return -1;
4163 len = PyInt_AsSsize_t(res);
4164 Py_DECREF(res);
4165 if (len < 0) {
4166 if (!PyErr_Occurred())
4167 PyErr_SetString(PyExc_ValueError,
4168 "__len__() should return >= 0");
4169 return -1;
4170 }
4171 return len;
4172}
4173
4174/* Super-optimized version of slot_sq_item.
4175 Other slots could do the same... */
4176static PyObject *
4177slot_sq_item(PyObject *self, Py_ssize_t i)
4178{
4179 static PyObject *getitem_str;
4180 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4181 descrgetfunc f;
4182
4183 if (getitem_str == NULL) {
4184 getitem_str = PyString_InternFromString("__getitem__");
4185 if (getitem_str == NULL)
4186 return NULL;
4187 }
4188 func = _PyType_Lookup(self->ob_type, getitem_str);
4189 if (func != NULL) {
4190 if ((f = func->ob_type->tp_descr_get) == NULL)
4191 Py_INCREF(func);
4192 else {
4193 func = f(func, self, (PyObject *)(self->ob_type));
4194 if (func == NULL) {
4195 return NULL;
4196 }
4197 }
4198 ival = PyInt_FromSsize_t(i);
4199 if (ival != NULL) {
4200 args = PyTuple_New(1);
4201 if (args != NULL) {
4202 PyTuple_SET_ITEM(args, 0, ival);
4203 retval = PyObject_Call(func, args, NULL);
4204 Py_XDECREF(args);
4205 Py_XDECREF(func);
4206 return retval;
4207 }
4208 }
4209 }
4210 else {
4211 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4212 }
4213 Py_XDECREF(args);
4214 Py_XDECREF(ival);
4215 Py_XDECREF(func);
4216 return NULL;
4217}
4218
4219SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn")
4220
4221static int
4222slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
4223{
4224 PyObject *res;
4225 static PyObject *delitem_str, *setitem_str;
4226
4227 if (value == NULL)
4228 res = call_method(self, "__delitem__", &delitem_str,
4229 "(n)", index);
4230 else
4231 res = call_method(self, "__setitem__", &setitem_str,
4232 "(nO)", index, value);
4233 if (res == NULL)
4234 return -1;
4235 Py_DECREF(res);
4236 return 0;
4237}
4238
4239static int
4240slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
4241{
4242 PyObject *res;
4243 static PyObject *delslice_str, *setslice_str;
4244
4245 if (value == NULL)
4246 res = call_method(self, "__delslice__", &delslice_str,
4247 "(nn)", i, j);
4248 else
4249 res = call_method(self, "__setslice__", &setslice_str,
4250 "(nnO)", i, j, value);
4251 if (res == NULL)
4252 return -1;
4253 Py_DECREF(res);
4254 return 0;
4255}
4256
4257static int
4258slot_sq_contains(PyObject *self, PyObject *value)
4259{
4260 PyObject *func, *res, *args;
4261 int result = -1;
4262
4263 static PyObject *contains_str;
4264
4265 func = lookup_maybe(self, "__contains__", &contains_str);
4266 if (func != NULL) {
4267 args = PyTuple_Pack(1, value);
4268 if (args == NULL)
4269 res = NULL;
4270 else {
4271 res = PyObject_Call(func, args, NULL);
4272 Py_DECREF(args);
4273 }
4274 Py_DECREF(func);
4275 if (res != NULL) {
4276 result = PyObject_IsTrue(res);
4277 Py_DECREF(res);
4278 }
4279 }
4280 else if (! PyErr_Occurred()) {
4281 /* Possible results: -1 and 1 */
4282 result = (int)_PySequence_IterSearch(self, value,
4283 PY_ITERSEARCH_CONTAINS);
4284 }
4285 return result;
4286}
4287
4288#define slot_mp_length slot_sq_length
4289
4290SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
4291
4292static int
4293slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4294{
4295 PyObject *res;
4296 static PyObject *delitem_str, *setitem_str;
4297
4298 if (value == NULL)
4299 res = call_method(self, "__delitem__", &delitem_str,
4300 "(O)", key);
4301 else
4302 res = call_method(self, "__setitem__", &setitem_str,
4303 "(OO)", key, value);
4304 if (res == NULL)
4305 return -1;
4306 Py_DECREF(res);
4307 return 0;
4308}
4309
4310SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4311SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4312SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4313SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
4314SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4315SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4316
4317static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
4318
4319SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4320 nb_power, "__pow__", "__rpow__")
4321
4322static PyObject *
4323slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4324{
4325 static PyObject *pow_str;
4326
4327 if (modulus == Py_None)
4328 return slot_nb_power_binary(self, other);
4329 /* Three-arg power doesn't use __rpow__. But ternary_op
4330 can call this when the second argument's type uses
4331 slot_nb_power, so check before calling self.__pow__. */
4332 if (self->ob_type->tp_as_number != NULL &&
4333 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4334 return call_method(self, "__pow__", &pow_str,
4335 "(OO)", other, modulus);
4336 }
4337 Py_INCREF(Py_NotImplemented);
4338 return Py_NotImplemented;
4339}
4340
4341SLOT0(slot_nb_negative, "__neg__")
4342SLOT0(slot_nb_positive, "__pos__")
4343SLOT0(slot_nb_absolute, "__abs__")
4344
4345static int
4346slot_nb_nonzero(PyObject *self)
4347{
4348 PyObject *func, *args;
4349 static PyObject *nonzero_str, *len_str;
4350 int result = -1;
4351
4352 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
4353 if (func == NULL) {
4354 if (PyErr_Occurred())
4355 return -1;
4356 func = lookup_maybe(self, "__len__", &len_str);
4357 if (func == NULL)
4358 return PyErr_Occurred() ? -1 : 1;
4359 }
4360 args = PyTuple_New(0);
4361 if (args != NULL) {
4362 PyObject *temp = PyObject_Call(func, args, NULL);
4363 Py_DECREF(args);
4364 if (temp != NULL) {
4365 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
4366 result = PyObject_IsTrue(temp);
4367 else {
4368 PyErr_Format(PyExc_TypeError,
4369 "__nonzero__ should return "
4370 "bool or int, returned %s",
4371 temp->ob_type->tp_name);
4372 result = -1;
4373 }
4374 Py_DECREF(temp);
4375 }
4376 }
4377 Py_DECREF(func);
4378 return result;
4379}
4380
4381
4382static PyObject *
4383slot_nb_index(PyObject *self)
4384{
4385 static PyObject *index_str;
4386 return call_method(self, "__index__", &index_str, "()");
4387}
4388
4389
4390SLOT0(slot_nb_invert, "__invert__")
4391SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4392SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4393SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4394SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4395SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
4396
4397static int
4398slot_nb_coerce(PyObject **a, PyObject **b)
4399{
4400 static PyObject *coerce_str;
4401 PyObject *self = *a, *other = *b;
4402
4403 if (self->ob_type->tp_as_number != NULL &&
4404 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4405 PyObject *r;
4406 r = call_maybe(
4407 self, "__coerce__", &coerce_str, "(O)", other);
4408 if (r == NULL)
4409 return -1;
4410 if (r == Py_NotImplemented) {
4411 Py_DECREF(r);
4412 }
4413 else {
4414 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4415 PyErr_SetString(PyExc_TypeError,
4416 "__coerce__ didn't return a 2-tuple");
4417 Py_DECREF(r);
4418 return -1;
4419 }
4420 *a = PyTuple_GET_ITEM(r, 0);
4421 Py_INCREF(*a);
4422 *b = PyTuple_GET_ITEM(r, 1);
4423 Py_INCREF(*b);
4424 Py_DECREF(r);
4425 return 0;
4426 }
4427 }
4428 if (other->ob_type->tp_as_number != NULL &&
4429 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4430 PyObject *r;
4431 r = call_maybe(
4432 other, "__coerce__", &coerce_str, "(O)", self);
4433 if (r == NULL)
4434 return -1;
4435 if (r == Py_NotImplemented) {
4436 Py_DECREF(r);
4437 return 1;
4438 }
4439 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4440 PyErr_SetString(PyExc_TypeError,
4441 "__coerce__ didn't return a 2-tuple");
4442 Py_DECREF(r);
4443 return -1;
4444 }
4445 *a = PyTuple_GET_ITEM(r, 1);
4446 Py_INCREF(*a);
4447 *b = PyTuple_GET_ITEM(r, 0);
4448 Py_INCREF(*b);
4449 Py_DECREF(r);
4450 return 0;
4451 }
4452 return 1;
4453}
4454
4455SLOT0(slot_nb_int, "__int__")
4456SLOT0(slot_nb_long, "__long__")
4457SLOT0(slot_nb_float, "__float__")
4458SLOT0(slot_nb_oct, "__oct__")
4459SLOT0(slot_nb_hex, "__hex__")
4460SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4461SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4462SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4463SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
4464SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
4465/* Can't use SLOT1 here, because nb_inplace_power is ternary */
4466static PyObject *
4467slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
4468{
4469 static PyObject *cache_str;
4470 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
4471}
4472SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4473SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4474SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4475SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4476SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4477SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4478 "__floordiv__", "__rfloordiv__")
4479SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4480SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4481SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
4482
4483static int
4484half_compare(PyObject *self, PyObject *other)
4485{
4486 PyObject *func, *args, *res;
4487 static PyObject *cmp_str;
4488 Py_ssize_t c;
4489
4490 func = lookup_method(self, "__cmp__", &cmp_str);
4491 if (func == NULL) {
4492 PyErr_Clear();
4493 }
4494 else {
4495 args = PyTuple_Pack(1, other);
4496 if (args == NULL)
4497 res = NULL;
4498 else {
4499 res = PyObject_Call(func, args, NULL);
4500 Py_DECREF(args);
4501 }
4502 Py_DECREF(func);
4503 if (res != Py_NotImplemented) {
4504 if (res == NULL)
4505 return -2;
4506 c = PyInt_AsLong(res);
4507 Py_DECREF(res);
4508 if (c == -1 && PyErr_Occurred())
4509 return -2;
4510 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4511 }
4512 Py_DECREF(res);
4513 }
4514 return 2;
4515}
4516
4517/* This slot is published for the benefit of try_3way_compare in object.c */
4518int
4519_PyObject_SlotCompare(PyObject *self, PyObject *other)
4520{
4521 int c;
4522
4523 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
4524 c = half_compare(self, other);
4525 if (c <= 1)
4526 return c;
4527 }
4528 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
4529 c = half_compare(other, self);
4530 if (c < -1)
4531 return -2;
4532 if (c <= 1)
4533 return -c;
4534 }
4535 return (void *)self < (void *)other ? -1 :
4536 (void *)self > (void *)other ? 1 : 0;
4537}
4538
4539static PyObject *
4540slot_tp_repr(PyObject *self)
4541{
4542 PyObject *func, *res;
4543 static PyObject *repr_str;
4544
4545 func = lookup_method(self, "__repr__", &repr_str);
4546 if (func != NULL) {
4547 res = PyEval_CallObject(func, NULL);
4548 Py_DECREF(func);
4549 return res;
4550 }
4551 PyErr_Clear();
4552 return PyString_FromFormat("<%s object at %p>",
4553 self->ob_type->tp_name, self);
4554}
4555
4556static PyObject *
4557slot_tp_str(PyObject *self)
4558{
4559 PyObject *func, *res;
4560 static PyObject *str_str;
4561
4562 func = lookup_method(self, "__str__", &str_str);
4563 if (func != NULL) {
4564 res = PyEval_CallObject(func, NULL);
4565 Py_DECREF(func);
4566 return res;
4567 }
4568 else {
4569 PyErr_Clear();
4570 return slot_tp_repr(self);
4571 }
4572}
4573
4574static long
4575slot_tp_hash(PyObject *self)
4576{
4577 PyObject *func;
4578 static PyObject *hash_str, *eq_str, *cmp_str;
4579 long h;
4580
4581 func = lookup_method(self, "__hash__", &hash_str);
4582
4583 if (func != NULL) {
4584 PyObject *res = PyEval_CallObject(func, NULL);
4585 Py_DECREF(func);
4586 if (res == NULL)
4587 return -1;
4588 if (PyLong_Check(res))
4589 h = PyLong_Type.tp_hash(res);
4590 else
4591 h = PyInt_AsLong(res);
4592 Py_DECREF(res);
4593 }
4594 else {
4595 PyErr_Clear();
4596 func = lookup_method(self, "__eq__", &eq_str);
4597 if (func == NULL) {
4598 PyErr_Clear();
4599 func = lookup_method(self, "__cmp__", &cmp_str);
4600 }
4601 if (func != NULL) {
4602 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
4603 self->ob_type->tp_name);
4604 Py_DECREF(func);
4605 return -1;
4606 }
4607 PyErr_Clear();
4608 h = _Py_HashPointer((void *)self);
4609 }
4610 if (h == -1 && !PyErr_Occurred())
4611 h = -2;
4612 return h;
4613}
4614
4615static PyObject *
4616slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4617{
4618 static PyObject *call_str;
4619 PyObject *meth = lookup_method(self, "__call__", &call_str);
4620 PyObject *res;
4621
4622 if (meth == NULL)
4623 return NULL;
4624
4625 /* PyObject_Call() will end up calling slot_tp_call() again if
4626 the object returned for __call__ has __call__ itself defined
4627 upon it. This can be an infinite recursion if you set
4628 __call__ in a class to an instance of it. */
4629 if (Py_EnterRecursiveCall(" in __call__")) {
4630 Py_DECREF(meth);
4631 return NULL;
4632 }
4633 res = PyObject_Call(meth, args, kwds);
4634 Py_LeaveRecursiveCall();
4635
4636 Py_DECREF(meth);
4637 return res;
4638}
4639
4640/* There are two slot dispatch functions for tp_getattro.
4641
4642 - slot_tp_getattro() is used when __getattribute__ is overridden
4643 but no __getattr__ hook is present;
4644
4645 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4646
4647 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4648 detects the absence of __getattr__ and then installs the simpler slot if
4649 necessary. */
4650
4651static PyObject *
4652slot_tp_getattro(PyObject *self, PyObject *name)
4653{
4654 static PyObject *getattribute_str = NULL;
4655 return call_method(self, "__getattribute__", &getattribute_str,
4656 "(O)", name);
4657}
4658
4659static PyObject *
4660slot_tp_getattr_hook(PyObject *self, PyObject *name)
4661{
4662 PyTypeObject *tp = self->ob_type;
4663 PyObject *getattr, *getattribute, *res;
4664 static PyObject *getattribute_str = NULL;
4665 static PyObject *getattr_str = NULL;
4666
4667 if (getattr_str == NULL) {
4668 getattr_str = PyString_InternFromString("__getattr__");
4669 if (getattr_str == NULL)
4670 return NULL;
4671 }
4672 if (getattribute_str == NULL) {
4673 getattribute_str =
4674 PyString_InternFromString("__getattribute__");
4675 if (getattribute_str == NULL)
4676 return NULL;
4677 }
4678 getattr = _PyType_Lookup(tp, getattr_str);
4679 if (getattr == NULL) {
4680 /* No __getattr__ hook: use a simpler dispatcher */
4681 tp->tp_getattro = slot_tp_getattro;
4682 return slot_tp_getattro(self, name);
4683 }
4684 getattribute = _PyType_Lookup(tp, getattribute_str);
4685 if (getattribute == NULL ||
4686 (getattribute->ob_type == &PyWrapperDescr_Type &&
4687 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4688 (void *)PyObject_GenericGetAttr))
4689 res = PyObject_GenericGetAttr(self, name);
4690 else
4691 res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL);
4692 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4693 PyErr_Clear();
4694 res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL);
4695 }
4696 return res;
4697}
4698
4699static int
4700slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4701{
4702 PyObject *res;
4703 static PyObject *delattr_str, *setattr_str;
4704
4705 if (value == NULL)
4706 res = call_method(self, "__delattr__", &delattr_str,
4707 "(O)", name);
4708 else
4709 res = call_method(self, "__setattr__", &setattr_str,
4710 "(OO)", name, value);
4711 if (res == NULL)
4712 return -1;
4713 Py_DECREF(res);
4714 return 0;
4715}
4716
4717/* Map rich comparison operators to their __xx__ namesakes */
4718static char *name_op[] = {
4719 "__lt__",
4720 "__le__",
4721 "__eq__",
4722 "__ne__",
4723 "__gt__",
4724 "__ge__",
4725};
4726
4727static PyObject *
4728half_richcompare(PyObject *self, PyObject *other, int op)
4729{
4730 PyObject *func, *args, *res;
4731 static PyObject *op_str[6];
4732
4733 func = lookup_method(self, name_op[op], &op_str[op]);
4734 if (func == NULL) {
4735 PyErr_Clear();
4736 Py_INCREF(Py_NotImplemented);
4737 return Py_NotImplemented;
4738 }
4739 args = PyTuple_Pack(1, other);
4740 if (args == NULL)
4741 res = NULL;
4742 else {
4743 res = PyObject_Call(func, args, NULL);
4744 Py_DECREF(args);
4745 }
4746 Py_DECREF(func);
4747 return res;
4748}
4749
4750static PyObject *
4751slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4752{
4753 PyObject *res;
4754
4755 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4756 res = half_richcompare(self, other, op);
4757 if (res != Py_NotImplemented)
4758 return res;
4759 Py_DECREF(res);
4760 }
4761 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
4762 res = half_richcompare(other, self, _Py_SwappedOp[op]);
4763 if (res != Py_NotImplemented) {
4764 return res;
4765 }
4766 Py_DECREF(res);
4767 }
4768 Py_INCREF(Py_NotImplemented);
4769 return Py_NotImplemented;
4770}
4771
4772static PyObject *
4773slot_tp_iter(PyObject *self)
4774{
4775 PyObject *func, *res;
4776 static PyObject *iter_str, *getitem_str;
4777
4778 func = lookup_method(self, "__iter__", &iter_str);
4779 if (func != NULL) {
4780 PyObject *args;
4781 args = res = PyTuple_New(0);
4782 if (args != NULL) {
4783 res = PyObject_Call(func, args, NULL);
4784 Py_DECREF(args);
4785 }
4786 Py_DECREF(func);
4787 return res;
4788 }
4789 PyErr_Clear();
4790 func = lookup_method(self, "__getitem__", &getitem_str);
4791 if (func == NULL) {
4792 PyErr_Format(PyExc_TypeError,
4793 "'%.200s' object is not iterable",
4794 self->ob_type->tp_name);
4795 return NULL;
4796 }
4797 Py_DECREF(func);
4798 return PySeqIter_New(self);
4799}
4800
4801static PyObject *
4802slot_tp_iternext(PyObject *self)
4803{
4804 static PyObject *next_str;
4805 return call_method(self, "next", &next_str, "()");
4806}
4807
4808static PyObject *
4809slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4810{
4811 PyTypeObject *tp = self->ob_type;
4812 PyObject *get;
4813 static PyObject *get_str = NULL;
4814
4815 if (get_str == NULL) {
4816 get_str = PyString_InternFromString("__get__");
4817 if (get_str == NULL)
4818 return NULL;
4819 }
4820 get = _PyType_Lookup(tp, get_str);
4821 if (get == NULL) {
4822 /* Avoid further slowdowns */
4823 if (tp->tp_descr_get == slot_tp_descr_get)
4824 tp->tp_descr_get = NULL;
4825 Py_INCREF(self);
4826 return self;
4827 }
4828 if (obj == NULL)
4829 obj = Py_None;
4830 if (type == NULL)
4831 type = Py_None;
4832 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
4833}
4834
4835static int
4836slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4837{
4838 PyObject *res;
4839 static PyObject *del_str, *set_str;
4840
4841 if (value == NULL)
4842 res = call_method(self, "__delete__", &del_str,
4843 "(O)", target);
4844 else
4845 res = call_method(self, "__set__", &set_str,
4846 "(OO)", target, value);
4847 if (res == NULL)
4848 return -1;
4849 Py_DECREF(res);
4850 return 0;
4851}
4852
4853static int
4854slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4855{
4856 static PyObject *init_str;
4857 PyObject *meth = lookup_method(self, "__init__", &init_str);
4858 PyObject *res;
4859
4860 if (meth == NULL)
4861 return -1;
4862 res = PyObject_Call(meth, args, kwds);
4863 Py_DECREF(meth);
4864 if (res == NULL)
4865 return -1;
4866 if (res != Py_None) {
4867 PyErr_Format(PyExc_TypeError,
4868 "__init__() should return None, not '%.200s'",
4869 res->ob_type->tp_name);
4870 Py_DECREF(res);
4871 return -1;
4872 }
4873 Py_DECREF(res);
4874 return 0;
4875}
4876
4877static PyObject *
4878slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4879{
4880 static PyObject *new_str;
4881 PyObject *func;
4882 PyObject *newargs, *x;
4883 Py_ssize_t i, n;
4884
4885 if (new_str == NULL) {
4886 new_str = PyString_InternFromString("__new__");
4887 if (new_str == NULL)
4888 return NULL;
4889 }
4890 func = PyObject_GetAttr((PyObject *)type, new_str);
4891 if (func == NULL)
4892 return NULL;
4893 assert(PyTuple_Check(args));
4894 n = PyTuple_GET_SIZE(args);
4895 newargs = PyTuple_New(n+1);
4896 if (newargs == NULL)
4897 return NULL;
4898 Py_INCREF(type);
4899 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4900 for (i = 0; i < n; i++) {
4901 x = PyTuple_GET_ITEM(args, i);
4902 Py_INCREF(x);
4903 PyTuple_SET_ITEM(newargs, i+1, x);
4904 }
4905 x = PyObject_Call(func, newargs, kwds);
4906 Py_DECREF(newargs);
4907 Py_DECREF(func);
4908 return x;
4909}
4910
4911static void
4912slot_tp_del(PyObject *self)
4913{
4914 static PyObject *del_str = NULL;
4915 PyObject *del, *res;
4916 PyObject *error_type, *error_value, *error_traceback;
4917
4918 /* Temporarily resurrect the object. */
4919 assert(self->ob_refcnt == 0);
4920 self->ob_refcnt = 1;
4921
4922 /* Save the current exception, if any. */
4923 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4924
4925 /* Execute __del__ method, if any. */
4926 del = lookup_maybe(self, "__del__", &del_str);
4927 if (del != NULL) {
4928 res = PyEval_CallObject(del, NULL);
4929 if (res == NULL)
4930 PyErr_WriteUnraisable(del);
4931 else
4932 Py_DECREF(res);
4933 Py_DECREF(del);
4934 }
4935
4936 /* Restore the saved exception. */
4937 PyErr_Restore(error_type, error_value, error_traceback);
4938
4939 /* Undo the temporary resurrection; can't use DECREF here, it would
4940 * cause a recursive call.
4941 */
4942 assert(self->ob_refcnt > 0);
4943 if (--self->ob_refcnt == 0)
4944 return; /* this is the normal path out */
4945
4946 /* __del__ resurrected it! Make it look like the original Py_DECREF
4947 * never happened.
4948 */
4949 {
4950 Py_ssize_t refcnt = self->ob_refcnt;
4951 _Py_NewReference(self);
4952 self->ob_refcnt = refcnt;
4953 }
4954 assert(!PyType_IS_GC(self->ob_type) ||
4955 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
4956 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
4957 * we need to undo that. */
4958 _Py_DEC_REFTOTAL;
4959 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4960 * chain, so no more to do there.
4961 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4962 * _Py_NewReference bumped tp_allocs: both of those need to be
4963 * undone.
4964 */
4965#ifdef COUNT_ALLOCS
4966 --self->ob_type->tp_frees;
4967 --self->ob_type->tp_allocs;
4968#endif
4969}
4970
4971
4972/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
4973 functions. The offsets here are relative to the 'PyHeapTypeObject'
4974 structure, which incorporates the additional structures used for numbers,
4975 sequences and mappings.
4976 Note that multiple names may map to the same slot (e.g. __eq__,
4977 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
4978 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4979 terminated with an all-zero entry. (This table is further initialized and
4980 sorted in init_slotdefs() below.) */
4981
4982typedef struct wrapperbase slotdef;
4983
4984#undef TPSLOT
4985#undef FLSLOT
4986#undef ETSLOT
4987#undef SQSLOT
4988#undef MPSLOT
4989#undef NBSLOT
4990#undef UNSLOT
4991#undef IBSLOT
4992#undef BINSLOT
4993#undef RBINSLOT
4994
4995#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4996 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4997 PyDoc_STR(DOC)}
4998#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4999 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \

892 more lines aren’t shown here.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t