annotate Objects/typeobject.c @ 65997:3207d9f4d00a 2.7

Document key-functions in the glossary.
author Raymond Hettinger <python@rcn.com>
date Sat, 06 Nov 2010 00:06:14 +0000
parents 0f87085b290f
children ddaa17c098d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Type object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
3 #include "Python.h"
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
6 #include <ctype.h>
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
7
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
8
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
9 /* Support type attribute cache */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
10
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
11 /* The cache can keep references to the names alive for longer than
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
12 they normally would. This is why the maximum size is limited to
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
13 MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
14 strings are used as attribute names. */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
15 #define MCACHE_MAX_ATTR_SIZE 100
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
16 #define MCACHE_SIZE_EXP 10
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
17 #define MCACHE_HASH(version, name_hash) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
18 (((unsigned int)(version) * (unsigned int)(name_hash)) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
19 >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
20 #define MCACHE_HASH_METHOD(type, name) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
21 MCACHE_HASH((type)->tp_version_tag, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
22 ((PyStringObject *)(name))->ob_shash)
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
23 #define MCACHE_CACHEABLE_NAME(name) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
24 PyString_CheckExact(name) && \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
25 PyString_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
26
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
27 struct method_cache_entry {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
28 unsigned int version;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
29 PyObject *name; /* reference to exactly a str or None */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
30 PyObject *value; /* borrowed */
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
31 };
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
32
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
33 static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
34 static unsigned int next_version_tag = 0;
45025
ea3fe302d392 Added clear cache methods to clear the internal type lookup cache for ref leak test runs.
Christian Heimes <christian@cheimes.de>
parents: 45004
diff changeset
35
ea3fe302d392 Added clear cache methods to clear the internal type lookup cache for ref leak test runs.
Christian Heimes <christian@cheimes.de>
parents: 45004
diff changeset
36 unsigned int
ea3fe302d392 Added clear cache methods to clear the internal type lookup cache for ref leak test runs.
Christian Heimes <christian@cheimes.de>
parents: 45004
diff changeset
37 PyType_ClearCache(void)
ea3fe302d392 Added clear cache methods to clear the internal type lookup cache for ref leak test runs.
Christian Heimes <christian@cheimes.de>
parents: 45004
diff changeset
38 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
39 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
40 unsigned int cur_version_tag = next_version_tag - 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
41
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
42 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
43 method_cache[i].version = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
44 Py_CLEAR(method_cache[i].name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
45 method_cache[i].value = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
46 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
47 next_version_tag = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
48 /* mark all version tags as invalid */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
49 PyType_Modified(&PyBaseObject_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
50 return cur_version_tag;
45025
ea3fe302d392 Added clear cache methods to clear the internal type lookup cache for ref leak test runs.
Christian Heimes <christian@cheimes.de>
parents: 45004
diff changeset
51 }
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
52
47358
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
53 void
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
54 PyType_Modified(PyTypeObject *type)
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
55 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
56 /* Invalidate any cached data for the specified type and all
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
57 subclasses. This function is called after the base
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
58 classes, mro, or attributes of the type are altered.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
59
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
60 Invariants:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
61
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
62 - Py_TPFLAGS_VALID_VERSION_TAG is never set if
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
63 Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
64 objects coming from non-recompiled extension modules)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
65
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
66 - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
67 it must first be set on all super types.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
68
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
69 This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
70 type (so it must first clear it on all subclasses). The
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
71 tp_version_tag value is meaningless unless this flag is set.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
72 We don't assign new version tags eagerly, but only as
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
73 needed.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
74 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
75 PyObject *raw, *ref;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
76 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
77
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
78 if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
79 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
80
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
81 raw = type->tp_subclasses;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
82 if (raw != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
83 n = PyList_GET_SIZE(raw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
84 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
85 ref = PyList_GET_ITEM(raw, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
86 ref = PyWeakref_GET_OBJECT(ref);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
87 if (ref != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
88 PyType_Modified((PyTypeObject *)ref);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
89 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
90 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
91 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
92 type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
93 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
94
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
95 static void
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
96 type_mro_modified(PyTypeObject *type, PyObject *bases) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
97 /*
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
98 Check that all base classes or elements of the mro of type are
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
99 able to be cached. This function is called after the base
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
100 classes or mro of the type are altered.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
101
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
102 Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
103 inherits from an old-style class, either directly or if it
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
104 appears in the MRO of a new-style class. No support either for
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
105 custom MROs that include types that are not officially super
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
106 types.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
107
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
108 Called from mro_internal, which will subsequently be called on
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
109 each subclass when their mro is recursively updated.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
110 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
111 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
112 int clear = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
113
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
114 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
115 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
116
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
117 n = PyTuple_GET_SIZE(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
118 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
119 PyObject *b = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
120 PyTypeObject *cls;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
121
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
122 if (!PyType_Check(b) ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
123 clear = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
124 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
125 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
126
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
127 cls = (PyTypeObject *)b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
128
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
129 if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
130 !PyType_IsSubtype(type, cls)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
131 clear = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
132 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
133 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
134 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
135
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
136 if (clear)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
137 type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
138 Py_TPFLAGS_VALID_VERSION_TAG);
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
139 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
140
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
141 static int
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
142 assign_version_tag(PyTypeObject *type)
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
143 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
144 /* Ensure that the tp_version_tag is valid and set
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
145 Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
146 must first be done on all super classes. Return 0 if this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
147 cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
148 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
149 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
150 PyObject *bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
151
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
152 if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
153 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
154 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
155 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
156 if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
157 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
158
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
159 type->tp_version_tag = next_version_tag++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
160 /* for stress-testing: next_version_tag &= 0xFF; */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
161
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
162 if (type->tp_version_tag == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
163 /* wrap-around or just starting Python - clear the whole
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
164 cache by filling names with references to Py_None.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
165 Values are also set to NULL for added protection, as they
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
166 are borrowed reference */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
167 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
168 method_cache[i].value = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
169 Py_XDECREF(method_cache[i].name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
170 method_cache[i].name = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
171 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
172 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
173 /* mark all version tags as invalid */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
174 PyType_Modified(&PyBaseObject_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
175 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
176 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
177 bases = type->tp_bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
178 n = PyTuple_GET_SIZE(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
179 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
180 PyObject *b = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
181 assert(PyType_Check(b));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
182 if (!assign_version_tag((PyTypeObject *)b))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
183 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
184 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
185 type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
186 return 1;
44664
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
187 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
188
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
189
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
190 static PyMemberDef type_members[] = {
64284
ebfb94f2cbc3 Merged revisions 84320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64078
diff changeset
191 {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY},
ebfb94f2cbc3 Merged revisions 84320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64078
diff changeset
192 {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
193 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
194 {"__weakrefoffset__", T_LONG,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
195 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
196 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
197 {"__dictoffset__", T_LONG,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
198 offsetof(PyTypeObject, tp_dictoffset), READONLY},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
199 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
200 {0}
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
201 };
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
203 static PyObject *
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
204 type_name(PyTypeObject *type, void *context)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
205 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
206 const char *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
207
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
208 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
209 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
210
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
211 Py_INCREF(et->ht_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
212 return et->ht_name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
213 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
214 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
215 s = strrchr(type->tp_name, '.');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
216 if (s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
217 s = type->tp_name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
218 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
219 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
220 return PyString_FromString(s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
221 }
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
222 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
223
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
224 static int
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
225 type_set_name(PyTypeObject *type, PyObject *value, void *context)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
226 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
227 PyHeapTypeObject* et;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
228
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
229 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
230 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
231 "can't set %s.__name__", type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
232 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
233 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
234 if (!value) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
235 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
236 "can't delete %s.__name__", type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
237 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
238 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
239 if (!PyString_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
240 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
241 "can only assign string to %s.__name__, not '%s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
242 type->tp_name, Py_TYPE(value)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
243 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
244 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
245 if (strlen(PyString_AS_STRING(value))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
246 != (size_t)PyString_GET_SIZE(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
247 PyErr_Format(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
248 "__name__ must not contain null bytes");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
249 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
250 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
251
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
252 et = (PyHeapTypeObject*)type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
253
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
254 Py_INCREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
255
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
256 Py_DECREF(et->ht_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
257 et->ht_name = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
258
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
259 type->tp_name = PyString_AS_STRING(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
260
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
261 return 0;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
262 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
263
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
264 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
265 type_module(PyTypeObject *type, void *context)
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
266 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
267 PyObject *mod;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
268 char *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
269
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
270 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
271 mod = PyDict_GetItemString(type->tp_dict, "__module__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
272 if (!mod) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
273 PyErr_Format(PyExc_AttributeError, "__module__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
274 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
275 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
276 Py_XINCREF(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
277 return mod;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
278 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
279 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
280 s = strrchr(type->tp_name, '.');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
281 if (s != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
282 return PyString_FromStringAndSize(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
283 type->tp_name, (Py_ssize_t)(s - type->tp_name));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
284 return PyString_FromString("__builtin__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
285 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
286 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
287
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
288 static int
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
289 type_set_module(PyTypeObject *type, PyObject *value, void *context)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
290 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
291 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
292 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
293 "can't set %s.__module__", type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
294 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
295 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
296 if (!value) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
297 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
298 "can't delete %s.__module__", type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
299 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
300 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
301
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
302 PyType_Modified(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
303
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
304 return PyDict_SetItemString(type->tp_dict, "__module__", value);
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
305 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
306
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
307 static PyObject *
45543
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
308 type_abstractmethods(PyTypeObject *type, void *context)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
309 {
65048
471dbd906aad Merged revisions 85154 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64284
diff changeset
310 PyObject *mod = NULL;
65068
0f87085b290f Merged revisions 85193 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 65048
diff changeset
311 /* type itself has an __abstractmethods__ descriptor (this). Don't return
0f87085b290f Merged revisions 85193 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 65048
diff changeset
312 that. */
65048
471dbd906aad Merged revisions 85154 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64284
diff changeset
313 if (type != &PyType_Type)
471dbd906aad Merged revisions 85154 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 64284
diff changeset
314 mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__");
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
315 if (!mod) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
316 PyErr_Format(PyExc_AttributeError, "__abstractmethods__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
317 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
318 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
319 Py_XINCREF(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
320 return mod;
45543
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
321 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
322
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
323 static int
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
324 type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
325 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
326 /* __abstractmethods__ should only be set once on a type, in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
327 abc.ABCMeta.__new__, so this function doesn't do anything
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
328 special to update subclasses.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
329 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
330 int res = PyDict_SetItemString(type->tp_dict,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
331 "__abstractmethods__", value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
332 if (res == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
333 PyType_Modified(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
334 if (value && PyObject_IsTrue(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
335 type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
336 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
337 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
338 type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
339 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
340 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
341 return res;
45543
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
342 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
343
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
344 static PyObject *
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
345 type_get_bases(PyTypeObject *type, void *context)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
346 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
347 Py_INCREF(type->tp_bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
348 return type->tp_bases;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
349 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
350
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
351 static PyTypeObject *best_base(PyObject *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
352 static int mro_internal(PyTypeObject *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
353 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
354 static int add_subclass(PyTypeObject*, PyTypeObject*);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
355 static void remove_subclass(PyTypeObject *, PyTypeObject *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
356 static void update_all_slots(PyTypeObject *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
357
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
358 typedef int (*update_callback)(PyTypeObject *, void *);
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
359 static int update_subclasses(PyTypeObject *type, PyObject *name,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
360 update_callback callback, void *data);
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
361 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
362 update_callback callback, void *data);
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
363
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
364 static int
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
365 mro_subclasses(PyTypeObject *type, PyObject* temp)
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
366 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
367 PyTypeObject *subclass;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
368 PyObject *ref, *subclasses, *old_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
369 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
370
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
371 subclasses = type->tp_subclasses;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
372 if (subclasses == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
373 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
374 assert(PyList_Check(subclasses));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
375 n = PyList_GET_SIZE(subclasses);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
376 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
377 ref = PyList_GET_ITEM(subclasses, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
378 assert(PyWeakref_CheckRef(ref));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
379 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
380 assert(subclass != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
381 if ((PyObject *)subclass == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
382 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
383 assert(PyType_Check(subclass));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
384 old_mro = subclass->tp_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
385 if (mro_internal(subclass) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
386 subclass->tp_mro = old_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
387 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
388 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
389 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
390 PyObject* tuple;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
391 tuple = PyTuple_Pack(2, subclass, old_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
392 Py_DECREF(old_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
393 if (!tuple)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
394 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
395 if (PyList_Append(temp, tuple) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
396 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
397 Py_DECREF(tuple);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
398 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
399 if (mro_subclasses(subclass, temp) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
400 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
401 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
402 return 0;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
403 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
404
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
405 static int
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
406 type_set_bases(PyTypeObject *type, PyObject *value, void *context)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
407 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
408 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
409 int r = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
410 PyObject *ob, *temp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
411 PyTypeObject *new_base, *old_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
412 PyObject *old_bases, *old_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
413
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
414 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
415 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
416 "can't set %s.__bases__", type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
417 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
418 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
419 if (!value) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
420 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
421 "can't delete %s.__bases__", type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
422 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
423 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
424 if (!PyTuple_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
425 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
426 "can only assign tuple to %s.__bases__, not %s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
427 type->tp_name, Py_TYPE(value)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
428 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
429 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
430 if (PyTuple_GET_SIZE(value) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
431 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
432 "can only assign non-empty tuple to %s.__bases__, not ()",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
433 type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
434 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
435 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
436 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
437 ob = PyTuple_GET_ITEM(value, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
438 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
439 PyErr_Format(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
440 PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
441 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
442 type->tp_name, Py_TYPE(ob)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
443 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
444 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
445 if (PyType_Check(ob)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
446 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
447 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
448 "a __bases__ item causes an inheritance cycle");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
449 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
450 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
451 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
452 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
453
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
454 new_base = best_base(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
455
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
456 if (!new_base) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
457 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
458 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
459
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
460 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
461 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
462
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
463 Py_INCREF(new_base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
464 Py_INCREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
465
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
466 old_bases = type->tp_bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
467 old_base = type->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
468 old_mro = type->tp_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
469
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
470 type->tp_bases = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
471 type->tp_base = new_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
472
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
473 if (mro_internal(type) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
474 goto bail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
475 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
476
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
477 temp = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
478 if (!temp)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
479 goto bail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
480
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
481 r = mro_subclasses(type, temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
482
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
483 if (r < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
484 for (i = 0; i < PyList_Size(temp); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
485 PyTypeObject* cls;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
486 PyObject* mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
487 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
488 "", 2, 2, &cls, &mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
489 Py_INCREF(mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
490 ob = cls->tp_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
491 cls->tp_mro = mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
492 Py_DECREF(ob);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
493 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
494 Py_DECREF(temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
495 goto bail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
496 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
497
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
498 Py_DECREF(temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
499
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
500 /* any base that was in __bases__ but now isn't, we
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
501 need to remove |type| from its tp_subclasses.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
502 conversely, any class now in __bases__ that wasn't
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
503 needs to have |type| added to its subclasses. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
504
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
505 /* for now, sod that: just remove from all old_bases,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
506 add to all new_bases */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
507
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
508 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
509 ob = PyTuple_GET_ITEM(old_bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
510 if (PyType_Check(ob)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
511 remove_subclass(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
512 (PyTypeObject*)ob, type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
513 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
514 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
515
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
516 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
517 ob = PyTuple_GET_ITEM(value, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
518 if (PyType_Check(ob)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
519 if (add_subclass((PyTypeObject*)ob, type) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
520 r = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
521 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
522 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
523
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
524 update_all_slots(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
525
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
526 Py_DECREF(old_bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
527 Py_DECREF(old_base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
528 Py_DECREF(old_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
529
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
530 return r;
26446
7777dceb66ee I don't know why staring at the email to python-checkins made me
Michael W. Hudson <mwh@python.net>
parents: 26445
diff changeset
531
7777dceb66ee I don't know why staring at the email to python-checkins made me
Michael W. Hudson <mwh@python.net>
parents: 26445
diff changeset
532 bail:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
533 Py_DECREF(type->tp_bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
534 Py_DECREF(type->tp_base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
535 if (type->tp_mro != old_mro) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
536 Py_DECREF(type->tp_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
537 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
538
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
539 type->tp_bases = old_bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
540 type->tp_base = old_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
541 type->tp_mro = old_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
542
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
543 return -1;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
544 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
545
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
546 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
547 type_dict(PyTypeObject *type, void *context)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
548 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
549 if (type->tp_dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
550 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
551 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
552 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
553 return PyDictProxy_New(type->tp_dict);
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
554 }
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
555
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
556 static PyObject *
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
557 type_get_doc(PyTypeObject *type, void *context)
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
558 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
559 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
560 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
561 return PyString_FromString(type->tp_doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
562 result = PyDict_GetItemString(type->tp_dict, "__doc__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
563 if (result == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
564 result = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
565 Py_INCREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
566 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
567 else if (Py_TYPE(result)->tp_descr_get) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
568 result = Py_TYPE(result)->tp_descr_get(result, NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
569 (PyObject *)type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
570 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
571 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
572 Py_INCREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
573 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
574 return result;
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
575 }
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
576
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
577 static PyObject *
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
578 type___instancecheck__(PyObject *type, PyObject *inst)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
579 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
580 switch (_PyObject_RealIsInstance(inst, type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
581 case -1:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
582 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
583 case 0:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
584 Py_RETURN_FALSE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
585 default:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
586 Py_RETURN_TRUE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
587 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
588 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
589
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
590
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
591 static PyObject *
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
592 type___subclasscheck__(PyObject *type, PyObject *inst)
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
593 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
594 switch (_PyObject_RealIsSubclass(inst, type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
595 case -1:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
596 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
597 case 0:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
598 Py_RETURN_FALSE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
599 default:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
600 Py_RETURN_TRUE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
601 }
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
602 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
603
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
604
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
605 static PyGetSetDef type_getsets[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
606 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
607 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
608 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
609 {"__abstractmethods__", (getter)type_abstractmethods,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
610 (setter)type_set_abstractmethods, NULL},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
611 {"__dict__", (getter)type_dict, NULL, NULL},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
612 {"__doc__", (getter)type_get_doc, NULL, NULL},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
613 {0}
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
614 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
615
18063
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
616
45860
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45543
diff changeset
617 static PyObject*
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45543
diff changeset
618 type_richcompare(PyObject *v, PyObject *w, int op)
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45543
diff changeset
619 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
620 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
621 Py_uintptr_t vv, ww;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
622 int c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
623
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
624 /* Make sure both arguments are types. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
625 if (!PyType_Check(v) || !PyType_Check(w) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
626 /* If there is a __cmp__ method defined, let it be called instead
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
627 of our dumb function designed merely to warn. See bug
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
628 #7491. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
629 Py_TYPE(v)->tp_compare || Py_TYPE(w)->tp_compare) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
630 result = Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
631 goto out;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
632 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
633
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
634 /* Py3K warning if comparison isn't == or != */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
635 if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
636 PyErr_WarnEx(PyExc_DeprecationWarning,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
637 "type inequality comparisons not supported "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
638 "in 3.x", 1) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
639 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
640 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
641
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
642 /* Compare addresses */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
643 vv = (Py_uintptr_t)v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
644 ww = (Py_uintptr_t)w;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
645 switch (op) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
646 case Py_LT: c = vv < ww; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
647 case Py_LE: c = vv <= ww; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
648 case Py_EQ: c = vv == ww; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
649 case Py_NE: c = vv != ww; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
650 case Py_GT: c = vv > ww; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
651 case Py_GE: c = vv >= ww; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
652 default:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
653 result = Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
654 goto out;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
655 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
656 result = c ? Py_True : Py_False;
45860
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45543
diff changeset
657
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45543
diff changeset
658 /* incref and return */
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45543
diff changeset
659 out:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
660 Py_INCREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
661 return result;
45860
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45543
diff changeset
662 }
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45543
diff changeset
663
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
664 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
665 type_repr(PyTypeObject *type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
667 PyObject *mod, *name, *rtn;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
668 char *kind;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
669
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
670 mod = type_module(type, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
671 if (mod == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
672 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
673 else if (!PyString_Check(mod)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
674 Py_DECREF(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
675 mod = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
676 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
677 name = type_name(type, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
678 if (name == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
679 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
680
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
681 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
682 kind = "class";
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
683 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
684 kind = "type";
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
685
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
686 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
687 rtn = PyString_FromFormat("<%s '%s.%s'>",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
688 kind,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
689 PyString_AS_STRING(mod),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
690 PyString_AS_STRING(name));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
691 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
692 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
693 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
694
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
695 Py_XDECREF(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
696 Py_DECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
697 return rtn;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
699
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
700 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
701 type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
702 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
703 PyObject *obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
704
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
705 if (type->tp_new == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
706 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
707 "cannot create '%.100s' instances",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
708 type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
709 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
710 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
711
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
712 obj = type->tp_new(type, args, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
713 if (obj != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
714 /* Ugly exception: when the call was type(something),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
715 don't call tp_init on the result. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
716 if (type == &PyType_Type &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
717 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
718 (kwds == NULL ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
719 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
720 return obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
721 /* If the returned object is not an instance of type,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
722 it won't be initialized. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
723 if (!PyType_IsSubtype(obj->ob_type, type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
724 return obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
725 type = obj->ob_type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
726 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
727 type->tp_init != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
728 type->tp_init(obj, args, kwds) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
729 Py_DECREF(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
730 obj = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
731 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
732 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
733 return obj;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
734 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
735
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
736 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
737 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
738 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
739 PyObject *obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
740 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
741 /* note that we need to add one, for the sentinel */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
742
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
743 if (PyType_IS_GC(type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
744 obj = _PyObject_GC_Malloc(size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
745 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
746 obj = (PyObject *)PyObject_MALLOC(size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
747
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
748 if (obj == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
749 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
750
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
751 memset(obj, '\0', size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
752
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
753 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
754 Py_INCREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
755
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
756 if (type->tp_itemsize == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
757 PyObject_INIT(obj, type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
758 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
759 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
760
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
761 if (PyType_IS_GC(type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
762 _PyObject_GC_TRACK(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
763 return obj;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
764 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
765
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
766 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
767 PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
768 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
769 return type->tp_alloc(type, 0);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
770 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
771
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
772 /* Helpers for subtyping */
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
773
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
774 static int
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
775 traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
776 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
777 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
778 PyMemberDef *mp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
779
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
780 n = Py_SIZE(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
781 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
782 for (i = 0; i < n; i++, mp++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
783 if (mp->type == T_OBJECT_EX) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
784 char *addr = (char *)self + mp->offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
785 PyObject *obj = *(PyObject **)addr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
786 if (obj != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
787 int err = visit(obj, arg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
788 if (err)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
789 return err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
790 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
791 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
792 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
793 return 0;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
794 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
795
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
796 static int
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
797 subtype_traverse(PyObject *self, visitproc visit, void *arg)
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
798 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
799 PyTypeObject *type, *base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
800 traverseproc basetraverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
801
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
802 /* Find the nearest base with a different tp_traverse,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
803 and traverse slots while we're at it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
804 type = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
805 base = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
806 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
807 if (Py_SIZE(base)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
808 int err = traverse_slots(base, self, visit, arg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
809 if (err)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
810 return err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
811 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
812 base = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
813 assert(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
814 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
815
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
816 if (type->tp_dictoffset != base->tp_dictoffset) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
817 PyObject **dictptr = _PyObject_GetDictPtr(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
818 if (dictptr && *dictptr)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
819 Py_VISIT(*dictptr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
820 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
821
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
822 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
823 /* For a heaptype, the instances count as references
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
824 to the type. Traverse the type so the collector
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
825 can find cycles involving this link. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
826 Py_VISIT(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
827
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
828 if (basetraverse)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
829 return basetraverse(self, visit, arg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
830 return 0;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
831 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
832
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
833 static void
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
834 clear_slots(PyTypeObject *type, PyObject *self)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
835 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
836 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
837 PyMemberDef *mp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
838
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
839 n = Py_SIZE(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
840 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
841 for (i = 0; i < n; i++, mp++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
842 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
843 char *addr = (char *)self + mp->offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
844 PyObject *obj = *(PyObject **)addr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
845 if (obj != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
846 *(PyObject **)addr = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
847 Py_DECREF(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
848 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
849 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
850 }
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
851 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
852
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
853 static int
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
854 subtype_clear(PyObject *self)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
855 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
856 PyTypeObject *type, *base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
857 inquiry baseclear;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
858
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
859 /* Find the nearest base with a different tp_clear
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
860 and clear slots while we're at it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
861 type = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
862 base = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
863 while ((baseclear = base->tp_clear) == subtype_clear) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
864 if (Py_SIZE(base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
865 clear_slots(base, self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
866 base = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
867 assert(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
868 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
869
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
870 /* There's no need to clear the instance dict (if any);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
871 the collector will call its tp_clear handler. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
872
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
873 if (baseclear)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
874 return baseclear(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
875 return 0;
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
876 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
877
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
878 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
879 subtype_dealloc(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
880 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
881 PyTypeObject *type, *base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
882 destructor basedealloc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
883
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
884 /* Extract the type; we expect it to be a heap type */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
885 type = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
886 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
887
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
888 /* Test whether the type has GC exactly once */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
889
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
890 if (!PyType_IS_GC(type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
891 /* It's really rare to find a dynamic type that doesn't have
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
892 GC; it can only happen when deriving from 'object' and not
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
893 adding any slots or instance variables. This allows
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
894 certain simplifications: there's no need to call
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
895 clear_slots(), or DECREF the dict, or clear weakrefs. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
896
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
897 /* Maybe call finalizer; exit early if resurrected */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
898 if (type->tp_del) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
899 type->tp_del(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
900 if (self->ob_refcnt > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
901 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
902 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
903
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
904 /* Find the nearest base with a different tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
905 base = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
906 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
907 assert(Py_SIZE(base) == 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
908 base = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
909 assert(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
910 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
911
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
912 /* Extract the type again; tp_del may have changed it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
913 type = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
914
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
915 /* Call the base tp_dealloc() */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
916 assert(basedealloc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
917 basedealloc(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
918
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
919 /* Can't reference self beyond this point */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
920 Py_DECREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
921
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
922 /* Done */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
923 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
924 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
925
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
926 /* We get here only if the type has GC */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
927
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
928 /* UnTrack and re-Track around the trashcan macro, alas */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
929 /* See explanation at end of function for full disclosure */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
930 PyObject_GC_UnTrack(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
931 ++_PyTrash_delete_nesting;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
932 Py_TRASHCAN_SAFE_BEGIN(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
933 --_PyTrash_delete_nesting;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
934 /* DO NOT restore GC tracking at this point. weakref callbacks
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
935 * (if any, and whether directly here or indirectly in something we
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
936 * call) may trigger GC, and if self is tracked at that point, it
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
937 * will look like trash to GC and GC will try to delete self again.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
938 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
939
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
940 /* Find the nearest base with a different tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
941 base = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
942 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
943 base = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
944 assert(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
945 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
946
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
947 /* If we added a weaklist, we clear it. Do this *before* calling
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
948 the finalizer (__del__), clearing slots, or clearing the instance
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
949 dict. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
950
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
951 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
952 PyObject_ClearWeakRefs(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
953
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
954 /* Maybe call finalizer; exit early if resurrected */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
955 if (type->tp_del) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
956 _PyObject_GC_TRACK(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
957 type->tp_del(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
958 if (self->ob_refcnt > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
959 goto endlabel; /* resurrected */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
960 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
961 _PyObject_GC_UNTRACK(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
962 /* New weakrefs could be created during the finalizer call.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
963 If this occurs, clear them out without calling their
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
964 finalizers since they might rely on part of the object
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
965 being finalized that has already been destroyed. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
966 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
967 /* Modeled after GET_WEAKREFS_LISTPTR() */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
968 PyWeakReference **list = (PyWeakReference **) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
969 PyObject_GET_WEAKREFS_LISTPTR(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
970 while (*list)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
971 _PyWeakref_ClearRef(*list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
972 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
973 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
974
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
975 /* Clear slots up to the nearest base with a different tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
976 base = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
977 while (base->tp_dealloc == subtype_dealloc) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
978 if (Py_SIZE(base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
979 clear_slots(base, self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
980 base = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
981 assert(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
982 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
983
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
984 /* If we added a dict, DECREF it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
985 if (type->tp_dictoffset && !base->tp_dictoffset) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
986 PyObject **dictptr = _PyObject_GetDictPtr(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
987 if (dictptr != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
988 PyObject *dict = *dictptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
989 if (dict != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
990 Py_DECREF(dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
991 *dictptr = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
992 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
993 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
994 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
995
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
996 /* Extract the type again; tp_del may have changed it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
997 type = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
998
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
999 /* Call the base tp_dealloc(); first retrack self if
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1000 * basedealloc knows about gc.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1001 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1002 if (PyType_IS_GC(base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1003 _PyObject_GC_TRACK(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1004 assert(basedealloc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1005 basedealloc(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1006
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1007 /* Can't reference self beyond this point */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1008 Py_DECREF(type);
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
1009
24653
1ab27c1770df Fix a subtle bug in the trashcan code I added yesterday to
Guido van Rossum <guido@python.org>
parents: 24614
diff changeset
1010 endlabel:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1011 ++_PyTrash_delete_nesting;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1012 Py_TRASHCAN_SAFE_END(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1013 --_PyTrash_delete_nesting;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1014
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1015 /* Explanation of the weirdness around the trashcan macros:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1016
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1017 Q. What do the trashcan macros do?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1018
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1019 A. Read the comment titled "Trashcan mechanism" in object.h.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1020 For one, this explains why there must be a call to GC-untrack
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1021 before the trashcan begin macro. Without understanding the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1022 trashcan code, the answers to the following questions don't make
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1023 sense.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1024
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1025 Q. Why do we GC-untrack before the trashcan and then immediately
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1026 GC-track again afterward?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1027
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1028 A. In the case that the base class is GC-aware, the base class
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1029 probably GC-untracks the object. If it does that using the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1030 UNTRACK macro, this will crash when the object is already
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1031 untracked. Because we don't know what the base class does, the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1032 only safe thing is to make sure the object is tracked when we
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1033 call the base class dealloc. But... The trashcan begin macro
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1034 requires that the object is *untracked* before it is called. So
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1035 the dance becomes:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1036
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1037 GC untrack
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1038 trashcan begin
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1039 GC track
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1040
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1041 Q. Why did the last question say "immediately GC-track again"?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1042 It's nowhere near immediately.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1043
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1044 A. Because the code *used* to re-track immediately. Bad Idea.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1045 self has a refcount of 0, and if gc ever gets its hands on it
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1046 (which can happen if any weakref callback gets invoked), it
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1047 looks like trash to gc too, and gc also tries to delete self
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1048 then. But we're already deleting self. Double dealloction is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1049 a subtle disaster.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1050
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1051 Q. Why the bizarre (net-zero) manipulation of
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1052 _PyTrash_delete_nesting around the trashcan macros?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1053
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1054 A. Some base classes (e.g. list) also use the trashcan mechanism.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1055 The following scenario used to be possible:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1056
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1057 - suppose the trashcan level is one below the trashcan limit
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1058
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1059 - subtype_dealloc() is called
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1060
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1061 - the trashcan limit is not yet reached, so the trashcan level
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1062 is incremented and the code between trashcan begin and end is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1063 executed
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1064
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1065 - this destroys much of the object's contents, including its
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1066 slots and __dict__
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1067
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1068 - basedealloc() is called; this is really list_dealloc(), or
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1069 some other type which also uses the trashcan macros
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1070
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1071 - the trashcan limit is now reached, so the object is put on the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1072 trashcan's to-be-deleted-later list
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1073
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1074 - basedealloc() returns
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1075
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1076 - subtype_dealloc() decrefs the object's type
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1077
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1078 - subtype_dealloc() returns
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1079
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1080 - later, the trashcan code starts deleting the objects from its
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1081 to-be-deleted-later list
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1082
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1083 - subtype_dealloc() is called *AGAIN* for the same object
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1084
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1085 - at the very least (if the destroyed slots and __dict__ don't
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1086 cause problems) the object's type gets decref'ed a second
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1087 time, which is *BAD*!!!
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1088
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1089 The remedy is to make sure that if the code between trashcan
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1090 begin and end in subtype_dealloc() is called, the code between
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1091 trashcan begin and end in basedealloc() will also be called.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1092 This is done by decrementing the level after passing into the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1093 trashcan block, and incrementing it just before leaving the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1094 block.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1095
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1096 But now it's possible that a chain of objects consisting solely
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1097 of objects whose deallocator is subtype_dealloc() will defeat
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1098 the trashcan mechanism completely: the decremented level means
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1099 that the effective level never reaches the limit. Therefore, we
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1100 *increment* the level *before* entering the trashcan block, and
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1101 matchingly decrement it after leaving. This means the trashcan
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1102 code will trigger a little early, but that's no big deal.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1103
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1104 Q. Are there any live examples of code in need of all this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1105 complexity?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1106
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1107 A. Yes. See SF bug 668433 for code that crashed (when Python was
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1108 compiled in debug mode) before the trashcan level manipulations
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1109 were added. For more discussion, see SF patches 581742, 575073
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1110 and bug 574207.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1111 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1112 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1113
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1114 static PyTypeObject *solid_base(PyTypeObject *type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1115
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1116 /* type test with subclassing support */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1117
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1118 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1119 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1120 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1121 PyObject *mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1122
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1123 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1124 return b == a || b == &PyBaseObject_Type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1125
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1126 mro = a->tp_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1127 if (mro != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1128 /* Deal with multiple inheritance without recursion
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1129 by walking the MRO tuple */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1130 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1131 assert(PyTuple_Check(mro));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1132 n = PyTuple_GET_SIZE(mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1133 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1134 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1135 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1136 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1137 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1138 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1139 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1140 /* a is not completely initilized yet; follow tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1141 do {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1142 if (a == b)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1143 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1144 a = a->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1145 } while (a != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1146 return b == &PyBaseObject_Type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1147 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1148 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1149
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1150 /* Internal routines to do a method lookup in the type
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
1151 without looking in the instance dictionary
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
1152 (so we can't use PyObject_GetAttr) but still binding
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1153 it to the instance. The arguments are the object,
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
1154 the method name as a C string, and the address of a
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1155 static variable used to cache the interned Python string.
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1156
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1157 Two variants:
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1158
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1159 - lookup_maybe() returns NULL without raising an exception
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1160 when the _PyType_Lookup() call fails;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1161
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1162 - lookup_method() always raises an exception upon errors.
53904
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1163
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1164 - _PyObject_LookupSpecial() exported for the benefit of other places.
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1165 */
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
1166
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
1167 static PyObject *
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1168 lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
1169 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1170 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1171
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1172 if (*attrobj == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1173 *attrobj = PyString_InternFromString(attrstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1174 if (*attrobj == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1175 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1176 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1177 res = _PyType_Lookup(Py_TYPE(self), *attrobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1178 if (res != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1179 descrgetfunc f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1180 if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1181 Py_INCREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1182 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1183 res = f(res, self, (PyObject *)(Py_TYPE(self)));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1184 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1185 return res;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
1186 }
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
1187
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1188 static PyObject *
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1189 lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1190 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1191 PyObject *res = lookup_maybe(self, attrstr, attrobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1192 if (res == NULL && !PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1193 PyErr_SetObject(PyExc_AttributeError, *attrobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1194 return res;
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1195 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1196
53904
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1197 PyObject *
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1198 _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1199 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1200 assert(!PyInstance_Check(self));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1201 return lookup_maybe(self, attrstr, attrobj);
53904
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1202 }
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1203
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1204 /* A variation of PyObject_CallMethod that uses lookup_method()
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1205 instead of PyObject_GetAttrString(). This uses the same convention
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1206 as lookup_method to cache the interned name string object. */
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1207
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
1208 static PyObject *
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1209 call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1210 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1211 va_list va;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1212 PyObject *args, *func = 0, *retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1213 va_start(va, format);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1214
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1215 func = lookup_maybe(o, name, nameobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1216 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1217 va_end(va);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1218 if (!PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1219 PyErr_SetObject(PyExc_AttributeError, *nameobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1220 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1221 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1222
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1223 if (format && *format)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1224 args = Py_VaBuildValue(format, va);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1225 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1226 args = PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1227
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1228 va_end(va);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1229
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1230 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1231 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1232
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1233 assert(PyTuple_Check(args));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1234 retval = PyObject_Call(func, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1235
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1236 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1237 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1238
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1239 return retval;
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1240 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1241
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1242 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1243
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
1244 static PyObject *
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1245 call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1246 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1247 va_list va;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1248 PyObject *args, *func = 0, *retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1249 va_start(va, format);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1250
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1251 func = lookup_maybe(o, name, nameobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1252 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1253 va_end(va);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1254 if (!PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1255 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1256 return Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1257 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1258 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1259 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1260
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1261 if (format && *format)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1262 args = Py_VaBuildValue(format, va);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1263 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1264 args = PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1265
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1266 va_end(va);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1267
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1268 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1269 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1270
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1271 assert(PyTuple_Check(args));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1272 retval = PyObject_Call(func, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1273
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1274 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1275 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1276
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1277 return retval;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1278 }
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1279
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1280 static int
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1281 fill_classic_mro(PyObject *mro, PyObject *cls)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1282 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1283 PyObject *bases, *base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1284 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1285
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1286 assert(PyList_Check(mro));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1287 assert(PyClass_Check(cls));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1288 i = PySequence_Contains(mro, cls);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1289 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1290 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1291 if (!i) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1292 if (PyList_Append(mro, cls) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1293 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1294 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1295 bases = ((PyClassObject *)cls)->cl_bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1296 assert(bases && PyTuple_Check(bases));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1297 n = PyTuple_GET_SIZE(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1298 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1299 base = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1300 if (fill_classic_mro(mro, base) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1301 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1302 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1303 return 0;
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1304 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1305
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1306 static PyObject *
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1307 classic_mro(PyObject *cls)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1308 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1309 PyObject *mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1310
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1311 assert(PyClass_Check(cls));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1312 mro = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1313 if (mro != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1314 if (fill_classic_mro(mro, cls) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1315 return mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1316 Py_DECREF(mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1317 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1318 return NULL;
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1319 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1320
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1321 /*
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1322 Method resolution order algorithm C3 described in
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1323 "A Monotonic Superclass Linearization for Dylan",
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1324 by Kim Barrett, Bob Cassel, Paul Haahr,
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1325 David A. Moon, Keith Playford, and P. Tucker Withington.
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1326 (OOPSLA 1996)
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1327
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1328 Some notes about the rules implied by C3:
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1329
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1330 No duplicate bases.
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1331 It isn't legal to repeat a class in a list of base classes.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1332
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1333 The next three properties are the 3 constraints in "C3".
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1334
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1335 Local precendece order.
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1336 If A precedes B in C's MRO, then A will precede B in the MRO of all
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1337 subclasses of C.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1338
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1339 Monotonicity.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1340 The MRO of a class must be an extension without reordering of the
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1341 MRO of each of its superclasses.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1342
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1343 Extended Precedence Graph (EPG).
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1344 Linearization is consistent if there is a path in the EPG from
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1345 each class to all its successors in the linearization. See
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1346 the paper for definition of EPG.
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1347 */
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1348
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1349 static int
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1350 tail_contains(PyObject *list, int whence, PyObject *o) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1351 Py_ssize_t j, size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1352 size = PyList_GET_SIZE(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1353
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1354 for (j = whence+1; j < size; j++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1355 if (PyList_GET_ITEM(list, j) == o)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1356 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1357 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1358 return 0;
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1359 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1360
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1361 static PyObject *
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1362 class_name(PyObject *cls)
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1363 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1364 PyObject *name = PyObject_GetAttrString(cls, "__name__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1365 if (name == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1366 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1367 Py_XDECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1368 name = PyObject_Repr(cls);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1369 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1370 if (name == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1371 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1372 if (!PyString_Check(name)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1373 Py_DECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1374 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1375 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1376 return name;
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1377 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1378
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1379 static int
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1380 check_duplicates(PyObject *list)
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1381 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1382 Py_ssize_t i, j, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1383 /* Let's use a quadratic time algorithm,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1384 assuming that the bases lists is short.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1385 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1386 n = PyList_GET_SIZE(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1387 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1388 PyObject *o = PyList_GET_ITEM(list, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1389 for (j = i + 1; j < n; j++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1390 if (PyList_GET_ITEM(list, j) == o) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1391 o = class_name(o);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1392 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1393 "duplicate base class %s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1394 o ? PyString_AS_STRING(o) : "?");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1395 Py_XDECREF(o);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1396 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1397 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1398 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1399 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1400 return 0;
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1401 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1402
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1403 /* Raise a TypeError for an MRO order disagreement.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1404
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1405 It's hard to produce a good error message. In the absence of better
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1406 insight into error reporting, report the classes that were candidates
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1407 to be put next into the MRO. There is some conflict between the
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1408 order in which they should be put in the MRO, but it's hard to
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1409 diagnose what constraint can't be satisfied.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1410 */
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1411
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1412 static void
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1413 set_mro_error(PyObject *to_merge, int *remain)
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1414 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1415 Py_ssize_t i, n, off, to_merge_size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1416 char buf[1000];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1417 PyObject *k, *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1418 PyObject *set = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1419 if (!set) return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1420
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1421 to_merge_size = PyList_GET_SIZE(to_merge);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1422 for (i = 0; i < to_merge_size; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1423 PyObject *L = PyList_GET_ITEM(to_merge, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1424 if (remain[i] < PyList_GET_SIZE(L)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1425 PyObject *c = PyList_GET_ITEM(L, remain[i]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1426 if (PyDict_SetItem(set, c, Py_None) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1427 Py_DECREF(set);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1428 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1429 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1430 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1431 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1432 n = PyDict_Size(set);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1433
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1434 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
28514
9400504cb0b2 SF bug #699934: Obscure error message
Raymond Hettinger <python@rcn.com>
parents: 28403
diff changeset
1435 consistent method resolution\norder (MRO) for bases");
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1436 i = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1437 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1438 PyObject *name = class_name(k);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1439 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1440 name ? PyString_AS_STRING(name) : "?");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1441 Py_XDECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1442 if (--n && (size_t)(off+1) < sizeof(buf)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1443 buf[off++] = ',';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1444 buf[off] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1445 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1446 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1447 PyErr_SetString(PyExc_TypeError, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1448 Py_DECREF(set);
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1449 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1450
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1451 static int
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1452 pmerge(PyObject *acc, PyObject* to_merge) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1453 Py_ssize_t i, j, to_merge_size, empty_cnt;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1454 int *remain;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1455 int ok;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1456
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1457 to_merge_size = PyList_GET_SIZE(to_merge);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1458
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1459 /* remain stores an index into each sublist of to_merge.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1460 remain[i] is the index of the next base in to_merge[i]
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1461 that is not included in acc.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1462 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1463 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1464 if (remain == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1465 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1466 for (i = 0; i < to_merge_size; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1467 remain[i] = 0;
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1468
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1469 again:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1470 empty_cnt = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1471 for (i = 0; i < to_merge_size; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1472 PyObject *candidate;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1473
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1474 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1475
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1476 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1477 empty_cnt++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1478 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1479 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1480
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1481 /* Choose next candidate for MRO.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1482
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1483 The input sequences alone can determine the choice.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1484 If not, choose the class which appears in the MRO
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1485 of the earliest direct superclass of the new class.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1486 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1487
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1488 candidate = PyList_GET_ITEM(cur_list, remain[i]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1489 for (j = 0; j < to_merge_size; j++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1490 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1491 if (tail_contains(j_lst, remain[j], candidate)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1492 goto skip; /* continue outer loop */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1493 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1494 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1495 ok = PyList_Append(acc, candidate);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1496 if (ok < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1497 PyMem_Free(remain);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1498 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1499 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1500 for (j = 0; j < to_merge_size; j++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1501 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1502 if (remain[j] < PyList_GET_SIZE(j_lst) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1503 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1504 remain[j]++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1505 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1506 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1507 goto again;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1508 skip: ;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1509 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1510
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1511 if (empty_cnt == to_merge_size) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1512 PyMem_FREE(remain);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1513 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1514 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1515 set_mro_error(to_merge, remain);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1516 PyMem_FREE(remain);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1517 return -1;
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1518 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1519
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1520 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1521 mro_implementation(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1522 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1523 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1524 int ok;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1525 PyObject *bases, *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1526 PyObject *to_merge, *bases_aslist;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1527
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1528 if (type->tp_dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1529 if (PyType_Ready(type) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1530 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1531 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1532
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1533 /* Find a superclass linearization that honors the constraints
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1534 of the explicit lists of bases and the constraints implied by
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1535 each base class.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1536
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1537 to_merge is a list of lists, where each list is a superclass
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1538 linearization implied by a base class. The last element of
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1539 to_merge is the declared list of bases.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1540 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1541
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1542 bases = type->tp_bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1543 n = PyTuple_GET_SIZE(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1544
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1545 to_merge = PyList_New(n+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1546 if (to_merge == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1547 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1548
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1549 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1550 PyObject *base = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1551 PyObject *parentMRO;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1552 if (PyType_Check(base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1553 parentMRO = PySequence_List(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1554 ((PyTypeObject*)base)->tp_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1555 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1556 parentMRO = classic_mro(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1557 if (parentMRO == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1558 Py_DECREF(to_merge);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1559 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1560 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1561
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1562 PyList_SET_ITEM(to_merge, i, parentMRO);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1563 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1564
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1565 bases_aslist = PySequence_List(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1566 if (bases_aslist == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1567 Py_DECREF(to_merge);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1568 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1569 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1570 /* This is just a basic sanity check. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1571 if (check_duplicates(bases_aslist) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1572 Py_DECREF(to_merge);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1573 Py_DECREF(bases_aslist);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1574 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1575 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1576 PyList_SET_ITEM(to_merge, n, bases_aslist);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1577
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1578 result = Py_BuildValue("[O]", (PyObject *)type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1579 if (result == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1580 Py_DECREF(to_merge);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1581 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1582 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1583
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1584 ok = pmerge(result, to_merge);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1585 Py_DECREF(to_merge);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1586 if (ok < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1587 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1588 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1589 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1590
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1591 return result;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1592 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1593
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1594 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19300
diff changeset
1595 mro_external(PyObject *self)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1596 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1597 PyTypeObject *type = (PyTypeObject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1598
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1599 return mro_implementation(type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1600 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1601
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1602 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1603 mro_internal(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1604 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1605 PyObject *mro, *result, *tuple;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1606 int checkit = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1607
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1608 if (Py_TYPE(type) == &PyType_Type) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1609 result = mro_implementation(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1610 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1611 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1612 static PyObject *mro_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1613 checkit = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1614 mro = lookup_method((PyObject *)type, "mro", &mro_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1615 if (mro == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1616 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1617 result = PyObject_CallObject(mro, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1618 Py_DECREF(mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1619 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1620 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1621 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1622 tuple = PySequence_Tuple(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1623 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1624 if (tuple == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1625 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1626 if (checkit) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1627 Py_ssize_t i, len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1628 PyObject *cls;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1629 PyTypeObject *solid;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1630
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1631 solid = solid_base(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1632
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1633 len = PyTuple_GET_SIZE(tuple);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1634
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1635 for (i = 0; i < len; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1636 PyTypeObject *t;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1637 cls = PyTuple_GET_ITEM(tuple, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1638 if (PyClass_Check(cls))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1639 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1640 else if (!PyType_Check(cls)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1641 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1642 "mro() returned a non-class ('%.500s')",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1643 Py_TYPE(cls)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1644 Py_DECREF(tuple);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1645 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1646 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1647 t = (PyTypeObject*)cls;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1648 if (!PyType_IsSubtype(solid, solid_base(t))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1649 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1650 "mro() returned base with unsuitable layout ('%.500s')",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1651 t->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1652 Py_DECREF(tuple);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1653 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1654 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1655 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1656 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1657 type->tp_mro = tuple;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1658
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1659 type_mro_modified(type, type->tp_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1660 /* corner case: the old-style super class might have been hidden
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1661 from the custom MRO */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1662 type_mro_modified(type, type->tp_bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1663
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1664 PyType_Modified(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1665
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1666 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1667 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1668
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1669
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1670 /* Calculate the best base amongst multiple base classes.
41488
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
1671 This is the first one that's on the path to the "solid base". */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1672
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1673 static PyTypeObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1674 best_base(PyObject *bases)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1675 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1676 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1677 PyTypeObject *base, *winner, *candidate, *base_i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1678 PyObject *base_proto;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1679
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1680 assert(PyTuple_Check(bases));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1681 n = PyTuple_GET_SIZE(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1682 assert(n > 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1683 base = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1684 winner = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1685 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1686 base_proto = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1687 if (PyClass_Check(base_proto))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1688 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1689 if (!PyType_Check(base_proto)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1690 PyErr_SetString(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1691 PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1692 "bases must be types");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1693 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1694 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1695 base_i = (PyTypeObject *)base_proto;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1696 if (base_i->tp_dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1697 if (PyType_Ready(base_i) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1698 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1699 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1700 candidate = solid_base(base_i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1701 if (winner == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1702 winner = candidate;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1703 base = base_i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1704 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1705 else if (PyType_IsSubtype(winner, candidate))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1706 ;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1707 else if (PyType_IsSubtype(candidate, winner)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1708 winner = candidate;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1709 base = base_i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1710 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1711 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1712 PyErr_SetString(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1713 PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1714 "multiple bases have "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1715 "instance lay-out conflict");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1716 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1717 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1718 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1719 if (base == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1720 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1721 "a new-style class can't have only classic bases");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1722 return base;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1723 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1724
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1725 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1726 extra_ivars(PyTypeObject *type, PyTypeObject *base)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1727 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1728 size_t t_size = type->tp_basicsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1729 size_t b_size = base->tp_basicsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1730
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1731 assert(t_size >= b_size); /* Else type smaller than base! */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1732 if (type->tp_itemsize || base->tp_itemsize) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1733 /* If itemsize is involved, stricter rules */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1734 return t_size != b_size ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1735 type->tp_itemsize != base->tp_itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1736 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1737 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1738 type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1739 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1740 t_size -= sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1741 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1742 type->tp_dictoffset + sizeof(PyObject *) == t_size &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1743 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1744 t_size -= sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1745
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1746 return t_size != b_size;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1747 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1748
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1749 static PyTypeObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1750 solid_base(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1751 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1752 PyTypeObject *base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1753
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1754 if (type->tp_base)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1755 base = solid_base(type->tp_base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1756 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1757 base = &PyBaseObject_Type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1758 if (extra_ivars(type, base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1759 return type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1760 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1761 return base;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1762 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1763
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1764 static void object_dealloc(PyObject *);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1765 static int object_init(PyObject *, PyObject *, PyObject *);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1766 static int update_slot(PyTypeObject *, PyObject *);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1767 static void fixup_slot_dispatchers(PyTypeObject *);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1768
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1769 /*
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1770 * Helpers for __dict__ descriptor. We don't want to expose the dicts
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1771 * inherited from various builtin types. The builtin base usually provides
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1772 * its own __dict__ descriptor, so we use that when we can.
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1773 */
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1774 static PyTypeObject *
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1775 get_builtin_base_with_dict(PyTypeObject *type)
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1776 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1777 while (type->tp_base != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1778 if (type->tp_dictoffset != 0 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1779 !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1780 return type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1781 type = type->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1782 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1783 return NULL;
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1784 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1785
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1786 static PyObject *
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1787 get_dict_descriptor(PyTypeObject *type)
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1788 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1789 static PyObject *dict_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1790 PyObject *descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1791
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1792 if (dict_str == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1793 dict_str = PyString_InternFromString("__dict__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1794 if (dict_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1795 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1796 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1797 descr = _PyType_Lookup(type, dict_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1798 if (descr == NULL || !PyDescr_IsData(descr))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1799 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1800
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1801 return descr;
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1802 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1803
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1804 static void
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1805 raise_dict_descr_error(PyObject *obj)
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1806 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1807 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1808 "this __dict__ descriptor does not support "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1809 "'%.200s' objects", obj->ob_type->tp_name);
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1810 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1811
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1812 static PyObject *
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1813 subtype_dict(PyObject *obj, void *context)
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1814 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1815 PyObject **dictptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1816 PyObject *dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1817 PyTypeObject *base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1818
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1819 base = get_builtin_base_with_dict(obj->ob_type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1820 if (base != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1821 descrgetfunc func;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1822 PyObject *descr = get_dict_descriptor(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1823 if (descr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1824 raise_dict_descr_error(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1825 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1826 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1827 func = descr->ob_type->tp_descr_get;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1828 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1829 raise_dict_descr_error(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1830 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1831 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1832 return func(descr, obj, (PyObject *)(obj->ob_type));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1833 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1834
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1835 dictptr = _PyObject_GetDictPtr(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1836 if (dictptr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1837 PyErr_SetString(PyExc_AttributeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1838 "This object has no __dict__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1839 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1840 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1841 dict = *dictptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1842 if (dict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1843 *dictptr = dict = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1844 Py_XINCREF(dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1845 return dict;
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1846 }
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1847
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1848 static int
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1849 subtype_setdict(PyObject *obj, PyObject *value, void *context)
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1850 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1851 PyObject **dictptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1852 PyObject *dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1853 PyTypeObject *base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1854
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1855 base = get_builtin_base_with_dict(obj->ob_type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1856 if (base != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1857 descrsetfunc func;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1858 PyObject *descr = get_dict_descriptor(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1859 if (descr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1860 raise_dict_descr_error(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1861 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1862 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1863 func = descr->ob_type->tp_descr_set;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1864 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1865 raise_dict_descr_error(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1866 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1867 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1868 return func(descr, obj, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1869 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1870
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1871 dictptr = _PyObject_GetDictPtr(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1872 if (dictptr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1873 PyErr_SetString(PyExc_AttributeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1874 "This object has no __dict__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1875 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1876 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1877 if (value != NULL && !PyDict_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1878 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1879 "__dict__ must be set to a dictionary, "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1880 "not a '%.200s'", Py_TYPE(value)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1881 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1882 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1883 dict = *dictptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1884 Py_XINCREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1885 *dictptr = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1886 Py_XDECREF(dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1887 return 0;
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1888 }
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1889
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1890 static PyObject *
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1891 subtype_getweakref(PyObject *obj, void *context)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1892 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1893 PyObject **weaklistptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1894 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1895
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1896 if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1897 PyErr_SetString(PyExc_AttributeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1898 "This object has no __weakref__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1899 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1900 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1901 assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1902 assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1903 (size_t)(Py_TYPE(obj)->tp_basicsize));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1904 weaklistptr = (PyObject **)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1905 ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1906 if (*weaklistptr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1907 result = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1908 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1909 result = *weaklistptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1910 Py_INCREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1911 return result;
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1912 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1913
27132
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1914 /* Three variants on the subtype_getsets list. */
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1915
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1916 static PyGetSetDef subtype_getsets_full[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1917 {"__dict__", subtype_dict, subtype_setdict,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1918 PyDoc_STR("dictionary for instance variables (if defined)")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1919 {"__weakref__", subtype_getweakref, NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1920 PyDoc_STR("list of weak references to the object (if defined)")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1921 {0}
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1922 };
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1923
27132
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1924 static PyGetSetDef subtype_getsets_dict_only[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1925 {"__dict__", subtype_dict, subtype_setdict,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1926 PyDoc_STR("dictionary for instance variables (if defined)")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1927 {0}
27132
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1928 };
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1929
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1930 static PyGetSetDef subtype_getsets_weakref_only[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1931 {"__weakref__", subtype_getweakref, NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1932 PyDoc_STR("list of weak references to the object (if defined)")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1933 {0}
27132
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1934 };
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1935
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1936 static int
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1937 valid_identifier(PyObject *s)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1938 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1939 unsigned char *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1940 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1941
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1942 if (!PyString_Check(s)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1943 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1944 "__slots__ items must be strings, not '%.200s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1945 Py_TYPE(s)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1946 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1947 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1948 p = (unsigned char *) PyString_AS_STRING(s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1949 n = PyString_GET_SIZE(s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1950 /* We must reject an empty name. As a hack, we bump the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1951 length to 1 so that the loop will balk on the trailing \0. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1952 if (n == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1953 n = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1954 for (i = 0; i < n; i++, p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1955 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1956 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1957 "__slots__ must be identifiers");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1958 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1959 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1960 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1961 return 1;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1962 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1963
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1964 #ifdef Py_USING_UNICODE
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1965 /* Replace Unicode objects in slots. */
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1966
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1967 static PyObject *
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
1968 _unicode_to_string(PyObject *slots, Py_ssize_t nslots)
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1969 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1970 PyObject *tmp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1971 PyObject *slot_name, *new_name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1972 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1973
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1974 for (i = 0; i < nslots; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1975 if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1976 if (tmp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1977 tmp = PySequence_List(slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1978 if (tmp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1979 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1980 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1981 new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1982 NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1983 if (new_name == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1984 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1985 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1986 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1987 Py_INCREF(new_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1988 PyList_SET_ITEM(tmp, i, new_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1989 Py_DECREF(slot_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1990 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1991 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1992 if (tmp != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1993 slots = PyList_AsTuple(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1994 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1995 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
1996 return slots;
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1997 }
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1998 #endif
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1999
41287
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2000 /* Forward */
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2001 static int
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2002 object_init(PyObject *self, PyObject *args, PyObject *kwds);
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2003
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2004 static int
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2005 type_init(PyObject *cls, PyObject *args, PyObject *kwds)
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2006 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2007 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2008
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2009 assert(args != NULL && PyTuple_Check(args));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2010 assert(kwds == NULL || PyDict_Check(kwds));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2011
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2012 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2013 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2014 "type.__init__() takes no keyword arguments");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2015 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2016 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2017
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2018 if (args != NULL && PyTuple_Check(args) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2019 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2020 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2021 "type.__init__() takes 1 or 3 arguments");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2022 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2023 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2024
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2025 /* Call object.__init__(self) now. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2026 /* XXX Could call super(type, cls).__init__() but what's the point? */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2027 args = PyTuple_GetSlice(args, 0, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2028 res = object_init(cls, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2029 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2030 return res;
41287
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2031 }
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2032
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
2033 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2034 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2035 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2036 PyObject *name, *bases, *dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2037 static char *kwlist[] = {"name", "bases", "dict", 0};
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2038 PyObject *slots, *tmp, *newslots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2039 PyTypeObject *type, *base, *tmptype, *winner;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2040 PyHeapTypeObject *et;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2041 PyMemberDef *mp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2042 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2043 int j, may_add_dict, may_add_weak;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2044
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2045 assert(args != NULL && PyTuple_Check(args));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2046 assert(kwds == NULL || PyDict_Check(kwds));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2047
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2048 /* Special case: type(x) should return x->ob_type */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2049 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2050 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2051 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2052
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2053 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2054 PyObject *x = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2055 Py_INCREF(Py_TYPE(x));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2056 return (PyObject *) Py_TYPE(x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2057 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2058
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2059 /* SF bug 475327 -- if that didn't trigger, we need 3
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2060 arguments. but PyArg_ParseTupleAndKeywords below may give
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2061 a msg saying type() needs exactly 3. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2062 if (nargs + nkwds != 3) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2063 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2064 "type() takes 1 or 3 arguments");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2065 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2066 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2067 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2068
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2069 /* Check arguments: (name, bases, dict) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2070 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2071 &name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2072 &PyTuple_Type, &bases,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2073 &PyDict_Type, &dict))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2074 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2075
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2076 /* Determine the proper metatype to deal with this,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2077 and check for metatype conflicts while we're at it.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2078 Note that if some other metatype wins to contract,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2079 it's possible that its instances are not types. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2080 nbases = PyTuple_GET_SIZE(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2081 winner = metatype;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2082 for (i = 0; i < nbases; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2083 tmp = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2084 tmptype = tmp->ob_type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2085 if (tmptype == &PyClass_Type)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2086 continue; /* Special case classic classes */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2087 if (PyType_IsSubtype(winner, tmptype))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2088 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2089 if (PyType_IsSubtype(tmptype, winner)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2090 winner = tmptype;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2091 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2092 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2093 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2094 "metaclass conflict: "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2095 "the metaclass of a derived class "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2096 "must be a (non-strict) subclass "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2097 "of the metaclasses of all its bases");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2098 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2099 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2100 if (winner != metatype) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2101 if (winner->tp_new != type_new) /* Pass it to the winner */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2102 return winner->tp_new(winner, args, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2103 metatype = winner;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2104 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2105
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2106 /* Adjust for empty tuple bases */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2107 if (nbases == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2108 bases = PyTuple_Pack(1, &PyBaseObject_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2109 if (bases == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2110 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2111 nbases = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2112 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2113 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2114 Py_INCREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2115
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2116 /* XXX From here until type is allocated, "return NULL" leaks bases! */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2117
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2118 /* Calculate best base, and check that all bases are type objects */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2119 base = best_base(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2120 if (base == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2121 Py_DECREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2122 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2123 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2124 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2125 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2126 "type '%.100s' is not an acceptable base type",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2127 base->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2128 Py_DECREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2129 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2130 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2131
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2132 /* Check for a __slots__ sequence variable in dict, and count it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2133 slots = PyDict_GetItemString(dict, "__slots__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2134 nslots = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2135 add_dict = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2136 add_weak = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2137 may_add_dict = base->tp_dictoffset == 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2138 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2139 if (slots == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2140 if (may_add_dict) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2141 add_dict++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2142 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2143 if (may_add_weak) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2144 add_weak++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2145 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2146 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2147 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2148 /* Have slots */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2149
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2150 /* Make it into a tuple */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2151 if (PyString_Check(slots) || PyUnicode_Check(slots))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2152 slots = PyTuple_Pack(1, slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2153 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2154 slots = PySequence_Tuple(slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2155 if (slots == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2156 Py_DECREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2157 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2158 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2159 assert(PyTuple_Check(slots));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2160
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2161 /* Are slots allowed? */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2162 nslots = PyTuple_GET_SIZE(slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2163 if (nslots > 0 && base->tp_itemsize != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2164 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2165 "nonempty __slots__ "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2166 "not supported for subtype of '%s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2167 base->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2168 bad_slots:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2169 Py_DECREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2170 Py_DECREF(slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2171 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2172 }
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2173
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
2174 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2175 tmp = _unicode_to_string(slots, nslots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2176 if (tmp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2177 goto bad_slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2178 if (tmp != slots) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2179 Py_DECREF(slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2180 slots = tmp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2181 }
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
2182 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2183 /* Check for valid slot names and two special cases */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2184 for (i = 0; i < nslots; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2185 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2186 char *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2187 if (!valid_identifier(tmp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2188 goto bad_slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2189 assert(PyString_Check(tmp));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2190 s = PyString_AS_STRING(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2191 if (strcmp(s, "__dict__") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2192 if (!may_add_dict || add_dict) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2193 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2194 "__dict__ slot disallowed: "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2195 "we already got one");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2196 goto bad_slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2197 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2198 add_dict++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2199 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2200 if (strcmp(s, "__weakref__") == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2201 if (!may_add_weak || add_weak) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2202 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2203 "__weakref__ slot disallowed: "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2204 "either we already got one, "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2205 "or __itemsize__ != 0");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2206 goto bad_slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2207 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2208 add_weak++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2209 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2210 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2211
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2212 /* Copy slots into a list, mangle names and sort them.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2213 Sorted names are needed for __class__ assignment.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2214 Convert them back to tuple at the end.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2215 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2216 newslots = PyList_New(nslots - add_dict - add_weak);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2217 if (newslots == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2218 goto bad_slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2219 for (i = j = 0; i < nslots; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2220 char *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2221 tmp = PyTuple_GET_ITEM(slots, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2222 s = PyString_AS_STRING(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2223 if ((add_dict && strcmp(s, "__dict__") == 0) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2224 (add_weak && strcmp(s, "__weakref__") == 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2225 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2226 tmp =_Py_Mangle(name, tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2227 if (!tmp)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2228 goto bad_slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2229 PyList_SET_ITEM(newslots, j, tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2230 j++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2231 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2232 assert(j == nslots - add_dict - add_weak);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2233 nslots = j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2234 Py_DECREF(slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2235 if (PyList_Sort(newslots) == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2236 Py_DECREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2237 Py_DECREF(newslots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2238 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2239 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2240 slots = PyList_AsTuple(newslots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2241 Py_DECREF(newslots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2242 if (slots == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2243 Py_DECREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2244 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2245 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2246
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2247 /* Secondary bases may provide weakrefs or dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2248 if (nbases > 1 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2249 ((may_add_dict && !add_dict) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2250 (may_add_weak && !add_weak))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2251 for (i = 0; i < nbases; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2252 tmp = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2253 if (tmp == (PyObject *)base)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2254 continue; /* Skip primary base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2255 if (PyClass_Check(tmp)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2256 /* Classic base class provides both */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2257 if (may_add_dict && !add_dict)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2258 add_dict++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2259 if (may_add_weak && !add_weak)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2260 add_weak++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2261 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2262 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2263 assert(PyType_Check(tmp));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2264 tmptype = (PyTypeObject *)tmp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2265 if (may_add_dict && !add_dict &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2266 tmptype->tp_dictoffset != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2267 add_dict++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2268 if (may_add_weak && !add_weak &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2269 tmptype->tp_weaklistoffset != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2270 add_weak++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2271 if (may_add_dict && !add_dict)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2272 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2273 if (may_add_weak && !add_weak)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2274 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2275 /* Nothing more to check */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2276 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2277 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2278 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2279 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2280
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2281 /* XXX From here until type is safely allocated,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2282 "return NULL" may leak slots! */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2283
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2284 /* Allocate the type object */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2285 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2286 if (type == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2287 Py_XDECREF(slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2288 Py_DECREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2289 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2290 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2291
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2292 /* Keep name and slots alive in the extended type object */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2293 et = (PyHeapTypeObject *)type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2294 Py_INCREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2295 et->ht_name = name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2296 et->ht_slots = slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2297
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2298 /* Initialize tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2299 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2300 Py_TPFLAGS_BASETYPE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2301 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2302 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2303 if (base->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2304 type->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2305
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2306 /* It's a new-style number unless it specifically inherits any
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2307 old-style numeric behavior */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2308 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2309 (base->tp_as_number == NULL))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2310 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2311
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2312 /* Initialize essential fields */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2313 type->tp_as_number = &et->as_number;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2314 type->tp_as_sequence = &et->as_sequence;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2315 type->tp_as_mapping = &et->as_mapping;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2316 type->tp_as_buffer = &et->as_buffer;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2317 type->tp_name = PyString_AS_STRING(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2318
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2319 /* Set tp_base and tp_bases */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2320 type->tp_bases = bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2321 Py_INCREF(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2322 type->tp_base = base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2323
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2324 /* Initialize tp_dict from passed-in dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2325 type->tp_dict = dict = PyDict_Copy(dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2326 if (dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2327 Py_DECREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2328 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2329 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2330
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2331 /* Set __module__ in the dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2332 if (PyDict_GetItemString(dict, "__module__") == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2333 tmp = PyEval_GetGlobals();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2334 if (tmp != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2335 tmp = PyDict_GetItemString(tmp, "__name__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2336 if (tmp != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2337 if (PyDict_SetItemString(dict, "__module__",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2338 tmp) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2339 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2340 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2341 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2342 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2343
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2344 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2345 and is a string. The __doc__ accessor will first look for tp_doc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2346 if that fails, it will still look into __dict__.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2347 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2348 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2349 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2350 if (doc != NULL && PyString_Check(doc)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2351 const size_t n = (size_t)PyString_GET_SIZE(doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2352 char *tp_doc = (char *)PyObject_MALLOC(n+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2353 if (tp_doc == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2354 Py_DECREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2355 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2356 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2357 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2358 type->tp_doc = tp_doc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2359 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2360 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2361
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2362 /* Special-case __new__: if it's a plain function,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2363 make it a static function */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2364 tmp = PyDict_GetItemString(dict, "__new__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2365 if (tmp != NULL && PyFunction_Check(tmp)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2366 tmp = PyStaticMethod_New(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2367 if (tmp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2368 Py_DECREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2369 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2370 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2371 PyDict_SetItemString(dict, "__new__", tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2372 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2373 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2374
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2375 /* Add descriptors for custom slots from __slots__, or for __dict__ */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2376 mp = PyHeapType_GET_MEMBERS(et);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2377 slotoffset = base->tp_basicsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2378 if (slots != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2379 for (i = 0; i < nslots; i++, mp++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2380 mp->name = PyString_AS_STRING(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2381 PyTuple_GET_ITEM(slots, i));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2382 mp->type = T_OBJECT_EX;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2383 mp->offset = slotoffset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2384
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2385 /* __dict__ and __weakref__ are already filtered out */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2386 assert(strcmp(mp->name, "__dict__") != 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2387 assert(strcmp(mp->name, "__weakref__") != 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2388
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2389 slotoffset += sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2390 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2391 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2392 if (add_dict) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2393 if (base->tp_itemsize)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2394 type->tp_dictoffset = -(long)sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2395 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2396 type->tp_dictoffset = slotoffset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2397 slotoffset += sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2398 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2399 if (add_weak) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2400 assert(!base->tp_itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2401 type->tp_weaklistoffset = slotoffset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2402 slotoffset += sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2403 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2404 type->tp_basicsize = slotoffset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2405 type->tp_itemsize = base->tp_itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2406 type->tp_members = PyHeapType_GET_MEMBERS(et);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2407
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2408 if (type->tp_weaklistoffset && type->tp_dictoffset)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2409 type->tp_getset = subtype_getsets_full;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2410 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2411 type->tp_getset = subtype_getsets_weakref_only;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2412 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2413 type->tp_getset = subtype_getsets_dict_only;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2414 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2415 type->tp_getset = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2416
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2417 /* Special case some slots */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2418 if (type->tp_dictoffset != 0 || nslots > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2419 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2420 type->tp_getattro = PyObject_GenericGetAttr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2421 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2422 type->tp_setattro = PyObject_GenericSetAttr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2423 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2424 type->tp_dealloc = subtype_dealloc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2425
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2426 /* Enable GC unless there are really no instance variables possible */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2427 if (!(type->tp_basicsize == sizeof(PyObject) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2428 type->tp_itemsize == 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2429 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2430
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2431 /* Always override allocation strategy to use regular heap */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2432 type->tp_alloc = PyType_GenericAlloc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2433 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2434 type->tp_free = PyObject_GC_Del;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2435 type->tp_traverse = subtype_traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2436 type->tp_clear = subtype_clear;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2437 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2438 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2439 type->tp_free = PyObject_Del;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2440
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2441 /* Initialize the rest */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2442 if (PyType_Ready(type) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2443 Py_DECREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2444 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2445 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2446
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2447 /* Put the proper slots in place */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2448 fixup_slot_dispatchers(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2449
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2450 return (PyObject *)type;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2451 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2452
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2453 /* Internal API to look for a name through the MRO.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2454 This returns a borrowed reference, and doesn't set an exception! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2455 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2456 _PyType_Lookup(PyTypeObject *type, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2457 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2458 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2459 PyObject *mro, *res, *base, *dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2460 unsigned int h;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2461
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2462 if (MCACHE_CACHEABLE_NAME(name) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2463 PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2464 /* fast path */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2465 h = MCACHE_HASH_METHOD(type, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2466 if (method_cache[h].version == type->tp_version_tag &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2467 method_cache[h].name == name)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2468 return method_cache[h].value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2469 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2470
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2471 /* Look in tp_dict of types in MRO */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2472 mro = type->tp_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2473
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2474 /* If mro is NULL, the type is either not yet initialized
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2475 by PyType_Ready(), or already cleared by type_clear().
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2476 Either way the safest thing to do is to return NULL. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2477 if (mro == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2478 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2479
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2480 res = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2481 assert(PyTuple_Check(mro));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2482 n = PyTuple_GET_SIZE(mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2483 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2484 base = PyTuple_GET_ITEM(mro, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2485 if (PyClass_Check(base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2486 dict = ((PyClassObject *)base)->cl_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2487 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2488 assert(PyType_Check(base));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2489 dict = ((PyTypeObject *)base)->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2490 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2491 assert(dict && PyDict_Check(dict));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2492 res = PyDict_GetItem(dict, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2493 if (res != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2494 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2495 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2496
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2497 if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2498 h = MCACHE_HASH_METHOD(type, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2499 method_cache[h].version = type->tp_version_tag;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2500 method_cache[h].value = res; /* borrowed */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2501 Py_INCREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2502 Py_DECREF(method_cache[h].name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2503 method_cache[h].name = name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2504 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2505 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2506 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2507
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2508 /* This is similar to PyObject_GenericGetAttr(),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2509 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2510 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2511 type_getattro(PyTypeObject *type, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2512 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2513 PyTypeObject *metatype = Py_TYPE(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2514 PyObject *meta_attribute, *attribute;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2515 descrgetfunc meta_get;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2516
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2517 /* Initialize this type (we'll assume the metatype is initialized) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2518 if (type->tp_dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2519 if (PyType_Ready(type) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2520 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2521 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2522
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2523 /* No readable descriptor found yet */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2524 meta_get = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2525
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2526 /* Look for the attribute in the metatype */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2527 meta_attribute = _PyType_Lookup(metatype, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2528
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2529 if (meta_attribute != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2530 meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2531
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2532 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2533 /* Data descriptors implement tp_descr_set to intercept
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2534 * writes. Assume the attribute is not overridden in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2535 * type's tp_dict (and bases): call the descriptor now.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2536 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2537 return meta_get(meta_attribute, (PyObject *)type,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2538 (PyObject *)metatype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2539 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2540 Py_INCREF(meta_attribute);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2541 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2542
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2543 /* No data descriptor found on metatype. Look in tp_dict of this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2544 * type and its bases */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2545 attribute = _PyType_Lookup(type, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2546 if (attribute != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2547 /* Implement descriptor functionality, if any */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2548 descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2549
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2550 Py_XDECREF(meta_attribute);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2551
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2552 if (local_get != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2553 /* NULL 2nd argument indicates the descriptor was
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2554 * found on the target object itself (or a base) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2555 return local_get(attribute, (PyObject *)NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2556 (PyObject *)type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2557 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2558
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2559 Py_INCREF(attribute);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2560 return attribute;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2561 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2562
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2563 /* No attribute found in local __dict__ (or bases): use the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2564 * descriptor from the metatype, if any */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2565 if (meta_get != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2566 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2567 res = meta_get(meta_attribute, (PyObject *)type,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2568 (PyObject *)metatype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2569 Py_DECREF(meta_attribute);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2570 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2571 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2572
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2573 /* If an ordinary attribute was found on the metatype, return it now */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2574 if (meta_attribute != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2575 return meta_attribute;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2576 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2577
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2578 /* Give up */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2579 PyErr_Format(PyExc_AttributeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2580 "type object '%.50s' has no attribute '%.400s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2581 type->tp_name, PyString_AS_STRING(name));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2582 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2583 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2584
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2585 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2586 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2587 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2588 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2589 PyErr_Format(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2590 PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2591 "can't set attributes of built-in/extension type '%s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2592 type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2593 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2594 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2595 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2596 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2597 return update_slot(type, name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2598 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2599
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2600 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2601 type_dealloc(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2602 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2603 PyHeapTypeObject *et;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2604
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2605 /* Assert this is a heap-allocated type object */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2606 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2607 _PyObject_GC_UNTRACK(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2608 PyObject_ClearWeakRefs((PyObject *)type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2609 et = (PyHeapTypeObject *)type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2610 Py_XDECREF(type->tp_base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2611 Py_XDECREF(type->tp_dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2612 Py_XDECREF(type->tp_bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2613 Py_XDECREF(type->tp_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2614 Py_XDECREF(type->tp_cache);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2615 Py_XDECREF(type->tp_subclasses);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2616 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2617 * of most other objects. It's okay to cast it to char *.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2618 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2619 PyObject_Free((char *)type->tp_doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2620 Py_XDECREF(et->ht_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2621 Py_XDECREF(et->ht_slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2622 Py_TYPE(type)->tp_free((PyObject *)type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2623 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2624
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2625 static PyObject *
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2626 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2627 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2628 PyObject *list, *raw, *ref;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2629 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2630
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2631 list = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2632 if (list == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2633 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2634 raw = type->tp_subclasses;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2635 if (raw == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2636 return list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2637 assert(PyList_Check(raw));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2638 n = PyList_GET_SIZE(raw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2639 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2640 ref = PyList_GET_ITEM(raw, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2641 assert(PyWeakref_CheckRef(ref));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2642 ref = PyWeakref_GET_OBJECT(ref);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2643 if (ref != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2644 if (PyList_Append(list, ref) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2645 Py_DECREF(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2646 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2647 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2648 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2649 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2650 return list;
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2651 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2652
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2653 static PyMethodDef type_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2654 {"mro", (PyCFunction)mro_external, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2655 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2656 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2657 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2658 {"__instancecheck__", type___instancecheck__, METH_O,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2659 PyDoc_STR("__instancecheck__() -> check if an object is an instance")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2660 {"__subclasscheck__", type___subclasscheck__, METH_O,
64078
756f8b398230 Merged revisions 84106 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62105
diff changeset
2661 PyDoc_STR("__subclasscheck__() -> check if a class is a subclass")},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2662 {0}
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2663 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2664
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23830
diff changeset
2665 PyDoc_STRVAR(type_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2666 "type(object) -> the object's type\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23830
diff changeset
2667 "type(name, bases, dict) -> a new type");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2668
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2669 static int
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2670 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2671 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2672 /* Because of type_is_gc(), the collector only calls this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2673 for heaptypes. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2674 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2675
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2676 Py_VISIT(type->tp_dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2677 Py_VISIT(type->tp_cache);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2678 Py_VISIT(type->tp_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2679 Py_VISIT(type->tp_bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2680 Py_VISIT(type->tp_base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2681
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2682 /* There's no need to visit type->tp_subclasses or
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2683 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2684 in cycles; tp_subclasses is a list of weak references,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2685 and slots is a tuple of strings. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2686
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2687 return 0;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2688 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2689
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2690 static int
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2691 type_clear(PyTypeObject *type)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2692 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2693 /* Because of type_is_gc(), the collector only calls this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2694 for heaptypes. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2695 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2696
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2697 /* The only field we need to clear is tp_mro, which is part of a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2698 hard cycle (its first element is the class itself) that won't
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2699 be broken otherwise (it's a tuple and tuples don't have a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2700 tp_clear handler). None of the other fields need to be
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2701 cleared, and here's why:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2702
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2703 tp_dict:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2704 It is a dict, so the collector will call its tp_clear.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2705
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2706 tp_cache:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2707 Not used; if it were, it would be a dict.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2708
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2709 tp_bases, tp_base:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2710 If these are involved in a cycle, there must be at least
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2711 one other, mutable object in the cycle, e.g. a base
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2712 class's dict; the cycle will be broken that way.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2713
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2714 tp_subclasses:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2715 A list of weak references can't be part of a cycle; and
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2716 lists have their own tp_clear.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2717
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2718 slots (in PyHeapTypeObject):
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2719 A tuple of strings can't be part of a cycle.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2720 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2721
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2722 Py_CLEAR(type->tp_mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2723
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2724 return 0;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2725 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2726
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2727 static int
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2728 type_is_gc(PyTypeObject *type)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2729 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2730 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2731 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2732
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
2733 PyTypeObject PyType_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2734 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2735 "type", /* tp_name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2736 sizeof(PyHeapTypeObject), /* tp_basicsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2737 sizeof(PyMemberDef), /* tp_itemsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2738 (destructor)type_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2739 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2740 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2741 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2742 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2743 (reprfunc)type_repr, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2744 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2745 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2746 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2747 (hashfunc)_Py_HashPointer, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2748 (ternaryfunc)type_call, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2749 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2750 (getattrofunc)type_getattro, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2751 (setattrofunc)type_setattro, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2752 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2753 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2754 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2755 type_doc, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2756 (traverseproc)type_traverse, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2757 (inquiry)type_clear, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2758 type_richcompare, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2759 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2760 0, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2761 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2762 type_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2763 type_members, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2764 type_getsets, /* tp_getset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2765 0, /* tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2766 0, /* tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2767 0, /* tp_descr_get */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2768 0, /* tp_descr_set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2769 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2770 type_init, /* tp_init */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2771 0, /* tp_alloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2772 type_new, /* tp_new */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2773 PyObject_GC_Del, /* tp_free */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2774 (inquiry)type_is_gc, /* tp_is_gc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2775 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2776
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2777
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2778 /* The base type of all types (eventually)... except itself. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2779
41281
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2780 /* You may wonder why object.__new__() only complains about arguments
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2781 when object.__init__() is not overridden, and vice versa.
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2782
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2783 Consider the use cases:
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2784
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2785 1. When neither is overridden, we want to hear complaints about
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2786 excess (i.e., any) arguments, since their presence could
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2787 indicate there's a bug.
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2788
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2789 2. When defining an Immutable type, we are likely to override only
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2790 __new__(), since __init__() is called too late to initialize an
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2791 Immutable object. Since __new__() defines the signature for the
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2792 type, it would be a pain to have to override __init__() just to
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2793 stop it from complaining about excess arguments.
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2794
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2795 3. When defining a Mutable type, we are likely to override only
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2796 __init__(). So here the converse reasoning applies: we don't
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2797 want to have to override __new__() just to stop it from
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2798 complaining.
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2799
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2800 4. When __init__() is overridden, and the subclass __init__() calls
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2801 object.__init__(), the latter should complain about excess
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2802 arguments; ditto for __new__().
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2803
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2804 Use cases 2 and 3 make it unattractive to unconditionally check for
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2805 excess arguments. The best solution that addresses all four use
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2806 cases is as follows: __init__() complains about excess arguments
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2807 unless __new__() is overridden and __init__() is not overridden
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2808 (IOW, if __init__() is overridden or __new__() is not overridden);
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2809 symmetrically, __new__() complains about excess arguments unless
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2810 __init__() is overridden and __new__() is not overridden
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2811 (IOW, if __new__() is overridden or __init__() is not overridden).
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2812
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2813 However, for backwards compatibility, this breaks too much code.
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2814 Therefore, in 2.6, we'll *warn* about excess arguments when both
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2815 methods are overridden; for all other cases we'll use the above
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2816 rules.
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2817
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2818 */
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2819
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2820 /* Forward */
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2821 static PyObject *
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2822 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2823
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2824 static int
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2825 excess_args(PyObject *args, PyObject *kwds)
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2826 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2827 return PyTuple_GET_SIZE(args) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2828 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
41281
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2829 }
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2830
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2831 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2832 object_init(PyObject *self, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2833 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2834 int err = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2835 if (excess_args(args, kwds)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2836 PyTypeObject *type = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2837 if (type->tp_init != object_init &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2838 type->tp_new != object_new)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2839 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2840 err = PyErr_WarnEx(PyExc_DeprecationWarning,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2841 "object.__init__() takes no parameters",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2842 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2843 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2844 else if (type->tp_init != object_init ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2845 type->tp_new == object_new)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2846 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2847 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2848 "object.__init__() takes no parameters");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2849 err = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2850 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2851 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2852 return err;
41281
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2853 }
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2854
27866
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2855 static PyObject *
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2856 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2857 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2858 int err = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2859 if (excess_args(args, kwds)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2860 if (type->tp_new != object_new &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2861 type->tp_init != object_init)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2862 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2863 err = PyErr_WarnEx(PyExc_DeprecationWarning,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2864 "object.__new__() takes no parameters",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2865 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2866 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2867 else if (type->tp_new != object_new ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2868 type->tp_init == object_init)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2869 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2870 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2871 "object.__new__() takes no parameters");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2872 err = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2873 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2874 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2875 if (err < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2876 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2877
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2878 if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2879 static PyObject *comma = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2880 PyObject *abstract_methods = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2881 PyObject *builtins;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2882 PyObject *sorted;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2883 PyObject *sorted_methods = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2884 PyObject *joined = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2885 const char *joined_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2886
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2887 /* Compute ", ".join(sorted(type.__abstractmethods__))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2888 into joined. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2889 abstract_methods = type_abstractmethods(type, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2890 if (abstract_methods == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2891 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2892 builtins = PyEval_GetBuiltins();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2893 if (builtins == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2894 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2895 sorted = PyDict_GetItemString(builtins, "sorted");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2896 if (sorted == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2897 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2898 sorted_methods = PyObject_CallFunctionObjArgs(sorted,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2899 abstract_methods,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2900 NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2901 if (sorted_methods == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2902 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2903 if (comma == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2904 comma = PyString_InternFromString(", ");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2905 if (comma == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2906 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2907 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2908 joined = PyObject_CallMethod(comma, "join",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2909 "O", sorted_methods);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2910 if (joined == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2911 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2912 joined_str = PyString_AsString(joined);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2913 if (joined_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2914 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2915
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2916 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2917 "Can't instantiate abstract class %s "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2918 "with abstract methods %s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2919 type->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2920 joined_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2921 error:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2922 Py_XDECREF(joined);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2923 Py_XDECREF(sorted_methods);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2924 Py_XDECREF(abstract_methods);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2925 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2926 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2927 return type->tp_alloc(type, 0);
27866
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2928 }
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2929
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2930 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2931 object_dealloc(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2932 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2933 Py_TYPE(self)->tp_free(self);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2934 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2935
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2936 static PyObject *
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2937 object_repr(PyObject *self)
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2938 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2939 PyTypeObject *type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2940 PyObject *mod, *name, *rtn;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2941
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2942 type = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2943 mod = type_module(type, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2944 if (mod == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2945 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2946 else if (!PyString_Check(mod)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2947 Py_DECREF(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2948 mod = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2949 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2950 name = type_name(type, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2951 if (name == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2952 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2953 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2954 rtn = PyString_FromFormat("<%s.%s object at %p>",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2955 PyString_AS_STRING(mod),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2956 PyString_AS_STRING(name),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2957 self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2958 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2959 rtn = PyString_FromFormat("<%s object at %p>",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2960 type->tp_name, self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2961 Py_XDECREF(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2962 Py_DECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2963 return rtn;
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2964 }
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2965
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2966 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2967 object_str(PyObject *self)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2968 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2969 unaryfunc f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2970
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2971 f = Py_TYPE(self)->tp_repr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2972 if (f == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2973 f = object_repr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2974 return f(self);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2975 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2976
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2977 static PyObject *
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2978 object_get_class(PyObject *self, void *closure)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2979 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2980 Py_INCREF(Py_TYPE(self));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2981 return (PyObject *)(Py_TYPE(self));
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2982 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2983
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2984 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2985 equiv_structs(PyTypeObject *a, PyTypeObject *b)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2986 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2987 return a == b ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2988 (a != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2989 b != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2990 a->tp_basicsize == b->tp_basicsize &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2991 a->tp_itemsize == b->tp_itemsize &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2992 a->tp_dictoffset == b->tp_dictoffset &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2993 a->tp_weaklistoffset == b->tp_weaklistoffset &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2994 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
2995 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2996 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2997
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2998 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2999 same_slots_added(PyTypeObject *a, PyTypeObject *b)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3000 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3001 PyTypeObject *base = a->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3002 Py_ssize_t size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3003 PyObject *slots_a, *slots_b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3004
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3005 if (base != b->tp_base)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3006 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3007 if (equiv_structs(a, base) && equiv_structs(b, base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3008 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3009 size = base->tp_basicsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3010 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3011 size += sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3012 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3013 size += sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3014
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3015 /* Check slots compliance */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3016 slots_a = ((PyHeapTypeObject *)a)->ht_slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3017 slots_b = ((PyHeapTypeObject *)b)->ht_slots;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3018 if (slots_a && slots_b) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3019 if (PyObject_Compare(slots_a, slots_b) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3020 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3021 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3022 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3023 return size == a->tp_basicsize && size == b->tp_basicsize;
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3024 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3025
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3026 static int
37596
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37308
diff changeset
3027 compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3028 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3029 PyTypeObject *newbase, *oldbase;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3030
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3031 if (newto->tp_dealloc != oldto->tp_dealloc ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3032 newto->tp_free != oldto->tp_free)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3033 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3034 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3035 "%s assignment: "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3036 "'%s' deallocator differs from '%s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3037 attr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3038 newto->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3039 oldto->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3040 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3041 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3042 newbase = newto;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3043 oldbase = oldto;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3044 while (equiv_structs(newbase, newbase->tp_base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3045 newbase = newbase->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3046 while (equiv_structs(oldbase, oldbase->tp_base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3047 oldbase = oldbase->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3048 if (newbase != oldbase &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3049 (newbase->tp_base != oldbase->tp_base ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3050 !same_slots_added(newbase, oldbase))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3051 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3052 "%s assignment: "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3053 "'%s' object layout differs from '%s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3054 attr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3055 newto->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3056 oldto->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3057 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3058 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3059
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3060 return 1;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3061 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3062
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3063 static int
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3064 object_set_class(PyObject *self, PyObject *value, void *closure)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3065 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3066 PyTypeObject *oldto = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3067 PyTypeObject *newto;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3068
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3069 if (value == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3070 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3071 "can't delete __class__ attribute");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3072 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3073 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3074 if (!PyType_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3075 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3076 "__class__ must be set to new-style class, not '%s' object",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3077 Py_TYPE(value)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3078 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3079 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3080 newto = (PyTypeObject *)value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3081 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3082 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3083 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3084 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3085 "__class__ assignment: only for heap types");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3086 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3087 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3088 if (compatible_for_assignment(newto, oldto, "__class__")) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3089 Py_INCREF(newto);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3090 Py_TYPE(self) = newto;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3091 Py_DECREF(oldto);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3092 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3093 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3094 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3095 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3096 }
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3097 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3098
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3099 static PyGetSetDef object_getsets[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3100 {"__class__", object_get_class, object_set_class,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3101 PyDoc_STR("the object's class")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3102 {0}
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3103 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3104
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3105
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3106 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 47108
diff changeset
3107 We fall back to helpers in copy_reg for:
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3108 - pickle protocols < 2
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3109 - calculating the list of slot names (done only once per class)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3110 - the __newobj__ function (which is used as a token but never called)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3111 */
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3112
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3113 static PyObject *
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3114 import_copyreg(void)
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3115 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3116 static PyObject *copyreg_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3117
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3118 if (!copyreg_str) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3119 copyreg_str = PyString_InternFromString("copy_reg");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3120 if (copyreg_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3121 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3122 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3123
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3124 return PyImport_Import(copyreg_str);
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3125 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3126
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3127 static PyObject *
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3128 slotnames(PyObject *cls)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3129 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3130 PyObject *clsdict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3131 PyObject *copyreg;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3132 PyObject *slotnames;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3133
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3134 if (!PyType_Check(cls)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3135 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3136 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3137 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3138
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3139 clsdict = ((PyTypeObject *)cls)->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3140 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3141 if (slotnames != NULL && PyList_Check(slotnames)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3142 Py_INCREF(slotnames);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3143 return slotnames;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3144 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3145
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3146 copyreg = import_copyreg();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3147 if (copyreg == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3148 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3149
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3150 slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3151 Py_DECREF(copyreg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3152 if (slotnames != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3153 slotnames != Py_None &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3154 !PyList_Check(slotnames))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3155 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3156 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3157 "copy_reg._slotnames didn't return a list or None");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3158 Py_DECREF(slotnames);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3159 slotnames = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3160 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3161
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3162 return slotnames;
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3163 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3164
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3165 static PyObject *
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3166 reduce_2(PyObject *obj)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3167 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3168 PyObject *cls, *getnewargs;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3169 PyObject *args = NULL, *args2 = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3170 PyObject *getstate = NULL, *state = NULL, *names = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3171 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3172 PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3173 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3174
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3175 cls = PyObject_GetAttrString(obj, "__class__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3176 if (cls == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3177 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3178
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3179 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3180 if (getnewargs != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3181 args = PyObject_CallObject(getnewargs, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3182 Py_DECREF(getnewargs);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3183 if (args != NULL && !PyTuple_Check(args)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3184 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3185 "__getnewargs__ should return a tuple, "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3186 "not '%.200s'", Py_TYPE(args)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3187 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3188 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3189 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3190 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3191 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3192 args = PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3193 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3194 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3195 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3196
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3197 getstate = PyObject_GetAttrString(obj, "__getstate__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3198 if (getstate != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3199 state = PyObject_CallObject(getstate, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3200 Py_DECREF(getstate);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3201 if (state == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3202 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3203 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3204 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3205 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3206 state = PyObject_GetAttrString(obj, "__dict__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3207 if (state == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3208 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3209 state = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3210 Py_INCREF(state);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3211 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3212 names = slotnames(cls);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3213 if (names == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3214 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3215 if (names != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3216 assert(PyList_Check(names));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3217 slots = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3218 if (slots == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3219 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3220 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3221 /* Can't pre-compute the list size; the list
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3222 is stored on the class so accessible to other
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3223 threads, which may be run by DECREF */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3224 for (i = 0; i < PyList_GET_SIZE(names); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3225 PyObject *name, *value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3226 name = PyList_GET_ITEM(names, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3227 value = PyObject_GetAttr(obj, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3228 if (value == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3229 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3230 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3231 int err = PyDict_SetItem(slots, name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3232 value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3233 Py_DECREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3234 if (err)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3235 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3236 n++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3237 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3238 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3239 if (n) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3240 state = Py_BuildValue("(NO)", state, slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3241 if (state == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3242 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3243 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3244 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3245 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3246
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3247 if (!PyList_Check(obj)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3248 listitems = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3249 Py_INCREF(listitems);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3250 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3251 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3252 listitems = PyObject_GetIter(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3253 if (listitems == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3254 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3255 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3256
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3257 if (!PyDict_Check(obj)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3258 dictitems = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3259 Py_INCREF(dictitems);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3260 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3261 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3262 dictitems = PyObject_CallMethod(obj, "iteritems", "");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3263 if (dictitems == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3264 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3265 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3266
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3267 copyreg = import_copyreg();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3268 if (copyreg == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3269 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3270 newobj = PyObject_GetAttrString(copyreg, "__newobj__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3271 if (newobj == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3272 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3273
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3274 n = PyTuple_GET_SIZE(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3275 args2 = PyTuple_New(n+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3276 if (args2 == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3277 goto end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3278 PyTuple_SET_ITEM(args2, 0, cls);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3279 cls = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3280 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3281 PyObject *v = PyTuple_GET_ITEM(args, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3282 Py_INCREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3283 PyTuple_SET_ITEM(args2, i+1, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3284 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3285
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3286 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3287
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3288 end:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3289 Py_XDECREF(cls);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3290 Py_XDECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3291 Py_XDECREF(args2);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3292 Py_XDECREF(slots);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3293 Py_XDECREF(state);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3294 Py_XDECREF(names);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3295 Py_XDECREF(listitems);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3296 Py_XDECREF(dictitems);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3297 Py_XDECREF(copyreg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3298 Py_XDECREF(newobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3299 return res;
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3300 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3301
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3302 /*
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3303 * There were two problems when object.__reduce__ and object.__reduce_ex__
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3304 * were implemented in the same function:
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3305 * - trying to pickle an object with a custom __reduce__ method that
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3306 * fell back to object.__reduce__ in certain circumstances led to
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3307 * infinite recursion at Python level and eventual RuntimeError.
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3308 * - Pickling objects that lied about their type by overwriting the
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3309 * __class__ descriptor could lead to infinite recursion at C level
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3310 * and eventual segfault.
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3311 *
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3312 * Because of backwards compatibility, the two methods still have to
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3313 * behave in the same way, even if this is not required by the pickle
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3314 * protocol. This common functionality was moved to the _common_reduce
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3315 * function.
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3316 */
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3317 static PyObject *
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3318 _common_reduce(PyObject *self, int proto)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3319 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3320 PyObject *copyreg, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3321
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3322 if (proto >= 2)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3323 return reduce_2(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3324
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3325 copyreg = import_copyreg();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3326 if (!copyreg)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3327 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3328
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3329 res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3330 Py_DECREF(copyreg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3331
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3332 return res;
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3333 }
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3334
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3335 static PyObject *
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3336 object_reduce(PyObject *self, PyObject *args)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3337 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3338 int proto = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3339
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3340 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3341 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3342
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3343 return _common_reduce(self, proto);
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3344 }
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3345
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3346 static PyObject *
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3347 object_reduce_ex(PyObject *self, PyObject *args)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3348 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3349 PyObject *reduce, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3350 int proto = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3351
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3352 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3353 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3354
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3355 reduce = PyObject_GetAttrString(self, "__reduce__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3356 if (reduce == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3357 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3358 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3359 PyObject *cls, *clsreduce, *objreduce;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3360 int override;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3361 cls = PyObject_GetAttrString(self, "__class__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3362 if (cls == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3363 Py_DECREF(reduce);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3364 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3365 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3366 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3367 Py_DECREF(cls);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3368 if (clsreduce == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3369 Py_DECREF(reduce);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3370 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3371 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3372 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3373 "__reduce__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3374 override = (clsreduce != objreduce);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3375 Py_DECREF(clsreduce);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3376 if (override) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3377 res = PyObject_CallObject(reduce, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3378 Py_DECREF(reduce);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3379 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3380 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3381 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3382 Py_DECREF(reduce);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3383 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3384
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3385 return _common_reduce(self, proto);
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3386 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3387
45543
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3388 static PyObject *
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3389 object_subclasshook(PyObject *cls, PyObject *args)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3390 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3391 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3392 return Py_NotImplemented;
45543
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3393 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3394
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3395 PyDoc_STRVAR(object_subclasshook_doc,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3396 "Abstract classes can override this to customize issubclass().\n"
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3397 "\n"
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3398 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3399 "It should return True, False or NotImplemented. If it returns\n"
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3400 "NotImplemented, the normal algorithm is used. Otherwise, it\n"
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3401 "overrides the normal algorithm (and the outcome is cached).\n");
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3402
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3403 /*
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3404 from PEP 3101, this code implements:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3405
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3406 class object:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3407 def __format__(self, format_spec):
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3408 if isinstance(format_spec, str):
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3409 return format(str(self), format_spec)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3410 elif isinstance(format_spec, unicode):
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3411 return format(unicode(self), format_spec)
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3412 */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3413 static PyObject *
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3414 object_format(PyObject *self, PyObject *args)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3415 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3416 PyObject *format_spec;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3417 PyObject *self_as_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3418 PyObject *result = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3419 Py_ssize_t format_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3420
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3421 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3422 return NULL;
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50828
diff changeset
3423 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3424 if (PyUnicode_Check(format_spec)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3425 format_len = PyUnicode_GET_SIZE(format_spec);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3426 self_as_str = PyObject_Unicode(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3427 } else if (PyString_Check(format_spec)) {
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50828
diff changeset
3428 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3429 if (PyString_Check(format_spec)) {
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50828
diff changeset
3430 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3431 format_len = PyString_GET_SIZE(format_spec);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3432 self_as_str = PyObject_Str(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3433 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3434 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3435 "argument to __format__ must be unicode or str");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3436 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3437 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3438
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3439 if (self_as_str != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3440 /* Issue 7994: If we're converting to a string, we
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3441 should reject format specifications */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3442 if (format_len > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3443 if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3444 "object.__format__ with a non-empty format "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3445 "string is deprecated", 1) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3446 goto done;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3447 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3448 /* Eventually this will become an error:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3449 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3450 "non-empty format string passed to object.__format__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3451 goto done;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3452 */
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3453 }
62087
142cb6f60fd7 fix ref counting
Benjamin Peterson <benjamin@python.org>
parents: 62086
diff changeset
3454 result = PyObject_Format(self_as_str, format_spec);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3455 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3456
60162
20497d85e011 Issue 7994: Make object.__format__ with a non-empty format string a PendingDecprecationWarning. Still need to remove uses of this from various tests.
Eric Smith <eric@trueblade.com>
parents: 57748
diff changeset
3457 done:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3458 Py_XDECREF(self_as_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3459
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3460 return result;
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3461 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3462
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3463 static PyObject *
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3464 object_sizeof(PyObject *self, PyObject *args)
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3465 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3466 Py_ssize_t res, isize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3467
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3468 res = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3469 isize = self->ob_type->tp_itemsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3470 if (isize > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3471 res = self->ob_type->ob_size * isize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3472 res += self->ob_type->tp_basicsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3473
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3474 return PyInt_FromSsize_t(res);
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3475 }
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3476
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3477 static PyMethodDef object_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3478 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3479 PyDoc_STR("helper for pickle")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3480 {"__reduce__", object_reduce, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3481 PyDoc_STR("helper for pickle")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3482 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3483 object_subclasshook_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3484 {"__format__", object_format, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3485 PyDoc_STR("default object formatter")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3486 {"__sizeof__", object_sizeof, METH_NOARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3487 PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3488 {0}
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3489 };
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3490
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3491
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3492 PyTypeObject PyBaseObject_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3493 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3494 "object", /* tp_name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3495 sizeof(PyObject), /* tp_basicsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3496 0, /* tp_itemsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3497 object_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3498 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3499 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3500 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3501 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3502 object_repr, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3503 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3504 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3505 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3506 (hashfunc)_Py_HashPointer, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3507 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3508 object_str, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3509 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3510 PyObject_GenericSetAttr, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3511 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3512 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3513 PyDoc_STR("The most base type"), /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3514 0, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3515 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3516 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3517 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3518 0, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3519 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3520 object_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3521 0, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3522 object_getsets, /* tp_getset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3523 0, /* tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3524 0, /* tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3525 0, /* tp_descr_get */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3526 0, /* tp_descr_set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3527 0, /* tp_dictoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3528 object_init, /* tp_init */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3529 PyType_GenericAlloc, /* tp_alloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3530 object_new, /* tp_new */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3531 PyObject_Del, /* tp_free */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3532 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3533
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3534
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3535 /* Initialize the __dict__ in a type object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3536
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3537 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3538 add_methods(PyTypeObject *type, PyMethodDef *meth)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3539 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3540 PyObject *dict = type->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3541
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3542 for (; meth->ml_name != NULL; meth++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3543 PyObject *descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3544 if (PyDict_GetItemString(dict, meth->ml_name) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3545 !(meth->ml_flags & METH_COEXIST))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3546 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3547 if (meth->ml_flags & METH_CLASS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3548 if (meth->ml_flags & METH_STATIC) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3549 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3550 "method cannot be both class and static");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3551 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3552 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3553 descr = PyDescr_NewClassMethod(type, meth);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3554 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3555 else if (meth->ml_flags & METH_STATIC) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3556 PyObject *cfunc = PyCFunction_New(meth, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3557 if (cfunc == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3558 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3559 descr = PyStaticMethod_New(cfunc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3560 Py_DECREF(cfunc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3561 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3562 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3563 descr = PyDescr_NewMethod(type, meth);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3564 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3565 if (descr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3566 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3567 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3568 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3569 Py_DECREF(descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3570 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3571 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3572 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3573
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3574 static int
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
3575 add_members(PyTypeObject *type, PyMemberDef *memb)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3576 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3577 PyObject *dict = type->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3578
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3579 for (; memb->name != NULL; memb++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3580 PyObject *descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3581 if (PyDict_GetItemString(dict, memb->name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3582 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3583 descr = PyDescr_NewMember(type, memb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3584 if (descr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3585 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3586 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3587 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3588 Py_DECREF(descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3589 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3590 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3591 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3592
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3593 static int
20015
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
3594 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3595 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3596 PyObject *dict = type->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3597
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3598 for (; gsp->name != NULL; gsp++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3599 PyObject *descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3600 if (PyDict_GetItemString(dict, gsp->name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3601 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3602 descr = PyDescr_NewGetSet(type, gsp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3603
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3604 if (descr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3605 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3606 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3607 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3608 Py_DECREF(descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3609 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3610 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3611 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3612
52749
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52176
diff changeset
3613 #define BUFFER_FLAGS (Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER)
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52176
diff changeset
3614
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3615 static void
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3616 inherit_special(PyTypeObject *type, PyTypeObject *base)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3617 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3618 Py_ssize_t oldsize, newsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3619
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3620 /* Special flag magic */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3621 if (!type->tp_as_buffer && base->tp_as_buffer) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3622 type->tp_flags &= ~BUFFER_FLAGS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3623 type->tp_flags |=
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3624 base->tp_flags & BUFFER_FLAGS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3625 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3626 if (!type->tp_as_sequence && base->tp_as_sequence) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3627 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3628 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3629 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3630 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3631 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3632 if ((!type->tp_as_number && base->tp_as_number) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3633 (!type->tp_as_sequence && base->tp_as_sequence)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3634 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3635 if (!type->tp_as_number && !type->tp_as_sequence) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3636 type->tp_flags |= base->tp_flags &
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3637 Py_TPFLAGS_HAVE_INPLACEOPS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3638 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3639 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3640 /* Wow */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3641 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3642 if (!type->tp_as_number && base->tp_as_number) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3643 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3644 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3645 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3646
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3647 /* Copying basicsize is connected to the GC flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3648 oldsize = base->tp_basicsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3649 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3650 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3651 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3652 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3653 (!type->tp_traverse && !type->tp_clear)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3654 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3655 if (type->tp_traverse == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3656 type->tp_traverse = base->tp_traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3657 if (type->tp_clear == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3658 type->tp_clear = base->tp_clear;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3659 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3660 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3661 /* The condition below could use some explanation.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3662 It appears that tp_new is not inherited for static types
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3663 whose base class is 'object'; this seems to be a precaution
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3664 so that old extension types don't suddenly become
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3665 callable (object.__new__ wouldn't insure the invariants
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3666 that the extension type's own factory function ensures).
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3667 Heap types, of course, are under our control, so they do
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3668 inherit tp_new; static extension types that specify some
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3669 other built-in type as the default are considered
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3670 new-style-aware so they also inherit object.__new__. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3671 if (base != &PyBaseObject_Type ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3672 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3673 if (type->tp_new == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3674 type->tp_new = base->tp_new;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3675 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3676 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3677 type->tp_basicsize = newsize;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3678
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3679 /* Copy other non-function slots */
19253
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3680
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3681 #undef COPYVAL
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3682 #define COPYVAL(SLOT) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3683 if (type->SLOT == 0) type->SLOT = base->SLOT
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3684
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3685 COPYVAL(tp_itemsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3686 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3687 COPYVAL(tp_weaklistoffset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3688 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3689 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3690 COPYVAL(tp_dictoffset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3691 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3692
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3693 /* Setup fast subclass flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3694 if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3695 type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3696 else if (PyType_IsSubtype(base, &PyType_Type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3697 type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3698 else if (PyType_IsSubtype(base, &PyInt_Type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3699 type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3700 else if (PyType_IsSubtype(base, &PyLong_Type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3701 type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3702 else if (PyType_IsSubtype(base, &PyString_Type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3703 type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS;
44572
db5e01c33eb7 Make Python compile with --disable-unicode.
Georg Brandl <georg@python.org>
parents: 44365
diff changeset
3704 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3705 else if (PyType_IsSubtype(base, &PyUnicode_Type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3706 type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
44572
db5e01c33eb7 Make Python compile with --disable-unicode.
Georg Brandl <georg@python.org>
parents: 44365
diff changeset
3707 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3708 else if (PyType_IsSubtype(base, &PyTuple_Type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3709 type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3710 else if (PyType_IsSubtype(base, &PyList_Type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3711 type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3712 else if (PyType_IsSubtype(base, &PyDict_Type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3713 type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3714 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3715
48213
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47961
diff changeset
3716 static int
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47961
diff changeset
3717 overrides_name(PyTypeObject *type, char *name)
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47961
diff changeset
3718 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3719 PyObject *dict = type->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3720
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3721 assert(dict != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3722 if (PyDict_GetItemString(dict, name) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3723 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3724 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3725 return 0;
48213
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47961
diff changeset
3726 }
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47961
diff changeset
3727
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47961
diff changeset
3728 #define OVERRIDES_HASH(x) overrides_name(x, "__hash__")
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47961
diff changeset
3729 #define OVERRIDES_EQ(x) overrides_name(x, "__eq__")
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47961
diff changeset
3730
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3731 static void
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3732 inherit_slots(PyTypeObject *type, PyTypeObject *base)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3733 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3734 PyTypeObject *basebase;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3735
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3736 #undef SLOTDEFINED
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3737 #undef COPYSLOT
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3738 #undef COPYNUM
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3739 #undef COPYSEQ
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3740 #undef COPYMAP
20386
3c01c34d07f6 Now that COPYBUF is a new local macro, add #undef COPYBUF.
Guido van Rossum <guido@python.org>
parents: 20384
diff changeset
3741 #undef COPYBUF
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3742
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3743 #define SLOTDEFINED(SLOT) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3744 (base->SLOT != 0 && \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3745 (basebase == NULL || base->SLOT != basebase->SLOT))
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3746
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3747 #define COPYSLOT(SLOT) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3748 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3749
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3750 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3751 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3752 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
20384
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3753 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3754
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3755 /* This won't inherit indirect slots (from tp_as_number etc.)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3756 if type doesn't provide the space. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3757
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3758 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3759 basebase = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3760 if (basebase->tp_as_number == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3761 basebase = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3762 COPYNUM(nb_add);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3763 COPYNUM(nb_subtract);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3764 COPYNUM(nb_multiply);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3765 COPYNUM(nb_divide);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3766 COPYNUM(nb_remainder);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3767 COPYNUM(nb_divmod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3768 COPYNUM(nb_power);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3769 COPYNUM(nb_negative);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3770 COPYNUM(nb_positive);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3771 COPYNUM(nb_absolute);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3772 COPYNUM(nb_nonzero);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3773 COPYNUM(nb_invert);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3774 COPYNUM(nb_lshift);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3775 COPYNUM(nb_rshift);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3776 COPYNUM(nb_and);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3777 COPYNUM(nb_xor);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3778 COPYNUM(nb_or);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3779 COPYNUM(nb_coerce);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3780 COPYNUM(nb_int);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3781 COPYNUM(nb_long);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3782 COPYNUM(nb_float);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3783 COPYNUM(nb_oct);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3784 COPYNUM(nb_hex);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3785 COPYNUM(nb_inplace_add);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3786 COPYNUM(nb_inplace_subtract);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3787 COPYNUM(nb_inplace_multiply);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3788 COPYNUM(nb_inplace_divide);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3789 COPYNUM(nb_inplace_remainder);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3790 COPYNUM(nb_inplace_power);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3791 COPYNUM(nb_inplace_lshift);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3792 COPYNUM(nb_inplace_rshift);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3793 COPYNUM(nb_inplace_and);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3794 COPYNUM(nb_inplace_xor);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3795 COPYNUM(nb_inplace_or);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3796 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3797 COPYNUM(nb_true_divide);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3798 COPYNUM(nb_floor_divide);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3799 COPYNUM(nb_inplace_true_divide);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3800 COPYNUM(nb_inplace_floor_divide);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3801 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3802 if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3803 COPYNUM(nb_index);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3804 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3805 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3806
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3807 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3808 basebase = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3809 if (basebase->tp_as_sequence == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3810 basebase = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3811 COPYSEQ(sq_length);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3812 COPYSEQ(sq_concat);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3813 COPYSEQ(sq_repeat);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3814 COPYSEQ(sq_item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3815 COPYSEQ(sq_slice);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3816 COPYSEQ(sq_ass_item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3817 COPYSEQ(sq_ass_slice);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3818 COPYSEQ(sq_contains);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3819 COPYSEQ(sq_inplace_concat);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3820 COPYSEQ(sq_inplace_repeat);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3821 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3822
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3823 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3824 basebase = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3825 if (basebase->tp_as_mapping == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3826 basebase = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3827 COPYMAP(mp_length);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3828 COPYMAP(mp_subscript);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3829 COPYMAP(mp_ass_subscript);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3830 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3831
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3832 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3833 basebase = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3834 if (basebase->tp_as_buffer == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3835 basebase = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3836 COPYBUF(bf_getreadbuffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3837 COPYBUF(bf_getwritebuffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3838 COPYBUF(bf_getsegcount);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3839 COPYBUF(bf_getcharbuffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3840 COPYBUF(bf_getbuffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3841 COPYBUF(bf_releasebuffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3842 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3843
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3844 basebase = base->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3845
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3846 COPYSLOT(tp_dealloc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3847 COPYSLOT(tp_print);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3848 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3849 type->tp_getattr = base->tp_getattr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3850 type->tp_getattro = base->tp_getattro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3851 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3852 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3853 type->tp_setattr = base->tp_setattr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3854 type->tp_setattro = base->tp_setattro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3855 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3856 /* tp_compare see tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3857 COPYSLOT(tp_repr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3858 /* tp_hash see tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3859 COPYSLOT(tp_call);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3860 COPYSLOT(tp_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3861 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3862 if (type->tp_compare == NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3863 type->tp_richcompare == NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3864 type->tp_hash == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3865 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3866 type->tp_compare = base->tp_compare;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3867 type->tp_richcompare = base->tp_richcompare;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3868 type->tp_hash = base->tp_hash;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3869 /* Check for changes to inherited methods in Py3k*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3870 if (Py_Py3kWarningFlag) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3871 if (base->tp_hash &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3872 (base->tp_hash != PyObject_HashNotImplemented) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3873 !OVERRIDES_HASH(type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3874 if (OVERRIDES_EQ(type)) {
62101
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3875 if (PyErr_WarnPy3k("Overriding "
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3876 "__eq__ blocks inheritance "
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3877 "of __hash__ in 3.x",
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3878 1) < 0)
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3879 /* XXX This isn't right. If the warning is turned
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3880 into an exception, we should be communicating
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3881 the error back to the caller, but figuring out
62105
3721d4e5b2ee Fix comment typo.
Mark Dickinson <dickinsm@gmail.com>
parents: 62101
diff changeset
3882 how to clean up in that case is tricky. See
62101
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3883 issue 8627 for more. */
67c33cb9622d Issue #8627: Fix "XXX undetected error" from unchecked PyErr_WarnPy3k return.
Mark Dickinson <dickinsm@gmail.com>
parents: 62097
diff changeset
3884 PyErr_Clear();
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3885 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3886 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3887 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3888 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3889 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3890 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3891 COPYSLOT(tp_compare);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3892 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3893 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3894 COPYSLOT(tp_iter);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3895 COPYSLOT(tp_iternext);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3896 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3897 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3898 COPYSLOT(tp_descr_get);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3899 COPYSLOT(tp_descr_set);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3900 COPYSLOT(tp_dictoffset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3901 COPYSLOT(tp_init);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3902 COPYSLOT(tp_alloc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3903 COPYSLOT(tp_is_gc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3904 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3905 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3906 /* They agree about gc. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3907 COPYSLOT(tp_free);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3908 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3909 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3910 type->tp_free == NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3911 base->tp_free == _PyObject_Del) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3912 /* A bit of magic to plug in the correct default
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3913 * tp_free function when a derived class adds gc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3914 * didn't define tp_free, and the base uses the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3915 * default non-gc tp_free.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3916 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3917 type->tp_free = PyObject_GC_Del;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3918 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3919 /* else they didn't agree about gc, and there isn't something
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3920 * obvious to be done -- the type is on its own.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3921 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3922 }
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3923 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3924
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
3925 static int add_operators(PyTypeObject *);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3926
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3927 int
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
3928 PyType_Ready(PyTypeObject *type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3929 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3930 PyObject *dict, *bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3931 PyTypeObject *base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3932 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3933
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3934 if (type->tp_flags & Py_TPFLAGS_READY) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3935 assert(type->tp_dict != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3936 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3937 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3938 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3939
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3940 type->tp_flags |= Py_TPFLAGS_READYING;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3941
28389
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3942 #ifdef Py_TRACE_REFS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3943 /* PyType_Ready is the closest thing we have to a choke point
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3944 * for type objects, so is the best place I can think of to try
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3945 * to get type objects into the doubly-linked list of all objects.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3946 * Still, not all type objects go thru PyType_Ready.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3947 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3948 _Py_AddToAllObjects((PyObject *)type, 0);
28389
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3949 #endif
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3950
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3951 /* Initialize tp_base (defaults to BaseObject unless that's us) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3952 base = type->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3953 if (base == NULL && type != &PyBaseObject_Type) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3954 base = type->tp_base = &PyBaseObject_Type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3955 Py_INCREF(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3956 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3957
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3958 /* Now the only way base can still be NULL is if type is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3959 * &PyBaseObject_Type.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3960 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3961
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3962 /* Initialize the base class */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3963 if (base && base->tp_dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3964 if (PyType_Ready(base) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3965 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3966 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3967
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3968 /* Initialize ob_type if NULL. This means extensions that want to be
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3969 compilable separately on Windows can call PyType_Ready() instead of
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3970 initializing the ob_type field of their type objects. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3971 /* The test for base != NULL is really unnecessary, since base is only
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3972 NULL when type is &PyBaseObject_Type, and we know its ob_type is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3973 not NULL (it's initialized to &PyType_Type). But coverity doesn't
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3974 know that. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3975 if (Py_TYPE(type) == NULL && base != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3976 Py_TYPE(type) = Py_TYPE(base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3977
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3978 /* Initialize tp_bases */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3979 bases = type->tp_bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3980 if (bases == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3981 if (base == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3982 bases = PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3983 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3984 bases = PyTuple_Pack(1, base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3985 if (bases == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3986 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3987 type->tp_bases = bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3988 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3989
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3990 /* Initialize tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3991 dict = type->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3992 if (dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3993 dict = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3994 if (dict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3995 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3996 type->tp_dict = dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3997 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3998
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
3999 /* Add type-specific descriptors to tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4000 if (add_operators(type) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4001 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4002 if (type->tp_methods != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4003 if (add_methods(type, type->tp_methods) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4004 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4005 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4006 if (type->tp_members != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4007 if (add_members(type, type->tp_members) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4008 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4009 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4010 if (type->tp_getset != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4011 if (add_getset(type, type->tp_getset) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4012 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4013 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4014
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4015 /* Calculate method resolution order */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4016 if (mro_internal(type) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4017 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4018 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4019
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4020 /* Inherit special flags from dominant base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4021 if (type->tp_base != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4022 inherit_special(type, type->tp_base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4023
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4024 /* Initialize tp_dict properly */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4025 bases = type->tp_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4026 assert(bases != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4027 assert(PyTuple_Check(bases));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4028 n = PyTuple_GET_SIZE(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4029 for (i = 1; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4030 PyObject *b = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4031 if (PyType_Check(b))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4032 inherit_slots(type, (PyTypeObject *)b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4033 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4034
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4035 /* Sanity check for tp_free. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4036 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4037 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4038 /* This base class needs to call tp_free, but doesn't have
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4039 * one, or its tp_free is for non-gc'ed objects.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4040 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4041 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4042 "gc and is a base type but has inappropriate "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4043 "tp_free slot",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4044 type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4045 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4046 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4047
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4048 /* if the type dictionary doesn't contain a __doc__, set it from
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4049 the tp_doc slot.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4050 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4051 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4052 if (type->tp_doc != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4053 PyObject *doc = PyString_FromString(type->tp_doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4054 if (doc == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4055 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4056 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4057 Py_DECREF(doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4058 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4059 PyDict_SetItemString(type->tp_dict,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4060 "__doc__", Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4061 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4062 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4063
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4064 /* Some more special stuff */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4065 base = type->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4066 if (base != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4067 if (type->tp_as_number == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4068 type->tp_as_number = base->tp_as_number;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4069 if (type->tp_as_sequence == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4070 type->tp_as_sequence = base->tp_as_sequence;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4071 if (type->tp_as_mapping == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4072 type->tp_as_mapping = base->tp_as_mapping;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4073 if (type->tp_as_buffer == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4074 type->tp_as_buffer = base->tp_as_buffer;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4075 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4076
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4077 /* Link into each base class's list of subclasses */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4078 bases = type->tp_bases;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4079 n = PyTuple_GET_SIZE(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4080 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4081 PyObject *b = PyTuple_GET_ITEM(bases, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4082 if (PyType_Check(b) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4083 add_subclass((PyTypeObject *)b, type) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4084 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4085 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4086
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4087 /* All done -- set the ready flag */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4088 assert(type->tp_dict != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4089 type->tp_flags =
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4090 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4091 return 0;
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
4092
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
4093 error:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4094 type->tp_flags &= ~Py_TPFLAGS_READYING;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4095 return -1;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4096 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4097
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4098 static int
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4099 add_subclass(PyTypeObject *base, PyTypeObject *type)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4100 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4101 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4102 int result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4103 PyObject *list, *ref, *newobj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4104
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4105 list = base->tp_subclasses;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4106 if (list == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4107 base->tp_subclasses = list = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4108 if (list == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4109 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4110 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4111 assert(PyList_Check(list));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4112 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4113 i = PyList_GET_SIZE(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4114 while (--i >= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4115 ref = PyList_GET_ITEM(list, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4116 assert(PyWeakref_CheckRef(ref));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4117 if (PyWeakref_GET_OBJECT(ref) == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4118 return PyList_SetItem(list, i, newobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4119 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4120 result = PyList_Append(list, newobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4121 Py_DECREF(newobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4122 return result;
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4123 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4124
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4125 static void
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4126 remove_subclass(PyTypeObject *base, PyTypeObject *type)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4127 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4128 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4129 PyObject *list, *ref;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4130
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4131 list = base->tp_subclasses;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4132 if (list == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4133 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4134 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4135 assert(PyList_Check(list));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4136 i = PyList_GET_SIZE(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4137 while (--i >= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4138 ref = PyList_GET_ITEM(list, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4139 assert(PyWeakref_CheckRef(ref));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4140 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4141 /* this can't fail, right? */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4142 PySequence_DelItem(list, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4143 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4144 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4145 }
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4146 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4147
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4148 static int
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4149 check_num_args(PyObject *ob, int n)
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4150 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4151 if (!PyTuple_CheckExact(ob)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4152 PyErr_SetString(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4153 "PyArg_UnpackTuple() argument list is not a tuple");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4154 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4155 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4156 if (n == PyTuple_GET_SIZE(ob))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4157 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4158 PyErr_Format(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4159 PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4160 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4161 return 0;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4162 }
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4163
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4164 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4165
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4166 /* There's a wrapper *function* for each distinct function typedef used
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4167 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4168 wrapper *table* for each distinct operation (e.g. __len__, __add__).
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4169 Most tables have only one entry; the tables for binary operators have two
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4170 entries, one regular and one with reversed arguments. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4171
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4172 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4173 wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4174 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4175 lenfunc func = (lenfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4176 Py_ssize_t res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4177
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4178 if (!check_num_args(args, 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4179 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4180 res = (*func)(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4181 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4182 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4183 return PyInt_FromLong((long)res);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4184 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4185
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4186 static PyObject *
30670
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4187 wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4188 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4189 inquiry func = (inquiry)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4190 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4191
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4192 if (!check_num_args(args, 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4193 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4194 res = (*func)(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4195 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4196 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4197 return PyBool_FromLong((long)res);
30670
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4198 }
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4199
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4200 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4201 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4202 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4203 binaryfunc func = (binaryfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4204 PyObject *other;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4205
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4206 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4207 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4208 other = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4209 return (*func)(self, other);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4210 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4211
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4212 static PyObject *
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4213 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4214 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4215 binaryfunc func = (binaryfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4216 PyObject *other;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4217
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4218 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4219 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4220 other = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4221 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4222 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4223 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4224 return Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4225 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4226 return (*func)(self, other);
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4227 }
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4228
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4229 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4230 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4231 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4232 binaryfunc func = (binaryfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4233 PyObject *other;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4234
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4235 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4236 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4237 other = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4238 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4239 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4240 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4241 return Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4242 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4243 return (*func)(other, self);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4244 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4245
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4246 static PyObject *
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4247 wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4248 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4249 coercion func = (coercion)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4250 PyObject *other, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4251 int ok;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4252
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4253 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4254 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4255 other = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4256 ok = func(&self, &other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4257 if (ok < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4258 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4259 if (ok > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4260 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4261 return Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4262 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4263 res = PyTuple_New(2);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4264 if (res == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4265 Py_DECREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4266 Py_DECREF(other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4267 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4268 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4269 PyTuple_SET_ITEM(res, 0, self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4270 PyTuple_SET_ITEM(res, 1, other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4271 return res;
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4272 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4273
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4274 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4275 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4276 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4277 ternaryfunc func = (ternaryfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4278 PyObject *other;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4279 PyObject *third = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4280
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4281 /* Note: This wrapper only works for __pow__() */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4282
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4283 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4284 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4285 return (*func)(self, other, third);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4286 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4287
20168
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4288 static PyObject *
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4289 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4290 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4291 ternaryfunc func = (ternaryfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4292 PyObject *other;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4293 PyObject *third = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4294
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4295 /* Note: This wrapper only works for __pow__() */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4296
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4297 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4298 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4299 return (*func)(other, self, third);
20168
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4300 }
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4301
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4302 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4303 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4304 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4305 unaryfunc func = (unaryfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4306
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4307 if (!check_num_args(args, 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4308 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4309 return (*func)(self);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4310 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4311
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4312 static PyObject *
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
4313 wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4314 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4315 ssizeargfunc func = (ssizeargfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4316 PyObject* o;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4317 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4318
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4319 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4320 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4321 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4322 if (i == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4323 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4324 return (*func)(self, i);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4325 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4326
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4327 static Py_ssize_t
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4328 getindex(PyObject *self, PyObject *arg)
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4329 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4330 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4331
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4332 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4333 if (i == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4334 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4335 if (i < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4336 PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4337 if (sq && sq->sq_length) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4338 Py_ssize_t n = (*sq->sq_length)(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4339 if (n < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4340 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4341 i += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4342 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4343 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4344 return i;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4345 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4346
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4347 static PyObject *
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4348 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4349 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4350 ssizeargfunc func = (ssizeargfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4351 PyObject *arg;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4352 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4353
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4354 if (PyTuple_GET_SIZE(args) == 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4355 arg = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4356 i = getindex(self, arg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4357 if (i == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4358 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4359 return (*func)(self, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4360 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4361 check_num_args(args, 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4362 assert(PyErr_Occurred());
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4363 return NULL;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4364 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4365
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4366 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4367 wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4368 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4369 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4370 Py_ssize_t i, j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4371
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4372 if (!PyArg_ParseTuple(args, "nn", &i, &j))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4373 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4374 return (*func)(self, i, j);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4375 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4376
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4377 static PyObject *
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4378 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4379 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4380 ssizeobjargproc func = (ssizeobjargproc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4381 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4382 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4383 PyObject *arg, *value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4384
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4385 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4386 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4387 i = getindex(self, arg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4388 if (i == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4389 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4390 res = (*func)(self, i, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4391 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4392 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4393 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4394 return Py_None;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4395 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4396
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4397 static PyObject *
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4398 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4399 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4400 ssizeobjargproc func = (ssizeobjargproc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4401 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4402 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4403 PyObject *arg;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4404
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4405 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4406 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4407 arg = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4408 i = getindex(self, arg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4409 if (i == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4410 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4411 res = (*func)(self, i, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4412 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4413 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4414 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4415 return Py_None;
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4416 }
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4417
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4418 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4419 wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4420 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4421 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4422 Py_ssize_t i, j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4423 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4424 PyObject *value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4425
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4426 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4427 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4428 res = (*func)(self, i, j, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4429 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4430 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4431 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4432 return Py_None;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4433 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4434
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4435 static PyObject *
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4436 wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4437 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4438 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4439 Py_ssize_t i, j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4440 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4441
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4442 if (!PyArg_ParseTuple(args, "nn", &i, &j))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4443 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4444 res = (*func)(self, i, j, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4445 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4446 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4447 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4448 return Py_None;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4449 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4450
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4451 /* XXX objobjproc is a misnomer; should be objargpred */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4452 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4453 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4454 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4455 objobjproc func = (objobjproc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4456 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4457 PyObject *value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4458
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4459 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4460 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4461 value = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4462 res = (*func)(self, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4463 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4464 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4465 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4466 return PyBool_FromLong(res);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4467 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4468
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4469 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4470 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4471 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4472 objobjargproc func = (objobjargproc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4473 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4474 PyObject *key, *value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4475
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4476 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4477 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4478 res = (*func)(self, key, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4479 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4480 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4481 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4482 return Py_None;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4483 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4484
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4485 static PyObject *
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4486 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4487 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4488 objobjargproc func = (objobjargproc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4489 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4490 PyObject *key;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4491
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4492 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4493 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4494 key = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4495 res = (*func)(self, key, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4496 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4497 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4498 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4499 return Py_None;
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4500 }
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4501
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4502 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4503 wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4504 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4505 cmpfunc func = (cmpfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4506 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4507 PyObject *other;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4508
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4509 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4510 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4511 other = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4512 if (Py_TYPE(other)->tp_compare != func &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4513 !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4514 PyErr_Format(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4515 PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4516 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4517 Py_TYPE(self)->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4518 Py_TYPE(self)->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4519 Py_TYPE(other)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4520 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4521 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4522 res = (*func)(self, other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4523 if (PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4524 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4525 return PyInt_FromLong((long)res);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4526 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4527
28608
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4528 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
28621
824dc28f922d Ouch, it's Carlo Verre, not Verre Carlo.
Guido van Rossum <guido@python.org>
parents: 28608
diff changeset
4529 This is called the Carlo Verre hack after its discoverer. */
28608
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4530 static int
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4531 hackcheck(PyObject *self, setattrofunc func, char *what)
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4532 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4533 PyTypeObject *type = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4534 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4535 type = type->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4536 /* If type is NULL now, this is a really weird type.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4537 In the spirit of backwards compatibility (?), just shut up. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4538 if (type && type->tp_setattro != func) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4539 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4540 "can't apply this %s to %s object",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4541 what,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4542 type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4543 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4544 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4545 return 1;
28608
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4546 }
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4547
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4548 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4549 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4550 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4551 setattrofunc func = (setattrofunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4552 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4553 PyObject *name, *value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4554
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4555 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4556 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4557 if (!hackcheck(self, func, "__setattr__"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4558 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4559 res = (*func)(self, name, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4560 if (res < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4561 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4562 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4563 return Py_None;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4564 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4565
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4566 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4567 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4568 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4569 setattrofunc func = (setattrofunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4570 int res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4571 PyObject *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4572
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4573 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4574 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4575 name = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4576 if (!hackcheck(self, func, "__delattr__"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4577 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4578 res = (*func)(self, name, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4579 if (res < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4580 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4581 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4582 return Py_None;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4583 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4584
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4585 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4586 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4587 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4588 hashfunc func = (hashfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4589 long res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4590
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4591 if (!check_num_args(args, 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4592 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4593 res = (*func)(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4594 if (res == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4595 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4596 return PyInt_FromLong(res);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4597 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4598
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4599 static PyObject *
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
4600 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4601 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4602 ternaryfunc func = (ternaryfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4603
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4604 return (*func)(self, args, kwds);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4605 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4606
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4607 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4608 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4609 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4610 richcmpfunc func = (richcmpfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4611 PyObject *other;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4612
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4613 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4614 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4615 other = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4616 return (*func)(self, other, op);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4617 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4618
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4619 #undef RICHCMP_WRAPPER
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4620 #define RICHCMP_WRAPPER(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4621 static PyObject * \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4622 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4623 { \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4624 return wrap_richcmpfunc(self, args, wrapped, OP); \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4625 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4626
19070
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
4627 RICHCMP_WRAPPER(lt, Py_LT)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
4628 RICHCMP_WRAPPER(le, Py_LE)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
4629 RICHCMP_WRAPPER(eq, Py_EQ)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
4630 RICHCMP_WRAPPER(ne, Py_NE)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
4631 RICHCMP_WRAPPER(gt, Py_GT)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
4632 RICHCMP_WRAPPER(ge, Py_GE)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4633
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4634 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4635 wrap_next(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4636 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4637 unaryfunc func = (unaryfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4638 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4639
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4640 if (!check_num_args(args, 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4641 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4642 res = (*func)(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4643 if (res == NULL && !PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4644 PyErr_SetNone(PyExc_StopIteration);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4645 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4646 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4647
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4648 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4649 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4650 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4651 descrgetfunc func = (descrgetfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4652 PyObject *obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4653 PyObject *type = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4654
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4655 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4656 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4657 if (obj == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4658 obj = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4659 if (type == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4660 type = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4661 if (type == NULL &&obj == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4662 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4663 "__get__(None, None) is invalid");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4664 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4665 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4666 return (*func)(self, obj, type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4667 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4668
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4669 static PyObject *
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4670 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4671 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4672 descrsetfunc func = (descrsetfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4673 PyObject *obj, *value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4674 int ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4675
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4676 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4677 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4678 ret = (*func)(self, obj, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4679 if (ret < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4680 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4681 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4682 return Py_None;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4683 }
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
4684
24460
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4685 static PyObject *
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4686 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4687 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4688 descrsetfunc func = (descrsetfunc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4689 PyObject *obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4690 int ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4691
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4692 if (!check_num_args(args, 1))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4693 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4694 obj = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4695 ret = (*func)(self, obj, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4696 if (ret < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4697 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4698 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4699 return Py_None;
24460
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4700 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4701
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4702 static PyObject *
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
4703 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4704 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4705 initproc func = (initproc)wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4706
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4707 if (func(self, args, kwds) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4708 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4709 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4710 return Py_None;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4711 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4712
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4713 static PyObject *
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4714 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4715 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4716 PyTypeObject *type, *subtype, *staticbase;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4717 PyObject *arg0, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4718
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4719 if (self == NULL || !PyType_Check(self))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4720 Py_FatalError("__new__() called with non-type 'self'");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4721 type = (PyTypeObject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4722 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4723 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4724 "%s.__new__(): not enough arguments",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4725 type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4726 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4727 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4728 arg0 = PyTuple_GET_ITEM(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4729 if (!PyType_Check(arg0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4730 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4731 "%s.__new__(X): X is not a type object (%s)",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4732 type->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4733 Py_TYPE(arg0)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4734 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4735 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4736 subtype = (PyTypeObject *)arg0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4737 if (!PyType_IsSubtype(subtype, type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4738 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4739 "%s.__new__(%s): %s is not a subtype of %s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4740 type->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4741 subtype->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4742 subtype->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4743 type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4744 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4745 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4746
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4747 /* Check that the use doesn't do something silly and unsafe like
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4748 object.__new__(dict). To do this, we check that the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4749 most derived base that's not a heap type is this type. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4750 staticbase = subtype;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4751 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4752 staticbase = staticbase->tp_base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4753 /* If staticbase is NULL now, it is a really weird type.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4754 In the spirit of backwards compatibility (?), just shut up. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4755 if (staticbase && staticbase->tp_new != type->tp_new) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4756 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4757 "%s.__new__(%s) is not safe, use %s.__new__()",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4758 type->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4759 subtype->tp_name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4760 staticbase == NULL ? "?" : staticbase->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4761 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4762 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4763
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4764 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4765 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4766 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4767 res = type->tp_new(subtype, args, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4768 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4769 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4770 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4771
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4772 static struct PyMethodDef tp_new_methoddef[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4773 {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4774 PyDoc_STR("T.__new__(S, ...) -> "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4775 "a new object with type S, a subtype of T")},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4776 {0}
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4777 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4778
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4779 static int
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4780 add_tp_new_wrapper(PyTypeObject *type)
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4781 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4782 PyObject *func;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4783
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4784 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4785 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4786 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4787 if (func == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4788 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4789 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4790 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4791 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4792 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4793 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4794 return 0;
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4795 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4796
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
4797 /* Slot wrappers that call the corresponding __foo__ slot. See comments
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
4798 below at override_slots() for more explanation. */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4799
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4800 #define SLOT0(FUNCNAME, OPSTR) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4801 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4802 FUNCNAME(PyObject *self) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4803 { \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4804 static PyObject *cache_str; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4805 return call_method(self, OPSTR, &cache_str, "()"); \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4806 }
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4807
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4808 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4809 static PyObject * \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4810 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4811 { \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4812 static PyObject *cache_str; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4813 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4814 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4815
27124
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4816 /* Boolean helper for SLOT1BINFULL().
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4817 right.__class__ is a nontrivial subclass of left.__class__. */
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4818 static int
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4819 method_is_overloaded(PyObject *left, PyObject *right, char *name)
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4820 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4821 PyObject *a, *b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4822 int ok;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4823
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4824 b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4825 if (b == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4826 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4827 /* If right doesn't have it, it's not overloaded */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4828 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4829 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4830
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4831 a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4832 if (a == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4833 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4834 Py_DECREF(b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4835 /* If right has it but left doesn't, it's overloaded */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4836 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4837 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4838
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4839 ok = PyObject_RichCompareBool(a, b, Py_NE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4840 Py_DECREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4841 Py_DECREF(b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4842 if (ok < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4843 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4844 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4845 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4846
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4847 return ok;
27124
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4848 }
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4849
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4850
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4851 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4852 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4853 FUNCNAME(PyObject *self, PyObject *other) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4854 { \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4855 static PyObject *cache_str, *rcache_str; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4856 int do_other = Py_TYPE(self) != Py_TYPE(other) && \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4857 Py_TYPE(other)->tp_as_number != NULL && \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4858 Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4859 if (Py_TYPE(self)->tp_as_number != NULL && \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4860 Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4861 PyObject *r; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4862 if (do_other && \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4863 PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4864 method_is_overloaded(self, other, ROPSTR)) { \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4865 r = call_maybe( \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4866 other, ROPSTR, &rcache_str, "(O)", self); \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4867 if (r != Py_NotImplemented) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4868 return r; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4869 Py_DECREF(r); \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4870 do_other = 0; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4871 } \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4872 r = call_maybe( \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4873 self, OPSTR, &cache_str, "(O)", other); \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4874 if (r != Py_NotImplemented || \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4875 Py_TYPE(other) == Py_TYPE(self)) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4876 return r; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4877 Py_DECREF(r); \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4878 } \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4879 if (do_other) { \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4880 return call_maybe( \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4881 other, ROPSTR, &rcache_str, "(O)", self); \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4882 } \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4883 Py_INCREF(Py_NotImplemented); \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4884 return Py_NotImplemented; \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4885 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4886
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4887 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4888 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4889
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4890 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4891 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4892 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4893 { \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4894 static PyObject *cache_str; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4895 return call_method(self, OPSTR, &cache_str, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4896 "(" ARGCODES ")", arg1, arg2); \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4897 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4898
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4899 static Py_ssize_t
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4900 slot_sq_length(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4901 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4902 static PyObject *len_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4903 PyObject *res = call_method(self, "__len__", &len_str, "()");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4904 Py_ssize_t len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4905
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4906 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4907 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4908 len = PyInt_AsSsize_t(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4909 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4910 if (len < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4911 if (!PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4912 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4913 "__len__() should return >= 0");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4914 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4915 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4916 return len;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4917 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4918
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4919 /* Super-optimized version of slot_sq_item.
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4920 Other slots could do the same... */
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4921 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4922 slot_sq_item(PyObject *self, Py_ssize_t i)
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4923 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4924 static PyObject *getitem_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4925 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4926 descrgetfunc f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4927
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4928 if (getitem_str == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4929 getitem_str = PyString_InternFromString("__getitem__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4930 if (getitem_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4931 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4932 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4933 func = _PyType_Lookup(Py_TYPE(self), getitem_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4934 if (func != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4935 if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4936 Py_INCREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4937 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4938 func = f(func, self, (PyObject *)(Py_TYPE(self)));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4939 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4940 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4941 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4942 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4943 ival = PyInt_FromSsize_t(i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4944 if (ival != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4945 args = PyTuple_New(1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4946 if (args != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4947 PyTuple_SET_ITEM(args, 0, ival);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4948 retval = PyObject_Call(func, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4949 Py_XDECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4950 Py_XDECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4951 return retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4952 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4953 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4954 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4955 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4956 PyErr_SetObject(PyExc_AttributeError, getitem_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4957 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4958 Py_XDECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4959 Py_XDECREF(ival);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4960 Py_XDECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4961 return NULL;
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4962 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4963
48854
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4964 static PyObject*
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4965 slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j)
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4966 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4967 static PyObject *getslice_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4968
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4969 if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4970 "use __getitem__", 1) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4971 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4972 return call_method(self, "__getslice__", &getslice_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4973 "nn", i, j);
48854
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4974 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4975
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4976 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4977 slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4978 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4979 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4980 static PyObject *delitem_str, *setitem_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4981
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4982 if (value == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4983 res = call_method(self, "__delitem__", &delitem_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4984 "(n)", index);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4985 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4986 res = call_method(self, "__setitem__", &setitem_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4987 "(nO)", index, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4988 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4989 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4990 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4991 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4992 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4993
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4994 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4995 slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4996 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4997 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4998 static PyObject *delslice_str, *setslice_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
4999
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5000 if (value == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5001 if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5002 "use __delitem__", 1) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5003 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5004 res = call_method(self, "__delslice__", &delslice_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5005 "(nn)", i, j);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5006 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5007 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5008 if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5009 "use __setitem__", 1) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5010 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5011 res = call_method(self, "__setslice__", &setslice_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5012 "(nnO)", i, j, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5013 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5014 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5015 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5016 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5017 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5018 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5019
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5020 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5021 slot_sq_contains(PyObject *self, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5022 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5023 PyObject *func, *res, *args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5024 int result = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5025
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5026 static PyObject *contains_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5027
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5028 func = lookup_maybe(self, "__contains__", &contains_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5029 if (func != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5030 args = PyTuple_Pack(1, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5031 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5032 res = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5033 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5034 res = PyObject_Call(func, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5035 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5036 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5037 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5038 if (res != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5039 result = PyObject_IsTrue(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5040 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5041 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5042 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5043 else if (! PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5044 /* Possible results: -1 and 1 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5045 result = (int)_PySequence_IterSearch(self, value,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5046 PY_ITERSEARCH_CONTAINS);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5047 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5048 return result;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5049 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5050
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5051 #define slot_mp_length slot_sq_length
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5052
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5053 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5054
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5055 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5056 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5057 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5058 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5059 static PyObject *delitem_str, *setitem_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5060
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5061 if (value == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5062 res = call_method(self, "__delitem__", &delitem_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5063 "(O)", key);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5064 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5065 res = call_method(self, "__setitem__", &setitem_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5066 "(OO)", key, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5067 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5068 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5069 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5070 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5071 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5072
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5073 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5074 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5075 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5076 SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5077 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5078 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5079
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
5080 static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5081
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5082 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5083 nb_power, "__pow__", "__rpow__")
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5084
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5085 static PyObject *
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5086 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5087 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5088 static PyObject *pow_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5089
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5090 if (modulus == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5091 return slot_nb_power_binary(self, other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5092 /* Three-arg power doesn't use __rpow__. But ternary_op
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5093 can call this when the second argument's type uses
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5094 slot_nb_power, so check before calling self.__pow__. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5095 if (Py_TYPE(self)->tp_as_number != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5096 Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5097 return call_method(self, "__pow__", &pow_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5098 "(OO)", other, modulus);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5099 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5100 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5101 return Py_NotImplemented;
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5102 }
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5103
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5104 SLOT0(slot_nb_negative, "__neg__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5105 SLOT0(slot_nb_positive, "__pos__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5106 SLOT0(slot_nb_absolute, "__abs__")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5107
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5108 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5109 slot_nb_nonzero(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5110 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5111 PyObject *func, *args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5112 static PyObject *nonzero_str, *len_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5113 int result = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5114 int using_len = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5115
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5116 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5117 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5118 if (PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5119 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5120 func = lookup_maybe(self, "__len__", &len_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5121 if (func == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5122 return PyErr_Occurred() ? -1 : 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5123 using_len = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5124 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5125 args = PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5126 if (args != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5127 PyObject *temp = PyObject_Call(func, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5128 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5129 if (temp != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5130 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5131 result = PyObject_IsTrue(temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5132 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5133 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5134 "%s should return "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5135 "bool or int, returned %s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5136 (using_len ? "__len__"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5137 : "__nonzero__"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5138 temp->ob_type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5139 result = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5140 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5141 Py_DECREF(temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5142 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5143 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5144 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5145 return result;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5146 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5147
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5148
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39415
diff changeset
5149 static PyObject *
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5150 slot_nb_index(PyObject *self)
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5151 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5152 static PyObject *index_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5153 return call_method(self, "__index__", &index_str, "()");
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5154 }
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5155
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5156
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5157 SLOT0(slot_nb_invert, "__invert__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5158 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5159 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5160 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5161 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5162 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5163
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5164 static int
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5165 slot_nb_coerce(PyObject **a, PyObject **b)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5166 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5167 static PyObject *coerce_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5168 PyObject *self = *a, *other = *b;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5169
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5170 if (self->ob_type->tp_as_number != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5171 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5172 PyObject *r;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5173 r = call_maybe(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5174 self, "__coerce__", &coerce_str, "(O)", other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5175 if (r == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5176 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5177 if (r == Py_NotImplemented) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5178 Py_DECREF(r);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5179 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5180 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5181 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5182 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5183 "__coerce__ didn't return a 2-tuple");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5184 Py_DECREF(r);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5185 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5186 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5187 *a = PyTuple_GET_ITEM(r, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5188 Py_INCREF(*a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5189 *b = PyTuple_GET_ITEM(r, 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5190 Py_INCREF(*b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5191 Py_DECREF(r);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5192 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5193 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5194 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5195 if (other->ob_type->tp_as_number != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5196 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5197 PyObject *r;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5198 r = call_maybe(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5199 other, "__coerce__", &coerce_str, "(O)", self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5200 if (r == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5201 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5202 if (r == Py_NotImplemented) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5203 Py_DECREF(r);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5204 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5205 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5206 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5207 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5208 "__coerce__ didn't return a 2-tuple");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5209 Py_DECREF(r);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5210 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5211 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5212 *a = PyTuple_GET_ITEM(r, 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5213 Py_INCREF(*a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5214 *b = PyTuple_GET_ITEM(r, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5215 Py_INCREF(*b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5216 Py_DECREF(r);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5217 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5218 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5219 return 1;
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5220 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5221
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5222 SLOT0(slot_nb_int, "__int__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5223 SLOT0(slot_nb_long, "__long__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5224 SLOT0(slot_nb_float, "__float__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5225 SLOT0(slot_nb_oct, "__oct__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5226 SLOT0(slot_nb_hex, "__hex__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5227 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5228 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5229 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5230 SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5231 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
40777
f6f391d1d050 Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
Martin v. Löwis <martin@v.loewis.de>
parents: 40707
diff changeset
5232 /* Can't use SLOT1 here, because nb_inplace_power is ternary */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5233 static PyObject *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5234 slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5235 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5236 static PyObject *cache_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5237 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
40777
f6f391d1d050 Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
Martin v. Löwis <martin@v.loewis.de>
parents: 40707
diff changeset
5238 }
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5239 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5240 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5241 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5242 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5243 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5244 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5245 "__floordiv__", "__rfloordiv__")
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5246 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5247 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5248 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5249
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5250 static int
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5251 half_compare(PyObject *self, PyObject *other)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5252 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5253 PyObject *func, *args, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5254 static PyObject *cmp_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5255 Py_ssize_t c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5256
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5257 func = lookup_method(self, "__cmp__", &cmp_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5258 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5259 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5260 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5261 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5262 args = PyTuple_Pack(1, other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5263 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5264 res = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5265 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5266 res = PyObject_Call(func, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5267 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5268 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5269 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5270 if (res != Py_NotImplemented) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5271 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5272 return -2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5273 c = PyInt_AsLong(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5274 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5275 if (c == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5276 return -2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5277 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5278 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5279 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5280 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5281 return 2;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5282 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5283
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
5284 /* This slot is published for the benefit of try_3way_compare in object.c */
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
5285 int
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
5286 _PyObject_SlotCompare(PyObject *self, PyObject *other)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5287 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5288 int c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5289
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5290 if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5291 c = half_compare(self, other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5292 if (c <= 1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5293 return c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5294 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5295 if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5296 c = half_compare(other, self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5297 if (c < -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5298 return -2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5299 if (c <= 1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5300 return -c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5301 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5302 return (void *)self < (void *)other ? -1 :
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5303 (void *)self > (void *)other ? 1 : 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5304 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5305
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5306 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5307 slot_tp_repr(PyObject *self)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5308 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5309 PyObject *func, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5310 static PyObject *repr_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5311
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5312 func = lookup_method(self, "__repr__", &repr_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5313 if (func != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5314 res = PyEval_CallObject(func, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5315 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5316 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5317 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5318 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5319 return PyString_FromFormat("<%s object at %p>",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5320 Py_TYPE(self)->tp_name, self);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5321 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5322
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5323 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5324 slot_tp_str(PyObject *self)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5325 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5326 PyObject *func, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5327 static PyObject *str_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5328
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5329 func = lookup_method(self, "__str__", &str_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5330 if (func != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5331 res = PyEval_CallObject(func, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5332 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5333 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5334 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5335 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5336 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5337 return slot_tp_repr(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5338 }
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5339 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5340
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5341 static long
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5342 slot_tp_hash(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5343 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5344 PyObject *func;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5345 static PyObject *hash_str, *eq_str, *cmp_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5346 long h;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5347
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5348 func = lookup_method(self, "__hash__", &hash_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5349
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5350 if (func != NULL && func != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5351 PyObject *res = PyEval_CallObject(func, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5352 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5353 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5354 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5355 if (PyLong_Check(res))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5356 h = PyLong_Type.tp_hash(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5357 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5358 h = PyInt_AsLong(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5359 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5360 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5361 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5362 Py_XDECREF(func); /* may be None */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5363 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5364 func = lookup_method(self, "__eq__", &eq_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5365 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5366 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5367 func = lookup_method(self, "__cmp__", &cmp_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5368 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5369 if (func != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5370 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5371 return PyObject_HashNotImplemented(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5372 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5373 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5374 h = _Py_HashPointer((void *)self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5375 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5376 if (h == -1 && !PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5377 h = -2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5378 return h;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5379 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5380
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5381 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5382 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5383 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5384 static PyObject *call_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5385 PyObject *meth = lookup_method(self, "__call__", &call_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5386 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5387
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5388 if (meth == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5389 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5390
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5391 res = PyObject_Call(meth, args, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5392
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5393 Py_DECREF(meth);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5394 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5395 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5396
20499
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
5397 /* There are two slot dispatch functions for tp_getattro.
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
5398
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
5399 - slot_tp_getattro() is used when __getattribute__ is overridden
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
5400 but no __getattr__ hook is present;
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
5401
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
5402 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
5403
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
5404 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
5405 detects the absence of __getattr__ and then installs the simpler slot if
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
5406 necessary. */
20499
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
5407
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5408 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5409 slot_tp_getattro(PyObject *self, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5410 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5411 static PyObject *getattribute_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5412 return call_method(self, "__getattribute__", &getattribute_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5413 "(O)", name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5414 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5415
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5416 static PyObject *
49741
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5417 call_attribute(PyObject *self, PyObject *attr, PyObject *name)
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5418 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5419 PyObject *res, *descr = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5420 descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5421
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5422 if (f != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5423 descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5424 if (descr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5425 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5426 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5427 attr = descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5428 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5429 res = PyObject_CallFunctionObjArgs(attr, name, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5430 Py_XDECREF(descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5431 return res;
49741
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5432 }
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5433
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5434 static PyObject *
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5435 slot_tp_getattr_hook(PyObject *self, PyObject *name)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5436 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5437 PyTypeObject *tp = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5438 PyObject *getattr, *getattribute, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5439 static PyObject *getattribute_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5440 static PyObject *getattr_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5441
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5442 if (getattr_str == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5443 getattr_str = PyString_InternFromString("__getattr__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5444 if (getattr_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5445 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5446 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5447 if (getattribute_str == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5448 getattribute_str =
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5449 PyString_InternFromString("__getattribute__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5450 if (getattribute_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5451 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5452 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5453 /* speed hack: we could use lookup_maybe, but that would resolve the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5454 method fully for each attribute lookup for classes with
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5455 __getattr__, even when the attribute is present. So we use
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5456 _PyType_Lookup and create the method only when needed, with
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5457 call_attribute. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5458 getattr = _PyType_Lookup(tp, getattr_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5459 if (getattr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5460 /* No __getattr__ hook: use a simpler dispatcher */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5461 tp->tp_getattro = slot_tp_getattro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5462 return slot_tp_getattro(self, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5463 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5464 Py_INCREF(getattr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5465 /* speed hack: we could use lookup_maybe, but that would resolve the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5466 method fully for each attribute lookup for classes with
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5467 __getattr__, even when self has the default __getattribute__
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5468 method. So we use _PyType_Lookup and create the method only when
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5469 needed, with call_attribute. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5470 getattribute = _PyType_Lookup(tp, getattribute_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5471 if (getattribute == NULL ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5472 (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5473 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5474 (void *)PyObject_GenericGetAttr))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5475 res = PyObject_GenericGetAttr(self, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5476 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5477 Py_INCREF(getattribute);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5478 res = call_attribute(self, getattribute, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5479 Py_DECREF(getattribute);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5480 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5481 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5482 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5483 res = call_attribute(self, getattr, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5484 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5485 Py_DECREF(getattr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5486 return res;
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5487 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5488
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5489 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5490 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5491 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5492 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5493 static PyObject *delattr_str, *setattr_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5494
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5495 if (value == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5496 res = call_method(self, "__delattr__", &delattr_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5497 "(O)", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5498 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5499 res = call_method(self, "__setattr__", &setattr_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5500 "(OO)", name, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5501 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5502 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5503 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5504 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5505 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5506
44357
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5507 static char *name_op[] = {
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5508 "__lt__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5509 "__le__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5510 "__eq__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5511 "__ne__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5512 "__gt__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5513 "__ge__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5514 };
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5515
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5516 static PyObject *
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5517 half_richcompare(PyObject *self, PyObject *other, int op)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5518 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5519 PyObject *func, *args, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5520 static PyObject *op_str[6];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5521
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5522 func = lookup_method(self, name_op[op], &op_str[op]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5523 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5524 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5525 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5526 return Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5527 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5528 args = PyTuple_Pack(1, other);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5529 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5530 res = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5531 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5532 res = PyObject_Call(func, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5533 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5534 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5535 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5536 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5537 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5538
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5539 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5540 slot_tp_richcompare(PyObject *self, PyObject *other, int op)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5541 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5542 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5543
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5544 if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5545 res = half_richcompare(self, other, op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5546 if (res != Py_NotImplemented)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5547 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5548 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5549 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5550 if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5551 res = half_richcompare(other, self, _Py_SwappedOp[op]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5552 if (res != Py_NotImplemented) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5553 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5554 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5555 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5556 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5557 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5558 return Py_NotImplemented;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5559 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5560
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5561 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5562 slot_tp_iter(PyObject *self)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5563 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5564 PyObject *func, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5565 static PyObject *iter_str, *getitem_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5566
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5567 func = lookup_method(self, "__iter__", &iter_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5568 if (func != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5569 PyObject *args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5570 args = res = PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5571 if (args != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5572 res = PyObject_Call(func, args, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5573 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5574 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5575 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5576 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5577 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5578 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5579 func = lookup_method(self, "__getitem__", &getitem_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5580 if (func == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5581 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5582 "'%.200s' object is not iterable",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5583 Py_TYPE(self)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5584 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5585 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5586 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5587 return PySeqIter_New(self);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5588 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5589
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5590 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5591 slot_tp_iternext(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5592 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5593 static PyObject *next_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5594 return call_method(self, "next", &next_str, "()");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5595 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5596
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5597 static PyObject *
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5598 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5599 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5600 PyTypeObject *tp = Py_TYPE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5601 PyObject *get;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5602 static PyObject *get_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5603
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5604 if (get_str == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5605 get_str = PyString_InternFromString("__get__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5606 if (get_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5607 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5608 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5609 get = _PyType_Lookup(tp, get_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5610 if (get == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5611 /* Avoid further slowdowns */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5612 if (tp->tp_descr_get == slot_tp_descr_get)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5613 tp->tp_descr_get = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5614 Py_INCREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5615 return self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5616 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5617 if (obj == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5618 obj = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5619 if (type == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5620 type = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5621 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5622 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5623
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5624 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5625 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5626 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5627 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5628 static PyObject *del_str, *set_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5629
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5630 if (value == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5631 res = call_method(self, "__delete__", &del_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5632 "(O)", target);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5633 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5634 res = call_method(self, "__set__", &set_str,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5635 "(OO)", target, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5636 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5637 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5638 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5639 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5640 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5641
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5642 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5643 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5644 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5645 static PyObject *init_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5646 PyObject *meth = lookup_method(self, "__init__", &init_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5647 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5648
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5649 if (meth == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5650 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5651 res = PyObject_Call(meth, args, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5652 Py_DECREF(meth);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5653 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5654 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5655 if (res != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5656 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5657 "__init__() should return None, not '%.200s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5658 Py_TYPE(res)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5659 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5660 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5661 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5662 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5663 return 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5664 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5665
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5666 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5667 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5668 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5669 static PyObject *new_str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5670 PyObject *func;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5671 PyObject *newargs, *x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5672 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5673
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5674 if (new_str == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5675 new_str = PyString_InternFromString("__new__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5676 if (new_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5677 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5678 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5679 func = PyObject_GetAttr((PyObject *)type, new_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5680 if (func == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5681 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5682 assert(PyTuple_Check(args));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5683 n = PyTuple_GET_SIZE(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5684 newargs = PyTuple_New(n+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5685 if (newargs == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5686 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5687 Py_INCREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5688 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5689 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5690 x = PyTuple_GET_ITEM(args, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5691 Py_INCREF(x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5692 PyTuple_SET_ITEM(newargs, i+1, x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5693 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5694 x = PyObject_Call(func, newargs, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5695 Py_DECREF(newargs);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5696 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5697 return x;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5698 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5699
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5700 static void
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5701 slot_tp_del(PyObject *self)
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5702 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5703 static PyObject *del_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5704 PyObject *del, *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5705 PyObject *error_type, *error_value, *error_traceback;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5706
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5707 /* Temporarily resurrect the object. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5708 assert(self->ob_refcnt == 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5709 self->ob_refcnt = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5710
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5711 /* Save the current exception, if any. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5712 PyErr_Fetch(&error_type, &error_value, &error_traceback);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5713
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5714 /* Execute __del__ method, if any. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5715 del = lookup_maybe(self, "__del__", &del_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5716 if (del != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5717 res = PyEval_CallObject(del, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5718 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5719 PyErr_WriteUnraisable(del);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5720 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5721 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5722 Py_DECREF(del);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5723 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5724
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5725 /* Restore the saved exception. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5726 PyErr_Restore(error_type, error_value, error_traceback);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5727
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5728 /* Undo the temporary resurrection; can't use DECREF here, it would
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5729 * cause a recursive call.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5730 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5731 assert(self->ob_refcnt > 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5732 if (--self->ob_refcnt == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5733 return; /* this is the normal path out */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5734
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5735 /* __del__ resurrected it! Make it look like the original Py_DECREF
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5736 * never happened.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5737 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5738 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5739 Py_ssize_t refcnt = self->ob_refcnt;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5740 _Py_NewReference(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5741 self->ob_refcnt = refcnt;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5742 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5743 assert(!PyType_IS_GC(Py_TYPE(self)) ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5744 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5745 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5746 * we need to undo that. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5747 _Py_DEC_REFTOTAL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5748 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5749 * chain, so no more to do there.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5750 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5751 * _Py_NewReference bumped tp_allocs: both of those need to be
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5752 * undone.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5753 */
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5754 #ifdef COUNT_ALLOCS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5755 --Py_TYPE(self)->tp_frees;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5756 --Py_TYPE(self)->tp_allocs;
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5757 #endif
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5758 }
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5759
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5760
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5761 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
28390
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5762 functions. The offsets here are relative to the 'PyHeapTypeObject'
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
5763 structure, which incorporates the additional structures used for numbers,
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
5764 sequences and mappings.
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
5765 Note that multiple names may map to the same slot (e.g. __eq__,
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5766 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
5767 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
5768 terminated with an all-zero entry. (This table is further initialized and
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
5769 sorted in init_slotdefs() below.) */
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5770
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5771 typedef struct wrapperbase slotdef;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5772
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5773 #undef TPSLOT
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
5774 #undef FLSLOT
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5775 #undef ETSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5776 #undef SQSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5777 #undef MPSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5778 #undef NBSLOT
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5779 #undef UNSLOT
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5780 #undef IBSLOT
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5781 #undef BINSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5782 #undef RBINSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5783
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5784 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5785 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5786 PyDoc_STR(DOC)}
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
5787 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5788 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5789 PyDoc_STR(DOC), FLAGS}
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5790 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5791 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5792 PyDoc_STR(DOC)}
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5793 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5794 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5795 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5796 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5797 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5798 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5799 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5800 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5801 "x." NAME "() <==> " DOC)
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5802 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5803 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5804 "x." NAME "(y) <==> x" DOC "y")
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5805 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5806 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5807 "x." NAME "(y) <==> x" DOC "y")
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5808 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5809 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5810 "x." NAME "(y) <==> y" DOC "x")
35004
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5811 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5812 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5813 "x." NAME "(y) <==> " DOC)
35004
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5814 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5815 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5816 "x." NAME "(y) <==> " DOC)
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5817
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5818 static slotdef slotdefs[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5819 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5820 "x.__len__() <==> len(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5821 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5822 The logic in abstract.c always falls back to nb_add/nb_multiply in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5823 this case. Defining both the nb_* and the sq_* slots to call the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5824 user-defined methods has unexpected side-effects, as shown by
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5825 test_descr.notimplemented() */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5826 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5827 "x.__add__(y) <==> x+y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5828 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5829 "x.__mul__(n) <==> x*n"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5830 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5831 "x.__rmul__(n) <==> n*x"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5832 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5833 "x.__getitem__(y) <==> x[y]"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5834 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5835 "x.__getslice__(i, j) <==> x[i:j]\n\
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5836 \n\
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5837 Use of negative indices is not supported."),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5838 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5839 "x.__setitem__(i, y) <==> x[i]=y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5840 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5841 "x.__delitem__(y) <==> del x[y]"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5842 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5843 wrap_ssizessizeobjargproc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5844 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5845 \n\
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5846 Use of negative indices is not supported."),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5847 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5848 "x.__delslice__(i, j) <==> del x[i:j]\n\
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5849 \n\
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5850 Use of negative indices is not supported."),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5851 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5852 "x.__contains__(y) <==> y in x"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5853 SQSLOT("__iadd__", sq_inplace_concat, NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5854 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5855 SQSLOT("__imul__", sq_inplace_repeat, NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5856 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5857
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5858 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5859 "x.__len__() <==> len(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5860 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5861 wrap_binaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5862 "x.__getitem__(y) <==> x[y]"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5863 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5864 wrap_objobjargproc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5865 "x.__setitem__(i, y) <==> x[i]=y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5866 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5867 wrap_delitem,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5868 "x.__delitem__(y) <==> del x[y]"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5869
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5870 BINSLOT("__add__", nb_add, slot_nb_add,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5871 "+"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5872 RBINSLOT("__radd__", nb_add, slot_nb_add,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5873 "+"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5874 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5875 "-"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5876 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5877 "-"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5878 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5879 "*"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5880 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5881 "*"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5882 BINSLOT("__div__", nb_divide, slot_nb_divide,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5883 "/"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5884 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5885 "/"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5886 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5887 "%"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5888 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5889 "%"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5890 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5891 "divmod(x, y)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5892 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5893 "divmod(y, x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5894 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5895 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5896 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5897 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5898 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5899 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5900 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5901 "abs(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5902 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5903 "x != 0"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5904 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5905 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5906 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5907 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5908 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5909 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5910 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5911 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5912 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5913 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5914 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5915 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5916 "x.__coerce__(y) <==> coerce(x, y)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5917 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5918 "int(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5919 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5920 "long(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5921 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5922 "float(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5923 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5924 "oct(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5925 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5926 "hex(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5927 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5928 "x[y:z] <==> x[y.__index__():z.__index__()]"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5929 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5930 wrap_binaryfunc, "+"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5931 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5932 wrap_binaryfunc, "-"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5933 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5934 wrap_binaryfunc, "*"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5935 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5936 wrap_binaryfunc, "/"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5937 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5938 wrap_binaryfunc, "%"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5939 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5940 wrap_binaryfunc, "**"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5941 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5942 wrap_binaryfunc, "<<"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5943 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5944 wrap_binaryfunc, ">>"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5945 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5946 wrap_binaryfunc, "&"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5947 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5948 wrap_binaryfunc, "^"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5949 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5950 wrap_binaryfunc, "|"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5951 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5952 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5953 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5954 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5955 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5956 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5957 IBSLOT("__itruediv__", nb_inplace_true_divide,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5958 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5959
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5960 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5961 "x.__str__() <==> str(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5962 TPSLOT("__str__", tp_print, NULL, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5963 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5964 "x.__repr__() <==> repr(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5965 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5966 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5967 "x.__cmp__(y) <==> cmp(x,y)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5968 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5969 "x.__hash__() <==> hash(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5970 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5971 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5972 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5973 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5974 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5975 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5976 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5977 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5978 "x.__setattr__('name', value) <==> x.name = value"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5979 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5980 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5981 "x.__delattr__('name') <==> del x.name"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5982 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5983 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5984 "x.__lt__(y) <==> x<y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5985 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5986 "x.__le__(y) <==> x<=y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5987 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5988 "x.__eq__(y) <==> x==y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5989 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5990 "x.__ne__(y) <==> x!=y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5991 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5992 "x.__gt__(y) <==> x>y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5993 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5994 "x.__ge__(y) <==> x>=y"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5995 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5996 "x.__iter__() <==> iter(x)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5997 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5998 "x.next() -> the next value, or raise StopIteration"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
5999 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6000 "descr.__get__(obj[, type]) -> value"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6001 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6002 "descr.__set__(obj, value)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6003 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6004 wrap_descr_delete, "descr.__delete__(obj)"),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6005 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6006 "x.__init__(...) initializes x; "
64078
756f8b398230 Merged revisions 84106 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62105
diff changeset
6007 "see help(type(x)) for signature",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6008 PyWrapperFlag_KEYWORDS),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6009 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6010 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6011 {NULL}
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6012 };
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6013
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6014 /* Given a type pointer and an offset gotten from a slotdef entry, return a
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6015 pointer to the actual slot. This is not quite the same as simply adding
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6016 the offset to the type pointer, since it takes care to indirect through the
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6017 proper indirection pointer (as_buffer, etc.); it returns NULL if the
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6018 indirection pointer is NULL. */
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6019 static void **
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
6020 slotptr(PyTypeObject *type, int ioffset)
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6021 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6022 char *ptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6023 long offset = ioffset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6024
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6025 /* Note: this depends on the order of the members of PyHeapTypeObject! */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6026 assert(offset >= 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6027 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6028 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6029 ptr = (char *)type->tp_as_sequence;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6030 offset -= offsetof(PyHeapTypeObject, as_sequence);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6031 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6032 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6033 ptr = (char *)type->tp_as_mapping;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6034 offset -= offsetof(PyHeapTypeObject, as_mapping);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6035 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6036 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6037 ptr = (char *)type->tp_as_number;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6038 offset -= offsetof(PyHeapTypeObject, as_number);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6039 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6040 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6041 ptr = (char *)type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6042 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6043 if (ptr != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6044 ptr += offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6045 return (void **)ptr;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6046 }
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
6047
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6048 /* Length of array of slotdef pointers used to store slots with the
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6049 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6050 the same __name__, for any __name__. Since that's a static property, it is
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6051 appropriate to declare fixed-size arrays for this. */
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6052 #define MAX_EQUIV 10
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6053
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6054 /* Return a slot pointer for a given name, but ONLY if the attribute has
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6055 exactly one slot function. The name must be an interned string. */
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6056 static void **
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6057 resolve_slotdups(PyTypeObject *type, PyObject *name)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6058 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6059 /* XXX Maybe this could be optimized more -- but is it worth it? */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6060
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6061 /* pname and ptrs act as a little cache */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6062 static PyObject *pname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6063 static slotdef *ptrs[MAX_EQUIV];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6064 slotdef *p, **pp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6065 void **res, **ptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6066
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6067 if (pname != name) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6068 /* Collect all slotdefs that match name into ptrs. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6069 pname = name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6070 pp = ptrs;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6071 for (p = slotdefs; p->name_strobj; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6072 if (p->name_strobj == name)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6073 *pp++ = p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6074 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6075 *pp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6076 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6077
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6078 /* Look in all matching slots of the type; if exactly one of these has
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6079 a filled-in slot, return its value. Otherwise return NULL. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6080 res = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6081 for (pp = ptrs; *pp; pp++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6082 ptr = slotptr(type, (*pp)->offset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6083 if (ptr == NULL || *ptr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6084 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6085 if (res != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6086 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6087 res = ptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6088 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6089 return res;
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6090 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6091
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6092 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6093 does some incredibly complex thinking and then sticks something into the
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6094 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6095 interests, and then stores a generic wrapper or a specific function into
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6096 the slot.) Return a pointer to the next slotdef with a different offset,
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6097 because that's convenient for fixup_slot_dispatchers(). */
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6098 static slotdef *
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6099 update_one_slot(PyTypeObject *type, slotdef *p)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6100 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6101 PyObject *descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6102 PyWrapperDescrObject *d;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6103 void *generic = NULL, *specific = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6104 int use_generic = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6105 int offset = p->offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6106 void **ptr = slotptr(type, offset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6107
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6108 if (ptr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6109 do {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6110 ++p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6111 } while (p->offset == offset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6112 return p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6113 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6114 do {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6115 descr = _PyType_Lookup(type, p->name_strobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6116 if (descr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6117 if (ptr == (void**)&type->tp_iternext) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6118 specific = _PyObject_NextNotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6119 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6120 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6121 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6122 if (Py_TYPE(descr) == &PyWrapperDescr_Type) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6123 void **tptr = resolve_slotdups(type, p->name_strobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6124 if (tptr == NULL || tptr == ptr)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6125 generic = p->function;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6126 d = (PyWrapperDescrObject *)descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6127 if (d->d_base->wrapper == p->wrapper &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6128 PyType_IsSubtype(type, d->d_type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6129 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6130 if (specific == NULL ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6131 specific == d->d_wrapped)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6132 specific = d->d_wrapped;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6133 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6134 use_generic = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6135 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6136 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6137 else if (Py_TYPE(descr) == &PyCFunction_Type &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6138 PyCFunction_GET_FUNCTION(descr) ==
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6139 (PyCFunction)tp_new_wrapper &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6140 ptr == (void**)&type->tp_new)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6141 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6142 /* The __new__ wrapper is not a wrapper descriptor,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6143 so must be special-cased differently.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6144 If we don't do this, creating an instance will
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6145 always use slot_tp_new which will look up
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6146 __new__ in the MRO which will call tp_new_wrapper
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6147 which will look through the base classes looking
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6148 for a static base and call its tp_new (usually
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6149 PyType_GenericNew), after performing various
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6150 sanity checks and constructing a new argument
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6151 list. Cut all that nonsense short -- this speeds
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6152 up instance creation tremendously. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6153 specific = (void *)type->tp_new;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6154 /* XXX I'm not 100% sure that there isn't a hole
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6155 in this reasoning that requires additional
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6156 sanity checks. I'll buy the first person to
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6157 point out a bug in this reasoning a beer. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6158 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6159 else if (descr == Py_None &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6160 ptr == (void**)&type->tp_hash) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6161 /* We specifically allow __hash__ to be set to None
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6162 to prevent inheritance of the default
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6163 implementation from object.__hash__ */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6164 specific = PyObject_HashNotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6165 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6166 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6167 use_generic = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6168 generic = p->function;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6169 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6170 } while ((++p)->offset == offset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6171 if (specific && !use_generic)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6172 *ptr = specific;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6173 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6174 *ptr = generic;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6175 return p;
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6176 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6177
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6178 /* In the type, update the slots whose slotdefs are gathered in the pp array.
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6179 This is a callback for update_subclasses(). */
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6180 static int
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6181 update_slots_callback(PyTypeObject *type, void *data)
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6182 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6183 slotdef **pp = (slotdef **)data;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6184
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6185 for (; *pp; pp++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6186 update_one_slot(type, *pp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6187 return 0;
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6188 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6189
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6190 /* Comparison function for qsort() to compare slotdefs by their offset, and
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6191 for equal offset by their address (to force a stable sort). */
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6192 static int
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6193 slotdef_cmp(const void *aa, const void *bb)
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6194 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6195 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6196 int c = a->offset - b->offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6197 if (c != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6198 return c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6199 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6200 /* Cannot use a-b, as this gives off_t,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6201 which may lose precision when converted to int. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6202 return (a > b) ? 1 : (a < b) ? -1 : 0;
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6203 }
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6204
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6205 /* Initialize the slotdefs table by adding interned string objects for the
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6206 names and sorting the entries. */
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6207 static void
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6208 init_slotdefs(void)
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6209 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6210 slotdef *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6211 static int initialized = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6212
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6213 if (initialized)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6214 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6215 for (p = slotdefs; p->name; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6216 p->name_strobj = PyString_InternFromString(p->name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6217 if (!p->name_strobj)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6218 Py_FatalError("Out of memory interning slotdef names");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6219 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6220 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6221 slotdef_cmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6222 initialized = 1;
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6223 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6224
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6225 /* Update the slots after assignment to a class (type) attribute. */
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6226 static int
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6227 update_slot(PyTypeObject *type, PyObject *name)
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6228 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6229 slotdef *ptrs[MAX_EQUIV];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6230 slotdef *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6231 slotdef **pp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6232 int offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6233
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6234 /* Clear the VALID_VERSION flag of 'type' and all its
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6235 subclasses. This could possibly be unified with the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6236 update_subclasses() recursion below, but carefully:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6237 they each have their own conditions on which to stop
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6238 recursing into subclasses. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6239 PyType_Modified(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6240
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6241 init_slotdefs();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6242 pp = ptrs;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6243 for (p = slotdefs; p->name; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6244 /* XXX assume name is interned! */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6245 if (p->name_strobj == name)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6246 *pp++ = p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6247 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6248 *pp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6249 for (pp = ptrs; *pp; pp++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6250 p = *pp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6251 offset = p->offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6252 while (p > slotdefs && (p-1)->offset == offset)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6253 --p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6254 *pp = p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6255 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6256 if (ptrs[0] == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6257 return 0; /* Not an attribute that affects any slots */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6258 return update_subclasses(type, name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6259 update_slots_callback, (void *)ptrs);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6260 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6261
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6262 /* Store the proper functions in the slot dispatches at class (type)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6263 definition time, based upon which operations the class overrides in its
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6264 dict. */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
6265 static void
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6266 fixup_slot_dispatchers(PyTypeObject *type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
6267 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6268 slotdef *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6269
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6270 init_slotdefs();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6271 for (p = slotdefs; p->name; )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6272 p = update_one_slot(type, p);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
6273 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6274
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6275 static void
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6276 update_all_slots(PyTypeObject* type)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6277 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6278 slotdef *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6279
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6280 init_slotdefs();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6281 for (p = slotdefs; p->name; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6282 /* update_slot returns int but can't actually fail */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6283 update_slot(type, p->name_strobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6284 }
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6285 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6286
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6287 /* recurse_down_subclasses() and update_subclasses() are mutually
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6288 recursive functions to call a callback for all subclasses,
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6289 but refraining from recursing into subclasses that define 'name'. */
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6290
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6291 static int
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6292 update_subclasses(PyTypeObject *type, PyObject *name,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6293 update_callback callback, void *data)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6294 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6295 if (callback(type, data) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6296 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6297 return recurse_down_subclasses(type, name, callback, data);
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6298 }
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6299
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6300 static int
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6301 recurse_down_subclasses(PyTypeObject *type, PyObject *name,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6302 update_callback callback, void *data)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6303 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6304 PyTypeObject *subclass;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6305 PyObject *ref, *subclasses, *dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6306 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6307
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6308 subclasses = type->tp_subclasses;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6309 if (subclasses == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6310 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6311 assert(PyList_Check(subclasses));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6312 n = PyList_GET_SIZE(subclasses);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6313 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6314 ref = PyList_GET_ITEM(subclasses, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6315 assert(PyWeakref_CheckRef(ref));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6316 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6317 assert(subclass != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6318 if ((PyObject *)subclass == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6319 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6320 assert(PyType_Check(subclass));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6321 /* Avoid recursing down into unaffected classes */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6322 dict = subclass->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6323 if (dict != NULL && PyDict_Check(dict) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6324 PyDict_GetItem(dict, name) != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6325 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6326 if (update_subclasses(subclass, name, callback, data) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6327 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6328 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6329 return 0;
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6330 }
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6331
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6332 /* This function is called by PyType_Ready() to populate the type's
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6333 dictionary with method descriptors for function slots. For each
23830
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6334 function slot (like tp_repr) that's defined in the type, one or more
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6335 corresponding descriptors are added in the type's tp_dict dictionary
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6336 under the appropriate name (like __repr__). Some function slots
23830
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6337 cause more than one descriptor to be added (for example, the nb_add
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6338 slot adds both __add__ and __radd__ descriptors) and some function
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6339 slots compete for the same descriptor (for example both sq_item and
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6340 mp_subscript generate a __getitem__ descriptor).
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6341
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6342 In the latter case, the first slotdef entry encoutered wins. Since
28390
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
6343 slotdef entries are sorted by the offset of the slot in the
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
6344 PyHeapTypeObject, this gives us some control over disambiguating
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6345 between competing slots: the members of PyHeapTypeObject are listed
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6346 from most general to least general, so the most general slot is
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6347 preferred. In particular, because as_mapping comes before as_sequence,
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6348 for a type that defines both mp_subscript and sq_item, mp_subscript
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6349 wins.
23830
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6350
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6351 This only adds new descriptors and doesn't overwrite entries in
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6352 tp_dict that were previously defined. The descriptors contain a
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6353 reference to the C function they must call, so that it's safe if they
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6354 are copied into a subtype's __dict__ and the subtype has a different
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6355 C function in its slot -- calling the method defined by the
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6356 descriptor will call the C function that was used to create it,
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6357 rather than the C function present in the slot when it is called.
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6358 (This is important because a subtype may have a C function in the
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6359 slot that calls the method from the dictionary, and we want to avoid
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6360 infinite recursion here.) */
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6361
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6362 static int
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6363 add_operators(PyTypeObject *type)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6364 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6365 PyObject *dict = type->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6366 slotdef *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6367 PyObject *descr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6368 void **ptr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6369
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6370 init_slotdefs();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6371 for (p = slotdefs; p->name; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6372 if (p->wrapper == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6373 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6374 ptr = slotptr(type, p->offset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6375 if (!ptr || !*ptr)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6376 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6377 if (PyDict_GetItem(dict, p->name_strobj))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6378 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6379 if (*ptr == PyObject_HashNotImplemented) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6380 /* Classes may prevent the inheritance of the tp_hash
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6381 slot by storing PyObject_HashNotImplemented in it. Make it
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6382 visible as a None value for the __hash__ attribute. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6383 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6384 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6385 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6386 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6387 descr = PyDescr_NewWrapper(type, p, *ptr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6388 if (descr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6389 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6390 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6391 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6392 Py_DECREF(descr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6393 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6394 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6395 if (type->tp_new != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6396 if (add_tp_new_wrapper(type) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6397 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6398 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6399 return 0;
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6400 }
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6401
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6402
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6403 /* Cooperative 'super' */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6404
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6405 typedef struct {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6406 PyObject_HEAD
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6407 PyTypeObject *type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6408 PyObject *obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6409 PyTypeObject *obj_type;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6410 } superobject;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6411
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
6412 static PyMemberDef super_members[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6413 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6414 "the class invoking super()"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6415 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6416 "the instance invoking super(); may be None"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6417 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6418 "the type of the instance invoking super(); may be None"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6419 {0}
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6420 };
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6421
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6422 static void
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6423 super_dealloc(PyObject *self)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6424 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6425 superobject *su = (superobject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6426
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6427 _PyObject_GC_UNTRACK(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6428 Py_XDECREF(su->obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6429 Py_XDECREF(su->type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6430 Py_XDECREF(su->obj_type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6431 Py_TYPE(self)->tp_free(self);
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6432 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6433
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6434 static PyObject *
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6435 super_repr(PyObject *self)
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6436 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6437 superobject *su = (superobject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6438
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6439 if (su->obj_type)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6440 return PyString_FromFormat(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6441 "<super: <class '%s'>, <%s object>>",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6442 su->type ? su->type->tp_name : "NULL",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6443 su->obj_type->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6444 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6445 return PyString_FromFormat(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6446 "<super: <class '%s'>, NULL>",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6447 su->type ? su->type->tp_name : "NULL");
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6448 }
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6449
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6450 static PyObject *
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6451 super_getattro(PyObject *self, PyObject *name)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6452 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6453 superobject *su = (superobject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6454 int skip = su->obj_type == NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6455
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6456 if (!skip) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6457 /* We want __class__ to return the class of the super object
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6458 (i.e. super, or a subclass), not the class of su->obj. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6459 skip = (PyString_Check(name) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6460 PyString_GET_SIZE(name) == 9 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6461 strcmp(PyString_AS_STRING(name), "__class__") == 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6462 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6464 if (!skip) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6465 PyObject *mro, *res, *tmp, *dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6466 PyTypeObject *starttype;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6467 descrgetfunc f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6468 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6469
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6470 starttype = su->obj_type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6471 mro = starttype->tp_mro;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6472
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6473 if (mro == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6474 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6475 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6476 assert(PyTuple_Check(mro));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6477 n = PyTuple_GET_SIZE(mro);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6478 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6479 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6480 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6481 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6482 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6483 i++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6484 res = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6485 for (; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6486 tmp = PyTuple_GET_ITEM(mro, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6487 if (PyType_Check(tmp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6488 dict = ((PyTypeObject *)tmp)->tp_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6489 else if (PyClass_Check(tmp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6490 dict = ((PyClassObject *)tmp)->cl_dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6491 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6492 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6493 res = PyDict_GetItem(dict, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6494 if (res != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6495 Py_INCREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6496 f = Py_TYPE(res)->tp_descr_get;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6497 if (f != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6498 tmp = f(res,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6499 /* Only pass 'obj' param if
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6500 this is instance-mode super
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6501 (See SF ID #743627)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6502 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6503 (su->obj == (PyObject *)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6504 su->obj_type
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6505 ? (PyObject *)NULL
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6506 : su->obj),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6507 (PyObject *)starttype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6508 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6509 res = tmp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6510 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6511 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6512 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6513 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6514 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6515 return PyObject_GenericGetAttr(self, name);
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6516 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6517
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6518 static PyTypeObject *
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6519 supercheck(PyTypeObject *type, PyObject *obj)
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6520 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6521 /* Check that a super() call makes sense. Return a type object.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6522
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6523 obj can be a new-style class, or an instance of one:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6524
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6525 - If it is a class, it must be a subclass of 'type'. This case is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6526 used for class methods; the return value is obj.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6527
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6528 - If it is an instance, it must be an instance of 'type'. This is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6529 the normal case; the return value is obj.__class__.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6530
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6531 But... when obj is an instance, we want to allow for the case where
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6532 Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6533 This will allow using super() with a proxy for obj.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6534 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6535
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6536 /* Check for first bullet above (special case) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6537 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6538 Py_INCREF(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6539 return (PyTypeObject *)obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6540 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6541
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6542 /* Normal case */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6543 if (PyType_IsSubtype(Py_TYPE(obj), type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6544 Py_INCREF(Py_TYPE(obj));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6545 return Py_TYPE(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6546 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6547 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6548 /* Try the slow way */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6549 static PyObject *class_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6550 PyObject *class_attr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6551
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6552 if (class_str == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6553 class_str = PyString_FromString("__class__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6554 if (class_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6555 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6556 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6557
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6558 class_attr = PyObject_GetAttr(obj, class_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6559
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6560 if (class_attr != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6561 PyType_Check(class_attr) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6562 (PyTypeObject *)class_attr != Py_TYPE(obj))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6563 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6564 int ok = PyType_IsSubtype(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6565 (PyTypeObject *)class_attr, type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6566 if (ok)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6567 return (PyTypeObject *)class_attr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6568 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6569
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6570 if (class_attr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6571 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6572 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6573 Py_DECREF(class_attr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6574 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6575
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6576 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6577 "super(type, obj): "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6578 "obj must be an instance or subtype of type");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6579 return NULL;
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6580 }
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6581
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6582 static PyObject *
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6583 super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6584 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6585 superobject *su = (superobject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6586 superobject *newobj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6587
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6588 if (obj == NULL || obj == Py_None || su->obj != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6589 /* Not binding to an object, or already bound */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6590 Py_INCREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6591 return self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6592 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6593 if (Py_TYPE(su) != &PySuper_Type)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6594 /* If su is an instance of a (strict) subclass of super,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6595 call its type */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6596 return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6597 su->type, obj, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6598 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6599 /* Inline the common case */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6600 PyTypeObject *obj_type = supercheck(su->type, obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6601 if (obj_type == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6602 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6603 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6604 NULL, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6605 if (newobj == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6606 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6607 Py_INCREF(su->type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6608 Py_INCREF(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6609 newobj->type = su->type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6610 newobj->obj = obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6611 newobj->obj_type = obj_type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6612 return (PyObject *)newobj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6613 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6614 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6615
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6616 static int
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6617 super_init(PyObject *self, PyObject *args, PyObject *kwds)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6618 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6619 superobject *su = (superobject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6620 PyTypeObject *type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6621 PyObject *obj = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6622 PyTypeObject *obj_type = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6623
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6624 if (!_PyArg_NoKeywords("super", kwds))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6625 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6626 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6627 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6628 if (obj == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6629 obj = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6630 if (obj != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6631 obj_type = supercheck(type, obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6632 if (obj_type == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6633 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6634 Py_INCREF(obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6635 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6636 Py_INCREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6637 su->type = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6638 su->obj = obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6639 su->obj_type = obj_type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6640 return 0;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6641 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6642
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23830
diff changeset
6643 PyDoc_STRVAR(super_doc,
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6644 "super(type) -> unbound super object\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6645 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
6646 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6647 "Typical use to call a cooperative superclass method:\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6648 "class C(B):\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6649 " def meth(self, arg):\n"
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6650 " super(C, self).meth(arg)");
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6651
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6652 static int
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6653 super_traverse(PyObject *self, visitproc visit, void *arg)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6654 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6655 superobject *su = (superobject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6656
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6657 Py_VISIT(su->obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6658 Py_VISIT(su->type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6659 Py_VISIT(su->obj_type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6660
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6661 return 0;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6662 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6663
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6664 PyTypeObject PySuper_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6665 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6666 "super", /* tp_name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6667 sizeof(superobject), /* tp_basicsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6668 0, /* tp_itemsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6669 /* methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6670 super_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6671 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6672 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6673 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6674 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6675 super_repr, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6676 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6677 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6678 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6679 0, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6680 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6681 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6682 super_getattro, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6683 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6684 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6685 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6686 Py_TPFLAGS_BASETYPE, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6687 super_doc, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6688 super_traverse, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6689 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6690 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6691 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6692 0, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6693 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6694 0, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6695 super_members, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6696 0, /* tp_getset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6697 0, /* tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6698 0, /* tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6699 super_descr_get, /* tp_descr_get */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6700 0, /* tp_descr_set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6701 0, /* tp_dictoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6702 super_init, /* tp_init */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6703 PyType_GenericAlloc, /* tp_alloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6704 PyType_GenericNew, /* tp_new */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61266
diff changeset
6705 PyObject_GC_Del, /* tp_free */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6706 };