annotate Objects/typeobject.c @ 57303:4aa4c19dfaed legacy-trunk

#1757126: fix typo with the cyrillic_asian alias
author Philip Jenvey <pjenvey@underboss.org>
date Tue, 17 Nov 2009 02:42:26 +0000
parents ae006386ec39
children cb26299e0e52
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. */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
15 #define MCACHE_MAX_ATTR_SIZE 100
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
16 #define MCACHE_SIZE_EXP 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
17 #define MCACHE_HASH(version, name_hash) \
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
18 (((unsigned int)(version) * (unsigned int)(name_hash)) \
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
19 >> (8*sizeof(unsigned int) - 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
20 #define MCACHE_HASH_METHOD(type, name) \
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
21 MCACHE_HASH((type)->tp_version_tag, \
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
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) \
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
24 PyString_CheckExact(name) && \
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
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 {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
28 unsigned int version;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
29 PyObject *name; /* reference to exactly a str or None */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
30 PyObject *value; /* borrowed */
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 {
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
39 Py_ssize_t i;
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
40 unsigned int cur_version_tag = next_version_tag - 1;
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
41
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
42 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
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
43 method_cache[i].version = 0;
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
44 Py_CLEAR(method_cache[i].name);
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
45 method_cache[i].value = NULL;
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
46 }
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
47 next_version_tag = 0;
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
48 /* mark all version tags as invalid */
47358
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
49 PyType_Modified(&PyBaseObject_Type);
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
50 return cur_version_tag;
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 {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
56 /* Invalidate any cached data for the specified type and all
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
57 subclasses. This function is called after the base
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
58 classes, mro, or attributes of the type are altered.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
59
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
60 Invariants:
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
61
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
62 - Py_TPFLAGS_VALID_VERSION_TAG is never set if
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
63 Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on 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
64 objects coming from non-recompiled extension modules)
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
65
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
66 - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a 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
67 it must first be set on all super types.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
68
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
69 This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
70 type (so it must first clear it on all subclasses). The
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
71 tp_version_tag value is meaningless unless this flag is set.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
72 We don't assign new version tags eagerly, but only as
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
73 needed.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
74 */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
75 PyObject *raw, *ref;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
76 Py_ssize_t i, n;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
77
45004
e224ad4b8b8c Whitespace cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 44664
diff changeset
78 if (!PyType_HasFeature(type, 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
79 return;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
80
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
81 raw = type->tp_subclasses;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
82 if (raw != NULL) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
83 n = PyList_GET_SIZE(raw);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
84 for (i = 0; i < n; i++) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
85 ref = PyList_GET_ITEM(raw, i);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
86 ref = PyWeakref_GET_OBJECT(ref);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
87 if (ref != Py_None) {
47358
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
88 PyType_Modified((PyTypeObject *)ref);
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
89 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
90 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
91 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
92 type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
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) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
97 /*
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
98 Check that all base classes or elements of the mro of type are
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
99 able to be cached. This function is called after the base
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
100 classes or mro of the type are altered.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
101
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
102 Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the 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
103 inherits from an old-style class, either directly or if it
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
104 appears in the MRO of a new-style class. No support either for
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
105 custom MROs that include types that are not officially super
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
106 types.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
107
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
108 Called from mro_internal, which will subsequently be called on
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
109 each subclass when their mro is recursively updated.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
110 */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
111 Py_ssize_t i, n;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
112 int clear = 0;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
113
45004
e224ad4b8b8c Whitespace cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 44664
diff changeset
114 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_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
115 return;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
116
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
117 n = PyTuple_GET_SIZE(bases);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
118 for (i = 0; i < n; i++) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
119 PyObject *b = PyTuple_GET_ITEM(bases, i);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
120 PyTypeObject *cls;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
121
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
122 if (!PyType_Check(b) ) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
123 clear = 1;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
124 break;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
125 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
126
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
127 cls = (PyTypeObject *)b;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
128
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
129 if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
130 !PyType_IsSubtype(type, cls)) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
131 clear = 1;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
132 break;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
133 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
134 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
135
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
136 if (clear)
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
137 type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
138 Py_TPFLAGS_VALID_VERSION_TAG);
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 {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
144 /* Ensure that the tp_version_tag is valid and set
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
145 Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
146 must first be done on all super classes. Return 0 if this
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
147 cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
148 */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
149 Py_ssize_t i, n;
47371
e81d02d09697 Revert #2990 patch; it's not necessary as Armin showed.
Georg Brandl <georg@python.org>
parents: 47360
diff changeset
150 PyObject *bases;
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
151
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
152 if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
153 return 1;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
154 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
155 return 0;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
156 if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
157 return 0;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
158
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
159 type->tp_version_tag = next_version_tag++;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
160 /* for stress-testing: next_version_tag &= 0xFF; */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
161
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
162 if (type->tp_version_tag == 0) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
163 /* wrap-around or just starting Python - clear the whole
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
164 cache by filling names with references to Py_None.
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
165 Values are also set to NULL for added protection, as they
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
166 are borrowed reference */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
167 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
168 method_cache[i].value = NULL;
47371
e81d02d09697 Revert #2990 patch; it's not necessary as Armin showed.
Georg Brandl <georg@python.org>
parents: 47360
diff changeset
169 Py_XDECREF(method_cache[i].name);
e81d02d09697 Revert #2990 patch; it's not necessary as Armin showed.
Georg Brandl <georg@python.org>
parents: 47360
diff changeset
170 method_cache[i].name = Py_None;
47360
7ae79e96fc42 #2990: prevent inconsistent state while updating method cache.
Georg Brandl <georg@python.org>
parents: 47358
diff changeset
171 Py_INCREF(Py_None);
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
172 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
173 /* mark all version tags as invalid */
47358
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
174 PyType_Modified(&PyBaseObject_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
175 return 1;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
176 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
177 bases = type->tp_bases;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
178 n = PyTuple_GET_SIZE(bases);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
179 for (i = 0; i < n; i++) {
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
180 PyObject *b = PyTuple_GET_ITEM(bases, i);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
181 assert(PyType_Check(b));
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
182 if (!assign_version_tag((PyTypeObject *)b))
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
183 return 0;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
184 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
185 type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
186 return 1;
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[] = {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
191 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
192 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
193 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
194 {"__weakrefoffset__", T_LONG,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
195 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
196 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
197 {"__dictoffset__", T_LONG,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
198 offsetof(PyTypeObject, tp_dictoffset), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
199 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
200 {0}
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 {
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35742
diff changeset
206 const char *s;
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
207
26448
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
208 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
209 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
210
36631
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
211 Py_INCREF(et->ht_name);
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
212 return et->ht_name;
26448
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
213 }
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
214 else {
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
215 s = strrchr(type->tp_name, '.');
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
216 if (s == NULL)
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
217 s = type->tp_name;
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
218 else
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
219 s++;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
220 return PyString_FromString(s);
26448
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
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 {
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
227 PyHeapTypeObject* et;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
228
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
229 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
230 PyErr_Format(PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
231 "can't set %s.__name__", type->tp_name);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
232 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
233 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
234 if (!value) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
235 PyErr_Format(PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
236 "can't delete %s.__name__", type->tp_name);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
237 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
238 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
239 if (!PyString_Check(value)) {
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
240 PyErr_Format(PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
241 "can only assign string to %s.__name__, not '%s'",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
242 type->tp_name, Py_TYPE(value)->tp_name);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
243 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
244 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
245 if (strlen(PyString_AS_STRING(value))
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
246 != (size_t)PyString_GET_SIZE(value)) {
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
247 PyErr_Format(PyExc_ValueError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
248 "__name__ must not contain null bytes");
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
249 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
250 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
251
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
252 et = (PyHeapTypeObject*)type;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
253
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
254 Py_INCREF(value);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
255
36631
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
256 Py_DECREF(et->ht_name);
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
257 et->ht_name = value;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
258
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
259 type->tp_name = PyString_AS_STRING(value);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
260
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
261 return 0;
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 {
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
267 PyObject *mod;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
268 char *s;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
269
26448
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
270 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
271 mod = PyDict_GetItemString(type->tp_dict, "__module__");
32323
c1fbbc3eed39 Fix for bug #966623 - classes created with type() in an exec(, {}) don't
Anthony Baxter <anthonybaxter@gmail.com>
parents: 31813
diff changeset
272 if (!mod) {
c1fbbc3eed39 Fix for bug #966623 - classes created with type() in an exec(, {}) don't
Anthony Baxter <anthonybaxter@gmail.com>
parents: 31813
diff changeset
273 PyErr_Format(PyExc_AttributeError, "__module__");
c1fbbc3eed39 Fix for bug #966623 - classes created with type() in an exec(, {}) don't
Anthony Baxter <anthonybaxter@gmail.com>
parents: 31813
diff changeset
274 return 0;
c1fbbc3eed39 Fix for bug #966623 - classes created with type() in an exec(, {}) don't
Anthony Baxter <anthonybaxter@gmail.com>
parents: 31813
diff changeset
275 }
26448
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
276 Py_XINCREF(mod);
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
277 return mod;
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
278 }
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
279 else {
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
280 s = strrchr(type->tp_name, '.');
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
diff changeset
281 if (s != NULL)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
282 return PyString_FromStringAndSize(
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39921
diff changeset
283 type->tp_name, (Py_ssize_t)(s - type->tp_name));
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
284 return PyString_FromString("__builtin__");
26448
bddc41315336 Nudge getting __module__ and __name__ for new-style classes so that
Michael W. Hudson <mwh@python.net>
parents: 26446
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 {
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
291 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
292 PyErr_Format(PyExc_TypeError,
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
293 "can't set %s.__module__", type->tp_name);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
294 return -1;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
295 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
296 if (!value) {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
297 PyErr_Format(PyExc_TypeError,
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
298 "can't delete %s.__module__", type->tp_name);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
299 return -1;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
300 }
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
301
47358
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
302 PyType_Modified(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
303
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
304 return PyDict_SetItemString(type->tp_dict, "__module__", value);
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 {
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
310 PyObject *mod = PyDict_GetItemString(type->tp_dict,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
311 "__abstractmethods__");
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
312 if (!mod) {
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
313 PyErr_Format(PyExc_AttributeError, "__abstractmethods__");
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
314 return NULL;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
315 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
316 Py_XINCREF(mod);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
317 return mod;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
318 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
319
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
320 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
321 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
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 /* __abstractmethods__ should only be set once on a type, in
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 abc.ABCMeta.__new__, so this function doesn't do anything
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 special to update subclasses.
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
326 */
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
327 int res = PyDict_SetItemString(type->tp_dict,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
328 "__abstractmethods__", value);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
329 if (res == 0) {
47358
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
330 PyType_Modified(type);
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
331 if (value && PyObject_IsTrue(value)) {
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
332 type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
333 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
334 else {
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
335 type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
336 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
337 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
338 return res;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
339 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
340
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
341 static PyObject *
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
342 type_get_bases(PyTypeObject *type, void *context)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
343 {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
344 Py_INCREF(type->tp_bases);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
345 return type->tp_bases;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
346 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
347
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
348 static PyTypeObject *best_base(PyObject *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
349 static int mro_internal(PyTypeObject *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
350 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
351 static int add_subclass(PyTypeObject*, PyTypeObject*);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
352 static void remove_subclass(PyTypeObject *, PyTypeObject *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
353 static void update_all_slots(PyTypeObject *);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
354
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
355 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
356 static int update_subclasses(PyTypeObject *type, PyObject *name,
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
357 update_callback callback, void *data);
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
358 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
359 update_callback callback, void *data);
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
360
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
361 static int
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
362 mro_subclasses(PyTypeObject *type, PyObject* temp)
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
363 {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
364 PyTypeObject *subclass;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
365 PyObject *ref, *subclasses, *old_mro;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
366 Py_ssize_t i, n;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
367
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
368 subclasses = type->tp_subclasses;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
369 if (subclasses == NULL)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
370 return 0;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
371 assert(PyList_Check(subclasses));
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
372 n = PyList_GET_SIZE(subclasses);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
373 for (i = 0; i < n; i++) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
374 ref = PyList_GET_ITEM(subclasses, i);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
375 assert(PyWeakref_CheckRef(ref));
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
376 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
377 assert(subclass != NULL);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
378 if ((PyObject *)subclass == Py_None)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
379 continue;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
380 assert(PyType_Check(subclass));
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
381 old_mro = subclass->tp_mro;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
382 if (mro_internal(subclass) < 0) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
383 subclass->tp_mro = old_mro;
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
384 return -1;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
385 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
386 else {
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
387 PyObject* tuple;
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
388 tuple = PyTuple_Pack(2, subclass, old_mro);
28625
07d8e0eab3af Fix three (!) object leaks in the code for assignment to __bases__.
Guido van Rossum <guido@python.org>
parents: 28621
diff changeset
389 Py_DECREF(old_mro);
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
390 if (!tuple)
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
391 return -1;
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
392 if (PyList_Append(temp, tuple) < 0)
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
393 return -1;
28625
07d8e0eab3af Fix three (!) object leaks in the code for assignment to __bases__.
Guido van Rossum <guido@python.org>
parents: 28621
diff changeset
394 Py_DECREF(tuple);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
395 }
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
396 if (mro_subclasses(subclass, temp) < 0)
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
397 return -1;
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
398 }
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
399 return 0;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
400 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
401
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
402 static int
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
403 type_set_bases(PyTypeObject *type, PyObject *value, void *context)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
404 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
405 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
406 int r = 0;
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
407 PyObject *ob, *temp;
41488
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
408 PyTypeObject *new_base, *old_base;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
409 PyObject *old_bases, *old_mro;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
410
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
411 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
412 PyErr_Format(PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
413 "can't set %s.__bases__", type->tp_name);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
414 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
415 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
416 if (!value) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
417 PyErr_Format(PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
418 "can't delete %s.__bases__", type->tp_name);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
419 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
420 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
421 if (!PyTuple_Check(value)) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
422 PyErr_Format(PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
423 "can only assign tuple to %s.__bases__, not %s",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
424 type->tp_name, Py_TYPE(value)->tp_name);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
425 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
426 }
26645
d176cbfd3765 Tighten the tests for assignment to __bases__: disallow empty tuple.
Guido van Rossum <guido@python.org>
parents: 26583
diff changeset
427 if (PyTuple_GET_SIZE(value) == 0) {
d176cbfd3765 Tighten the tests for assignment to __bases__: disallow empty tuple.
Guido van Rossum <guido@python.org>
parents: 26583
diff changeset
428 PyErr_Format(PyExc_TypeError,
d176cbfd3765 Tighten the tests for assignment to __bases__: disallow empty tuple.
Guido van Rossum <guido@python.org>
parents: 26583
diff changeset
429 "can only assign non-empty tuple to %s.__bases__, not ()",
d176cbfd3765 Tighten the tests for assignment to __bases__: disallow empty tuple.
Guido van Rossum <guido@python.org>
parents: 26583
diff changeset
430 type->tp_name);
d176cbfd3765 Tighten the tests for assignment to __bases__: disallow empty tuple.
Guido van Rossum <guido@python.org>
parents: 26583
diff changeset
431 return -1;
d176cbfd3765 Tighten the tests for assignment to __bases__: disallow empty tuple.
Guido van Rossum <guido@python.org>
parents: 26583
diff changeset
432 }
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
433 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
434 ob = PyTuple_GET_ITEM(value, i);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
435 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
436 PyErr_Format(
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
437 PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
438 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
439 type->tp_name, Py_TYPE(ob)->tp_name);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
440 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
441 }
26440
aa0e19b6e1e7 I had the inheritance cycle stuff backwards. Oops!
Michael W. Hudson <mwh@python.net>
parents: 26425
diff changeset
442 if (PyType_Check(ob)) {
aa0e19b6e1e7 I had the inheritance cycle stuff backwards. Oops!
Michael W. Hudson <mwh@python.net>
parents: 26425
diff changeset
443 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
aa0e19b6e1e7 I had the inheritance cycle stuff backwards. Oops!
Michael W. Hudson <mwh@python.net>
parents: 26425
diff changeset
444 PyErr_SetString(PyExc_TypeError,
aa0e19b6e1e7 I had the inheritance cycle stuff backwards. Oops!
Michael W. Hudson <mwh@python.net>
parents: 26425
diff changeset
445 "a __bases__ item causes an inheritance cycle");
aa0e19b6e1e7 I had the inheritance cycle stuff backwards. Oops!
Michael W. Hudson <mwh@python.net>
parents: 26425
diff changeset
446 return -1;
aa0e19b6e1e7 I had the inheritance cycle stuff backwards. Oops!
Michael W. Hudson <mwh@python.net>
parents: 26425
diff changeset
447 }
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
448 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
449 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
450
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
451 new_base = best_base(value);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
452
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
453 if (!new_base) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
454 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
455 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
456
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
457 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
458 return -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
459
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
460 Py_INCREF(new_base);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
461 Py_INCREF(value);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
462
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
463 old_bases = type->tp_bases;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
464 old_base = type->tp_base;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
465 old_mro = type->tp_mro;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
466
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
467 type->tp_bases = value;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
468 type->tp_base = new_base;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
469
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
470 if (mro_internal(type) < 0) {
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
471 goto bail;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
472 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
473
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
474 temp = PyList_New(0);
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
475 if (!temp)
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
476 goto bail;
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
477
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
478 r = mro_subclasses(type, temp);
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
479
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
480 if (r < 0) {
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
481 for (i = 0; i < PyList_Size(temp); i++) {
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
482 PyTypeObject* cls;
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
483 PyObject* mro;
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
484 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
485 "", 2, 2, &cls, &mro);
41489
da403d2b886a Fix a usage of the dangerous pattern decref - modify field - incref.
Armin Rigo <arigo@tunes.org>
parents: 41488
diff changeset
486 Py_INCREF(mro);
da403d2b886a Fix a usage of the dangerous pattern decref - modify field - incref.
Armin Rigo <arigo@tunes.org>
parents: 41488
diff changeset
487 ob = cls->tp_mro;
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
488 cls->tp_mro = mro;
41489
da403d2b886a Fix a usage of the dangerous pattern decref - modify field - incref.
Armin Rigo <arigo@tunes.org>
parents: 41488
diff changeset
489 Py_DECREF(ob);
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
490 }
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
491 Py_DECREF(temp);
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
492 goto bail;
26445
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
493 }
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
494
7421367274b4 Readjustments to the way we cope with exceptions from subclasses'
Michael W. Hudson <mwh@python.net>
parents: 26440
diff changeset
495 Py_DECREF(temp);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
496
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
497 /* any base that was in __bases__ but now isn't, we
26660
f838bc9056cc Punctuation fix.
Raymond Hettinger <python@rcn.com>
parents: 26645
diff changeset
498 need to remove |type| from its tp_subclasses.
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
499 conversely, any class now in __bases__ that wasn't
26660
f838bc9056cc Punctuation fix.
Raymond Hettinger <python@rcn.com>
parents: 26645
diff changeset
500 needs to have |type| added to its subclasses. */
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
501
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
502 /* for now, sod that: just remove from all old_bases,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
503 add to all new_bases */
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
504
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
505 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
506 ob = PyTuple_GET_ITEM(old_bases, i);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
507 if (PyType_Check(ob)) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
508 remove_subclass(
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
509 (PyTypeObject*)ob, type);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
510 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
511 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
512
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
513 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
514 ob = PyTuple_GET_ITEM(value, i);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
515 if (PyType_Check(ob)) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
516 if (add_subclass((PyTypeObject*)ob, type) < 0)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
517 r = -1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
518 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
519 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
520
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
521 update_all_slots(type);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
522
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
523 Py_DECREF(old_bases);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
524 Py_DECREF(old_base);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
525 Py_DECREF(old_mro);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
526
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
527 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
528
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
529 bail:
30084
d05d0ed465d8 Repair refcounting on error return from type_set_bases.
Michael W. Hudson <mwh@python.net>
parents: 29900
diff changeset
530 Py_DECREF(type->tp_bases);
d05d0ed465d8 Repair refcounting on error return from type_set_bases.
Michael W. Hudson <mwh@python.net>
parents: 29900
diff changeset
531 Py_DECREF(type->tp_base);
d05d0ed465d8 Repair refcounting on error return from type_set_bases.
Michael W. Hudson <mwh@python.net>
parents: 29900
diff changeset
532 if (type->tp_mro != old_mro) {
d05d0ed465d8 Repair refcounting on error return from type_set_bases.
Michael W. Hudson <mwh@python.net>
parents: 29900
diff changeset
533 Py_DECREF(type->tp_mro);
d05d0ed465d8 Repair refcounting on error return from type_set_bases.
Michael W. Hudson <mwh@python.net>
parents: 29900
diff changeset
534 }
d05d0ed465d8 Repair refcounting on error return from type_set_bases.
Michael W. Hudson <mwh@python.net>
parents: 29900
diff changeset
535
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
536 type->tp_bases = old_bases;
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
537 type->tp_base = old_base;
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
538 type->tp_mro = old_mro;
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
539
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
540 return -1;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
541 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
542
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
543 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
544 type_dict(PyTypeObject *type, void *context)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
545 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
546 if (type->tp_dict == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
547 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
548 return Py_None;
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
549 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
550 return PyDictProxy_New(type->tp_dict);
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
551 }
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
552
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
553 static PyObject *
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
554 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
555 {
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
556 PyObject *result;
23118
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
557 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
558 return PyString_FromString(type->tp_doc);
23118
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
559 result = PyDict_GetItemString(type->tp_dict, "__doc__");
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
560 if (result == NULL) {
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
561 result = Py_None;
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
562 Py_INCREF(result);
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
563 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
564 else if (Py_TYPE(result)->tp_descr_get) {
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
565 result = Py_TYPE(result)->tp_descr_get(result, NULL,
23127
70650ae1a2cf type_get_doc(): Squash compiler wng about incompatible ptr types.
Tim Peters <tim.peters@gmail.com>
parents: 23118
diff changeset
566 (PyObject *)type);
23118
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
567 }
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
568 else {
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
569 Py_INCREF(result);
f917e7a6e911 SF bug 542984.
Guido van Rossum <guido@python.org>
parents: 23037
diff changeset
570 }
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
571 return result;
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
572 }
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
573
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
574 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
575 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
576 {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
577 switch (_PyObject_RealIsInstance(inst, type)) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
578 case -1:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
579 return NULL;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
580 case 0:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
581 Py_RETURN_FALSE;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
582 default:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
583 Py_RETURN_TRUE;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
584 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
585 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
586
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
587
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
588 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
589 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
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 switch (_PyObject_RealIsSubclass(inst, type)) {
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
592 case -1:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
593 return NULL;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
594 case 0:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
595 Py_RETURN_FALSE;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
596 default:
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
597 Py_RETURN_TRUE;
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
598 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
599 }
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
600
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48854
diff changeset
601
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
602 static PyGetSetDef type_getsets[] = {
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
603 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
604 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
605 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
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
606 {"__abstractmethods__", (getter)type_abstractmethods,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
607 (setter)type_set_abstractmethods, NULL},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
608 {"__dict__", (getter)type_dict, NULL, NULL},
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
609 {"__doc__", (getter)type_get_doc, NULL, NULL},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
610 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
611 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
612
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
613 static int
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
614 type_compare(PyObject *v, PyObject *w)
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
615 {
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 /* This is called with type objects only. So we
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
617 can just compare the addresses. */
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
618 Py_uintptr_t vv = (Py_uintptr_t)v;
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
619 Py_uintptr_t ww = (Py_uintptr_t)w;
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
620 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
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
621 }
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
622
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
623 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
624 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
625 {
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
626 PyObject *result;
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
627 Py_uintptr_t vv, ww;
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
628 int c;
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
629
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
630 /* Make sure both arguments are types. */
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
631 if (!PyType_Check(v) || !PyType_Check(w)) {
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
632 result = Py_NotImplemented;
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
633 goto out;
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
634 }
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
635
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
636 /* Py3K warning if comparison isn't == or != */
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
637 if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
638 PyErr_WarnEx(PyExc_DeprecationWarning,
46075
84bb7701efba Make Py3k warnings consistent w.r.t. punctuation; also respect the
Georg Brandl <georg@python.org>
parents: 45860
diff changeset
639 "type inequality comparisons not supported "
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
640 "in 3.x", 1) < 0) {
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
641 return NULL;
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
642 }
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
643
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
644 /* Compare addresses */
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
645 vv = (Py_uintptr_t)v;
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
646 ww = (Py_uintptr_t)w;
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
647 switch (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
648 case Py_LT: c = vv < ww; break;
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
649 case Py_LE: c = vv <= ww; break;
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
650 case Py_EQ: c = vv == ww; break;
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
651 case Py_NE: c = vv != ww; break;
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
652 case Py_GT: c = vv > ww; break;
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
653 case Py_GE: c = vv >= ww; break;
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
654 default:
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
655 result = Py_NotImplemented;
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
656 goto out;
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 result = c ? Py_True : Py_False;
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
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
660 /* 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
661 out:
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 Py_INCREF(result);
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 return result;
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
664 }
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
665
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
666 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
667 type_repr(PyTypeObject *type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 {
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
669 PyObject *mod, *name, *rtn;
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
670 char *kind;
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
671
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
672 mod = type_module(type, NULL);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
673 if (mod == NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
674 PyErr_Clear();
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
675 else if (!PyString_Check(mod)) {
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
676 Py_DECREF(mod);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
677 mod = NULL;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
678 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
679 name = type_name(type, NULL);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
680 if (name == NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
681 return NULL;
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
682
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
683 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
684 kind = "class";
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
685 else
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
686 kind = "type";
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
687
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
688 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
689 rtn = PyString_FromFormat("<%s '%s.%s'>",
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
690 kind,
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
691 PyString_AS_STRING(mod),
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
692 PyString_AS_STRING(name));
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
693 }
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
694 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
695 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
696
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
697 Py_XDECREF(mod);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
698 Py_DECREF(name);
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
699 return rtn;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
701
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
702 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
703 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
704 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
705 PyObject *obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
706
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
707 if (type->tp_new == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
708 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
709 "cannot create '%.100s' instances",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
710 type->tp_name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
711 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
712 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
713
19898
fc40d0b54043 type_call(): Change in policy. The keyword args (if any) are now passed
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
714 obj = type->tp_new(type, args, kwds);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
715 if (obj != NULL) {
20516
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
716 /* Ugly exception: when the call was type(something),
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
717 don't call tp_init on the result. */
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
718 if (type == &PyType_Type &&
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
719 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
720 (kwds == NULL ||
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
721 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
722 return obj;
22859
bbf05af3c191 - Changed new-style class instantiation so that when C's __new__
Guido van Rossum <guido@python.org>
parents: 22854
diff changeset
723 /* If the returned object is not an instance of type,
bbf05af3c191 - Changed new-style class instantiation so that when C's __new__
Guido van Rossum <guido@python.org>
parents: 22854
diff changeset
724 it won't be initialized. */
bbf05af3c191 - Changed new-style class instantiation so that when C's __new__
Guido van Rossum <guido@python.org>
parents: 22854
diff changeset
725 if (!PyType_IsSubtype(obj->ob_type, type))
bbf05af3c191 - Changed new-style class instantiation so that when C's __new__
Guido van Rossum <guido@python.org>
parents: 22854
diff changeset
726 return obj;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
727 type = obj->ob_type;
24250
804ea51cfce6 The object returned by tp_new() may not have a tp_init.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24245
diff changeset
728 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
804ea51cfce6 The object returned by tp_new() may not have a tp_init.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24245
diff changeset
729 type->tp_init != NULL &&
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
730 type->tp_init(obj, args, kwds) < 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
731 Py_DECREF(obj);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
732 obj = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
733 }
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 return obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
736 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
737
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
738 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
739 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
740 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
741 PyObject *obj;
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
742 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
743 /* note that we need to add one, for the sentinel */
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
744
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
745 if (PyType_IS_GC(type))
22980
9f949027d7ba Change signature of _PyObject_GC_Malloc to match PyObject_MALLOC.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22871
diff changeset
746 obj = _PyObject_GC_Malloc(size);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
747 else
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
748 obj = (PyObject *)PyObject_MALLOC(size);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
749
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
750 if (obj == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
751 return PyErr_NoMemory();
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
752
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
753 memset(obj, '\0', size);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
754
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
755 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
756 Py_INCREF(type);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
757
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
758 if (type->tp_itemsize == 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
759 PyObject_INIT(obj, type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
760 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
761 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
762
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
763 if (PyType_IS_GC(type))
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
764 _PyObject_GC_TRACK(obj);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
765 return obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
766 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
767
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
768 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
769 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
770 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
771 return type->tp_alloc(type, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
772 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
773
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
774 /* 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
775
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
776 static int
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
777 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
778 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
779 Py_ssize_t i, n;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
780 PyMemberDef *mp;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
781
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
782 n = Py_SIZE(type);
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
783 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
784 for (i = 0; i < n; i++, mp++) {
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
785 if (mp->type == T_OBJECT_EX) {
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
786 char *addr = (char *)self + mp->offset;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
787 PyObject *obj = *(PyObject **)addr;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
788 if (obj != NULL) {
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
789 int err = visit(obj, arg);
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
790 if (err)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
791 return err;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
792 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
793 }
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 return 0;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
796 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
797
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
798 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
799 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
800 {
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
801 PyTypeObject *type, *base;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
802 traverseproc basetraverse;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
803
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
804 /* Find the nearest base with a different tp_traverse,
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
805 and traverse slots while we're at it */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
806 type = Py_TYPE(self);
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
807 base = type;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
808 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
809 if (Py_SIZE(base)) {
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
810 int err = traverse_slots(base, self, visit, arg);
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
811 if (err)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
812 return err;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
813 }
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
814 base = base->tp_base;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
815 assert(base);
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
816 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
817
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
818 if (type->tp_dictoffset != base->tp_dictoffset) {
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
819 PyObject **dictptr = _PyObject_GetDictPtr(self);
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
820 if (dictptr && *dictptr)
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
821 Py_VISIT(*dictptr);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
822 }
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
823
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
824 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
825 /* For a heaptype, the instances count as references
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
826 to the type. Traverse the type so the collector
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
827 can find cycles involving this link. */
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
828 Py_VISIT(type);
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
829
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
830 if (basetraverse)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
831 return basetraverse(self, visit, arg);
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
832 return 0;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
833 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
834
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
835 static void
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
836 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
837 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
838 Py_ssize_t i, n;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
839 PyMemberDef *mp;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
840
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
841 n = Py_SIZE(type);
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
842 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
843 for (i = 0; i < n; i++, mp++) {
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
844 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
845 char *addr = (char *)self + mp->offset;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
846 PyObject *obj = *(PyObject **)addr;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
847 if (obj != NULL) {
37748
d1547426ab61 Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents: 37603
diff changeset
848 *(PyObject **)addr = NULL;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
849 Py_DECREF(obj);
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
850 }
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 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
854
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
855 static int
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
856 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
857 {
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
858 PyTypeObject *type, *base;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
859 inquiry baseclear;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
860
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
861 /* Find the nearest base with a different tp_clear
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
862 and clear slots while we're at it */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
863 type = Py_TYPE(self);
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
864 base = type;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
865 while ((baseclear = base->tp_clear) == subtype_clear) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
866 if (Py_SIZE(base))
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
867 clear_slots(base, self);
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
868 base = base->tp_base;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
869 assert(base);
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
870 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
871
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
872 /* There's no need to clear the instance dict (if any);
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
873 the collector will call its tp_clear handler. */
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
874
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
875 if (baseclear)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
876 return baseclear(self);
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
877 return 0;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
878 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
879
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
880 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
881 subtype_dealloc(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
882 {
21246
cb921d483e00 The previous checkin to clear __slots__ variables did a little bit of
Guido van Rossum <guido@python.org>
parents: 21242
diff changeset
883 PyTypeObject *type, *base;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
884 destructor basedealloc;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
885
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
886 /* Extract the type; we expect it to be a heap type */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
887 type = Py_TYPE(self);
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
888 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
889
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
890 /* Test whether the type has GC exactly once */
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
891
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
892 if (!PyType_IS_GC(type)) {
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
893 /* It's really rare to find a dynamic type that doesn't have
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
894 GC; it can only happen when deriving from 'object' and not
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
895 adding any slots or instance variables. This allows
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
896 certain simplifications: there's no need to call
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
897 clear_slots(), or DECREF the dict, or clear weakrefs. */
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
898
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
899 /* Maybe call finalizer; exit early if resurrected */
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
900 if (type->tp_del) {
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
901 type->tp_del(self);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
902 if (self->ob_refcnt > 0)
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
903 return;
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
904 }
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
905
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
906 /* Find the nearest base with a different tp_dealloc */
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
907 base = type;
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
908 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
909 assert(Py_SIZE(base) == 0);
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
910 base = base->tp_base;
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
911 assert(base);
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
912 }
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
913
53372
318951890abb fix a segfault when setting __class__ in __del__ #5283
Benjamin Peterson <benjamin@python.org>
parents: 52749
diff changeset
914 /* Extract the type again; tp_del may have changed it */
318951890abb fix a segfault when setting __class__ in __del__ #5283
Benjamin Peterson <benjamin@python.org>
parents: 52749
diff changeset
915 type = Py_TYPE(self);
318951890abb fix a segfault when setting __class__ in __del__ #5283
Benjamin Peterson <benjamin@python.org>
parents: 52749
diff changeset
916
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
917 /* Call the base tp_dealloc() */
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
918 assert(basedealloc);
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
919 basedealloc(self);
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
920
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
921 /* Can't reference self beyond this point */
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
922 Py_DECREF(type);
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
923
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
924 /* Done */
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
925 return;
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
926 }
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
927
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
928 /* We get here only if the type has GC */
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
929
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
930 /* UnTrack and re-Track around the trashcan macro, alas */
27717
41313d20d1dd Comment typo fix
Andrew M. Kuchling <amk@amk.ca>
parents: 27707
diff changeset
931 /* See explanation at end of function for full disclosure */
24653
1ab27c1770df Fix a subtle bug in the trashcan code I added yesterday to
Guido van Rossum <guido@python.org>
parents: 24614
diff changeset
932 PyObject_GC_UnTrack(self);
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
933 ++_PyTrash_delete_nesting;
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
934 Py_TRASHCAN_SAFE_BEGIN(self);
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
935 --_PyTrash_delete_nesting;
30915
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
936 /* DO NOT restore GC tracking at this point. weakref callbacks
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
937 * (if any, and whether directly here or indirectly in something we
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
938 * call) may trigger GC, and if self is tracked at that point, it
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
939 * will look like trash to GC and GC will try to delete self again.
30907
3d9b36d6ab89 SF bug 840829: weakref callbacks and gc corrupt memory.
Tim Peters <tim.peters@gmail.com>
parents: 30722
diff changeset
940 */
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
941
29442
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
942 /* Find the nearest base with a different tp_dealloc */
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
943 base = type;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
944 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
945 base = base->tp_base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
946 assert(base);
21246
cb921d483e00 The previous checkin to clear __slots__ variables did a little bit of
Guido van Rossum <guido@python.org>
parents: 21242
diff changeset
947 }
cb921d483e00 The previous checkin to clear __slots__ variables did a little bit of
Guido van Rossum <guido@python.org>
parents: 21242
diff changeset
948
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
949 /* If we added a weaklist, we clear it. Do this *before* calling
29442
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
950 the finalizer (__del__), clearing slots, or clearing the instance
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
951 dict. */
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
952
29308
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
953 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
954 PyObject_ClearWeakRefs(self);
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
955
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
956 /* Maybe call finalizer; exit early if resurrected */
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
957 if (type->tp_del) {
30915
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
958 _PyObject_GC_TRACK(self);
29308
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
959 type->tp_del(self);
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
960 if (self->ob_refcnt > 0)
30915
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
961 goto endlabel; /* resurrected */
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
962 else
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
963 _PyObject_GC_UNTRACK(self);
40707
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
964 /* New weakrefs could be created during the finalizer call.
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
965 If this occurs, clear them out without calling their
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
966 finalizers since they might rely on part of the object
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
967 being finalized that has already been destroyed. */
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
968 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
969 /* Modeled after GET_WEAKREFS_LISTPTR() */
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
970 PyWeakReference **list = (PyWeakReference **) \
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
971 PyObject_GET_WEAKREFS_LISTPTR(self);
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
972 while (*list)
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
973 _PyWeakref_ClearRef(*list);
dbbdfc038dc9 Fix crasher for when an object's __del__ creates a new weakref to itself.
Brett Cannon <bcannon@gmail.com>
parents: 40066
diff changeset
974 }
29308
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
975 }
ad1e67ac567b Fix for SF 742911. We now clear the weakrefs *before* calling __del__
Guido van Rossum <guido@python.org>
parents: 29201
diff changeset
976
29442
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
977 /* Clear slots up to the nearest base with a different tp_dealloc */
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
978 base = type;
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
979 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
980 if (Py_SIZE(base))
29442
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
981 clear_slots(base, self);
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
982 base = base->tp_base;
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
983 assert(base);
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
984 }
6fecc99afa69 - SF patch 751998 fixes an unwanted side effect of the previous fix
Guido van Rossum <guido@python.org>
parents: 29408
diff changeset
985
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
986 /* If we added a dict, DECREF it */
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
987 if (type->tp_dictoffset && !base->tp_dictoffset) {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
988 PyObject **dictptr = _PyObject_GetDictPtr(self);
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
989 if (dictptr != NULL) {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
990 PyObject *dict = *dictptr;
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
991 if (dict != NULL) {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
992 Py_DECREF(dict);
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
993 *dictptr = NULL;
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
994 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
995 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
996 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
997
53372
318951890abb fix a segfault when setting __class__ in __del__ #5283
Benjamin Peterson <benjamin@python.org>
parents: 52749
diff changeset
998 /* Extract the type again; tp_del may have changed it */
318951890abb fix a segfault when setting __class__ in __del__ #5283
Benjamin Peterson <benjamin@python.org>
parents: 52749
diff changeset
999 type = Py_TYPE(self);
318951890abb fix a segfault when setting __class__ in __del__ #5283
Benjamin Peterson <benjamin@python.org>
parents: 52749
diff changeset
1000
30917
12c22d6b1ec0 subtype_dealloc(): Simplified overly contorted retracking logic. With
Tim Peters <tim.peters@gmail.com>
parents: 30915
diff changeset
1001 /* Call the base tp_dealloc(); first retrack self if
12c22d6b1ec0 subtype_dealloc(): Simplified overly contorted retracking logic. With
Tim Peters <tim.peters@gmail.com>
parents: 30915
diff changeset
1002 * basedealloc knows about gc.
12c22d6b1ec0 subtype_dealloc(): Simplified overly contorted retracking logic. With
Tim Peters <tim.peters@gmail.com>
parents: 30915
diff changeset
1003 */
12c22d6b1ec0 subtype_dealloc(): Simplified overly contorted retracking logic. With
Tim Peters <tim.peters@gmail.com>
parents: 30915
diff changeset
1004 if (PyType_IS_GC(base))
12c22d6b1ec0 subtype_dealloc(): Simplified overly contorted retracking logic. With
Tim Peters <tim.peters@gmail.com>
parents: 30915
diff changeset
1005 _PyObject_GC_TRACK(self);
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1006 assert(basedealloc);
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1007 basedealloc(self);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1008
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1009 /* Can't reference self beyond this point */
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
1010 Py_DECREF(type);
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
1011
24653
1ab27c1770df Fix a subtle bug in the trashcan code I added yesterday to
Guido van Rossum <guido@python.org>
parents: 24614
diff changeset
1012 endlabel:
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1013 ++_PyTrash_delete_nesting;
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
1014 Py_TRASHCAN_SAFE_END(self);
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1015 --_PyTrash_delete_nesting;
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1016
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1017 /* Explanation of the weirdness around the trashcan macros:
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1018
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1019 Q. What do the trashcan macros do?
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1020
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1021 A. Read the comment titled "Trashcan mechanism" in object.h.
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1022 For one, this explains why there must be a call to GC-untrack
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1023 before the trashcan begin macro. Without understanding the
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1024 trashcan code, the answers to the following questions don't make
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1025 sense.
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1026
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1027 Q. Why do we GC-untrack before the trashcan and then immediately
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1028 GC-track again afterward?
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1029
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1030 A. In the case that the base class is GC-aware, the base class
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1031 probably GC-untracks the object. If it does that using the
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1032 UNTRACK macro, this will crash when the object is already
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1033 untracked. Because we don't know what the base class does, the
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1034 only safe thing is to make sure the object is tracked when we
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1035 call the base class dealloc. But... The trashcan begin macro
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1036 requires that the object is *untracked* before it is called. So
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1037 the dance becomes:
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1038
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1039 GC untrack
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1040 trashcan begin
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1041 GC track
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1042
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1043 Q. Why did the last question say "immediately GC-track again"?
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1044 It's nowhere near immediately.
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1045
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1046 A. Because the code *used* to re-track immediately. Bad Idea.
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1047 self has a refcount of 0, and if gc ever gets its hands on it
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1048 (which can happen if any weakref callback gets invoked), it
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1049 looks like trash to gc too, and gc also tries to delete self
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1050 then. But we're already deleting self. Double dealloction is
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1051 a subtle disaster.
30915
8081643c373f subtype_dealloc(): A more complete fix for critical bug 840829 +
Tim Peters <tim.peters@gmail.com>
parents: 30907
diff changeset
1052
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1053 Q. Why the bizarre (net-zero) manipulation of
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1054 _PyTrash_delete_nesting around the trashcan macros?
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1055
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1056 A. Some base classes (e.g. list) also use the trashcan mechanism.
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1057 The following scenario used to be possible:
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1058
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1059 - suppose the trashcan level is one below the trashcan limit
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1060
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1061 - subtype_dealloc() is called
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1062
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1063 - the trashcan limit is not yet reached, so the trashcan level
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1064 is incremented and the code between trashcan begin and end is
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1065 executed
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1066
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1067 - this destroys much of the object's contents, including its
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1068 slots and __dict__
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1069
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1070 - basedealloc() is called; this is really list_dealloc(), or
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1071 some other type which also uses the trashcan macros
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1072
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1073 - the trashcan limit is now reached, so the object is put on the
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1074 trashcan's to-be-deleted-later list
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1075
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1076 - basedealloc() returns
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1077
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1078 - subtype_dealloc() decrefs the object's type
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1079
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1080 - subtype_dealloc() returns
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1081
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1082 - later, the trashcan code starts deleting the objects from its
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1083 to-be-deleted-later list
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1084
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1085 - subtype_dealloc() is called *AGAIN* for the same object
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1086
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1087 - at the very least (if the destroyed slots and __dict__ don't
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1088 cause problems) the object's type gets decref'ed a second
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1089 time, which is *BAD*!!!
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1090
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1091 The remedy is to make sure that if the code between trashcan
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1092 begin and end in subtype_dealloc() is called, the code between
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1093 trashcan begin and end in basedealloc() will also be called.
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1094 This is done by decrementing the level after passing into the
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1095 trashcan block, and incrementing it just before leaving the
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1096 block.
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1097
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1098 But now it's possible that a chain of objects consisting solely
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1099 of objects whose deallocator is subtype_dealloc() will defeat
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1100 the trashcan mechanism completely: the decremented level means
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1101 that the effective level never reaches the limit. Therefore, we
27707
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1102 *increment* the level *before* entering the trashcan block, and
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1103 matchingly decrement it after leaving. This means the trashcan
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1104 code will trigger a little early, but that's no big deal.
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1105
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1106 Q. Are there any live examples of code in need of all this
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1107 complexity?
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1108
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1109 A. Yes. See SF bug 668433 for code that crashed (when Python was
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1110 compiled in debug mode) before the trashcan level manipulations
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1111 were added. For more discussion, see SF patches 581742, 575073
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1112 and bug 574207.
e0fa52067dbe Fix for SF #668433. I'm not explaining it here; ample comments are in
Guido van Rossum <guido@python.org>
parents: 27132
diff changeset
1113 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1114 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1115
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1116 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
1117
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1118 /* type test with subclassing support */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1119
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1120 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1121 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1122 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1123 PyObject *mro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1124
19803
32cfce4d83dc PyType_IsSubtype(): test tp_flags for HAVE_CLASS bit before accessing
Guido van Rossum <guido@python.org>
parents: 19630
diff changeset
1125 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
32cfce4d83dc PyType_IsSubtype(): test tp_flags for HAVE_CLASS bit before accessing
Guido van Rossum <guido@python.org>
parents: 19630
diff changeset
1126 return b == a || b == &PyBaseObject_Type;
32cfce4d83dc PyType_IsSubtype(): test tp_flags for HAVE_CLASS bit before accessing
Guido van Rossum <guido@python.org>
parents: 19630
diff changeset
1127
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1128 mro = a->tp_mro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1129 if (mro != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1130 /* Deal with multiple inheritance without recursion
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1131 by walking the MRO tuple */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1132 Py_ssize_t i, n;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1133 assert(PyTuple_Check(mro));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1134 n = PyTuple_GET_SIZE(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1135 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1136 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1137 return 1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1138 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1139 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1140 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1141 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1142 /* a is not completely initilized yet; follow tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1143 do {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1144 if (a == b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1145 return 1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1146 a = a->tp_base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1147 } while (a != NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1148 return b == &PyBaseObject_Type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1149 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1150 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1151
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1152 /* 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
1153 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
1154 (so we can't use PyObject_GetAttr) but still binding
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1155 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
1156 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
1157 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
1158
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1159 Two variants:
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1160
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1161 - 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
1162 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
1163
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1164 - 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
1165
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1166 - _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
1167 */
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
1168
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 static PyObject *
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1170 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
1171 {
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
1172 PyObject *res;
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
1173
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
1174 if (*attrobj == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1175 *attrobj = PyString_InternFromString(attrstr);
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
1176 if (*attrobj == NULL)
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
1177 return NULL;
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
1178 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1179 res = _PyType_Lookup(Py_TYPE(self), *attrobj);
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1180 if (res != NULL) {
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
1181 descrgetfunc f;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1182 if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
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
1183 Py_INCREF(res);
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
1184 else
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1185 res = f(res, self, (PyObject *)(Py_TYPE(self)));
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 return res;
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
1188 }
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
1189
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1190 static PyObject *
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1191 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
1192 {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1193 PyObject *res = lookup_maybe(self, attrstr, attrobj);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1194 if (res == NULL && !PyErr_Occurred())
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1195 PyErr_SetObject(PyExc_AttributeError, *attrobj);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1196 return res;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1197 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1198
53904
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1199 PyObject *
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1200 _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
1201 {
53949
b7920e183422 can't handle classic classes here
Benjamin Peterson <benjamin@python.org>
parents: 53904
diff changeset
1202 assert(!PyInstance_Check(self));
53904
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1203 return lookup_maybe(self, attrstr, attrobj);
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1204 }
1ecdca11eb9e add _PyObject_LookupSpecial to handle fetching special method lookup
Benjamin Peterson <benjamin@python.org>
parents: 53372
diff changeset
1205
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1206 /* A variation of PyObject_CallMethod that uses lookup_method()
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1207 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
1208 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
1209
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
1210 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
1211 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
1212 {
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1213 va_list va;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1214 PyObject *args, *func = 0, *retval;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1215 va_start(va, format);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1216
20225
aa18c0ecc4fb call_method(), call_maybe(): fix a performance bug: the argument
Guido van Rossum <guido@python.org>
parents: 20220
diff changeset
1217 func = lookup_maybe(o, name, nameobj);
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1218 if (func == NULL) {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1219 va_end(va);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1220 if (!PyErr_Occurred())
20225
aa18c0ecc4fb call_method(), call_maybe(): fix a performance bug: the argument
Guido van Rossum <guido@python.org>
parents: 20220
diff changeset
1221 PyErr_SetObject(PyExc_AttributeError, *nameobj);
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1222 return NULL;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1223 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1224
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1225 if (format && *format)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1226 args = Py_VaBuildValue(format, va);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1227 else
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1228 args = PyTuple_New(0);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1229
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1230 va_end(va);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1231
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1232 if (args == NULL)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1233 return NULL;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1234
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1235 assert(PyTuple_Check(args));
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1236 retval = PyObject_Call(func, args, NULL);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1237
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1238 Py_DECREF(args);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1239 Py_DECREF(func);
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 return retval;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1242 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1243
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1244 /* 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
1245
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
1246 static PyObject *
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1247 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
1248 {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1249 va_list va;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1250 PyObject *args, *func = 0, *retval;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1251 va_start(va, format);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1252
20225
aa18c0ecc4fb call_method(), call_maybe(): fix a performance bug: the argument
Guido van Rossum <guido@python.org>
parents: 20220
diff changeset
1253 func = lookup_maybe(o, name, nameobj);
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1254 if (func == NULL) {
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1255 va_end(va);
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1256 if (!PyErr_Occurred()) {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1257 Py_INCREF(Py_NotImplemented);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1258 return Py_NotImplemented;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
1259 }
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
1260 return NULL;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1261 }
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1262
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1263 if (format && *format)
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1264 args = Py_VaBuildValue(format, va);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1265 else
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1266 args = PyTuple_New(0);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1267
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1268 va_end(va);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1269
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
1270 if (args == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1271 return NULL;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1272
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
1273 assert(PyTuple_Check(args));
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
1274 retval = PyObject_Call(func, args, NULL);
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1275
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1276 Py_DECREF(args);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1277 Py_DECREF(func);
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 return retval;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1280 }
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
1281
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1282 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
1283 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
1284 {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1285 PyObject *bases, *base;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1286 Py_ssize_t i, n;
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1287
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1288 assert(PyList_Check(mro));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1289 assert(PyClass_Check(cls));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1290 i = PySequence_Contains(mro, cls);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1291 if (i < 0)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1292 return -1;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1293 if (!i) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1294 if (PyList_Append(mro, cls) < 0)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1295 return -1;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1296 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1297 bases = ((PyClassObject *)cls)->cl_bases;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1298 assert(bases && PyTuple_Check(bases));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1299 n = PyTuple_GET_SIZE(bases);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1300 for (i = 0; i < n; i++) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1301 base = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1302 if (fill_classic_mro(mro, base) < 0)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1303 return -1;
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 return 0;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1306 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1307
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1308 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
1309 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
1310 {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1311 PyObject *mro;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1312
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1313 assert(PyClass_Check(cls));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1314 mro = PyList_New(0);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1315 if (mro != NULL) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1316 if (fill_classic_mro(mro, cls) == 0)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1317 return mro;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1318 Py_DECREF(mro);
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 return NULL;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1321 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1322
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1323 /*
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1324 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
1325 "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
1326 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
1327 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
1328 (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
1329
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1330 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
1331
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1332 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
1333 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
1334
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1335 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
1336
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1337 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
1338 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
1339 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
1340
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1341 Monotonicity.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1342 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
1343 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
1344
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1345 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
1346 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
1347 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
1348 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
1349 */
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1350
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1351 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
1352 tail_contains(PyObject *list, int whence, PyObject *o) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1353 Py_ssize_t j, size;
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1354 size = PyList_GET_SIZE(list);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1355
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1356 for (j = whence+1; j < size; j++) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1357 if (PyList_GET_ITEM(list, j) == o)
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1358 return 1;
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 return 0;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1361 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1362
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1363 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
1364 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
1365 {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1366 PyObject *name = PyObject_GetAttrString(cls, "__name__");
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1367 if (name == NULL) {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1368 PyErr_Clear();
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1369 Py_XDECREF(name);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1370 name = PyObject_Repr(cls);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1371 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1372 if (name == NULL)
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1373 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1374 if (!PyString_Check(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
1375 Py_DECREF(name);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1376 return NULL;
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 return name;
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1379 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1380
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1381 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
1382 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
1383 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1384 Py_ssize_t i, j, n;
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1385 /* Let's use a quadratic time algorithm,
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1386 assuming that the bases lists is short.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1387 */
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1388 n = PyList_GET_SIZE(list);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1389 for (i = 0; i < n; i++) {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1390 PyObject *o = PyList_GET_ITEM(list, i);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1391 for (j = i + 1; j < n; j++) {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1392 if (PyList_GET_ITEM(list, j) == o) {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1393 o = class_name(o);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1394 PyErr_Format(PyExc_TypeError,
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1395 "duplicate base class %s",
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1396 o ? PyString_AS_STRING(o) : "?");
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1397 Py_XDECREF(o);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1398 return -1;
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1399 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1400 }
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 return 0;
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1403 }
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 /* 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
1406
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1407 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
1408 insight into error reporting, report the classes that were candidates
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1409 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
1410 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
1411 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
1412 */
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1413
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1414 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
1415 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
1416 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1417 Py_ssize_t i, n, off, to_merge_size;
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1418 char buf[1000];
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1419 PyObject *k, *v;
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1420 PyObject *set = PyDict_New();
37067
1f4549865aad Fix some missing checks after PyTuple_New, PyList_New, PyDict_New
Georg Brandl <georg@python.org>
parents: 36925
diff changeset
1421 if (!set) return;
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1422
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1423 to_merge_size = PyList_GET_SIZE(to_merge);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1424 for (i = 0; i < to_merge_size; i++) {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1425 PyObject *L = PyList_GET_ITEM(to_merge, i);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1426 if (remain[i] < PyList_GET_SIZE(L)) {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1427 PyObject *c = PyList_GET_ITEM(L, remain[i]);
37067
1f4549865aad Fix some missing checks after PyTuple_New, PyList_New, PyDict_New
Georg Brandl <georg@python.org>
parents: 36925
diff changeset
1428 if (PyDict_SetItem(set, c, Py_None) < 0) {
1f4549865aad Fix some missing checks after PyTuple_New, PyList_New, PyDict_New
Georg Brandl <georg@python.org>
parents: 36925
diff changeset
1429 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
1430 return;
37067
1f4549865aad Fix some missing checks after PyTuple_New, PyList_New, PyDict_New
Georg Brandl <georg@python.org>
parents: 36925
diff changeset
1431 }
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1432 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1433 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1434 n = PyDict_Size(set);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1435
28514
9400504cb0b2 SF bug #699934: Obscure error message
Raymond Hettinger <python@rcn.com>
parents: 28403
diff changeset
1436 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
9400504cb0b2 SF bug #699934: Obscure error message
Raymond Hettinger <python@rcn.com>
parents: 28403
diff changeset
1437 consistent method resolution\norder (MRO) for 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
1438 i = 0;
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37759
diff changeset
1439 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1440 PyObject *name = class_name(k);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1441 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1442 name ? PyString_AS_STRING(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
1443 Py_XDECREF(name);
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37759
diff changeset
1444 if (--n && (size_t)(off+1) < sizeof(buf)) {
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1445 buf[off++] = ',';
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1446 buf[off] = '\0';
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1447 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1448 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1449 PyErr_SetString(PyExc_TypeError, buf);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1450 Py_DECREF(set);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1451 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1452
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1453 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
1454 pmerge(PyObject *acc, PyObject* to_merge) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1455 Py_ssize_t i, j, to_merge_size, empty_cnt;
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1456 int *remain;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1457 int ok;
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1458
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1459 to_merge_size = PyList_GET_SIZE(to_merge);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1460
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1461 /* remain stores an index into each sublist of to_merge.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1462 remain[i] is the index of the next base in to_merge[i]
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1463 that is not included in acc.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1464 */
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
1465 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1466 if (remain == NULL)
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1467 return -1;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1468 for (i = 0; i < to_merge_size; i++)
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1469 remain[i] = 0;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1470
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1471 again:
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1472 empty_cnt = 0;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1473 for (i = 0; i < to_merge_size; i++) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1474 PyObject *candidate;
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1475
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1476 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1477
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1478 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1479 empty_cnt++;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1480 continue;
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1481 }
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1482
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1483 /* Choose next candidate for MRO.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1484
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1485 The input sequences alone can determine the choice.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1486 If not, choose the class which appears in the MRO
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1487 of the earliest direct superclass of the new class.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1488 */
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1489
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1490 candidate = PyList_GET_ITEM(cur_list, remain[i]);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1491 for (j = 0; j < to_merge_size; j++) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1492 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1493 if (tail_contains(j_lst, remain[j], candidate)) {
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1494 goto skip; /* continue outer loop */
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1495 }
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1496 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1497 ok = PyList_Append(acc, candidate);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1498 if (ok < 0) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1499 PyMem_Free(remain);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1500 return -1;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1501 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1502 for (j = 0; j < to_merge_size; j++) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1503 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
26964
5df7a284eaf4 Fix an out-of-bound index in pmerge() discovered by Zooko (SF bug
Guido van Rossum <guido@python.org>
parents: 26660
diff changeset
1504 if (remain[j] < PyList_GET_SIZE(j_lst) &&
5df7a284eaf4 Fix an out-of-bound index in pmerge() discovered by Zooko (SF bug
Guido van Rossum <guido@python.org>
parents: 26660
diff changeset
1505 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1506 remain[j]++;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1507 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1508 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1509 goto again;
26227
529719a5350e Repaired illegal syntax most compilers probably let slide (but MSVC
Tim Peters <tim.peters@gmail.com>
parents: 26222
diff changeset
1510 skip: ;
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1511 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1512
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1513 if (empty_cnt == to_merge_size) {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1514 PyMem_FREE(remain);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1515 return 0;
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1516 }
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1517 set_mro_error(to_merge, remain);
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 PyMem_FREE(remain);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1519 return -1;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1520 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1521
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1522 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1523 mro_implementation(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1524 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1525 Py_ssize_t i, n;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1526 int ok;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1527 PyObject *bases, *result;
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1528 PyObject *to_merge, *bases_aslist;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1529
45004
e224ad4b8b8c Whitespace cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 44664
diff changeset
1530 if (type->tp_dict == NULL) {
e224ad4b8b8c Whitespace cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 44664
diff changeset
1531 if (PyType_Ready(type) < 0)
23903
066f8938071a Patch from SF bug 570483 (Tim Northover).
Guido van Rossum <guido@python.org>
parents: 23856
diff changeset
1532 return NULL;
066f8938071a Patch from SF bug 570483 (Tim Northover).
Guido van Rossum <guido@python.org>
parents: 23856
diff changeset
1533 }
066f8938071a Patch from SF bug 570483 (Tim Northover).
Guido van Rossum <guido@python.org>
parents: 23856
diff changeset
1534
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1535 /* Find a superclass linearization that honors the constraints
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1536 of the explicit lists of bases and the constraints implied by
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
1537 each base class.
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1538
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1539 to_merge is a list of lists, where each list is a superclass
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1540 linearization implied by a base class. The last element of
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1541 to_merge is the declared list of bases.
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1542 */
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1543
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1544 bases = type->tp_bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1545 n = PyTuple_GET_SIZE(bases);
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1546
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1547 to_merge = PyList_New(n+1);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1548 if (to_merge == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1549 return NULL;
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1550
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1551 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1552 PyObject *base = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1553 PyObject *parentMRO;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1554 if (PyType_Check(base))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1555 parentMRO = PySequence_List(
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1556 ((PyTypeObject*)base)->tp_mro);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1557 else
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1558 parentMRO = classic_mro(base);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1559 if (parentMRO == NULL) {
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1560 Py_DECREF(to_merge);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1561 return NULL;
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
1562 }
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1563
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1564 PyList_SET_ITEM(to_merge, i, parentMRO);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1565 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1566
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1567 bases_aslist = PySequence_List(bases);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1568 if (bases_aslist == NULL) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1569 Py_DECREF(to_merge);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1570 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1571 }
26410
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1572 /* This is just a basic sanity check. */
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1573 if (check_duplicates(bases_aslist) < 0) {
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1574 Py_DECREF(to_merge);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1575 Py_DECREF(bases_aslist);
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1576 return NULL;
26918e2efd17 A tweaked version of Jeremy's patch #642489, to produce better error
Guido van Rossum <guido@python.org>
parents: 26227
diff changeset
1577 }
26222
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1578 PyList_SET_ITEM(to_merge, n, bases_aslist);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1579
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1580 result = Py_BuildValue("[O]", (PyObject *)type);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1581 if (result == NULL) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1582 Py_DECREF(to_merge);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1583 return NULL;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1584 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1585
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1586 ok = pmerge(result, to_merge);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1587 Py_DECREF(to_merge);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1588 if (ok < 0) {
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1589 Py_DECREF(result);
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1590 return NULL;
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1591 }
c95190532c0c Use the new C3 MRO algorithm, implemented by Samuele Pedroni (SF patch
Guido van Rossum <guido@python.org>
parents: 25853
diff changeset
1592
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1593 return result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1594 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1595
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1596 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
1597 mro_external(PyObject *self)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1598 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1599 PyTypeObject *type = (PyTypeObject *)self;
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 return mro_implementation(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1602 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1603
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1604 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1605 mro_internal(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1606 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1607 PyObject *mro, *result, *tuple;
36153
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1608 int checkit = 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1609
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1610 if (Py_TYPE(type) == &PyType_Type) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1611 result = mro_implementation(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1612 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1613 else {
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
1614 static PyObject *mro_str;
36153
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1615 checkit = 1;
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
1616 mro = lookup_method((PyObject *)type, "mro", &mro_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1617 if (mro == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1618 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1619 result = PyObject_CallObject(mro, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1620 Py_DECREF(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1621 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1622 if (result == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1623 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1624 tuple = PySequence_Tuple(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1625 Py_DECREF(result);
36153
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1626 if (tuple == NULL)
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1627 return -1;
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1628 if (checkit) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1629 Py_ssize_t i, len;
36153
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1630 PyObject *cls;
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1631 PyTypeObject *solid;
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1632
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1633 solid = solid_base(type);
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1634
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1635 len = PyTuple_GET_SIZE(tuple);
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1636
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1637 for (i = 0; i < len; i++) {
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1638 PyTypeObject *t;
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1639 cls = PyTuple_GET_ITEM(tuple, i);
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1640 if (PyClass_Check(cls))
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1641 continue;
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1642 else if (!PyType_Check(cls)) {
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1643 PyErr_Format(PyExc_TypeError,
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1644 "mro() returned a non-class ('%.500s')",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1645 Py_TYPE(cls)->tp_name);
36169
652429c4f2a4 Fix ref/memory leak introduced in rev 41845.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36153
diff changeset
1646 Py_DECREF(tuple);
36153
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1647 return -1;
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1648 }
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1649 t = (PyTypeObject*)cls;
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1650 if (!PyType_IsSubtype(solid, solid_base(t))) {
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1651 PyErr_Format(PyExc_TypeError,
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1652 "mro() returned base with unsuitable layout ('%.500s')",
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1653 t->tp_name);
36169
652429c4f2a4 Fix ref/memory leak introduced in rev 41845.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36153
diff changeset
1654 Py_DECREF(tuple);
36153
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1655 return -1;
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1656 }
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1657 }
042eb3d1ce3f SF bug #1153075: "PyXxx_Check(x) trusts x->ob_type->tp_mro".
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
1658 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1659 type->tp_mro = tuple;
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
1660
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
1661 type_mro_modified(type, type->tp_mro);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
1662 /* corner case: the old-style super class might have been hidden
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
1663 from the custom MRO */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
1664 type_mro_modified(type, type->tp_bases);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
1665
47358
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
1666 PyType_Modified(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
1667
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1668 return 0;
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
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1671
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1672 /* Calculate the best base amongst multiple base classes.
41488
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
1673 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
1674
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1675 static PyTypeObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1676 best_base(PyObject *bases)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1677 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1678 Py_ssize_t i, n;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1679 PyTypeObject *base, *winner, *candidate, *base_i;
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1680 PyObject *base_proto;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1681
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1682 assert(PyTuple_Check(bases));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1683 n = PyTuple_GET_SIZE(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1684 assert(n > 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
1685 base = NULL;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1686 winner = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1687 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1688 base_proto = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1689 if (PyClass_Check(base_proto))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1690 continue;
41488
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
1691 if (!PyType_Check(base_proto)) {
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
1692 PyErr_SetString(
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
1693 PyExc_TypeError,
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
1694 "bases must be types");
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
1695 return NULL;
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
1696 }
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1697 base_i = (PyTypeObject *)base_proto;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1698 if (base_i->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
1699 if (PyType_Ready(base_i) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1700 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1701 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1702 candidate = solid_base(base_i);
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1703 if (winner == NULL) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1704 winner = candidate;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1705 base = base_i;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1706 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1707 else if (PyType_IsSubtype(winner, candidate))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1708 ;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1709 else if (PyType_IsSubtype(candidate, winner)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1710 winner = candidate;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1711 base = base_i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1712 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1713 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1714 PyErr_SetString(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1715 PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1716 "multiple bases have "
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1717 "instance lay-out conflict");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1718 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1719 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1720 }
21404
064311149cb4 (Merge into trunk.)
Guido van Rossum <guido@python.org>
parents: 21246
diff changeset
1721 if (base == NULL)
064311149cb4 (Merge into trunk.)
Guido van Rossum <guido@python.org>
parents: 21246
diff changeset
1722 PyErr_SetString(PyExc_TypeError,
064311149cb4 (Merge into trunk.)
Guido van Rossum <guido@python.org>
parents: 21246
diff changeset
1723 "a new-style class can't have only classic bases");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1724 return base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1725 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1726
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1727 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1728 extra_ivars(PyTypeObject *type, PyTypeObject *base)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1729 {
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1730 size_t t_size = type->tp_basicsize;
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1731 size_t b_size = base->tp_basicsize;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1732
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1733 assert(t_size >= b_size); /* Else type smaller than base! */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1734 if (type->tp_itemsize || base->tp_itemsize) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1735 /* If itemsize is involved, stricter rules */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1736 return t_size != b_size ||
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1737 type->tp_itemsize != base->tp_itemsize;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1738 }
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1739 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1740 type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1741 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1742 t_size -= sizeof(PyObject *);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1743 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1744 type->tp_dictoffset + sizeof(PyObject *) == t_size &&
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1745 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1746 t_size -= sizeof(PyObject *);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1747
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1748 return t_size != b_size;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1749 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1750
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1751 static PyTypeObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1752 solid_base(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1753 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1754 PyTypeObject *base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1755
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1756 if (type->tp_base)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1757 base = solid_base(type->tp_base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1758 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1759 base = &PyBaseObject_Type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1760 if (extra_ivars(type, base))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1761 return type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1762 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1763 return base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1764 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1765
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1766 static void object_dealloc(PyObject *);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1767 static int object_init(PyObject *, PyObject *, PyObject *);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1768 static int update_slot(PyTypeObject *, PyObject *);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
1769 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
1770
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1771 /*
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1772 * 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
1773 * 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
1774 * 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
1775 */
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1776 static PyTypeObject *
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1777 get_builtin_base_with_dict(PyTypeObject *type)
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1778 {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1779 while (type->tp_base != NULL) {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1780 if (type->tp_dictoffset != 0 &&
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1781 !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1782 return type;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1783 type = type->tp_base;
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 return NULL;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1786 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1787
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1788 static PyObject *
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1789 get_dict_descriptor(PyTypeObject *type)
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1790 {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1791 static PyObject *dict_str;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1792 PyObject *descr;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1793
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1794 if (dict_str == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1795 dict_str = PyString_InternFromString("__dict__");
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1796 if (dict_str == NULL)
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1797 return NULL;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1798 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1799 descr = _PyType_Lookup(type, dict_str);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1800 if (descr == NULL || !PyDescr_IsData(descr))
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1801 return NULL;
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 return descr;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1804 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1805
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1806 static void
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1807 raise_dict_descr_error(PyObject *obj)
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1808 {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1809 PyErr_Format(PyExc_TypeError,
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1810 "this __dict__ descriptor does not support "
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1811 "'%.200s' objects", obj->ob_type->tp_name);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1812 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1813
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1814 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
1815 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
1816 {
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1817 PyObject **dictptr;
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
1818 PyObject *dict;
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1819 PyTypeObject *base;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1820
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1821 base = get_builtin_base_with_dict(obj->ob_type);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1822 if (base != NULL) {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1823 descrgetfunc func;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1824 PyObject *descr = get_dict_descriptor(base);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1825 if (descr == NULL) {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1826 raise_dict_descr_error(obj);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1827 return NULL;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1828 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1829 func = descr->ob_type->tp_descr_get;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1830 if (func == NULL) {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1831 raise_dict_descr_error(obj);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1832 return NULL;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1833 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1834 return func(descr, obj, (PyObject *)(obj->ob_type));
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1835 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1836
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1837 dictptr = _PyObject_GetDictPtr(obj);
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
1838 if (dictptr == NULL) {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1839 PyErr_SetString(PyExc_AttributeError,
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1840 "This object has no __dict__");
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1841 return NULL;
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1842 }
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1843 dict = *dictptr;
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1844 if (dict == NULL)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1845 *dictptr = dict = PyDict_New();
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1846 Py_XINCREF(dict);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1847 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
1848 }
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1849
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1850 static int
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1851 subtype_setdict(PyObject *obj, PyObject *value, void *context)
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1852 {
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1853 PyObject **dictptr;
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1854 PyObject *dict;
41576
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1855 PyTypeObject *base;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1856
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1857 base = get_builtin_base_with_dict(obj->ob_type);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1858 if (base != NULL) {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1859 descrsetfunc func;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1860 PyObject *descr = get_dict_descriptor(base);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1861 if (descr == NULL) {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1862 raise_dict_descr_error(obj);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1863 return -1;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1864 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1865 func = descr->ob_type->tp_descr_set;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1866 if (func == NULL) {
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1867 raise_dict_descr_error(obj);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1868 return -1;
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1869 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1870 return func(descr, obj, value);
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1871 }
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1872
980ff0b1b957 Fix for #1303614 and #1174712:
Armin Rigo <arigo@tunes.org>
parents: 41489
diff changeset
1873 dictptr = _PyObject_GetDictPtr(obj);
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1874 if (dictptr == NULL) {
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1875 PyErr_SetString(PyExc_AttributeError,
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1876 "This object has no __dict__");
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1877 return -1;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1878 }
21235
b782490da7f0 At the PythonLabs meeting someone mentioned it would make Jim really
Guido van Rossum <guido@python.org>
parents: 21192
diff changeset
1879 if (value != NULL && !PyDict_Check(value)) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
1880 PyErr_Format(PyExc_TypeError,
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
1881 "__dict__ must be set to a dictionary, "
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1882 "not a '%.200s'", Py_TYPE(value)->tp_name);
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1883 return -1;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1884 }
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1885 dict = *dictptr;
21235
b782490da7f0 At the PythonLabs meeting someone mentioned it would make Jim really
Guido van Rossum <guido@python.org>
parents: 21192
diff changeset
1886 Py_XINCREF(value);
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1887 *dictptr = value;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1888 Py_XDECREF(dict);
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1889 return 0;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1890 }
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
1891
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1892 static PyObject *
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1893 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
1894 {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1895 PyObject **weaklistptr;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1896 PyObject *result;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1897
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1898 if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1899 PyErr_SetString(PyExc_AttributeError,
39367
55f830183532 SF patch #1534048 (bug #1531003): fix typo in error message
Fred Drake <fdrake@acm.org>
parents: 39129
diff changeset
1900 "This object has no __weakref__");
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1901 return NULL;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1902 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1903 assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1904 assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1905 (size_t)(Py_TYPE(obj)->tp_basicsize));
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1906 weaklistptr = (PyObject **)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1907 ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1908 if (*weaklistptr == NULL)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1909 result = Py_None;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1910 else
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1911 result = *weaklistptr;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1912 Py_INCREF(result);
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1913 return result;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1914 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1915
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
1916 /* 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
1917
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1918 static PyGetSetDef subtype_getsets_full[] = {
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1919 {"__dict__", subtype_dict, subtype_setdict,
24785
4de9ac0e4173 Allow docstrings to be removed during compilation
Neal Norwitz <nnorwitz@gmail.com>
parents: 24762
diff changeset
1920 PyDoc_STR("dictionary for instance variables (if defined)")},
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1921 {"__weakref__", subtype_getweakref, NULL,
24785
4de9ac0e4173 Allow docstrings to be removed during compilation
Neal Norwitz <nnorwitz@gmail.com>
parents: 24762
diff changeset
1922 PyDoc_STR("list of weak references to the object (if defined)")},
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
1923 {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
1924 };
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
1925
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
1926 static PyGetSetDef subtype_getsets_dict_only[] = {
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1927 {"__dict__", subtype_dict, subtype_setdict,
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 PyDoc_STR("dictionary for instance variables (if defined)")},
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 {0}
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 };
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1931
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1932 static PyGetSetDef subtype_getsets_weakref_only[] = {
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1933 {"__weakref__", subtype_getweakref, NULL,
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 PyDoc_STR("list of weak references to the object (if defined)")},
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 {0}
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1936 };
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
1937
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1938 static int
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1939 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
1940 {
24245
50443127b21d valid_identifier(): use an unsigned char* so that isalpha() will do
Guido van Rossum <guido@python.org>
parents: 24184
diff changeset
1941 unsigned char *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
1942 Py_ssize_t i, n;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1943
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1944 if (!PyString_Check(s)) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
1945 PyErr_Format(PyExc_TypeError,
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
1946 "__slots__ items must be strings, not '%.200s'",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
1947 Py_TYPE(s)->tp_name);
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1948 return 0;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1949 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1950 p = (unsigned char *) PyString_AS_STRING(s);
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1951 n = PyString_GET_SIZE(s);
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1952 /* We must reject an empty name. As a hack, we bump the
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1953 length to 1 so that the loop will balk on the trailing \0. */
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1954 if (n == 0)
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1955 n = 1;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1956 for (i = 0; i < n; i++, p++) {
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1957 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1958 PyErr_SetString(PyExc_TypeError,
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1959 "__slots__ must be identifiers");
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1960 return 0;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1961 }
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 return 1;
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1964 }
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
1965
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1966 #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
1967 /* 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
1968
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1969 static PyObject *
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
1970 _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
1971 {
41188
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1972 PyObject *tmp = NULL;
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1973 PyObject *slot_name, *new_name;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
1974 Py_ssize_t i;
41188
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1975
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1976 for (i = 0; i < nslots; i++) {
41188
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1977 if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1978 if (tmp == NULL) {
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1979 tmp = PySequence_List(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
1980 if (tmp == NULL)
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1981 return NULL;
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1982 }
41188
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1983 new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1984 NULL);
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1985 if (new_name == NULL) {
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1986 Py_DECREF(tmp);
41188
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1987 return NULL;
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1988 }
41188
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1989 Py_INCREF(new_name);
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1990 PyList_SET_ITEM(tmp, i, new_name);
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1991 Py_DECREF(slot_name);
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1992 }
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
1993 }
41188
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1994 if (tmp != NULL) {
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1995 slots = PyList_AsTuple(tmp);
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1996 Py_DECREF(tmp);
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1997 }
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
1998 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
1999 }
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
2000 #endif
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
2001
41287
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2002 /* Forward */
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2003 static int
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2004 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
2005
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2006 static int
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2007 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
2008 {
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2009 int res;
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2010
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2011 assert(args != NULL && PyTuple_Check(args));
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2012 assert(kwds == NULL || PyDict_Check(kwds));
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2013
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2014 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2015 PyErr_SetString(PyExc_TypeError,
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2016 "type.__init__() takes no keyword arguments");
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2017 return -1;
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2018 }
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2019
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2020 if (args != NULL && PyTuple_Check(args) &&
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2021 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2022 PyErr_SetString(PyExc_TypeError,
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2023 "type.__init__() takes 1 or 3 arguments");
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2024 return -1;
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2025 }
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2026
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2027 /* Call object.__init__(self) now. */
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2028 /* XXX Could call super(type, cls).__init__() but what's the point? */
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2029 args = PyTuple_GetSlice(args, 0, 0);
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2030 res = object_init(cls, args, NULL);
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2031 Py_DECREF(args);
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2032 return res;
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2033 }
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2034
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
2035 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2036 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
2037 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2038 PyObject *name, *bases, *dict;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36631
diff changeset
2039 static char *kwlist[] = {"name", "bases", "dict", 0};
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23903
diff changeset
2040 PyObject *slots, *tmp, *newslots;
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
2041 PyTypeObject *type, *base, *tmptype, *winner;
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
2042 PyHeapTypeObject *et;
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
2043 PyMemberDef *mp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
2044 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2045 int j, may_add_dict, may_add_weak;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2046
20725
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2047 assert(args != NULL && PyTuple_Check(args));
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2048 assert(kwds == NULL || PyDict_Check(kwds));
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2049
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
2050 /* Special case: type(x) should return x->ob_type */
20725
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2051 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
2052 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
2053 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
20725
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2054
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2055 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2056 PyObject *x = PyTuple_GET_ITEM(args, 0);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2057 Py_INCREF(Py_TYPE(x));
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2058 return (PyObject *) Py_TYPE(x);
20725
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2059 }
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2060
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2061 /* SF bug 475327 -- if that didn't trigger, we need 3
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2062 arguments. but PyArg_ParseTupleAndKeywords below may give
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2063 a msg saying type() needs exactly 3. */
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2064 if (nargs + nkwds != 3) {
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2065 PyErr_SetString(PyExc_TypeError,
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2066 "type() takes 1 or 3 arguments");
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2067 return NULL;
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
2068 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2069 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2070
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
2071 /* Check arguments: (name, bases, dict) */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2072 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2073 &name,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2074 &PyTuple_Type, &bases,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2075 &PyDict_Type, &dict))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2076 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2077
41488
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2078 /* Determine the proper metatype to deal with this,
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2079 and check for metatype conflicts while we're at it.
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2080 Note that if some other metatype wins to contract,
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2081 it's possible that its instances are not types. */
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2082 nbases = PyTuple_GET_SIZE(bases);
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2083 winner = metatype;
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2084 for (i = 0; i < nbases; i++) {
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2085 tmp = PyTuple_GET_ITEM(bases, i);
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2086 tmptype = tmp->ob_type;
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2087 if (tmptype == &PyClass_Type)
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2088 continue; /* Special case classic classes */
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2089 if (PyType_IsSubtype(winner, tmptype))
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2090 continue;
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2091 if (PyType_IsSubtype(tmptype, winner)) {
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2092 winner = tmptype;
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2093 continue;
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2094 }
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2095 PyErr_SetString(PyExc_TypeError,
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2096 "metaclass conflict: "
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2097 "the metaclass of a derived class "
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2098 "must be a (non-strict) subclass "
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2099 "of the metaclasses of all its bases");
40975
d62805a993c5 Add checking for a number of metaclass error conditions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40974
diff changeset
2100 return NULL;
41488
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2101 }
40975
d62805a993c5 Add checking for a number of metaclass error conditions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40974
diff changeset
2102 if (winner != metatype) {
41488
5a6d903ab7b5 Revert r53997 as per
Armin Rigo <arigo@tunes.org>
parents: 41463
diff changeset
2103 if (winner->tp_new != type_new) /* Pass it to the winner */
40975
d62805a993c5 Add checking for a number of metaclass error conditions.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40974
diff changeset
2104 return winner->tp_new(winner, args, kwds);
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
2105 metatype = winner;
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
2106 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2107
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2108 /* Adjust for empty tuple bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2109 if (nbases == 0) {
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
2110 bases = PyTuple_Pack(1, &PyBaseObject_Type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2111 if (bases == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2112 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2113 nbases = 1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2114 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2115 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2116 Py_INCREF(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2117
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2118 /* XXX From here until type is allocated, "return NULL" leaks bases! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2119
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2120 /* Calculate best base, and check that all bases are type objects */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2121 base = best_base(bases);
30098
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2122 if (base == NULL) {
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2123 Py_DECREF(bases);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2124 return NULL;
30098
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2125 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2126 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2127 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2128 "type '%.100s' is not an acceptable base type",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2129 base->tp_name);
30098
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2130 Py_DECREF(bases);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2131 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2132 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2133
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2134 /* Check for a __slots__ sequence variable in dict, and count it */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2135 slots = PyDict_GetItemString(dict, "__slots__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2136 nslots = 0;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
2137 add_dict = 0;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
2138 add_weak = 0;
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2139 may_add_dict = base->tp_dictoffset == 0;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2140 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2141 if (slots == NULL) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2142 if (may_add_dict) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2143 add_dict++;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2144 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2145 if (may_add_weak) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2146 add_weak++;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2147 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2148 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2149 else {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2150 /* Have slots */
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2151
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2152 /* Make it into a tuple */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2153 if (PyString_Check(slots) || PyUnicode_Check(slots))
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
2154 slots = PyTuple_Pack(1, slots);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2155 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2156 slots = PySequence_Tuple(slots);
30098
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2157 if (slots == NULL) {
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2158 Py_DECREF(bases);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2159 return NULL;
30098
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2160 }
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2161 assert(PyTuple_Check(slots));
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2162
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2163 /* Are slots allowed? */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2164 nslots = PyTuple_GET_SIZE(slots);
29899
434b6cda4fa5 Remove unnecessary check in tests for slots allowed.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29611
diff changeset
2165 if (nslots > 0 && base->tp_itemsize != 0) {
19595
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
2166 PyErr_Format(PyExc_TypeError,
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
2167 "nonempty __slots__ "
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
2168 "not supported for subtype of '%s'",
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
2169 base->tp_name);
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2170 bad_slots:
30098
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2171 Py_DECREF(bases);
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2172 Py_DECREF(slots);
19595
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
2173 return NULL;
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
2174 }
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2175
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
2176 #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
2177 tmp = _unicode_to_string(slots, nslots);
41188
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
2178 if (tmp == NULL)
2900ec04031d Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41110
diff changeset
2179 goto bad_slots;
25793
b858f4c883dd Don't drop old slots if _unicode_to_string did not change anything.
Martin v. Löwis <martin@v.loewis.de>
parents: 25792
diff changeset
2180 if (tmp != slots) {
b858f4c883dd Don't drop old slots if _unicode_to_string did not change anything.
Martin v. Löwis <martin@v.loewis.de>
parents: 25792
diff changeset
2181 Py_DECREF(slots);
b858f4c883dd Don't drop old slots if _unicode_to_string did not change anything.
Martin v. Löwis <martin@v.loewis.de>
parents: 25792
diff changeset
2182 slots = tmp;
b858f4c883dd Don't drop old slots if _unicode_to_string did not change anything.
Martin v. Löwis <martin@v.loewis.de>
parents: 25792
diff changeset
2183 }
25792
a3d612f0563f Allow Unicode strings in __slots__, converting them to byte strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 24867
diff changeset
2184 #endif
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2185 /* Check for valid slot names and two special cases */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2186 for (i = 0; i < nslots; i++) {
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2187 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2188 char *s;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2189 if (!valid_identifier(tmp))
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2190 goto bad_slots;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2191 assert(PyString_Check(tmp));
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2192 s = PyString_AS_STRING(tmp);
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2193 if (strcmp(s, "__dict__") == 0) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2194 if (!may_add_dict || add_dict) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2195 PyErr_SetString(PyExc_TypeError,
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2196 "__dict__ slot disallowed: "
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2197 "we already got one");
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2198 goto bad_slots;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2199 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2200 add_dict++;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2201 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2202 if (strcmp(s, "__weakref__") == 0) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2203 if (!may_add_weak || add_weak) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2204 PyErr_SetString(PyExc_TypeError,
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2205 "__weakref__ slot disallowed: "
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2206 "either we already got one, "
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2207 "or __itemsize__ != 0");
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2208 goto bad_slots;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2209 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2210 add_weak++;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2211 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2212 }
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23903
diff changeset
2213
41212
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2214 /* Copy slots into a list, mangle names and sort them.
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2215 Sorted names are needed for __class__ assignment.
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2216 Convert them back to tuple at the end.
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2217 */
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2218 newslots = PyList_New(nslots - add_dict - add_weak);
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23903
diff changeset
2219 if (newslots == NULL)
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2220 goto bad_slots;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2221 for (i = j = 0; i < nslots; i++) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2222 char *s;
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23903
diff changeset
2223 tmp = PyTuple_GET_ITEM(slots, i);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2224 s = PyString_AS_STRING(tmp);
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2225 if ((add_dict && strcmp(s, "__dict__") == 0) ||
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2226 (add_weak && strcmp(s, "__weakref__") == 0))
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2227 continue;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35637
diff changeset
2228 tmp =_Py_Mangle(name, tmp);
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2229 if (!tmp)
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2230 goto bad_slots;
41212
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2231 PyList_SET_ITEM(newslots, j, tmp);
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2232 j++;
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23903
diff changeset
2233 }
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2234 assert(j == nslots - add_dict - add_weak);
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2235 nslots = j;
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23903
diff changeset
2236 Py_DECREF(slots);
41212
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2237 if (PyList_Sort(newslots) == -1) {
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2238 Py_DECREF(bases);
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2239 Py_DECREF(newslots);
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2240 return NULL;
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2241 }
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2242 slots = PyList_AsTuple(newslots);
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2243 Py_DECREF(newslots);
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2244 if (slots == NULL) {
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2245 Py_DECREF(bases);
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2246 return NULL;
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
2247 }
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23903
diff changeset
2248
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2249 /* Secondary bases may provide weakrefs or dict */
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2250 if (nbases > 1 &&
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2251 ((may_add_dict && !add_dict) ||
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2252 (may_add_weak && !add_weak))) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2253 for (i = 0; i < nbases; i++) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2254 tmp = PyTuple_GET_ITEM(bases, i);
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2255 if (tmp == (PyObject *)base)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2256 continue; /* Skip primary base */
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2257 if (PyClass_Check(tmp)) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2258 /* Classic base class provides both */
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2259 if (may_add_dict && !add_dict)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2260 add_dict++;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2261 if (may_add_weak && !add_weak)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2262 add_weak++;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2263 break;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2264 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2265 assert(PyType_Check(tmp));
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2266 tmptype = (PyTypeObject *)tmp;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2267 if (may_add_dict && !add_dict &&
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2268 tmptype->tp_dictoffset != 0)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2269 add_dict++;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2270 if (may_add_weak && !add_weak &&
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2271 tmptype->tp_weaklistoffset != 0)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2272 add_weak++;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2273 if (may_add_dict && !add_dict)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2274 continue;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2275 if (may_add_weak && !add_weak)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2276 continue;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2277 /* Nothing more to check */
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2278 break;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2279 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2280 }
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
2281 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2282
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2283 /* XXX From here until type is safely allocated,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2284 "return NULL" may leak slots! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2285
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2286 /* Allocate the type object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2287 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2288 if (type == NULL) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2289 Py_XDECREF(slots);
30098
d6379620fb08 /* XXX From here until type is allocated, "return NULL" leaks bases! */
Michael W. Hudson <mwh@python.net>
parents: 30084
diff changeset
2290 Py_DECREF(bases);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2291 return NULL;
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2292 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2293
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2294 /* Keep name and slots alive in the extended type object */
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
2295 et = (PyHeapTypeObject *)type;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2296 Py_INCREF(name);
36631
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
2297 et->ht_name = name;
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
2298 et->ht_slots = slots;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2299
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2300 /* Initialize tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2301 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2302 Py_TPFLAGS_BASETYPE;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2303 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2304 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
52749
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52176
diff changeset
2305 if (base->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52176
diff changeset
2306 type->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2307
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2308 /* It's a new-style number unless it specifically inherits any
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2309 old-style numeric behavior */
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2310 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2311 (base->tp_as_number == NULL))
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2312 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2313
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2314 /* Initialize essential fields */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2315 type->tp_as_number = &et->as_number;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2316 type->tp_as_sequence = &et->as_sequence;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2317 type->tp_as_mapping = &et->as_mapping;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2318 type->tp_as_buffer = &et->as_buffer;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2319 type->tp_name = PyString_AS_STRING(name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2320
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2321 /* Set tp_base and tp_bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2322 type->tp_bases = bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2323 Py_INCREF(base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2324 type->tp_base = base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2325
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2326 /* Initialize tp_dict from passed-in dict */
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2327 type->tp_dict = dict = PyDict_Copy(dict);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2328 if (dict == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2329 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2330 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2331 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2332
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2333 /* Set __module__ in the dict */
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2334 if (PyDict_GetItemString(dict, "__module__") == NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2335 tmp = PyEval_GetGlobals();
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2336 if (tmp != NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2337 tmp = PyDict_GetItemString(tmp, "__name__");
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2338 if (tmp != NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2339 if (PyDict_SetItemString(dict, "__module__",
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2340 tmp) < 0)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2341 return NULL;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2342 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2343 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2344 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
2345
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2346 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
2347 and is a string. The __doc__ accessor will first look for tp_doc;
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
2348 if that fails, it will still look into __dict__.
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2349 */
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2350 {
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2351 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2352 if (doc != NULL && PyString_Check(doc)) {
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2353 const size_t n = (size_t)PyString_GET_SIZE(doc);
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2354 char *tp_doc = (char *)PyObject_MALLOC(n+1);
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35742
diff changeset
2355 if (tp_doc == NULL) {
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2356 Py_DECREF(type);
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2357 return NULL;
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2358 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2359 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2360 type->tp_doc = tp_doc;
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2361 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2362 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
2363
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2364 /* Special-case __new__: if it's a plain function,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2365 make it a static function */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2366 tmp = PyDict_GetItemString(dict, "__new__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2367 if (tmp != NULL && PyFunction_Check(tmp)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2368 tmp = PyStaticMethod_New(tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2369 if (tmp == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2370 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2371 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2372 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2373 PyDict_SetItemString(dict, "__new__", tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2374 Py_DECREF(tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2375 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2376
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2377 /* Add descriptors for custom slots from __slots__, or for __dict__ */
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
2378 mp = PyHeapType_GET_MEMBERS(et);
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
2379 slotoffset = base->tp_basicsize;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2380 if (slots != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2381 for (i = 0; i < nslots; i++, mp++) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2382 mp->name = PyString_AS_STRING(
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2383 PyTuple_GET_ITEM(slots, i));
21192
c18ad52dece3 Fix SF bug #486144: Uninitialized __slot__ vrbl is None.
Guido van Rossum <guido@python.org>
parents: 21151
diff changeset
2384 mp->type = T_OBJECT_EX;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2385 mp->offset = slotoffset;
41110
a8ae2c29c66a Patch #1675981: remove unreachable code from type.__new__() method.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 40975
diff changeset
2386
a8ae2c29c66a Patch #1675981: remove unreachable code from type.__new__() method.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 40975
diff changeset
2387 /* __dict__ and __weakref__ are already filtered out */
a8ae2c29c66a Patch #1675981: remove unreachable code from type.__new__() method.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 40975
diff changeset
2388 assert(strcmp(mp->name, "__dict__") != 0);
a8ae2c29c66a Patch #1675981: remove unreachable code from type.__new__() method.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 40975
diff changeset
2389 assert(strcmp(mp->name, "__weakref__") != 0);
a8ae2c29c66a Patch #1675981: remove unreachable code from type.__new__() method.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 40975
diff changeset
2390
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2391 slotoffset += sizeof(PyObject *);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2392 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2393 }
24761
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2394 if (add_dict) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2395 if (base->tp_itemsize)
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2396 type->tp_dictoffset = -(long)sizeof(PyObject *);
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2397 else
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2398 type->tp_dictoffset = slotoffset;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2399 slotoffset += sizeof(PyObject *);
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2400 }
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2401 if (add_weak) {
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2402 assert(!base->tp_itemsize);
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2403 type->tp_weaklistoffset = slotoffset;
e0431d9c4474 Refactor how __dict__ and __weakref__ interact with __slots__.
Guido van Rossum <guido@python.org>
parents: 24728
diff changeset
2404 slotoffset += sizeof(PyObject *);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2405 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2406 type->tp_basicsize = slotoffset;
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
2407 type->tp_itemsize = base->tp_itemsize;
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
2408 type->tp_members = PyHeapType_GET_MEMBERS(et);
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
2409
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
2410 if (type->tp_weaklistoffset && type->tp_dictoffset)
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
2411 type->tp_getset = subtype_getsets_full;
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
2412 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
2413 type->tp_getset = subtype_getsets_weakref_only;
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
2414 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
2415 type->tp_getset = subtype_getsets_dict_only;
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
2416 else
22d660494f29 Fix for SF bug #642358: only provide a new with a __dict__ or
Guido van Rossum <guido@python.org>
parents: 27124
diff changeset
2417 type->tp_getset = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2418
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2419 /* Special case some slots */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2420 if (type->tp_dictoffset != 0 || nslots > 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2421 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2422 type->tp_getattro = PyObject_GenericGetAttr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2423 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2424 type->tp_setattro = PyObject_GenericSetAttr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2425 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2426 type->tp_dealloc = subtype_dealloc;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2427
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
2428 /* Enable GC unless there are really no instance variables possible */
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
2429 if (!(type->tp_basicsize == sizeof(PyObject) &&
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
2430 type->tp_itemsize == 0))
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
2431 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
2432
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2433 /* Always override allocation strategy to use regular heap */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2434 type->tp_alloc = PyType_GenericAlloc;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2435 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
22980
9f949027d7ba Change signature of _PyObject_GC_Malloc to match PyObject_MALLOC.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22871
diff changeset
2436 type->tp_free = PyObject_GC_Del;
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
2437 type->tp_traverse = subtype_traverse;
23666
2becb5793acd Address SF bug 519621: slots weren't traversed by GC.
Guido van Rossum <guido@python.org>
parents: 23649
diff changeset
2438 type->tp_clear = subtype_clear;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2439 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2440 else
22980
9f949027d7ba Change signature of _PyObject_GC_Malloc to match PyObject_MALLOC.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22871
diff changeset
2441 type->tp_free = PyObject_Del;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2442
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2443 /* Initialize the rest */
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
2444 if (PyType_Ready(type) < 0) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2445 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2446 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2447 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2448
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2449 /* Put the proper slots in place */
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2450 fixup_slot_dispatchers(type);
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2451
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2452 return (PyObject *)type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2453 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2454
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2455 /* 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
2456 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
2457 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2458 _PyType_Lookup(PyTypeObject *type, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2459 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
2460 Py_ssize_t i, n;
47371
e81d02d09697 Revert #2990 patch; it's not necessary as Armin showed.
Georg Brandl <georg@python.org>
parents: 47360
diff changeset
2461 PyObject *mro, *res, *base, *dict;
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
2462 unsigned int h;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2463
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2464 if (MCACHE_CACHEABLE_NAME(name) &&
45004
e224ad4b8b8c Whitespace cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 44664
diff changeset
2465 PyType_HasFeature(type, 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
2466 /* fast path */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2467 h = MCACHE_HASH_METHOD(type, name);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2468 if (method_cache[h].version == type->tp_version_tag &&
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2469 method_cache[h].name == name)
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2470 return method_cache[h].value;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2471 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2472
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2473 /* Look in tp_dict of types in MRO */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2474 mro = type->tp_mro;
23753
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
2475
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
2476 /* If mro is NULL, the type is either not yet initialized
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
2477 by PyType_Ready(), or already cleared by type_clear().
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
2478 Either way the safest thing to do is to return NULL. */
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
2479 if (mro == NULL)
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
2480 return NULL;
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
2481
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
2482 res = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2483 assert(PyTuple_Check(mro));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2484 n = PyTuple_GET_SIZE(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2485 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2486 base = PyTuple_GET_ITEM(mro, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2487 if (PyClass_Check(base))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2488 dict = ((PyClassObject *)base)->cl_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2489 else {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2490 assert(PyType_Check(base));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2491 dict = ((PyTypeObject *)base)->tp_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2492 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2493 assert(dict && PyDict_Check(dict));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2494 res = PyDict_GetItem(dict, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2495 if (res != NULL)
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
2496 break;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2497 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2498
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2499 if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(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
2500 h = MCACHE_HASH_METHOD(type, name);
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2501 method_cache[h].version = type->tp_version_tag;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2502 method_cache[h].value = res; /* borrowed */
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2503 Py_INCREF(name);
47371
e81d02d09697 Revert #2990 patch; it's not necessary as Armin showed.
Georg Brandl <georg@python.org>
parents: 47360
diff changeset
2504 Py_DECREF(method_cache[h].name);
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
2505 method_cache[h].name = name;
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2506 }
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
2507 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2508 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2509
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2510 /* This is similar to PyObject_GenericGetAttr(),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2511 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
2512 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2513 type_getattro(PyTypeObject *type, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2514 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2515 PyTypeObject *metatype = Py_TYPE(type);
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2516 PyObject *meta_attribute, *attribute;
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2517 descrgetfunc meta_get;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2518
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2519 /* Initialize this type (we'll assume the metatype is initialized) */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2520 if (type->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
2521 if (PyType_Ready(type) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2522 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2523 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2524
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2525 /* No readable descriptor found yet */
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2526 meta_get = NULL;
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 23967
diff changeset
2527
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2528 /* Look for the attribute in the metatype */
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2529 meta_attribute = _PyType_Lookup(metatype, name);
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2530
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2531 if (meta_attribute != NULL) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2532 meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 23967
diff changeset
2533
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2534 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2535 /* Data descriptors implement tp_descr_set to intercept
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2536 * writes. Assume the attribute is not overridden in
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2537 * type's tp_dict (and bases): call the descriptor now.
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2538 */
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2539 return meta_get(meta_attribute, (PyObject *)type,
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2540 (PyObject *)metatype);
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2541 }
30159
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2542 Py_INCREF(meta_attribute);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2543 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2544
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2545 /* No data descriptor found on metatype. Look in tp_dict of this
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2546 * type and its bases */
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2547 attribute = _PyType_Lookup(type, name);
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2548 if (attribute != NULL) {
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2549 /* Implement descriptor functionality, if any */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2550 descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
30159
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2551
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2552 Py_XDECREF(meta_attribute);
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2553
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2554 if (local_get != NULL) {
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2555 /* NULL 2nd argument indicates the descriptor was
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2556 * found on the target object itself (or a base) */
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2557 return local_get(attribute, (PyObject *)NULL,
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2558 (PyObject *)type);
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2559 }
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 23967
diff changeset
2560
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2561 Py_INCREF(attribute);
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2562 return attribute;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2563 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2564
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2565 /* No attribute found in local __dict__ (or bases): use the
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2566 * descriptor from the metatype, if any */
30159
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2567 if (meta_get != NULL) {
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2568 PyObject *res;
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2569 res = meta_get(meta_attribute, (PyObject *)type,
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2570 (PyObject *)metatype);
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2571 Py_DECREF(meta_attribute);
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2572 return res;
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 30098
diff changeset
2573 }
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2574
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2575 /* If an ordinary attribute was found on the metatype, return it now */
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2576 if (meta_attribute != NULL) {
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2577 return meta_attribute;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2578 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2579
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2580 /* Give up */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2581 PyErr_Format(PyExc_AttributeError,
22804
77343aa03e2f Clarifying code rearrangement and comments by David Abrahams. I've
Guido van Rossum <guido@python.org>
parents: 22774
diff changeset
2582 "type object '%.50s' has no attribute '%.400s'",
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2583 type->tp_name, PyString_AS_STRING(name));
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2584 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2585 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2586
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2587 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2588 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
2589 {
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2590 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2591 PyErr_Format(
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2592 PyExc_TypeError,
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2593 "can't set attributes of built-in/extension type '%s'",
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2594 type->tp_name);
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2595 return -1;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2596 }
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2597 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2598 return -1;
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2599 return update_slot(type, name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2600 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2601
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2602 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2603 type_dealloc(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2604 {
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
2605 PyHeapTypeObject *et;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2606
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2607 /* Assert this is a heap-allocated type object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2608 assert(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
2609 _PyObject_GC_UNTRACK(type);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2610 PyObject_ClearWeakRefs((PyObject *)type);
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
2611 et = (PyHeapTypeObject *)type;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2612 Py_XDECREF(type->tp_base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2613 Py_XDECREF(type->tp_dict);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2614 Py_XDECREF(type->tp_bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2615 Py_XDECREF(type->tp_mro);
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2616 Py_XDECREF(type->tp_cache);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2617 Py_XDECREF(type->tp_subclasses);
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2618 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2619 * of most other objects. It's okay to cast it to char *.
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2620 */
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35742
diff changeset
2621 PyObject_Free((char *)type->tp_doc);
36631
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
2622 Py_XDECREF(et->ht_name);
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
2623 Py_XDECREF(et->ht_slots);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2624 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
2625 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2626
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2627 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
2628 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
2629 {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2630 PyObject *list, *raw, *ref;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
2631 Py_ssize_t i, n;
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2632
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2633 list = PyList_New(0);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2634 if (list == NULL)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2635 return NULL;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2636 raw = type->tp_subclasses;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2637 if (raw == NULL)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2638 return list;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2639 assert(PyList_Check(raw));
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2640 n = PyList_GET_SIZE(raw);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2641 for (i = 0; i < n; i++) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2642 ref = PyList_GET_ITEM(raw, i);
20320
f29b8ea0b695 type_subclasses(): debug build was broken due to typo in new assert().
Tim Peters <tim.peters@gmail.com>
parents: 20315
diff changeset
2643 assert(PyWeakref_CheckRef(ref));
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2644 ref = PyWeakref_GET_OBJECT(ref);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2645 if (ref != Py_None) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2646 if (PyList_Append(list, ref) < 0) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2647 Py_DECREF(list);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2648 return NULL;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2649 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2650 }
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 return list;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2653 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2654
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2655 static PyMethodDef type_methods[] = {
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19300
diff changeset
2656 {"mro", (PyCFunction)mro_external, METH_NOARGS,
24795
452d6a916196 Allow more docstrings to be removed during compilation
Neal Norwitz <nnorwitz@gmail.com>
parents: 24789
diff changeset
2657 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2658 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
24795
452d6a916196 Allow more docstrings to be removed during compilation
Neal Norwitz <nnorwitz@gmail.com>
parents: 24789
diff changeset
2659 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
54118
ae006386ec39 properly lookup __instancecheck__ and __subclasscheck__
Benjamin Peterson <benjamin@python.org>
parents: 53949
diff changeset
2660 {"__instancecheck__", type___instancecheck__, METH_O,
ae006386ec39 properly lookup __instancecheck__ and __subclasscheck__
Benjamin Peterson <benjamin@python.org>
parents: 53949
diff changeset
2661 PyDoc_STR("__instancecheck__() -> check if an object is an instance")},
ae006386ec39 properly lookup __instancecheck__ and __subclasscheck__
Benjamin Peterson <benjamin@python.org>
parents: 53949
diff changeset
2662 {"__subclasscheck__", type___subclasscheck__, METH_O,
ae006386ec39 properly lookup __instancecheck__ and __subclasscheck__
Benjamin Peterson <benjamin@python.org>
parents: 53949
diff changeset
2663 PyDoc_STR("__subclasschck__ -> check if an class is a subclass")},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2664 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2665 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2666
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23830
diff changeset
2667 PyDoc_STRVAR(type_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2668 "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
2669 "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
2670
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2671 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
2672 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
2673 {
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2674 /* Because of type_is_gc(), the collector only calls this
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2675 for heaptypes. */
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2676 assert(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
2677
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
2678 Py_VISIT(type->tp_dict);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
2679 Py_VISIT(type->tp_cache);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
2680 Py_VISIT(type->tp_mro);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
2681 Py_VISIT(type->tp_bases);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
2682 Py_VISIT(type->tp_base);
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2683
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2684 /* There's no need to visit type->tp_subclasses or
36631
49d9c120357a Bug #1086854: Rename PyHeapType members adding ht_ prefix.
Georg Brandl <georg@python.org>
parents: 36519
diff changeset
2685 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2686 in cycles; tp_subclasses is a list of weak references,
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2687 and slots is a tuple of strings. */
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 return 0;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2690 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2691
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2692 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
2693 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
2694 {
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2695 /* Because of type_is_gc(), the collector only calls this
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2696 for heaptypes. */
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2697 assert(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
2698
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2699 /* The only field we need to clear is tp_mro, which is part of a
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2700 hard cycle (its first element is the class itself) that won't
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2701 be broken otherwise (it's a tuple and tuples don't have a
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2702 tp_clear handler). None of the other fields need to be
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2703 cleared, and here's why:
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2704
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2705 tp_dict:
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2706 It is a dict, so the collector will call its tp_clear.
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2707
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2708 tp_cache:
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2709 Not used; if it were, it would be a dict.
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2710
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2711 tp_bases, tp_base:
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2712 If these are involved in a cycle, there must be at least
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2713 one other, mutable object in the cycle, e.g. a base
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2714 class's dict; the cycle will be broken that way.
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2715
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2716 tp_subclasses:
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2717 A list of weak references can't be part of a cycle; and
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2718 lists have their own tp_clear.
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2719
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
2720 slots (in PyHeapTypeObject):
23755
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2721 A tuple of strings can't be part of a cycle.
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2722 */
0279040ae03c In the recent python-dev thread "Bizarre new test failure", we
Guido van Rossum <guido@python.org>
parents: 23753
diff changeset
2723
37748
d1547426ab61 Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents: 37603
diff changeset
2724 Py_CLEAR(type->tp_mro);
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 return 0;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2727 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2728
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2729 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
2730 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
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 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2733 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2734
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
2735 PyTypeObject PyType_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41816
diff changeset
2736 PyVarObject_HEAD_INIT(&PyType_Type, 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2737 "type", /* tp_name */
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
2738 sizeof(PyHeapTypeObject), /* tp_basicsize */
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
2739 sizeof(PyMemberDef), /* tp_itemsize */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2740 (destructor)type_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2741 0, /* tp_print */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2742 0, /* tp_getattr */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2743 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2744 type_compare, /* tp_compare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2745 (reprfunc)type_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2746 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2747 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2748 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2749 (hashfunc)_Py_HashPointer, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2750 (ternaryfunc)type_call, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2751 0, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2752 (getattrofunc)type_getattro, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2753 (setattrofunc)type_setattro, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2754 0, /* tp_as_buffer */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2755 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
2756 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2757 type_doc, /* tp_doc */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2758 (traverseproc)type_traverse, /* tp_traverse */
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2759 (inquiry)type_clear, /* tp_clear */
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
2760 type_richcompare, /* tp_richcompare */
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2761 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2762 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2763 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2764 type_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2765 type_members, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2766 type_getsets, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2767 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2768 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2769 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2770 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2771 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
41287
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
2772 type_init, /* tp_init */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2773 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2774 type_new, /* tp_new */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
2775 PyObject_GC_Del, /* tp_free */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
2776 (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
2777 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2778
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2779
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2780 /* 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
2781
41281
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2782 /* 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
2783 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
2784
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2785 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
2786
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2787 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
2788 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
2789 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
2790
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2791 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
2792 __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
2793 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
2794 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
2795 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
2796
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2797 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
2798 __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
2799 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
2800 complaining.
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2801
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2802 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
2803 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
2804 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
2805
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2806 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
2807 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
2808 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
2809 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
2810 (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
2811 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
2812 __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
2813 (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
2814
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2815 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
2816 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
2817 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
2818 rules.
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 */
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2821
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2822 /* Forward */
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2823 static PyObject *
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2824 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
2825
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2826 static int
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2827 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
2828 {
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2829 return PyTuple_GET_SIZE(args) ||
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2830 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2831 }
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2832
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2833 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2834 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
2835 {
41281
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2836 int err = 0;
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2837 if (excess_args(args, kwds)) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2838 PyTypeObject *type = Py_TYPE(self);
41281
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2839 if (type->tp_init != object_init &&
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2840 type->tp_new != object_new)
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2841 {
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2842 err = PyErr_WarnEx(PyExc_DeprecationWarning,
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2843 "object.__init__() takes no parameters",
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2844 1);
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2845 }
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2846 else if (type->tp_init != object_init ||
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2847 type->tp_new == object_new)
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2848 {
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2849 PyErr_SetString(PyExc_TypeError,
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2850 "object.__init__() takes no parameters");
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2851 err = -1;
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2852 }
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 return err;
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2855 }
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2856
27866
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2857 static PyObject *
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2858 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
2859 {
41281
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2860 int err = 0;
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2861 if (excess_args(args, kwds)) {
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2862 if (type->tp_new != object_new &&
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2863 type->tp_init != object_init)
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2864 {
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2865 err = PyErr_WarnEx(PyExc_DeprecationWarning,
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2866 "object.__new__() takes no parameters",
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2867 1);
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2868 }
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2869 else if (type->tp_new != object_new ||
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2870 type->tp_init == object_init)
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2871 {
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2872 PyErr_SetString(PyExc_TypeError,
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2873 "object.__new__() takes no parameters");
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2874 err = -1;
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2875 }
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2876 }
8debce2c4fb1 - Bug #1683368: The object.__init__() and object.__new__() methods are
Guido van Rossum <guido@python.org>
parents: 41212
diff changeset
2877 if (err < 0)
27866
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2878 return NULL;
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
2879
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2880 if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2881 static PyObject *comma = NULL;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2882 PyObject *abstract_methods = NULL;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2883 PyObject *builtins;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2884 PyObject *sorted;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2885 PyObject *sorted_methods = NULL;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2886 PyObject *joined = NULL;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2887 const char *joined_str;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2888
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2889 /* Compute ", ".join(sorted(type.__abstractmethods__))
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2890 into joined. */
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2891 abstract_methods = type_abstractmethods(type, NULL);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2892 if (abstract_methods == NULL)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2893 goto error;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2894 builtins = PyEval_GetBuiltins();
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2895 if (builtins == NULL)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2896 goto error;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2897 sorted = PyDict_GetItemString(builtins, "sorted");
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2898 if (sorted == NULL)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2899 goto error;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2900 sorted_methods = PyObject_CallFunctionObjArgs(sorted,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2901 abstract_methods,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2902 NULL);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2903 if (sorted_methods == NULL)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2904 goto error;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2905 if (comma == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2906 comma = PyString_InternFromString(", ");
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
2907 if (comma == NULL)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2908 goto error;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2909 }
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2910 joined = PyObject_CallMethod(comma, "join",
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2911 "O", sorted_methods);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2912 if (joined == NULL)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2913 goto error;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2914 joined_str = PyString_AsString(joined);
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
2915 if (joined_str == NULL)
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2916 goto error;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2917
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2918 PyErr_Format(PyExc_TypeError,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2919 "Can't instantiate abstract class %s "
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2920 "with abstract methods %s",
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2921 type->tp_name,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2922 joined_str);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2923 error:
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2924 Py_XDECREF(joined);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2925 Py_XDECREF(sorted_methods);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2926 Py_XDECREF(abstract_methods);
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2927 return NULL;
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
2928 }
27866
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2929 return type->tp_alloc(type, 0);
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2930 }
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
2931
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2932 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2933 object_dealloc(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2934 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2935 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
2936 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2937
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2938 static PyObject *
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2939 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
2940 {
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2941 PyTypeObject *type;
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
2942 PyObject *mod, *name, *rtn;
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2943
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2944 type = Py_TYPE(self);
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2945 mod = type_module(type, NULL);
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2946 if (mod == NULL)
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2947 PyErr_Clear();
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2948 else if (!PyString_Check(mod)) {
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2949 Py_DECREF(mod);
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2950 mod = NULL;
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2951 }
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2952 name = type_name(type, NULL);
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2953 if (name == NULL)
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2954 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2955 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2956 rtn = PyString_FromFormat("<%s.%s object at %p>",
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2957 PyString_AS_STRING(mod),
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2958 PyString_AS_STRING(name),
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
2959 self);
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2960 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2961 rtn = PyString_FromFormat("<%s object at %p>",
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
2962 type->tp_name, self);
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2963 Py_XDECREF(mod);
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
2964 Py_DECREF(name);
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
2965 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
2966 }
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
2967
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2968 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2969 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
2970 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2971 unaryfunc f;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2972
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2973 f = Py_TYPE(self)->tp_repr;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2974 if (f == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2975 f = object_repr;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2976 return f(self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2977 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2978
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2979 static PyObject *
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2980 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
2981 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2982 Py_INCREF(Py_TYPE(self));
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
2983 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
2984 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2985
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2986 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2987 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
2988 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2989 return a == b ||
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2990 (a != NULL &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2991 b != NULL &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2992 a->tp_basicsize == b->tp_basicsize &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2993 a->tp_itemsize == b->tp_itemsize &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2994 a->tp_dictoffset == b->tp_dictoffset &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2995 a->tp_weaklistoffset == b->tp_weaklistoffset &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2996 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2997 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2998 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
2999
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3000 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3001 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
3002 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3003 PyTypeObject *base = a->tp_base;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
3004 Py_ssize_t size;
41212
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3005 PyObject *slots_a, *slots_b;
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3006
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3007 if (base != b->tp_base)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3008 return 0;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3009 if (equiv_structs(a, base) && equiv_structs(b, base))
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3010 return 1;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3011 size = base->tp_basicsize;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3012 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3013 size += sizeof(PyObject *);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3014 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3015 size += sizeof(PyObject *);
41212
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3016
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3017 /* Check slots compliance */
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3018 slots_a = ((PyHeapTypeObject *)a)->ht_slots;
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3019 slots_b = ((PyHeapTypeObject *)b)->ht_slots;
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3020 if (slots_a && slots_b) {
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3021 if (PyObject_Compare(slots_a, slots_b) != 0)
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3022 return 0;
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3023 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
c57cd546612d Patch #1623563: allow __class__ assignment for classes with __slots__.
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41201
diff changeset
3024 }
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3025 return size == a->tp_basicsize && size == b->tp_basicsize;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3026 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3027
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3028 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
3029 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
3030 {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3031 PyTypeObject *newbase, *oldbase;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3032
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
3033 if (newto->tp_dealloc != oldto->tp_dealloc ||
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
3034 newto->tp_free != oldto->tp_free)
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3035 {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3036 PyErr_Format(PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3037 "%s assignment: "
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3038 "'%s' deallocator differs from '%s'",
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3039 attr,
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
3040 newto->tp_name,
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
3041 oldto->tp_name);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3042 return 0;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3043 }
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
3044 newbase = newto;
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
3045 oldbase = oldto;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3046 while (equiv_structs(newbase, newbase->tp_base))
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3047 newbase = newbase->tp_base;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3048 while (equiv_structs(oldbase, oldbase->tp_base))
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3049 oldbase = oldbase->tp_base;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3050 if (newbase != oldbase &&
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3051 (newbase->tp_base != oldbase->tp_base ||
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3052 !same_slots_added(newbase, oldbase))) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3053 PyErr_Format(PyExc_TypeError,
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3054 "%s assignment: "
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3055 "'%s' object layout differs from '%s'",
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3056 attr,
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
3057 newto->tp_name,
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
3058 oldto->tp_name);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3059 return 0;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3060 }
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
3061
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3062 return 1;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3063 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3064
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3065 static int
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3066 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
3067 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
3068 PyTypeObject *oldto = Py_TYPE(self);
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
3069 PyTypeObject *newto;
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3070
23037
89db9e3f35a7 SF bug #541883 (Vincent Fiack).
Guido van Rossum <guido@python.org>
parents: 22980
diff changeset
3071 if (value == NULL) {
89db9e3f35a7 SF bug #541883 (Vincent Fiack).
Guido van Rossum <guido@python.org>
parents: 22980
diff changeset
3072 PyErr_SetString(PyExc_TypeError,
89db9e3f35a7 SF bug #541883 (Vincent Fiack).
Guido van Rossum <guido@python.org>
parents: 22980
diff changeset
3073 "can't delete __class__ attribute");
89db9e3f35a7 SF bug #541883 (Vincent Fiack).
Guido van Rossum <guido@python.org>
parents: 22980
diff changeset
3074 return -1;
89db9e3f35a7 SF bug #541883 (Vincent Fiack).
Guido van Rossum <guido@python.org>
parents: 22980
diff changeset
3075 }
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3076 if (!PyType_Check(value)) {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3077 PyErr_Format(PyExc_TypeError,
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3078 "__class__ must be set to new-style class, not '%s' object",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
3079 Py_TYPE(value)->tp_name);
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3080 return -1;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3081 }
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
3082 newto = (PyTypeObject *)value;
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
3083 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
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
3084 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
24728
3b8a707b41c4 Disallow class assignment completely unless both old and new are heap
Guido van Rossum <guido@python.org>
parents: 24685
diff changeset
3085 {
3b8a707b41c4 Disallow class assignment completely unless both old and new are heap
Guido van Rossum <guido@python.org>
parents: 24685
diff changeset
3086 PyErr_Format(PyExc_TypeError,
3b8a707b41c4 Disallow class assignment completely unless both old and new are heap
Guido van Rossum <guido@python.org>
parents: 24685
diff changeset
3087 "__class__ assignment: only for heap types");
3b8a707b41c4 Disallow class assignment completely unless both old and new are heap
Guido van Rossum <guido@python.org>
parents: 24685
diff changeset
3088 return -1;
3b8a707b41c4 Disallow class assignment completely unless both old and new are heap
Guido van Rossum <guido@python.org>
parents: 24685
diff changeset
3089 }
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
3090 if (compatible_for_assignment(newto, oldto, "__class__")) {
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
3091 Py_INCREF(newto);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
3092 Py_TYPE(self) = newto;
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
3093 Py_DECREF(oldto);
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3094 return 0;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3095 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
3096 else {
23541
f6ded6efabcf Add a safeguard against setting the class to something with a
Guido van Rossum <guido@python.org>
parents: 23127
diff changeset
3097 return -1;
f6ded6efabcf Add a safeguard against setting the class to something with a
Guido van Rossum <guido@python.org>
parents: 23127
diff changeset
3098 }
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3099 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3100
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3101 static PyGetSetDef object_getsets[] = {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3102 {"__class__", object_get_class, object_set_class,
24785
4de9ac0e4173 Allow docstrings to be removed during compilation
Neal Norwitz <nnorwitz@gmail.com>
parents: 24762
diff changeset
3103 PyDoc_STR("the object's class")},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3104 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3105 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3106
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3107
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3108 /* 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
3109 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
3110 - pickle protocols < 2
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3111 - 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
3112 - 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
3113 */
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3114
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3115 static PyObject *
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3116 import_copyreg(void)
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3117 {
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3118 static PyObject *copyreg_str;
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3119
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3120 if (!copyreg_str) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
3121 copyreg_str = PyString_InternFromString("copy_reg");
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3122 if (copyreg_str == NULL)
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3123 return NULL;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3124 }
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3125
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3126 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
3127 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3128
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3129 static PyObject *
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3130 slotnames(PyObject *cls)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3131 {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3132 PyObject *clsdict;
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3133 PyObject *copyreg;
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3134 PyObject *slotnames;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3135
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3136 if (!PyType_Check(cls)) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3137 Py_INCREF(Py_None);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3138 return Py_None;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3139 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3140
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3141 clsdict = ((PyTypeObject *)cls)->tp_dict;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3142 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
35637
7dd4e6cb9d27 (pedronis, arigo)
Armin Rigo <arigo@tunes.org>
parents: 35610
diff changeset
3143 if (slotnames != NULL && PyList_Check(slotnames)) {
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3144 Py_INCREF(slotnames);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3145 return slotnames;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3146 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3147
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3148 copyreg = import_copyreg();
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3149 if (copyreg == NULL)
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3150 return NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3151
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3152 slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3153 Py_DECREF(copyreg);
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3154 if (slotnames != NULL &&
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3155 slotnames != Py_None &&
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3156 !PyList_Check(slotnames))
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3157 {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3158 PyErr_SetString(PyExc_TypeError,
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 47108
diff changeset
3159 "copy_reg._slotnames didn't return a list or None");
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3160 Py_DECREF(slotnames);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3161 slotnames = NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3162 }
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 return slotnames;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3165 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3166
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3167 static PyObject *
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3168 reduce_2(PyObject *obj)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3169 {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3170 PyObject *cls, *getnewargs;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3171 PyObject *args = NULL, *args2 = NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3172 PyObject *getstate = NULL, *state = NULL, *names = NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3173 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3174 PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
3175 Py_ssize_t i, n;
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3176
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3177 cls = PyObject_GetAttrString(obj, "__class__");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3178 if (cls == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3179 return NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3180
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3181 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3182 if (getnewargs != NULL) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3183 args = PyObject_CallObject(getnewargs, NULL);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3184 Py_DECREF(getnewargs);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3185 if (args != NULL && !PyTuple_Check(args)) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
3186 PyErr_Format(PyExc_TypeError,
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
3187 "__getnewargs__ should return a tuple, "
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
3188 "not '%.200s'", Py_TYPE(args)->tp_name);
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3189 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3190 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3191 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3192 else {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3193 PyErr_Clear();
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3194 args = PyTuple_New(0);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3195 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3196 if (args == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3197 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3198
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3199 getstate = PyObject_GetAttrString(obj, "__getstate__");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3200 if (getstate != NULL) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3201 state = PyObject_CallObject(getstate, NULL);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3202 Py_DECREF(getstate);
29382
585e303a9491 Fix SF #749831, copy raises SystemError when getstate raises exception
Neal Norwitz <nnorwitz@gmail.com>
parents: 29308
diff changeset
3203 if (state == NULL)
585e303a9491 Fix SF #749831, copy raises SystemError when getstate raises exception
Neal Norwitz <nnorwitz@gmail.com>
parents: 29308
diff changeset
3204 goto end;
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3205 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3206 else {
31477
ba9aaddeb956 Fixed a bug in object.__reduce_ex__ (reduce_2) when using protocol
Jim Fulton <jim@zope.com>
parents: 31176
diff changeset
3207 PyErr_Clear();
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3208 state = PyObject_GetAttrString(obj, "__dict__");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3209 if (state == NULL) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3210 PyErr_Clear();
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3211 state = Py_None;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3212 Py_INCREF(state);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3213 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3214 names = slotnames(cls);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3215 if (names == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3216 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3217 if (names != Py_None) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3218 assert(PyList_Check(names));
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3219 slots = PyDict_New();
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3220 if (slots == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3221 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3222 n = 0;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3223 /* Can't pre-compute the list size; the list
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3224 is stored on the class so accessible to other
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3225 threads, which may be run by DECREF */
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3226 for (i = 0; i < PyList_GET_SIZE(names); i++) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3227 PyObject *name, *value;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3228 name = PyList_GET_ITEM(names, i);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3229 value = PyObject_GetAttr(obj, name);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3230 if (value == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3231 PyErr_Clear();
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3232 else {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3233 int err = PyDict_SetItem(slots, name,
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3234 value);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3235 Py_DECREF(value);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3236 if (err)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3237 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3238 n++;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3239 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3240 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3241 if (n) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3242 state = Py_BuildValue("(NO)", state, slots);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3243 if (state == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3244 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3245 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3246 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3247 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3248
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3249 if (!PyList_Check(obj)) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3250 listitems = Py_None;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3251 Py_INCREF(listitems);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3252 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3253 else {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3254 listitems = PyObject_GetIter(obj);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3255 if (listitems == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3256 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3257 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3258
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3259 if (!PyDict_Check(obj)) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3260 dictitems = Py_None;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3261 Py_INCREF(dictitems);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3262 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3263 else {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3264 dictitems = PyObject_CallMethod(obj, "iteritems", "");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3265 if (dictitems == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3266 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3267 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3268
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3269 copyreg = import_copyreg();
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3270 if (copyreg == NULL)
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3271 goto end;
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3272 newobj = PyObject_GetAttrString(copyreg, "__newobj__");
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3273 if (newobj == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3274 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3275
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3276 n = PyTuple_GET_SIZE(args);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3277 args2 = PyTuple_New(n+1);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3278 if (args2 == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3279 goto end;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3280 PyTuple_SET_ITEM(args2, 0, cls);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3281 cls = NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3282 for (i = 0; i < n; i++) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3283 PyObject *v = PyTuple_GET_ITEM(args, i);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3284 Py_INCREF(v);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3285 PyTuple_SET_ITEM(args2, i+1, v);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3286 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3287
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
3288 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
3289
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3290 end:
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3291 Py_XDECREF(cls);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3292 Py_XDECREF(args);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3293 Py_XDECREF(args2);
28566
9ddc498b7013 Missing DECREF.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28514
diff changeset
3294 Py_XDECREF(slots);
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3295 Py_XDECREF(state);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3296 Py_XDECREF(names);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3297 Py_XDECREF(listitems);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3298 Py_XDECREF(dictitems);
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3299 Py_XDECREF(copyreg);
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3300 Py_XDECREF(newobj);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3301 return res;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3302 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3303
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3304 /*
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3305 * 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
3306 * 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
3307 * - 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
3308 * 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
3309 * 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
3310 * - 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
3311 * __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
3312 * 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
3313 *
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3314 * 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
3315 * 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
3316 * 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
3317 * function.
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3318 */
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3319 static PyObject *
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3320 _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
3321 {
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3322 PyObject *copyreg, *res;
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3323
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3324 if (proto >= 2)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3325 return reduce_2(self);
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3326
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3327 copyreg = import_copyreg();
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3328 if (!copyreg)
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3329 return NULL;
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3330
46865
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3331 res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
b40827eb4b80 Added module stub for copy_reg renaming in 3.0.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46573
diff changeset
3332 Py_DECREF(copyreg);
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 return res;
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3335 }
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3336
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3337 static PyObject *
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3338 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
3339 {
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3340 int proto = 0;
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3341
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3342 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3343 return NULL;
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 return _common_reduce(self, proto);
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3346 }
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3347
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3348 static PyObject *
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3349 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
3350 {
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3351 PyObject *reduce, *res;
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3352 int proto = 0;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3353
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3354 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3355 return NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3356
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3357 reduce = PyObject_GetAttrString(self, "__reduce__");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3358 if (reduce == NULL)
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3359 PyErr_Clear();
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3360 else {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3361 PyObject *cls, *clsreduce, *objreduce;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3362 int override;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3363 cls = PyObject_GetAttrString(self, "__class__");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3364 if (cls == NULL) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3365 Py_DECREF(reduce);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3366 return NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3367 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3368 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3369 Py_DECREF(cls);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3370 if (clsreduce == NULL) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3371 Py_DECREF(reduce);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3372 return NULL;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3373 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3374 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3375 "__reduce__");
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3376 override = (clsreduce != objreduce);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3377 Py_DECREF(clsreduce);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3378 if (override) {
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3379 res = PyObject_CallObject(reduce, NULL);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3380 Py_DECREF(reduce);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3381 return res;
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3382 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3383 else
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3384 Py_DECREF(reduce);
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3385 }
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3386
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3387 return _common_reduce(self, proto);
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3388 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3389
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
3390 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
3391 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
3392 {
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 Py_INCREF(Py_NotImplemented);
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 return Py_NotImplemented;
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 }
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
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 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
3398 "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
3399 "\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 "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
3401 "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
3402 "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
3403 "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
3404
45381
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 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
3407
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3408 class object:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3409 def __format__(self, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3410 if isinstance(format_spec, str):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3411 return format(str(self), format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3412 elif isinstance(format_spec, unicode):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3413 return format(unicode(self), format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3414 */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3415 static PyObject *
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3416 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
3417 {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3418 PyObject *format_spec;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3419 PyObject *self_as_str = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3420 PyObject *result = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3421 PyObject *format_meth = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3422
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3423 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3424 return NULL;
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50828
diff changeset
3425 #ifdef Py_USING_UNICODE
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3426 if (PyUnicode_Check(format_spec)) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3427 self_as_str = PyObject_Unicode(self);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
3428 } else if (PyString_Check(format_spec)) {
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50828
diff changeset
3429 #else
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50828
diff changeset
3430 if (PyString_Check(format_spec)) {
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50828
diff changeset
3431 #endif
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3432 self_as_str = PyObject_Str(self);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3433 } else {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3434 PyErr_SetString(PyExc_TypeError, "argument to __format__ must be unicode or str");
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3435 return NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3436 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3437
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3438 if (self_as_str != NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3439 /* find the format function */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3440 format_meth = PyObject_GetAttrString(self_as_str, "__format__");
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3441 if (format_meth != NULL) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3442 /* and call it */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3443 result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3444 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3445 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3446
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3447 Py_XDECREF(self_as_str);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3448 Py_XDECREF(format_meth);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3449
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3450 return result;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3451 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3452
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3453 static PyObject *
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3454 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
3455 {
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3456 Py_ssize_t res, isize;
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3457
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3458 res = 0;
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3459 isize = self->ob_type->tp_itemsize;
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3460 if (isize > 0)
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3461 res = self->ob_type->ob_size * isize;
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3462 res += self->ob_type->tp_basicsize;
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3463
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3464 return PyInt_FromSsize_t(res);
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3465 }
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3466
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3467 static PyMethodDef object_methods[] = {
27948
db1d4584a5e1 Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents: 27943
diff changeset
3468 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
db1d4584a5e1 Introducing __reduce_ex__, which is called with a protocol number argument
Guido van Rossum <guido@python.org>
parents: 27943
diff changeset
3469 PyDoc_STR("helper for pickle")},
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 41188
diff changeset
3470 {"__reduce__", object_reduce, METH_VARARGS,
24795
452d6a916196 Allow more docstrings to be removed during compilation
Neal Norwitz <nnorwitz@gmail.com>
parents: 24789
diff changeset
3471 PyDoc_STR("helper for pickle")},
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
3472 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45381
diff changeset
3473 object_subclasshook_doc},
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3474 {"__format__", object_format, METH_VARARGS,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45025
diff changeset
3475 PyDoc_STR("default object formatter")},
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47371
diff changeset
3476 {"__sizeof__", object_sizeof, METH_NOARGS,
47421
a65f095b27f3 Some style nits. Also clarify in the docstrings what __sizeof__ does.
Georg Brandl <georg@python.org>
parents: 47419
diff changeset
3477 PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3478 {0}
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3479 };
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3480
28016
b849482c70c6 Implementing the salient parts of __reduce_ex__ in C.
Guido van Rossum <guido@python.org>
parents: 27948
diff changeset
3481
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3482 PyTypeObject PyBaseObject_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41816
diff changeset
3483 PyVarObject_HEAD_INIT(&PyType_Type, 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3484 "object", /* tp_name */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3485 sizeof(PyObject), /* tp_basicsize */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3486 0, /* tp_itemsize */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37067
diff changeset
3487 object_dealloc, /* tp_dealloc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3488 0, /* tp_print */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3489 0, /* tp_getattr */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3490 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3491 0, /* tp_compare */
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3492 object_repr, /* tp_repr */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3493 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3494 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3495 0, /* tp_as_mapping */
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43276
diff changeset
3496 (hashfunc)_Py_HashPointer, /* tp_hash */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3497 0, /* tp_call */
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3498 object_str, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3499 PyObject_GenericGetAttr, /* tp_getattro */
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3500 PyObject_GenericSetAttr, /* tp_setattro */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3501 0, /* tp_as_buffer */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3502 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
24795
452d6a916196 Allow more docstrings to be removed during compilation
Neal Norwitz <nnorwitz@gmail.com>
parents: 24789
diff changeset
3503 PyDoc_STR("The most base type"), /* tp_doc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3504 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3505 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3506 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3507 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3508 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3509 0, /* tp_iternext */
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3510 object_methods, /* tp_methods */
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3511 0, /* tp_members */
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
3512 object_getsets, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3513 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3514 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3515 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3516 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3517 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3518 object_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3519 PyType_GenericAlloc, /* tp_alloc */
27866
bbbba0ee61fb SF patch #685738 by Michael Stone.
Guido van Rossum <guido@python.org>
parents: 27831
diff changeset
3520 object_new, /* tp_new */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3521 PyObject_Del, /* tp_free */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3522 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3523
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3524
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3525 /* 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
3526
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3527 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3528 add_methods(PyTypeObject *type, PyMethodDef *meth)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3529 {
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
3530 PyObject *dict = type->tp_dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3531
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3532 for (; meth->ml_name != NULL; meth++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3533 PyObject *descr;
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 30917
diff changeset
3534 if (PyDict_GetItemString(dict, meth->ml_name) &&
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 30917
diff changeset
3535 !(meth->ml_flags & METH_COEXIST))
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 30917
diff changeset
3536 continue;
22613
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3537 if (meth->ml_flags & METH_CLASS) {
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3538 if (meth->ml_flags & METH_STATIC) {
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3539 PyErr_SetString(PyExc_ValueError,
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3540 "method cannot be both class and static");
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3541 return -1;
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3542 }
26583
8991f78817c7 SF 548651: Fix the METH_CLASS implementation.
Tim Peters <tim.peters@gmail.com>
parents: 26575
diff changeset
3543 descr = PyDescr_NewClassMethod(type, meth);
22613
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3544 }
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3545 else if (meth->ml_flags & METH_STATIC) {
27814
48dc4a59a50e Inline create_specialmethod() -- since METH_CLASS is done differently
Guido van Rossum <guido@python.org>
parents: 27811
diff changeset
3546 PyObject *cfunc = PyCFunction_New(meth, NULL);
48dc4a59a50e Inline create_specialmethod() -- since METH_CLASS is done differently
Guido van Rossum <guido@python.org>
parents: 27811
diff changeset
3547 if (cfunc == NULL)
48dc4a59a50e Inline create_specialmethod() -- since METH_CLASS is done differently
Guido van Rossum <guido@python.org>
parents: 27811
diff changeset
3548 return -1;
48dc4a59a50e Inline create_specialmethod() -- since METH_CLASS is done differently
Guido van Rossum <guido@python.org>
parents: 27811
diff changeset
3549 descr = PyStaticMethod_New(cfunc);
48dc4a59a50e Inline create_specialmethod() -- since METH_CLASS is done differently
Guido van Rossum <guido@python.org>
parents: 27811
diff changeset
3550 Py_DECREF(cfunc);
22613
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3551 }
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3552 else {
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3553 descr = PyDescr_NewMethod(type, meth);
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3554 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3555 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3556 return -1;
22613
ddc9543c76a0 Introduce two new flag bits that can be set in a PyMethodDef method
Fred Drake <fdrake@acm.org>
parents: 22348
diff changeset
3557 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3558 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3559 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3560 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3561 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3562 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3563
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3564 static int
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
3565 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
3566 {
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
3567 PyObject *dict = type->tp_dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3568
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3569 for (; memb->name != NULL; memb++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3570 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3571 if (PyDict_GetItemString(dict, memb->name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3572 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3573 descr = PyDescr_NewMember(type, memb);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3574 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3575 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3576 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3577 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3578 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3579 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3580 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3581 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3582
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3583 static int
20015
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
3584 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
3585 {
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
3586 PyObject *dict = type->tp_dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3587
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3588 for (; gsp->name != NULL; gsp++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3589 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3590 if (PyDict_GetItemString(dict, gsp->name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3591 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3592 descr = PyDescr_NewGetSet(type, gsp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3593
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3594 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3595 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3596 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3597 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3598 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3599 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3600 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3601 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3602
52749
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52176
diff changeset
3603 #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
3604
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3605 static void
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3606 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
3607 {
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
3608 Py_ssize_t oldsize, newsize;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3609
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3610 /* Special flag magic */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3611 if (!type->tp_as_buffer && base->tp_as_buffer) {
52749
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52176
diff changeset
3612 type->tp_flags &= ~BUFFER_FLAGS;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3613 type->tp_flags |=
52749
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 52176
diff changeset
3614 base->tp_flags & BUFFER_FLAGS;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3615 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3616 if (!type->tp_as_sequence && base->tp_as_sequence) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3617 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3618 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3619 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3620 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3621 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3622 if ((!type->tp_as_number && base->tp_as_number) ||
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3623 (!type->tp_as_sequence && base->tp_as_sequence)) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3624 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3625 if (!type->tp_as_number && !type->tp_as_sequence) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3626 type->tp_flags |= base->tp_flags &
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3627 Py_TPFLAGS_HAVE_INPLACEOPS;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3628 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3629 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3630 /* Wow */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3631 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3632 if (!type->tp_as_number && base->tp_as_number) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3633 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3634 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3635 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3636
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3637 /* Copying basicsize is connected to the GC flags */
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
3638 oldsize = base->tp_basicsize;
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
3639 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
3640 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
3641 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3642 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3643 (!type->tp_traverse && !type->tp_clear)) {
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
3644 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3645 if (type->tp_traverse == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3646 type->tp_traverse = base->tp_traverse;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3647 if (type->tp_clear == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3648 type->tp_clear = base->tp_clear;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3649 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3650 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
21451
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3651 /* The condition below could use some explanation.
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3652 It appears that tp_new is not inherited for static types
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3653 whose base class is 'object'; this seems to be a precaution
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3654 so that old extension types don't suddenly become
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3655 callable (object.__new__ wouldn't insure the invariants
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3656 that the extension type's own factory function ensures).
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3657 Heap types, of course, are under our control, so they do
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3658 inherit tp_new; static extension types that specify some
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3659 other built-in type as the default are considered
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3660 new-style-aware so they also inherit object.__new__. */
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3661 if (base != &PyBaseObject_Type ||
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3662 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3663 if (type->tp_new == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3664 type->tp_new = base->tp_new;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3665 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3666 }
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
3667 type->tp_basicsize = newsize;
19253
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3668
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3669 /* Copy other non-function slots */
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3670
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3671 #undef COPYVAL
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3672 #define COPYVAL(SLOT) \
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3673 if (type->SLOT == 0) type->SLOT = base->SLOT
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3674
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3675 COPYVAL(tp_itemsize);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3676 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3677 COPYVAL(tp_weaklistoffset);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3678 }
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3679 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3680 COPYVAL(tp_dictoffset);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
3681 }
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3682
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3683 /* Setup fast subclass flags */
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3684 if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3685 type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3686 else if (PyType_IsSubtype(base, &PyType_Type))
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3687 type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3688 else if (PyType_IsSubtype(base, &PyInt_Type))
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3689 type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS;
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3690 else if (PyType_IsSubtype(base, &PyLong_Type))
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3691 type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
3692 else if (PyType_IsSubtype(base, &PyString_Type))
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3693 type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS;
44572
db5e01c33eb7 Make Python compile with --disable-unicode.
Georg Brandl <georg@python.org>
parents: 44365
diff changeset
3694 #ifdef Py_USING_UNICODE
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3695 else if (PyType_IsSubtype(base, &PyUnicode_Type))
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3696 type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
44572
db5e01c33eb7 Make Python compile with --disable-unicode.
Georg Brandl <georg@python.org>
parents: 44365
diff changeset
3697 #endif
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3698 else if (PyType_IsSubtype(base, &PyTuple_Type))
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3699 type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3700 else if (PyType_IsSubtype(base, &PyList_Type))
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3701 type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3702 else if (PyType_IsSubtype(base, &PyDict_Type))
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40777
diff changeset
3703 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
3704 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3705
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
3706 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
3707 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
3708 {
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
3709 PyObject *dict = type->tp_dict;
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
3710
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
3711 assert(dict != NULL);
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
3712 if (PyDict_GetItemString(dict, name) != NULL) {
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
3713 return 1;
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
3714 }
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
3715 return 0;
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 }
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
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 #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
3719 #define OVERRIDES_CMP(x) overrides_name(x, "__cmp__")
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
3720 #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
3721
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3722 static void
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3723 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
3724 {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3725 PyTypeObject *basebase;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3726
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3727 #undef SLOTDEFINED
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3728 #undef COPYSLOT
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3729 #undef COPYNUM
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3730 #undef COPYSEQ
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3731 #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
3732 #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
3733
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3734 #define SLOTDEFINED(SLOT) \
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3735 (base->SLOT != 0 && \
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3736 (basebase == NULL || base->SLOT != basebase->SLOT))
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3737
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3738 #define COPYSLOT(SLOT) \
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3739 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
3740
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3741 #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
3742 #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
3743 #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
3744 #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
3745
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3746 /* This won't inherit indirect slots (from tp_as_number etc.)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3747 if type doesn't provide the space. */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3748
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3749 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3750 basebase = base->tp_base;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3751 if (basebase->tp_as_number == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3752 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3753 COPYNUM(nb_add);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3754 COPYNUM(nb_subtract);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3755 COPYNUM(nb_multiply);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3756 COPYNUM(nb_divide);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3757 COPYNUM(nb_remainder);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3758 COPYNUM(nb_divmod);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3759 COPYNUM(nb_power);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3760 COPYNUM(nb_negative);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3761 COPYNUM(nb_positive);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3762 COPYNUM(nb_absolute);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3763 COPYNUM(nb_nonzero);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3764 COPYNUM(nb_invert);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3765 COPYNUM(nb_lshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3766 COPYNUM(nb_rshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3767 COPYNUM(nb_and);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3768 COPYNUM(nb_xor);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3769 COPYNUM(nb_or);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3770 COPYNUM(nb_coerce);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3771 COPYNUM(nb_int);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3772 COPYNUM(nb_long);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3773 COPYNUM(nb_float);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3774 COPYNUM(nb_oct);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3775 COPYNUM(nb_hex);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3776 COPYNUM(nb_inplace_add);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3777 COPYNUM(nb_inplace_subtract);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3778 COPYNUM(nb_inplace_multiply);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3779 COPYNUM(nb_inplace_divide);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3780 COPYNUM(nb_inplace_remainder);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3781 COPYNUM(nb_inplace_power);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3782 COPYNUM(nb_inplace_lshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3783 COPYNUM(nb_inplace_rshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3784 COPYNUM(nb_inplace_and);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3785 COPYNUM(nb_inplace_xor);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3786 COPYNUM(nb_inplace_or);
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3787 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3788 COPYNUM(nb_true_divide);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3789 COPYNUM(nb_floor_divide);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3790 COPYNUM(nb_inplace_true_divide);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3791 COPYNUM(nb_inplace_floor_divide);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3792 }
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
3793 if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
3794 COPYNUM(nb_index);
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
3795 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3796 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3797
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3798 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3799 basebase = base->tp_base;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3800 if (basebase->tp_as_sequence == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3801 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3802 COPYSEQ(sq_length);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3803 COPYSEQ(sq_concat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3804 COPYSEQ(sq_repeat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3805 COPYSEQ(sq_item);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3806 COPYSEQ(sq_slice);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3807 COPYSEQ(sq_ass_item);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3808 COPYSEQ(sq_ass_slice);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3809 COPYSEQ(sq_contains);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3810 COPYSEQ(sq_inplace_concat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3811 COPYSEQ(sq_inplace_repeat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3812 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3813
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3814 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3815 basebase = base->tp_base;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3816 if (basebase->tp_as_mapping == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3817 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3818 COPYMAP(mp_length);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3819 COPYMAP(mp_subscript);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3820 COPYMAP(mp_ass_subscript);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3821 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3822
20384
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3823 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3824 basebase = base->tp_base;
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3825 if (basebase->tp_as_buffer == NULL)
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3826 basebase = NULL;
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3827 COPYBUF(bf_getreadbuffer);
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3828 COPYBUF(bf_getwritebuffer);
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3829 COPYBUF(bf_getsegcount);
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3830 COPYBUF(bf_getcharbuffer);
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46075
diff changeset
3831 COPYBUF(bf_getbuffer);
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46075
diff changeset
3832 COPYBUF(bf_releasebuffer);
20384
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3833 }
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
3834
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3835 basebase = base->tp_base;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3836
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3837 COPYSLOT(tp_dealloc);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3838 COPYSLOT(tp_print);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3839 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3840 type->tp_getattr = base->tp_getattr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3841 type->tp_getattro = base->tp_getattro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3842 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3843 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3844 type->tp_setattr = base->tp_setattr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3845 type->tp_setattro = base->tp_setattro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3846 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3847 /* tp_compare see tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3848 COPYSLOT(tp_repr);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3849 /* tp_hash see tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3850 COPYSLOT(tp_call);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3851 COPYSLOT(tp_str);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3852 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3853 if (type->tp_compare == NULL &&
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3854 type->tp_richcompare == NULL &&
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
3855 type->tp_hash == NULL)
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3856 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3857 type->tp_compare = base->tp_compare;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3858 type->tp_richcompare = base->tp_richcompare;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3859 type->tp_hash = base->tp_hash;
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
3860 /* Check for changes to inherited methods in Py3k*/
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
3861 if (Py_Py3kWarningFlag) {
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
3862 if (base->tp_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
3863 (base->tp_hash != PyObject_HashNotImplemented) &&
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
3864 !OVERRIDES_HASH(type)) {
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
3865 if (OVERRIDES_CMP(type)) {
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
3866 PyErr_WarnPy3k("Overriding "
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
3867 "__cmp__ blocks inheritance "
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
3868 "of __hash__ in 3.x",
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
3869 1);
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
3870 }
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
3871 if (OVERRIDES_EQ(type)) {
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
3872 PyErr_WarnPy3k("Overriding "
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
3873 "__eq__ blocks inheritance "
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
3874 "of __hash__ in 3.x",
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
3875 1);
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
3876 }
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
3877 }
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
3878 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3879 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3880 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3881 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3882 COPYSLOT(tp_compare);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3883 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3884 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3885 COPYSLOT(tp_iter);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3886 COPYSLOT(tp_iternext);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3887 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3888 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3889 COPYSLOT(tp_descr_get);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3890 COPYSLOT(tp_descr_set);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3891 COPYSLOT(tp_dictoffset);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3892 COPYSLOT(tp_init);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3893 COPYSLOT(tp_alloc);
22848
e589e473f232 Inherit tp_new and tp_is_gc.
Guido van Rossum <guido@python.org>
parents: 22834
diff changeset
3894 COPYSLOT(tp_is_gc);
29201
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3895 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3896 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3897 /* They agree about gc. */
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3898 COPYSLOT(tp_free);
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3899 }
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3900 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3901 type->tp_free == NULL &&
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3902 base->tp_free == _PyObject_Del) {
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3903 /* A bit of magic to plug in the correct default
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3904 * tp_free function when a derived class adds gc,
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3905 * didn't define tp_free, and the base uses the
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3906 * default non-gc tp_free.
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3907 */
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3908 type->tp_free = PyObject_GC_Del;
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3909 }
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3910 /* else they didn't agree about gc, and there isn't something
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3911 * obvious to be done -- the type is on its own.
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
3912 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3913 }
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
3914 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3915
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
3916 static int add_operators(PyTypeObject *);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3917
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3918 int
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
3919 PyType_Ready(PyTypeObject *type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3920 {
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
3921 PyObject *dict, *bases;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3922 PyTypeObject *base;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
3923 Py_ssize_t i, n;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3924
23756
8e5f26796620 Undo the last chunk of the previous patch, putting back a useful
Guido van Rossum <guido@python.org>
parents: 23755
diff changeset
3925 if (type->tp_flags & Py_TPFLAGS_READY) {
8e5f26796620 Undo the last chunk of the previous patch, putting back a useful
Guido van Rossum <guido@python.org>
parents: 23755
diff changeset
3926 assert(type->tp_dict != NULL);
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
3927 return 0;
23756
8e5f26796620 Undo the last chunk of the previous patch, putting back a useful
Guido van Rossum <guido@python.org>
parents: 23755
diff changeset
3928 }
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
3929 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
3930
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
3931 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
3932
28389
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3933 #ifdef Py_TRACE_REFS
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3934 /* PyType_Ready is the closest thing we have to a choke point
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3935 * for type objects, so is the best place I can think of to try
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3936 * to get type objects into the doubly-linked list of all objects.
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3937 * Still, not all type objects go thru PyType_Ready.
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3938 */
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28390
diff changeset
3939 _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
3940 #endif
4ede57a42c62 Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents: 28288
diff changeset
3941
22871
252438bb05d9 - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22859
diff changeset
3942 /* Initialize tp_base (defaults to BaseObject unless that's us) */
252438bb05d9 - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22859
diff changeset
3943 base = type->tp_base;
33174
f6fa649fc4af Patch #980082: Missing INCREF in PyType_Ready.
Martin v. Löwis <martin@v.loewis.de>
parents: 32916
diff changeset
3944 if (base == NULL && type != &PyBaseObject_Type) {
22871
252438bb05d9 - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22859
diff changeset
3945 base = type->tp_base = &PyBaseObject_Type;
33174
f6fa649fc4af Patch #980082: Missing INCREF in PyType_Ready.
Martin v. Löwis <martin@v.loewis.de>
parents: 32916
diff changeset
3946 Py_INCREF(base);
f6fa649fc4af Patch #980082: Missing INCREF in PyType_Ready.
Martin v. Löwis <martin@v.loewis.de>
parents: 32916
diff changeset
3947 }
22871
252438bb05d9 - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22859
diff changeset
3948
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3949 /* Now the only way base can still be NULL is if type is
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3950 * &PyBaseObject_Type.
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3951 */
36925
7c17e9221f94 Fix three nits found by Coverity, adding null checks and comments.
Guido van Rossum <guido@python.org>
parents: 36882
diff changeset
3952
24825
c001e5a15369 PyType_Ready(): initialize the base class a bit earlier, so that if we
Guido van Rossum <guido@python.org>
parents: 24795
diff changeset
3953 /* Initialize the base class */
c001e5a15369 PyType_Ready(): initialize the base class a bit earlier, so that if we
Guido van Rossum <guido@python.org>
parents: 24795
diff changeset
3954 if (base && base->tp_dict == NULL) {
c001e5a15369 PyType_Ready(): initialize the base class a bit earlier, so that if we
Guido van Rossum <guido@python.org>
parents: 24795
diff changeset
3955 if (PyType_Ready(base) < 0)
c001e5a15369 PyType_Ready(): initialize the base class a bit earlier, so that if we
Guido van Rossum <guido@python.org>
parents: 24795
diff changeset
3956 goto error;
c001e5a15369 PyType_Ready(): initialize the base class a bit earlier, so that if we
Guido van Rossum <guido@python.org>
parents: 24795
diff changeset
3957 }
c001e5a15369 PyType_Ready(): initialize the base class a bit earlier, so that if we
Guido van Rossum <guido@python.org>
parents: 24795
diff changeset
3958
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3959 /* Initialize ob_type if NULL. This means extensions that want to be
21451
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3960 compilable separately on Windows can call PyType_Ready() instead of
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
3961 initializing the ob_type field of their type objects. */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3962 /* The test for base != NULL is really unnecessary, since base is only
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3963 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3964 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
3965 know that. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
3966 if (Py_TYPE(type) == NULL && base != NULL)
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
3967 Py_TYPE(type) = Py_TYPE(base);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3968
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3969 /* Initialize tp_bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3970 bases = type->tp_bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3971 if (bases == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3972 if (base == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3973 bases = PyTuple_New(0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3974 else
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
3975 bases = PyTuple_Pack(1, base);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3976 if (bases == NULL)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
3977 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3978 type->tp_bases = bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3979 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3980
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
3981 /* Initialize tp_dict */
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
3982 dict = type->tp_dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3983 if (dict == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3984 dict = PyDict_New();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3985 if (dict == NULL)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
3986 goto error;
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
3987 type->tp_dict = dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3988 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3989
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
3990 /* Add type-specific descriptors to tp_dict */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3991 if (add_operators(type) < 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
3992 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3993 if (type->tp_methods != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3994 if (add_methods(type, type->tp_methods) < 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
3995 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3996 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3997 if (type->tp_members != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3998 if (add_members(type, type->tp_members) < 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
3999 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4000 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4001 if (type->tp_getset != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4002 if (add_getset(type, type->tp_getset) < 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
4003 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4004 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4005
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4006 /* Calculate method resolution order */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4007 if (mro_internal(type) < 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
4008 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4009 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4010
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4011 /* Inherit special flags from dominant base */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4012 if (type->tp_base != NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4013 inherit_special(type, type->tp_base);
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4014
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4015 /* Initialize tp_dict properly */
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
4016 bases = type->tp_mro;
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
4017 assert(bases != NULL);
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
4018 assert(PyTuple_Check(bases));
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
4019 n = PyTuple_GET_SIZE(bases);
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
4020 for (i = 1; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4021 PyObject *b = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4022 if (PyType_Check(b))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4023 inherit_slots(type, (PyTypeObject *)b);
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4024 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4025
29201
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4026 /* Sanity check for tp_free. */
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4027 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4028 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4029 /* This base class needs to call tp_free, but doesn't have
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4030 * one, or its tp_free is for non-gc'ed objects.
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4031 */
29201
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4032 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4033 "gc and is a base type but has inappropriate "
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4034 "tp_free slot",
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4035 type->tp_name);
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4036 goto error;
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4037 }
754fd34ad978 PyType_Ready(): Complain if the type is a base type, and gc'able, and
Tim Peters <tim.peters@gmail.com>
parents: 29173
diff changeset
4038
21997
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4039 /* if the type dictionary doesn't contain a __doc__, set it from
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4040 the tp_doc slot.
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4041 */
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4042 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4043 if (type->tp_doc != NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4044 PyObject *doc = PyString_FromString(type->tp_doc);
39129
f2031f15d99d Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38919
diff changeset
4045 if (doc == NULL)
f2031f15d99d Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38919
diff changeset
4046 goto error;
21997
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4047 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4048 Py_DECREF(doc);
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4049 } else {
22774
02fddf7c25c2 Fold some long lines. Delete blank initial line.
Guido van Rossum <guido@python.org>
parents: 22770
diff changeset
4050 PyDict_SetItemString(type->tp_dict,
02fddf7c25c2 Fold some long lines. Delete blank initial line.
Guido van Rossum <guido@python.org>
parents: 22770
diff changeset
4051 "__doc__", Py_None);
21997
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4052 }
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4053 }
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
4054
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4055 /* Some more special stuff */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4056 base = type->tp_base;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4057 if (base != NULL) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4058 if (type->tp_as_number == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4059 type->tp_as_number = base->tp_as_number;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4060 if (type->tp_as_sequence == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4061 type->tp_as_sequence = base->tp_as_sequence;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4062 if (type->tp_as_mapping == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4063 type->tp_as_mapping = base->tp_as_mapping;
27821
b804ee5beb49 Fix from SF #681367: inherit tp_as_buffer. This only applies to C
Guido van Rossum <guido@python.org>
parents: 27814
diff changeset
4064 if (type->tp_as_buffer == NULL)
b804ee5beb49 Fix from SF #681367: inherit tp_as_buffer. This only applies to C
Guido van Rossum <guido@python.org>
parents: 27814
diff changeset
4065 type->tp_as_buffer = base->tp_as_buffer;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4066 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4067
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4068 /* Link into each base class's list of subclasses */
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4069 bases = type->tp_bases;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4070 n = PyTuple_GET_SIZE(bases);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4071 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4072 PyObject *b = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4073 if (PyType_Check(b) &&
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4074 add_subclass((PyTypeObject *)b, type) < 0)
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4075 goto error;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4076 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4077
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
4078 /* All done -- set the ready flag */
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
4079 assert(type->tp_dict != NULL);
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
4080 type->tp_flags =
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
4081 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4082 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
4083
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
4084 error:
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
4085 type->tp_flags &= ~Py_TPFLAGS_READYING;
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
4086 return -1;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4087 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4088
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4089 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
4090 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
4091 {
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
4092 Py_ssize_t i;
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
4093 int result;
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
4094 PyObject *list, *ref, *newobj;
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4095
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4096 list = base->tp_subclasses;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4097 if (list == NULL) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4098 base->tp_subclasses = list = PyList_New(0);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4099 if (list == NULL)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4100 return -1;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4101 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4102 assert(PyList_Check(list));
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
4103 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4104 i = PyList_GET_SIZE(list);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4105 while (--i >= 0) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4106 ref = PyList_GET_ITEM(list, i);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4107 assert(PyWeakref_CheckRef(ref));
25839
243503926c81 Sigh. That wasn't a memory leak, that was Guido committing before
Guido van Rossum <guido@python.org>
parents: 25837
diff changeset
4108 if (PyWeakref_GET_OBJECT(ref) == Py_None)
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
4109 return PyList_SetItem(list, i, newobj);
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
4110 }
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
4111 result = PyList_Append(list, newobj);
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
4112 Py_DECREF(newobj);
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
4113 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
4114 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
4115
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4116 static void
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4117 remove_subclass(PyTypeObject *base, PyTypeObject *type)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4118 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4119 Py_ssize_t i;
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4120 PyObject *list, *ref;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4121
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4122 list = base->tp_subclasses;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4123 if (list == NULL) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4124 return;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4125 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4126 assert(PyList_Check(list));
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4127 i = PyList_GET_SIZE(list);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4128 while (--i >= 0) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4129 ref = PyList_GET_ITEM(list, i);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4130 assert(PyWeakref_CheckRef(ref));
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4131 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4132 /* this can't fail, right? */
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4133 PySequence_DelItem(list, i);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4134 return;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4135 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4136 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
4137 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4138
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4139 static int
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4140 check_num_args(PyObject *ob, int n)
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4141 {
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4142 if (!PyTuple_CheckExact(ob)) {
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4143 PyErr_SetString(PyExc_SystemError,
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4144 "PyArg_UnpackTuple() argument list is not a tuple");
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4145 return 0;
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4146 }
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4147 if (n == PyTuple_GET_SIZE(ob))
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4148 return 1;
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4149 PyErr_Format(
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4150 PyExc_TypeError,
36510
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
4151 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4152 return 0;
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4153 }
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4154
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4155 /* 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
4156
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4157 /* There's a wrapper *function* for each distinct function typedef used
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4158 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
4159 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
4160 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
4161 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
4162
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4163 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4164 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
4165 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4166 lenfunc func = (lenfunc)wrapped;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4167 Py_ssize_t res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4168
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4169 if (!check_num_args(args, 0))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4170 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4171 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4172 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4173 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4174 return PyInt_FromLong((long)res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4175 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4176
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4177 static PyObject *
30670
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4178 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
4179 {
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4180 inquiry func = (inquiry)wrapped;
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4181 int res;
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4182
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4183 if (!check_num_args(args, 0))
30670
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4184 return NULL;
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4185 res = (*func)(self);
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4186 if (res == -1 && PyErr_Occurred())
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4187 return NULL;
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4188 return PyBool_FromLong((long)res);
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4189 }
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4190
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
4191 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4192 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
4193 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4194 binaryfunc func = (binaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4195 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4196
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4197 if (!check_num_args(args, 1))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4198 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4199 other = PyTuple_GET_ITEM(args, 0);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4200 return (*func)(self, other);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4201 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4202
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4203 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
4204 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
4205 {
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4206 binaryfunc func = (binaryfunc)wrapped;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4207 PyObject *other;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4208
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4209 if (!check_num_args(args, 1))
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4210 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4211 other = PyTuple_GET_ITEM(args, 0);
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4212 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4213 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4214 Py_INCREF(Py_NotImplemented);
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4215 return Py_NotImplemented;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4216 }
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4217 return (*func)(self, other);
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4218 }
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4219
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4220 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4221 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
4222 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4223 binaryfunc func = (binaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4224 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4225
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4226 if (!check_num_args(args, 1))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4227 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4228 other = PyTuple_GET_ITEM(args, 0);
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4229 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4230 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4231 Py_INCREF(Py_NotImplemented);
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4232 return Py_NotImplemented;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
4233 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4234 return (*func)(other, self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4235 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4236
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4237 static PyObject *
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4238 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
4239 {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4240 coercion func = (coercion)wrapped;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4241 PyObject *other, *res;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4242 int ok;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4243
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4244 if (!check_num_args(args, 1))
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4245 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4246 other = PyTuple_GET_ITEM(args, 0);
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4247 ok = func(&self, &other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4248 if (ok < 0)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4249 return NULL;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4250 if (ok > 0) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4251 Py_INCREF(Py_NotImplemented);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4252 return Py_NotImplemented;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4253 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4254 res = PyTuple_New(2);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4255 if (res == NULL) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4256 Py_DECREF(self);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4257 Py_DECREF(other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4258 return NULL;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4259 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4260 PyTuple_SET_ITEM(res, 0, self);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4261 PyTuple_SET_ITEM(res, 1, other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4262 return res;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4263 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
4264
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4265 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4266 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
4267 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4268 ternaryfunc func = (ternaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4269 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4270 PyObject *third = Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4271
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4272 /* Note: This wrapper only works for __pow__() */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4273
30671
adc60015670a Use the simpler and faster PyArg_UnpackTuple() instead of
Raymond Hettinger <python@rcn.com>
parents: 30670
diff changeset
4274 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4275 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4276 return (*func)(self, other, third);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4277 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4278
20168
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4279 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
4280 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
4281 {
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4282 ternaryfunc func = (ternaryfunc)wrapped;
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4283 PyObject *other;
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4284 PyObject *third = Py_None;
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4285
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4286 /* Note: This wrapper only works for __pow__() */
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4287
30671
adc60015670a Use the simpler and faster PyArg_UnpackTuple() instead of
Raymond Hettinger <python@rcn.com>
parents: 30670
diff changeset
4288 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &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
4289 return NULL;
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4290 return (*func)(other, self, third);
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4291 }
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
4292
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4293 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4294 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
4295 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4296 unaryfunc func = (unaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4297
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4298 if (!check_num_args(args, 0))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4299 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4300 return (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4301 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4302
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4303 static PyObject *
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
4304 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
4305 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4306 ssizeargfunc func = (ssizeargfunc)wrapped;
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
4307 PyObject* o;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4308 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4309
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
4310 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
4311 return NULL;
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39415
diff changeset
4312 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
4313 if (i == -1 && PyErr_Occurred())
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4314 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4315 return (*func)(self, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4316 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4317
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4318 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
4319 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
4320 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4321 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4322
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39415
diff changeset
4323 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4324 if (i == -1 && PyErr_Occurred())
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4325 return -1;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4326 if (i < 0) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4327 PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4328 if (sq && sq->sq_length) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4329 Py_ssize_t n = (*sq->sq_length)(self);
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4330 if (n < 0)
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4331 return -1;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4332 i += n;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4333 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4334 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4335 return i;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4336 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4337
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4338 static PyObject *
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4339 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
4340 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4341 ssizeargfunc func = (ssizeargfunc)wrapped;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4342 PyObject *arg;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4343 Py_ssize_t i;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4344
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4345 if (PyTuple_GET_SIZE(args) == 1) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4346 arg = PyTuple_GET_ITEM(args, 0);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4347 i = getindex(self, arg);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4348 if (i == -1 && PyErr_Occurred())
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4349 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4350 return (*func)(self, i);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4351 }
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4352 check_num_args(args, 1);
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4353 assert(PyErr_Occurred());
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4354 return NULL;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4355 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4356
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4357 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4358 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
4359 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4360 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4361 Py_ssize_t i, j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4362
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4363 if (!PyArg_ParseTuple(args, "nn", &i, &j))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4364 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4365 return (*func)(self, i, j);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4366 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4367
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4368 static PyObject *
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4369 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
4370 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4371 ssizeobjargproc func = (ssizeobjargproc)wrapped;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4372 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4373 int res;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4374 PyObject *arg, *value;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4375
30671
adc60015670a Use the simpler and faster PyArg_UnpackTuple() instead of
Raymond Hettinger <python@rcn.com>
parents: 30670
diff changeset
4376 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4377 return NULL;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4378 i = getindex(self, arg);
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4379 if (i == -1 && PyErr_Occurred())
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4380 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4381 res = (*func)(self, i, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4382 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4383 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4384 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4385 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4386 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4387
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4388 static PyObject *
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4389 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
4390 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4391 ssizeobjargproc func = (ssizeobjargproc)wrapped;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4392 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4393 int res;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4394 PyObject *arg;
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4395
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4396 if (!check_num_args(args, 1))
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4397 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4398 arg = PyTuple_GET_ITEM(args, 0);
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4399 i = getindex(self, arg);
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
4400 if (i == -1 && PyErr_Occurred())
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4401 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4402 res = (*func)(self, i, NULL);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4403 if (res == -1 && PyErr_Occurred())
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4404 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4405 Py_INCREF(Py_None);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4406 return Py_None;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4407 }
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4408
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4409 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4410 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
4411 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4412 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4413 Py_ssize_t i, j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4414 int res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4415 PyObject *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4416
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4417 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4418 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4419 res = (*func)(self, i, j, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4420 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4421 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4422 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4423 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4424 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4425
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4426 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
4427 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
4428 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4429 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4430 Py_ssize_t i, j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4431 int res;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4432
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4433 if (!PyArg_ParseTuple(args, "nn", &i, &j))
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4434 return NULL;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4435 res = (*func)(self, i, j, NULL);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4436 if (res == -1 && PyErr_Occurred())
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4437 return NULL;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4438 Py_INCREF(Py_None);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4439 return Py_None;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4440 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4441
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4442 /* 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
4443 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4444 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
4445 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4446 objobjproc func = (objobjproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4447 int res;
30660
36992ca918a3 Fix leak introduced by previous typeobject.c checkin.
Guido van Rossum <guido@python.org>
parents: 30659
diff changeset
4448 PyObject *value;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4449
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4450 if (!check_num_args(args, 1))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4451 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4452 value = PyTuple_GET_ITEM(args, 0);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4453 res = (*func)(self, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4454 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4455 return NULL;
30660
36992ca918a3 Fix leak introduced by previous typeobject.c checkin.
Guido van Rossum <guido@python.org>
parents: 30659
diff changeset
4456 else
36992ca918a3 Fix leak introduced by previous typeobject.c checkin.
Guido van Rossum <guido@python.org>
parents: 30659
diff changeset
4457 return PyBool_FromLong(res);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4458 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4459
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4460 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4461 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
4462 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4463 objobjargproc func = (objobjargproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4464 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4465 PyObject *key, *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4466
30671
adc60015670a Use the simpler and faster PyArg_UnpackTuple() instead of
Raymond Hettinger <python@rcn.com>
parents: 30670
diff changeset
4467 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4468 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4469 res = (*func)(self, key, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4470 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4471 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4472 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4473 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4474 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4475
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4476 static PyObject *
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4477 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
4478 {
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4479 objobjargproc func = (objobjargproc)wrapped;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4480 int res;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4481 PyObject *key;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4482
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4483 if (!check_num_args(args, 1))
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4484 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4485 key = PyTuple_GET_ITEM(args, 0);
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4486 res = (*func)(self, key, NULL);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4487 if (res == -1 && PyErr_Occurred())
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4488 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4489 Py_INCREF(Py_None);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4490 return Py_None;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4491 }
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
4492
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4493 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4494 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
4495 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4496 cmpfunc func = (cmpfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4497 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4498 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4499
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4500 if (!check_num_args(args, 1))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4501 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4502 other = PyTuple_GET_ITEM(args, 0);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4503 if (Py_TYPE(other)->tp_compare != func &&
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4504 !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
19964
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
4505 PyErr_Format(
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
4506 PyExc_TypeError,
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
4507 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4508 Py_TYPE(self)->tp_name,
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4509 Py_TYPE(self)->tp_name,
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4510 Py_TYPE(other)->tp_name);
19964
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
4511 return NULL;
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
4512 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4513 res = (*func)(self, other);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4514 if (PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4515 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4516 return PyInt_FromLong((long)res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4517 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4518
28608
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4519 /* 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
4520 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
4521 static int
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4522 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
4523 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4524 PyTypeObject *type = Py_TYPE(self);
28608
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4525 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4526 type = type->tp_base;
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4527 /* If type is NULL now, this is a really weird type.
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4528 In the spirit of backwards compatibility (?), just shut up. */
36925
7c17e9221f94 Fix three nits found by Coverity, adding null checks and comments.
Guido van Rossum <guido@python.org>
parents: 36882
diff changeset
4529 if (type && type->tp_setattro != func) {
28608
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4530 PyErr_Format(PyExc_TypeError,
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4531 "can't apply this %s to %s object",
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4532 what,
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4533 type->tp_name);
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4534 return 0;
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4535 }
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4536 return 1;
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4537 }
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4538
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4539 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4540 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
4541 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4542 setattrofunc func = (setattrofunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4543 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4544 PyObject *name, *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4545
30671
adc60015670a Use the simpler and faster PyArg_UnpackTuple() instead of
Raymond Hettinger <python@rcn.com>
parents: 30670
diff changeset
4546 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4547 return NULL;
28608
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4548 if (!hackcheck(self, func, "__setattr__"))
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4549 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4550 res = (*func)(self, name, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4551 if (res < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4552 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4553 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4554 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4555 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4556
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4557 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4558 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
4559 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4560 setattrofunc func = (setattrofunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4561 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4562 PyObject *name;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4563
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4564 if (!check_num_args(args, 1))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4565 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4566 name = PyTuple_GET_ITEM(args, 0);
28608
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4567 if (!hackcheck(self, func, "__delattr__"))
8322077d234c Close off the "Verre Carlo hack" as discussed on python-dev.
Guido van Rossum <guido@python.org>
parents: 28607
diff changeset
4568 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4569 res = (*func)(self, name, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4570 if (res < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4571 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4572 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4573 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4574 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4575
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4576 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4577 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
4578 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4579 hashfunc func = (hashfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4580 long res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4581
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4582 if (!check_num_args(args, 0))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4583 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4584 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4585 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4586 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4587 return PyInt_FromLong(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4588 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4589
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4590 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
4591 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
4592 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4593 ternaryfunc func = (ternaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4594
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
4595 return (*func)(self, args, kwds);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4596 }
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 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4599 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
4600 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4601 richcmpfunc func = (richcmpfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4602 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4603
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4604 if (!check_num_args(args, 1))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4605 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4606 other = PyTuple_GET_ITEM(args, 0);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4607 return (*func)(self, other, op);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4608 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4609
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4610 #undef RICHCMP_WRAPPER
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4611 #define RICHCMP_WRAPPER(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4612 static PyObject * \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4613 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
4614 { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4615 return wrap_richcmpfunc(self, args, wrapped, OP); \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4616 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4617
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
4618 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
4619 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
4620 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
4621 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
4622 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
4623 RICHCMP_WRAPPER(ge, Py_GE)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4624
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4625 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4626 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
4627 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4628 unaryfunc func = (unaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4629 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4630
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4631 if (!check_num_args(args, 0))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4632 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4633 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4634 if (res == NULL && !PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4635 PyErr_SetNone(PyExc_StopIteration);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4636 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4637 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4638
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4639 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4640 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
4641 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4642 descrgetfunc func = (descrgetfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4643 PyObject *obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4644 PyObject *type = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4645
30671
adc60015670a Use the simpler and faster PyArg_UnpackTuple() instead of
Raymond Hettinger <python@rcn.com>
parents: 30670
diff changeset
4646 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4647 return NULL;
27811
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4648 if (obj == Py_None)
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4649 obj = NULL;
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4650 if (type == Py_None)
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4651 type = NULL;
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4652 if (type == NULL &&obj == NULL) {
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4653 PyErr_SetString(PyExc_TypeError,
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4654 "__get__(None, None) is invalid");
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4655 return NULL;
42396a786f59 Add basic arg sanity checking to wrap_descr_get(). This is called
Guido van Rossum <guido@python.org>
parents: 27807
diff changeset
4656 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4657 return (*func)(self, obj, type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4658 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4659
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4660 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
4661 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
4662 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4663 descrsetfunc func = (descrsetfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4664 PyObject *obj, *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4665 int ret;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4666
30671
adc60015670a Use the simpler and faster PyArg_UnpackTuple() instead of
Raymond Hettinger <python@rcn.com>
parents: 30670
diff changeset
4667 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4668 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4669 ret = (*func)(self, obj, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4670 if (ret < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4671 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4672 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4673 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4674 }
24614
40eb80dc4250 Fix SF bug 574207 (chained __slots__ dealloc segfault).
Guido van Rossum <guido@python.org>
parents: 24460
diff changeset
4675
24460
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4676 static PyObject *
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4677 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
4678 {
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4679 descrsetfunc func = (descrsetfunc)wrapped;
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4680 PyObject *obj;
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4681 int ret;
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4682
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4683 if (!check_num_args(args, 1))
24460
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4684 return NULL;
31176
381140e7e9d7 Improve argument checking speed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
4685 obj = PyTuple_GET_ITEM(args, 0);
24460
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4686 ret = (*func)(self, obj, NULL);
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4687 if (ret < 0)
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4688 return NULL;
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4689 Py_INCREF(Py_None);
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4690 return Py_None;
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
4691 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4692
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4693 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
4694 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
4695 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4696 initproc func = (initproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4697
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
4698 if (func(self, args, kwds) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4699 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4700 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4701 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4702 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4703
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4704 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
4705 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
4706 {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4707 PyTypeObject *type, *subtype, *staticbase;
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
4708 PyObject *arg0, *res;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4709
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4710 if (self == NULL || !PyType_Check(self))
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4711 Py_FatalError("__new__() called with non-type 'self'");
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4712 type = (PyTypeObject *)self;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4713 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4714 PyErr_Format(PyExc_TypeError,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4715 "%s.__new__(): not enough arguments",
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4716 type->tp_name);
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
4717 return NULL;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4718 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4719 arg0 = PyTuple_GET_ITEM(args, 0);
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4720 if (!PyType_Check(arg0)) {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4721 PyErr_Format(PyExc_TypeError,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4722 "%s.__new__(X): X is not a type object (%s)",
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4723 type->tp_name,
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4724 Py_TYPE(arg0)->tp_name);
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
4725 return NULL;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4726 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4727 subtype = (PyTypeObject *)arg0;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4728 if (!PyType_IsSubtype(subtype, type)) {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4729 PyErr_Format(PyExc_TypeError,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4730 "%s.__new__(%s): %s is not a subtype of %s",
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4731 type->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4732 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4733 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4734 type->tp_name);
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
4735 return NULL;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4736 }
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4737
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4738 /* Check that the use doesn't do something silly and unsafe like
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20753
diff changeset
4739 object.__new__(dict). To do this, we check that the
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4740 most derived base that's not a heap type is this type. */
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4741 staticbase = subtype;
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4742 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4743 staticbase = staticbase->tp_base;
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4744 /* If staticbase is NULL now, it is a really weird type.
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4745 In the spirit of backwards compatibility (?), just shut up. */
36925
7c17e9221f94 Fix three nits found by Coverity, adding null checks and comments.
Guido van Rossum <guido@python.org>
parents: 36882
diff changeset
4746 if (staticbase && staticbase->tp_new != type->tp_new) {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4747 PyErr_Format(PyExc_TypeError,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4748 "%s.__new__(%s) is not safe, use %s.__new__()",
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4749 type->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4750 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4751 staticbase == NULL ? "?" : staticbase->tp_name);
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4752 return NULL;
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4753 }
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
4754
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
4755 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4756 if (args == NULL)
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4757 return NULL;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4758 res = type->tp_new(subtype, args, kwds);
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4759 Py_DECREF(args);
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4760 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4761 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4762
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
4763 static struct PyMethodDef tp_new_methoddef[] = {
41816
ec83d21065aa Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents: 41576
diff changeset
4764 {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
24795
452d6a916196 Allow more docstrings to be removed during compilation
Neal Norwitz <nnorwitz@gmail.com>
parents: 24789
diff changeset
4765 PyDoc_STR("T.__new__(S, ...) -> "
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
4766 "a new object with type S, a subtype of T")},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4767 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4768 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4769
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4770 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
4771 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
4772 {
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
4773 PyObject *func;
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
4774
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
4775 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
4776 return 0;
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
4777 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
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
4778 if (func == NULL)
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4779 return -1;
32407
aaf9295ed45b Cosmetic spacing fix.
Raymond Hettinger <python@rcn.com>
parents: 32404
diff changeset
4780 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
32404
75e5d1e8b617 Fix leak found by Eric Huss.
Raymond Hettinger <python@rcn.com>
parents: 32323
diff changeset
4781 Py_DECREF(func);
75e5d1e8b617 Fix leak found by Eric Huss.
Raymond Hettinger <python@rcn.com>
parents: 32323
diff changeset
4782 return -1;
75e5d1e8b617 Fix leak found by Eric Huss.
Raymond Hettinger <python@rcn.com>
parents: 32323
diff changeset
4783 }
75e5d1e8b617 Fix leak found by Eric Huss.
Raymond Hettinger <python@rcn.com>
parents: 32323
diff changeset
4784 Py_DECREF(func);
75e5d1e8b617 Fix leak found by Eric Huss.
Raymond Hettinger <python@rcn.com>
parents: 32323
diff changeset
4785 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
4786 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
4787
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
4788 /* Slot wrappers that call the corresponding __foo__ slot. See comments
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
4789 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
4790
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4791 #define SLOT0(FUNCNAME, OPSTR) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4792 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4793 FUNCNAME(PyObject *self) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4794 { \
19540
e141b392b61d Fix a typo in SLOT0 macro for the declaration of cache_str.
Guido van Rossum <guido@python.org>
parents: 19538
diff changeset
4795 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
4796 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
4797 }
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4798
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4799 #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
4800 static PyObject * \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4801 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
4802 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4803 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
4804 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
4805 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4806
27124
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4807 /* 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
4808 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
4809 static int
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4810 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
4811 {
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4812 PyObject *a, *b;
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4813 int ok;
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4814
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4815 b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
27124
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4816 if (b == NULL) {
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4817 PyErr_Clear();
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4818 /* If right doesn't have it, it's not overloaded */
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4819 return 0;
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4820 }
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4821
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4822 a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
27124
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4823 if (a == NULL) {
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4824 PyErr_Clear();
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4825 Py_DECREF(b);
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4826 /* If right has it but left doesn't, it's overloaded */
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4827 return 1;
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4828 }
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4829
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4830 ok = PyObject_RichCompareBool(a, b, Py_NE);
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4831 Py_DECREF(a);
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4832 Py_DECREF(b);
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4833 if (ok < 0) {
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4834 PyErr_Clear();
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4835 return 0;
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4836 }
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4837
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4838 return ok;
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4839 }
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4840
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4841
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4842 #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
4843 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4844 FUNCNAME(PyObject *self, PyObject *other) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4845 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4846 static PyObject *cache_str, *rcache_str; \
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4847 int do_other = Py_TYPE(self) != Py_TYPE(other) && \
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4848 Py_TYPE(other)->tp_as_number != NULL && \
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4849 Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4850 if (Py_TYPE(self)->tp_as_number != NULL && \
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4851 Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4852 PyObject *r; \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4853 if (do_other && \
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4854 PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
27124
d1850a36ae41 Add a refinement to SLOT1BINFULL() that fixes the problem reported in
Guido van Rossum <guido@python.org>
parents: 26964
diff changeset
4855 method_is_overloaded(self, other, ROPSTR)) { \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4856 r = call_maybe( \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4857 other, ROPSTR, &rcache_str, "(O)", self); \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4858 if (r != Py_NotImplemented) \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4859 return r; \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4860 Py_DECREF(r); \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4861 do_other = 0; \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4862 } \
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
4863 r = call_maybe( \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
4864 self, OPSTR, &cache_str, "(O)", other); \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4865 if (r != Py_NotImplemented || \
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4866 Py_TYPE(other) == Py_TYPE(self)) \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4867 return r; \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4868 Py_DECREF(r); \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4869 } \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
4870 if (do_other) { \
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
4871 return call_maybe( \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
4872 other, ROPSTR, &rcache_str, "(O)", self); \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4873 } \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4874 Py_INCREF(Py_NotImplemented); \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4875 return Py_NotImplemented; \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4876 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4877
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4878 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4879 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4880
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4881 #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
4882 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
4883 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
4884 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4885 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
4886 return call_method(self, OPSTR, &cache_str, \
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
4887 "(" ARGCODES ")", arg1, arg2); \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4888 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4889
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4890 static Py_ssize_t
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4891 slot_sq_length(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4892 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4893 static PyObject *len_str;
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
4894 PyObject *res = call_method(self, "__len__", &len_str, "()");
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4895 Py_ssize_t len;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4896
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4897 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4898 return -1;
39464
48f1dbc57845 Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
4899 len = PyInt_AsSsize_t(res);
20197
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
4900 Py_DECREF(res);
24376
aa9e054270a2 Complain if __len__() returns < 0, just like classic classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24280
diff changeset
4901 if (len < 0) {
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39921
diff changeset
4902 if (!PyErr_Occurred())
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39921
diff changeset
4903 PyErr_SetString(PyExc_ValueError,
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39921
diff changeset
4904 "__len__() should return >= 0");
24376
aa9e054270a2 Complain if __len__() returns < 0, just like classic classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24280
diff changeset
4905 return -1;
aa9e054270a2 Complain if __len__() returns < 0, just like classic classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24280
diff changeset
4906 }
20197
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
4907 return len;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4908 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4909
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4910 /* 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
4911 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
4912 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4913 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
4914 {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4915 static PyObject *getitem_str;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4916 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4917 descrgetfunc f;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4918
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4919 if (getitem_str == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4920 getitem_str = PyString_InternFromString("__getitem__");
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4921 if (getitem_str == NULL)
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4922 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4923 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4924 func = _PyType_Lookup(Py_TYPE(self), getitem_str);
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4925 if (func != NULL) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4926 if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4927 Py_INCREF(func);
25853
680bfcc9ca06 Fix SF # 624982, Potential AV in slot_sq_item, by Greg Chapman
Neal Norwitz <nnorwitz@gmail.com>
parents: 25839
diff changeset
4928 else {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
4929 func = f(func, self, (PyObject *)(Py_TYPE(self)));
25853
680bfcc9ca06 Fix SF # 624982, Potential AV in slot_sq_item, by Greg Chapman
Neal Norwitz <nnorwitz@gmail.com>
parents: 25839
diff changeset
4930 if (func == NULL) {
680bfcc9ca06 Fix SF # 624982, Potential AV in slot_sq_item, by Greg Chapman
Neal Norwitz <nnorwitz@gmail.com>
parents: 25839
diff changeset
4931 return NULL;
680bfcc9ca06 Fix SF # 624982, Potential AV in slot_sq_item, by Greg Chapman
Neal Norwitz <nnorwitz@gmail.com>
parents: 25839
diff changeset
4932 }
680bfcc9ca06 Fix SF # 624982, Potential AV in slot_sq_item, by Greg Chapman
Neal Norwitz <nnorwitz@gmail.com>
parents: 25839
diff changeset
4933 }
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
4934 ival = PyInt_FromSsize_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
4935 if (ival != NULL) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4936 args = PyTuple_New(1);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4937 if (args != NULL) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4938 PyTuple_SET_ITEM(args, 0, ival);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4939 retval = PyObject_Call(func, args, NULL);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4940 Py_XDECREF(args);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4941 Py_XDECREF(func);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4942 return retval;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4943 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4944 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4945 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4946 else {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4947 PyErr_SetObject(PyExc_AttributeError, getitem_str);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4948 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4949 Py_XDECREF(args);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4950 Py_XDECREF(ival);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4951 Py_XDECREF(func);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4952 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4953 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
4954
48854
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4955 static PyObject*
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4956 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
4957 {
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4958 static PyObject *getslice_str;
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4959
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4960 if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; "
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4961 "use __getitem__", 1) < 0)
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4962 return NULL;
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4963 return call_method(self, "__getslice__", &getslice_str,
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4964 "nn", i, j);
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4965 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4966
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4967 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4968 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
4969 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4970 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4971 static PyObject *delitem_str, *setitem_str;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4972
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4973 if (value == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4974 res = call_method(self, "__delitem__", &delitem_str,
37880
96aa5ce86445 Fix variable/format-char discrepancy in new-style class __getitem__,
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
4975 "(n)", index);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4976 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4977 res = call_method(self, "__setitem__", &setitem_str,
37880
96aa5ce86445 Fix variable/format-char discrepancy in new-style class __getitem__,
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
4978 "(nO)", index, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4979 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4980 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4981 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4982 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4983 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4984
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4985 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
4986 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
4987 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4988 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4989 static PyObject *delslice_str, *setslice_str;
48854
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4990
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4991 if (value == NULL) {
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4992 if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; "
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4993 "use __delitem__", 1) < 0)
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4994 return -1;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
4995 res = call_method(self, "__delslice__", &delslice_str,
37880
96aa5ce86445 Fix variable/format-char discrepancy in new-style class __getitem__,
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
4996 "(nn)", i, j);
48854
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4997 }
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4998 else {
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
4999 if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; "
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
5000 "use __setitem__", 1) < 0)
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
5001 return -1;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5002 res = call_method(self, "__setslice__", &setslice_str,
48854
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
5003 "(nnO)", i, j, value);
6ea1a720b057 generate py3k warnings on __getslice__, __delslice__, and __setslice__
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
5004 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5005 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5006 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5007 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5008 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5009 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5010
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5011 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5012 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
5013 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5014 PyObject *func, *res, *args;
28390
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5015 int result = -1;
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5016
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
5017 static PyObject *contains_str;
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
5018
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5019 func = lookup_maybe(self, "__contains__", &contains_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5020 if (func != NULL) {
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
5021 args = PyTuple_Pack(1, value);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5022 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5023 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5024 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5025 res = PyObject_Call(func, args, NULL);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5026 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5027 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5028 Py_DECREF(func);
28390
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5029 if (res != NULL) {
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5030 result = PyObject_IsTrue(res);
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5031 Py_DECREF(res);
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5032 }
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5033 }
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5034 else if (! PyErr_Occurred()) {
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36675
diff changeset
5035 /* Possible results: -1 and 1 */
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36675
diff changeset
5036 result = (int)_PySequence_IterSearch(self, value,
28390
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5037 PY_ITERSEARCH_CONTAINS);
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5038 }
33319458fe99 slot_sq_contains(): This leaked a reference to the result of calling
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
5039 return result;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5040 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5041
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5042 #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
5043
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5044 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
5045
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5046 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5047 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
5048 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5049 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5050 static PyObject *delitem_str, *setitem_str;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5051
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5052 if (value == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5053 res = call_method(self, "__delitem__", &delitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5054 "(O)", key);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5055 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5056 res = call_method(self, "__setitem__", &setitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5057 "(OO)", key, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5058 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5059 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5060 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5061 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5062 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5063
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5064 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
5065 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
5066 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
5067 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
5068 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
5069 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
5070
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24250
diff changeset
5071 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
5072
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5073 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5074 nb_power, "__pow__", "__rpow__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5075
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5076 static PyObject *
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5077 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
5078 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5079 static PyObject *pow_str;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5080
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5081 if (modulus == Py_None)
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5082 return slot_nb_power_binary(self, other);
23753
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
5083 /* Three-arg power doesn't use __rpow__. But ternary_op
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
5084 can call this when the second argument's type uses
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
5085 slot_nb_power, so check before calling self.__pow__. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5086 if (Py_TYPE(self)->tp_as_number != NULL &&
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5087 Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
23753
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
5088 return call_method(self, "__pow__", &pow_str,
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
5089 "(OO)", other, modulus);
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
5090 }
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
5091 Py_INCREF(Py_NotImplemented);
41097e92f560 Three's a charm: yet another fix for SF bug 551412. Thinking again
Guido van Rossum <guido@python.org>
parents: 23666
diff changeset
5092 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
5093 }
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5094
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5095 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
5096 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
5097 SLOT0(slot_nb_absolute, "__abs__")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5098
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5099 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5100 slot_nb_nonzero(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5101 {
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5102 PyObject *func, *args;
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
5103 static PyObject *nonzero_str, *len_str;
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5104 int result = -1;
52176
de7e4716d9cb Fix a small nit in the error message if bool() falls back on __len__ and it returns the wrong type: it would tell the user that __nonzero__ should return bool or int.
Georg Brandl <georg@python.org>
parents: 51032
diff changeset
5105 int using_len = 0;
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
5106
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5107 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5108 if (func == NULL) {
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5109 if (PyErr_Occurred())
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5110 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5111 func = lookup_maybe(self, "__len__", &len_str);
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5112 if (func == NULL)
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5113 return PyErr_Occurred() ? -1 : 1;
52176
de7e4716d9cb Fix a small nit in the error message if bool() falls back on __len__ and it returns the wrong type: it would tell the user that __nonzero__ should return bool or int.
Georg Brandl <georg@python.org>
parents: 51032
diff changeset
5114 using_len = 1;
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5115 }
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5116 args = PyTuple_New(0);
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5117 if (args != NULL) {
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5118 PyObject *temp = PyObject_Call(func, args, NULL);
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5119 Py_DECREF(args);
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5120 if (temp != NULL) {
29611
cac855387b56 Require that __nonzero__() return a bool or exactly an int.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29607
diff changeset
5121 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
29607
60b53db14aa3 Check return type of __nonzero__() method.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29442
diff changeset
5122 result = PyObject_IsTrue(temp);
60b53db14aa3 Check return type of __nonzero__() method.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29442
diff changeset
5123 else {
60b53db14aa3 Check return type of __nonzero__() method.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29442
diff changeset
5124 PyErr_Format(PyExc_TypeError,
52176
de7e4716d9cb Fix a small nit in the error message if bool() falls back on __len__ and it returns the wrong type: it would tell the user that __nonzero__ should return bool or int.
Georg Brandl <georg@python.org>
parents: 51032
diff changeset
5125 "%s should return "
29607
60b53db14aa3 Check return type of __nonzero__() method.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29442
diff changeset
5126 "bool or int, returned %s",
52176
de7e4716d9cb Fix a small nit in the error message if bool() falls back on __len__ and it returns the wrong type: it would tell the user that __nonzero__ should return bool or int.
Georg Brandl <georg@python.org>
parents: 51032
diff changeset
5127 (using_len ? "__len__"
de7e4716d9cb Fix a small nit in the error message if bool() falls back on __len__ and it returns the wrong type: it would tell the user that __nonzero__ should return bool or int.
Georg Brandl <georg@python.org>
parents: 51032
diff changeset
5128 : "__nonzero__"),
29607
60b53db14aa3 Check return type of __nonzero__() method.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29442
diff changeset
5129 temp->ob_type->tp_name);
29611
cac855387b56 Require that __nonzero__() return a bool or exactly an int.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29607
diff changeset
5130 result = -1;
29607
60b53db14aa3 Check return type of __nonzero__() method.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29442
diff changeset
5131 }
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5132 Py_DECREF(temp);
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5133 }
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5134 }
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5135 Py_DECREF(func);
26575
93b5ca9423eb slot_nb_nonzero(): Another leak uncovered by the sandbox datetime
Tim Peters <tim.peters@gmail.com>
parents: 26559
diff changeset
5136 return result;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5137 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5138
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5139
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39415
diff changeset
5140 static PyObject *
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5141 slot_nb_index(PyObject *self)
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5142 {
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5143 static PyObject *index_str;
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39415
diff changeset
5144 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
5145 }
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5146
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5147
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5148 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
5149 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
5150 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
5151 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
5152 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
5153 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
5154
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5155 static int
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5156 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
5157 {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5158 static PyObject *coerce_str;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5159 PyObject *self = *a, *other = *b;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5160
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5161 if (self->ob_type->tp_as_number != NULL &&
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5162 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5163 PyObject *r;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5164 r = call_maybe(
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5165 self, "__coerce__", &coerce_str, "(O)", other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5166 if (r == NULL)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5167 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5168 if (r == Py_NotImplemented) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5169 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5170 }
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5171 else {
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5172 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5173 PyErr_SetString(PyExc_TypeError,
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5174 "__coerce__ didn't return a 2-tuple");
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5175 Py_DECREF(r);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5176 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5177 }
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5178 *a = PyTuple_GET_ITEM(r, 0);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5179 Py_INCREF(*a);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5180 *b = PyTuple_GET_ITEM(r, 1);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5181 Py_INCREF(*b);
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5182 Py_DECREF(r);
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
5183 return 0;
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5184 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5185 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5186 if (other->ob_type->tp_as_number != NULL &&
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5187 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5188 PyObject *r;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5189 r = call_maybe(
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5190 other, "__coerce__", &coerce_str, "(O)", self);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5191 if (r == NULL)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5192 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5193 if (r == Py_NotImplemented) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5194 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5195 return 1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5196 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5197 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5198 PyErr_SetString(PyExc_TypeError,
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5199 "__coerce__ didn't return a 2-tuple");
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5200 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5201 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5202 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5203 *a = PyTuple_GET_ITEM(r, 1);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5204 Py_INCREF(*a);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5205 *b = PyTuple_GET_ITEM(r, 0);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5206 Py_INCREF(*b);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5207 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5208 return 0;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5209 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5210 return 1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5211 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
5212
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5213 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
5214 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
5215 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
5216 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
5217 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
5218 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
5219 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
5220 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
5221 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
5222 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
5223 /* Can't use SLOT1 here, because nb_inplace_power is ternary */
f6f391d1d050 Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
Martin v. Löwis <martin@v.loewis.de>
parents: 40707
diff changeset
5224 static PyObject *
f6f391d1d050 Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
Martin v. Löwis <martin@v.loewis.de>
parents: 40707
diff changeset
5225 slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
f6f391d1d050 Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
Martin v. Löwis <martin@v.loewis.de>
parents: 40707
diff changeset
5226 {
f6f391d1d050 Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
Martin v. Löwis <martin@v.loewis.de>
parents: 40707
diff changeset
5227 static PyObject *cache_str;
f6f391d1d050 Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
Martin v. Löwis <martin@v.loewis.de>
parents: 40707
diff changeset
5228 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
f6f391d1d050 Bug #1653736: Properly discard third argument to slot_nb_inplace_power.
Martin v. Löwis <martin@v.loewis.de>
parents: 40707
diff changeset
5229 }
19082
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_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
5231 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
5232 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
5233 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
5234 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
5235 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5236 "__floordiv__", "__rfloordiv__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
5237 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
5238 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
5239 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
5240
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5241 static int
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5242 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
5243 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5244 PyObject *func, *args, *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
5245 static PyObject *cmp_str;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
5246 Py_ssize_t c;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5247
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
5248 func = lookup_method(self, "__cmp__", &cmp_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5249 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5250 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5251 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5252 else {
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
5253 args = PyTuple_Pack(1, other);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5254 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5255 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5256 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5257 res = PyObject_Call(func, args, NULL);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5258 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5259 }
23967
1a8096bb4c48 Fix SF bug 572567: Memory leak in object comparison.
Raymond Hettinger <python@rcn.com>
parents: 23950
diff changeset
5260 Py_DECREF(func);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5261 if (res != Py_NotImplemented) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5262 if (res == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5263 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5264 c = PyInt_AsLong(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5265 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5266 if (c == -1 && PyErr_Occurred())
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5267 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5268 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5269 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5270 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5271 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5272 return 2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5273 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5274
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
5275 /* 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
5276 int
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
5277 _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
5278 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5279 int c;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5280
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5281 if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5282 c = half_compare(self, other);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5283 if (c <= 1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5284 return c;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5285 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5286 if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5287 c = half_compare(other, self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5288 if (c < -1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5289 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5290 if (c <= 1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5291 return -c;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5292 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5293 return (void *)self < (void *)other ? -1 :
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5294 (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
5295 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5296
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5297 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5298 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
5299 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5300 PyObject *func, *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
5301 static PyObject *repr_str;
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
5302
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
5303 func = lookup_method(self, "__repr__", &repr_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5304 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5305 res = PyEval_CallObject(func, NULL);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5306 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5307 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5308 }
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
5309 PyErr_Clear();
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
5310 return PyString_FromFormat("<%s object at %p>",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5311 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
5312 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5313
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5314 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5315 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
5316 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5317 PyObject *func, *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
5318 static PyObject *str_str;
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
5319
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
5320 func = lookup_method(self, "__str__", &str_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5321 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5322 res = PyEval_CallObject(func, NULL);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5323 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5324 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5325 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5326 else {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5327 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5328 return slot_tp_repr(self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5329 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5330 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5331
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5332 static long
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5333 slot_tp_hash(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5334 {
26559
0dbf98ee118e slot_tp_hash(): In the normal path, this leaked a reference to the
Tim Peters <tim.peters@gmail.com>
parents: 26448
diff changeset
5335 PyObject *func;
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
5336 static PyObject *hash_str, *eq_str, *cmp_str;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5337 long h;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5338
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
5339 func = lookup_method(self, "__hash__", &hash_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5340
44357
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5341 if (func != NULL && func != Py_None) {
26559
0dbf98ee118e slot_tp_hash(): In the normal path, this leaked a reference to the
Tim Peters <tim.peters@gmail.com>
parents: 26448
diff changeset
5342 PyObject *res = PyEval_CallObject(func, NULL);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5343 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5344 if (res == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5345 return -1;
39410
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39367
diff changeset
5346 if (PyLong_Check(res))
39415
9a8081cf4a70 Fix and test for an infinite C recursion.
Armin Rigo <arigo@tunes.org>
parents: 39410
diff changeset
5347 h = PyLong_Type.tp_hash(res);
39410
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39367
diff changeset
5348 else
5b9692f6629f __hash__ may now return long int; the final hash
Martin v. Löwis <martin@v.loewis.de>
parents: 39367
diff changeset
5349 h = PyInt_AsLong(res);
26559
0dbf98ee118e slot_tp_hash(): In the normal path, this leaked a reference to the
Tim Peters <tim.peters@gmail.com>
parents: 26448
diff changeset
5350 Py_DECREF(res);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5351 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5352 else {
44365
8dead6f81bae Fix refleak introduced in r59576.
Georg Brandl <georg@python.org>
parents: 44357
diff changeset
5353 Py_XDECREF(func); /* may be None */
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5354 PyErr_Clear();
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
5355 func = lookup_method(self, "__eq__", &eq_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5356 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5357 PyErr_Clear();
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
5358 func = lookup_method(self, "__cmp__", &cmp_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5359 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5360 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5361 Py_DECREF(func);
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
5362 return PyObject_HashNotImplemented(self);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5363 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5364 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5365 h = _Py_HashPointer((void *)self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5366 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5367 if (h == -1 && !PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5368 h = -2;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5369 return h;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5370 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5371
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5372 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5373 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
5374 {
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
5375 static PyObject *call_str;
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
5376 PyObject *meth = lookup_method(self, "__call__", &call_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5377 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5378
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5379 if (meth == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5380 return NULL;
38904
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38871
diff changeset
5381
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5382 res = PyObject_Call(meth, args, kwds);
38904
506c76847ec7 Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
Armin Rigo <arigo@tunes.org>
parents: 38871
diff changeset
5383
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5384 Py_DECREF(meth);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5385 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5386 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5387
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
5388 /* 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
5389
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
5390 - 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
5391 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
5392
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
5393 - 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
5394
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
5395 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
5396 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
5397 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
5398
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5399 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5400 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
5401 {
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
5402 static PyObject *getattribute_str = NULL;
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 return call_method(self, "__getattribute__", &getattribute_str,
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
5404 "(O)", name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5405 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5406
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5407 static PyObject *
49741
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5408 call_attribute(PyObject *self, PyObject *attr, PyObject *name)
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5409 {
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5410 PyObject *res, *descr = NULL;
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5411 descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5412
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5413 if (f != NULL) {
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5414 descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5415 if (descr == NULL)
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5416 return NULL;
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5417 else
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5418 attr = descr;
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5419 }
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5420 res = PyObject_CallFunctionObjArgs(attr, name, NULL);
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5421 Py_XDECREF(descr);
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5422 return res;
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5423 }
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5424
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5425 static PyObject *
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5426 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
5427 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5428 PyTypeObject *tp = Py_TYPE(self);
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5429 PyObject *getattr, *getattribute, *res;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5430 static PyObject *getattribute_str = NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5431 static PyObject *getattr_str = NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5432
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5433 if (getattr_str == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
5434 getattr_str = PyString_InternFromString("__getattr__");
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5435 if (getattr_str == NULL)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5436 return NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5437 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5438 if (getattribute_str == NULL) {
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5439 getattribute_str =
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
5440 PyString_InternFromString("__getattribute__");
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5441 if (getattribute_str == NULL)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5442 return NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5443 }
49741
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5444 /* speed hack: we could use lookup_maybe, but that would resolve the
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5445 method fully for each attribute lookup for classes with
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5446 __getattr__, even when the attribute is present. So we use
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5447 _PyType_Lookup and create the method only when needed, with
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5448 call_attribute. */
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5449 getattr = _PyType_Lookup(tp, getattr_str);
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
5450 if (getattr == NULL) {
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
5451 /* No __getattr__ hook: use a simpler dispatcher */
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
5452 tp->tp_getattro = slot_tp_getattro;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
5453 return slot_tp_getattro(self, name);
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
5454 }
49741
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5455 Py_INCREF(getattr);
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5456 /* speed hack: we could use lookup_maybe, but that would resolve the
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5457 method fully for each attribute lookup for classes with
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5458 __getattr__, even when self has the default __getattribute__
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5459 method. So we use _PyType_Lookup and create the method only when
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5460 needed, with call_attribute. */
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5461 getattribute = _PyType_Lookup(tp, getattribute_str);
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
5462 if (getattribute == NULL ||
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5463 (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
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
5464 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
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
5465 (void *)PyObject_GenericGetAttr))
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5466 res = PyObject_GenericGetAttr(self, name);
49741
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5467 else {
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5468 Py_INCREF(getattribute);
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5469 res = call_attribute(self, getattribute, name);
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5470 Py_DECREF(getattribute);
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5471 }
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
5472 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5473 PyErr_Clear();
49741
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5474 res = call_attribute(self, getattr, name);
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5475 }
73b9fe1c5397 Merged revisions 67246 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48876
diff changeset
5476 Py_DECREF(getattr);
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5477 return res;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5478 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
5479
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5480 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5481 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
5482 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5483 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5484 static PyObject *delattr_str, *setattr_str;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5485
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5486 if (value == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5487 res = call_method(self, "__delattr__", &delattr_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5488 "(O)", name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5489 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5490 res = call_method(self, "__setattr__", &setattr_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5491 "(OO)", name, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5492 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5493 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5494 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5495 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5496 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5497
44357
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5498 static char *name_op[] = {
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5499 "__lt__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5500 "__le__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5501 "__eq__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5502 "__ne__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5503 "__gt__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5504 "__ge__",
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5505 };
174e0bcb2c3c Patch #1549 by Thomas Herve.
Guido van Rossum <guido@python.org>
parents: 44350
diff changeset
5506
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5507 static PyObject *
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5508 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
5509 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5510 PyObject *func, *args, *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
5511 static PyObject *op_str[6];
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
5512
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
5513 func = lookup_method(self, name_op[op], &op_str[op]);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5514 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5515 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5516 Py_INCREF(Py_NotImplemented);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5517 return Py_NotImplemented;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5518 }
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30671
diff changeset
5519 args = PyTuple_Pack(1, other);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5520 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5521 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5522 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5523 res = PyObject_Call(func, args, NULL);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5524 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5525 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5526 Py_DECREF(func);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5527 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5528 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5529
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5530 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5531 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
5532 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5533 PyObject *res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5534
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5535 if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5536 res = half_richcompare(self, other, op);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5537 if (res != Py_NotImplemented)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5538 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5539 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5540 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5541 if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
33541
781aef974463 A static swapped_op[] array was defined in 3 different C files, & I think
Tim Peters <tim.peters@gmail.com>
parents: 33174
diff changeset
5542 res = half_richcompare(other, self, _Py_SwappedOp[op]);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5543 if (res != Py_NotImplemented) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5544 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5545 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5546 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5547 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5548 Py_INCREF(Py_NotImplemented);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5549 return Py_NotImplemented;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5550 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5551
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5552 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5553 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
5554 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5555 PyObject *func, *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
5556 static PyObject *iter_str, *getitem_str;
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
5557
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
5558 func = lookup_method(self, "__iter__", &iter_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5559 if (func != NULL) {
24867
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24862
diff changeset
5560 PyObject *args;
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24862
diff changeset
5561 args = res = PyTuple_New(0);
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24862
diff changeset
5562 if (args != NULL) {
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24862
diff changeset
5563 res = PyObject_Call(func, args, NULL);
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24862
diff changeset
5564 Py_DECREF(args);
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24862
diff changeset
5565 }
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24862
diff changeset
5566 Py_DECREF(func);
255d1d3e66a3 Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
Guido van Rossum <guido@python.org>
parents: 24862
diff changeset
5567 return res;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5568 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5569 PyErr_Clear();
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
5570 func = lookup_method(self, "__getitem__", &getitem_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5571 if (func == NULL) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
5572 PyErr_Format(PyExc_TypeError,
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
5573 "'%.200s' object is not iterable",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5574 Py_TYPE(self)->tp_name);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5575 return NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5576 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5577 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5578 return PySeqIter_New(self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
5579 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5580
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5581 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5582 slot_tp_iternext(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5583 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5584 static PyObject *next_str;
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5585 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
5586 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5587
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5588 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
5589 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
5590 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5591 PyTypeObject *tp = Py_TYPE(self);
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5592 PyObject *get;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5593 static PyObject *get_str = NULL;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5594
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5595 if (get_str == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
5596 get_str = PyString_InternFromString("__get__");
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 if (get_str == NULL)
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5598 return NULL;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5599 }
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5600 get = _PyType_Lookup(tp, get_str);
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5601 if (get == NULL) {
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5602 /* Avoid further slowdowns */
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5603 if (tp->tp_descr_get == slot_tp_descr_get)
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5604 tp->tp_descr_get = NULL;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5605 Py_INCREF(self);
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5606 return self;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
5607 }
19464
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
5608 if (obj == NULL)
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
5609 obj = Py_None;
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
5610 if (type == NULL)
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
5611 type = Py_None;
38314
1fdf29a88f0e Replace PyObject_CallFunction calls with only object args
Georg Brandl <georg@python.org>
parents: 37880
diff changeset
5612 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
5613 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5614
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5615 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5616 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
5617 {
19464
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
5618 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5619 static PyObject *del_str, *set_str;
19464
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
5620
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
5621 if (value == NULL)
21142
842b280421ba Fix for SF bug #485678.
Guido van Rossum <guido@python.org>
parents: 20978
diff changeset
5622 res = call_method(self, "__delete__", &del_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5623 "(O)", target);
19464
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
5624 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
5625 res = call_method(self, "__set__", &set_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
5626 "(OO)", target, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5627 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5628 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5629 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5630 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5631 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5632
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5633 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5634 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
5635 {
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
5636 static PyObject *init_str;
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
5637 PyObject *meth = lookup_method(self, "__init__", &init_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5638 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5639
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5640 if (meth == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5641 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5642 res = PyObject_Call(meth, args, kwds);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5643 Py_DECREF(meth);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5644 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5645 return -1;
34668
9b3a2c4302aa SF bug #1155938: Missing None check for __init__().
Raymond Hettinger <python@rcn.com>
parents: 33541
diff changeset
5646 if (res != Py_None) {
38871
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
5647 PyErr_Format(PyExc_TypeError,
a3c61c648fc8 Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents: 38314
diff changeset
5648 "__init__() should return None, not '%.200s'",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5649 Py_TYPE(res)->tp_name);
34668
9b3a2c4302aa SF bug #1155938: Missing None check for __init__().
Raymond Hettinger <python@rcn.com>
parents: 33541
diff changeset
5650 Py_DECREF(res);
9b3a2c4302aa SF bug #1155938: Missing None check for __init__().
Raymond Hettinger <python@rcn.com>
parents: 33541
diff changeset
5651 return -1;
9b3a2c4302aa SF bug #1155938: Missing None check for __init__().
Raymond Hettinger <python@rcn.com>
parents: 33541
diff changeset
5652 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5653 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5654 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5655 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5656
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5657 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5658 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
5659 {
24680
6d26d4b9c5a3 Significant speedup in new-style object creation: in slot_tp_new(),
Guido van Rossum <guido@python.org>
parents: 24678
diff changeset
5660 static PyObject *new_str;
6d26d4b9c5a3 Significant speedup in new-style object creation: in slot_tp_new(),
Guido van Rossum <guido@python.org>
parents: 24678
diff changeset
5661 PyObject *func;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5662 PyObject *newargs, *x;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
5663 Py_ssize_t i, n;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5664
24680
6d26d4b9c5a3 Significant speedup in new-style object creation: in slot_tp_new(),
Guido van Rossum <guido@python.org>
parents: 24678
diff changeset
5665 if (new_str == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
5666 new_str = PyString_InternFromString("__new__");
24680
6d26d4b9c5a3 Significant speedup in new-style object creation: in slot_tp_new(),
Guido van Rossum <guido@python.org>
parents: 24678
diff changeset
5667 if (new_str == NULL)
6d26d4b9c5a3 Significant speedup in new-style object creation: in slot_tp_new(),
Guido van Rossum <guido@python.org>
parents: 24678
diff changeset
5668 return NULL;
6d26d4b9c5a3 Significant speedup in new-style object creation: in slot_tp_new(),
Guido van Rossum <guido@python.org>
parents: 24678
diff changeset
5669 }
6d26d4b9c5a3 Significant speedup in new-style object creation: in slot_tp_new(),
Guido van Rossum <guido@python.org>
parents: 24678
diff changeset
5670 func = PyObject_GetAttr((PyObject *)type, new_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5671 if (func == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5672 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5673 assert(PyTuple_Check(args));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5674 n = PyTuple_GET_SIZE(args);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5675 newargs = PyTuple_New(n+1);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5676 if (newargs == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5677 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5678 Py_INCREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5679 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5680 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5681 x = PyTuple_GET_ITEM(args, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5682 Py_INCREF(x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5683 PyTuple_SET_ITEM(newargs, i+1, x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5684 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5685 x = PyObject_Call(func, newargs, kwds);
20195
3408d929bb94 slot_tp_new(): newargs was leaking.
Guido van Rossum <guido@python.org>
parents: 20190
diff changeset
5686 Py_DECREF(newargs);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5687 Py_DECREF(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5688 return x;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5689 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5690
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5691 static void
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5692 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
5693 {
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5694 static PyObject *del_str = NULL;
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5695 PyObject *del, *res;
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5696 PyObject *error_type, *error_value, *error_traceback;
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5697
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5698 /* Temporarily resurrect the object. */
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5699 assert(self->ob_refcnt == 0);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5700 self->ob_refcnt = 1;
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5701
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5702 /* Save the current exception, if any. */
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5703 PyErr_Fetch(&error_type, &error_value, &error_traceback);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5704
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5705 /* Execute __del__ method, if any. */
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5706 del = lookup_maybe(self, "__del__", &del_str);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5707 if (del != NULL) {
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5708 res = PyEval_CallObject(del, NULL);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5709 if (res == NULL)
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5710 PyErr_WriteUnraisable(del);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5711 else
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5712 Py_DECREF(res);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5713 Py_DECREF(del);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5714 }
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5715
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5716 /* Restore the saved exception. */
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5717 PyErr_Restore(error_type, error_value, error_traceback);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5718
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5719 /* Undo the temporary resurrection; can't use DECREF here, it would
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5720 * cause a recursive call.
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5721 */
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5722 assert(self->ob_refcnt > 0);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5723 if (--self->ob_refcnt == 0)
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5724 return; /* this is the normal path out */
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5725
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5726 /* __del__ resurrected it! Make it look like the original Py_DECREF
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5727 * never happened.
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5728 */
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5729 {
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36675
diff changeset
5730 Py_ssize_t refcnt = self->ob_refcnt;
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5731 _Py_NewReference(self);
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5732 self->ob_refcnt = refcnt;
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5733 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5734 assert(!PyType_IS_GC(Py_TYPE(self)) ||
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5735 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
32916
ff77727555ae Repair the same thinko in two places about handling of _Py_RefTotal in
Michael W. Hudson <mwh@python.net>
parents: 32407
diff changeset
5736 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
ff77727555ae Repair the same thinko in two places about handling of _Py_RefTotal in
Michael W. Hudson <mwh@python.net>
parents: 32407
diff changeset
5737 * we need to undo that. */
ff77727555ae Repair the same thinko in two places about handling of _Py_RefTotal in
Michael W. Hudson <mwh@python.net>
parents: 32407
diff changeset
5738 _Py_DEC_REFTOTAL;
ff77727555ae Repair the same thinko in two places about handling of _Py_RefTotal in
Michael W. Hudson <mwh@python.net>
parents: 32407
diff changeset
5739 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
ff77727555ae Repair the same thinko in two places about handling of _Py_RefTotal in
Michael W. Hudson <mwh@python.net>
parents: 32407
diff changeset
5740 * chain, so no more to do there.
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5741 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5742 * _Py_NewReference bumped tp_allocs: both of those need to be
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5743 * undone.
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5744 */
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5745 #ifdef COUNT_ALLOCS
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5746 --Py_TYPE(self)->tp_frees;
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
5747 --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
5748 #endif
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5749 }
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
5750
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5751
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5752 /* 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
5753 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
5754 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
5755 sequences and mappings.
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
5756 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
5757 __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
5758 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
5759 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
5760 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
5761
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5762 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
5763
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5764 #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
5765 #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
5766 #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
5767 #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
5768 #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
5769 #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
5770 #undef UNSLOT
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5771 #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
5772 #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
5773 #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
5774
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5775 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
24789
fcbde226545c Allow docstrings to be removed during compilation for *SLOT macro and friends
Neal Norwitz <nnorwitz@gmail.com>
parents: 24785
diff changeset
5776 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
fcbde226545c Allow docstrings to be removed during compilation for *SLOT macro and friends
Neal Norwitz <nnorwitz@gmail.com>
parents: 24785
diff changeset
5777 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
5778 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
5779 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
24789
fcbde226545c Allow docstrings to be removed during compilation for *SLOT macro and friends
Neal Norwitz <nnorwitz@gmail.com>
parents: 24785
diff changeset
5780 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
5781 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
5782 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
24789
fcbde226545c Allow docstrings to be removed during compilation for *SLOT macro and friends
Neal Norwitz <nnorwitz@gmail.com>
parents: 24785
diff changeset
5783 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
5784 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5785 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5786 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5787 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5788 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5789 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5790 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5791 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5792 "x." NAME "() <==> " DOC)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5793 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5794 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5795 "x." NAME "(y) <==> x" DOC "y")
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5796 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5797 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5798 "x." NAME "(y) <==> x" DOC "y")
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5799 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5800 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5801 "x." NAME "(y) <==> y" DOC "x")
35004
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5802 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5803 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5804 "x." NAME "(y) <==> " DOC)
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5805 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5806 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5807 "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
5808
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5809 static slotdef slotdefs[] = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
5810 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5811 "x.__len__() <==> len(x)"),
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
5812 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
5813 The logic in abstract.c always falls back to nb_add/nb_multiply in
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
5814 this case. Defining both the nb_* and the sq_* slots to call the
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
5815 user-defined methods has unexpected side-effects, as shown by
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
5816 test_descr.notimplemented() */
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
5817 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5818 "x.__add__(y) <==> x+y"),
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
5819 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5820 "x.__mul__(n) <==> x*n"),
37308
aaa04bfafc58 Minor bugs in the __index__ code (PEP 357), with tests.
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
5821 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5822 "x.__rmul__(n) <==> n*x"),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5823 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5824 "x.__getitem__(y) <==> x[y]"),
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
5825 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
29172
9ae17b3779bb Fix docstrings for __(get|set|del)slice__ to mention that negative indices are not supported.
Brett Cannon <bcannon@gmail.com>
parents: 29066
diff changeset
5826 "x.__getslice__(i, j) <==> x[i:j]\n\
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5827 \n\
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5828 Use of negative indices is not supported."),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5829 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
29173
421ffd7c57af Fixing the previous patch to have the changes be to the proper docstrings.
Brett Cannon <bcannon@gmail.com>
parents: 29172
diff changeset
5830 "x.__setitem__(i, y) <==> x[i]=y"),
421ffd7c57af Fixing the previous patch to have the changes be to the proper docstrings.
Brett Cannon <bcannon@gmail.com>
parents: 29172
diff changeset
5831 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
421ffd7c57af Fixing the previous patch to have the changes be to the proper docstrings.
Brett Cannon <bcannon@gmail.com>
parents: 29172
diff changeset
5832 "x.__delitem__(y) <==> del x[y]"),
421ffd7c57af Fixing the previous patch to have the changes be to the proper docstrings.
Brett Cannon <bcannon@gmail.com>
parents: 29172
diff changeset
5833 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
5834 wrap_ssizessizeobjargproc,
29173
421ffd7c57af Fixing the previous patch to have the changes be to the proper docstrings.
Brett Cannon <bcannon@gmail.com>
parents: 29172
diff changeset
5835 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5836 \n\
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5837 Use of negative indices is not supported."),
29173
421ffd7c57af Fixing the previous patch to have the changes be to the proper docstrings.
Brett Cannon <bcannon@gmail.com>
parents: 29172
diff changeset
5838 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
421ffd7c57af Fixing the previous patch to have the changes be to the proper docstrings.
Brett Cannon <bcannon@gmail.com>
parents: 29172
diff changeset
5839 "x.__delslice__(i, j) <==> del x[i:j]\n\
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5840 \n\
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5841 Use of negative indices is not supported."),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5842 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5843 "x.__contains__(y) <==> y in x"),
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
5844 SQSLOT("__iadd__", sq_inplace_concat, NULL,
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5845 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
5846 SQSLOT("__imul__", sq_inplace_repeat, NULL,
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
5847 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
5848
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
5849 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5850 "x.__len__() <==> len(x)"),
20345
67a7a99f2d5a The slot definition table entry for mp_getitem had a bogus wrapper
Guido van Rossum <guido@python.org>
parents: 20343
diff changeset
5851 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5852 wrap_binaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5853 "x.__getitem__(y) <==> x[y]"),
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5854 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5855 wrap_objobjargproc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5856 "x.__setitem__(i, y) <==> x[i]=y"),
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
5857 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5858 wrap_delitem,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5859 "x.__delitem__(y) <==> del x[y]"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5860
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5861 BINSLOT("__add__", nb_add, slot_nb_add,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5862 "+"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5863 RBINSLOT("__radd__", nb_add, slot_nb_add,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5864 "+"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5865 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5866 "-"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5867 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5868 "-"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5869 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5870 "*"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5871 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5872 "*"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5873 BINSLOT("__div__", nb_divide, slot_nb_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5874 "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5875 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5876 "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5877 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5878 "%"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5879 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5880 "%"),
35004
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5881 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5882 "divmod(x, y)"),
35004
8880d91cdb09 fix object.__divmod__.__doc__
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34958
diff changeset
5883 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5884 "divmod(y, x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5885 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5886 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5887 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5888 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5889 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5890 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5891 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5892 "abs(x)"),
30670
7f6134f8bd06 SF bug #820397: __nonzero__() returns 1/0
Raymond Hettinger <python@rcn.com>
parents: 30660
diff changeset
5893 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5894 "x != 0"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5895 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5896 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5897 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5898 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5899 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5900 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5901 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5902 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5903 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5904 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5905 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5906 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5907 "x.__coerce__(y) <==> coerce(x, y)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5908 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5909 "int(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5910 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5911 "long(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5912 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5913 "float(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5914 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5915 "oct(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5916 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5917 "hex(x)"),
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39415
diff changeset
5918 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
5919 "x[y:z] <==> x[y.__index__():z.__index__()]"),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5920 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5921 wrap_binaryfunc, "+"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5922 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5923 wrap_binaryfunc, "-"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5924 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5925 wrap_binaryfunc, "*"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5926 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5927 wrap_binaryfunc, "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5928 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5929 wrap_binaryfunc, "%"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5930 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
25794
001b789f1f84 For some reason (probably cut and paste), __ipow__ for new-style
Guido van Rossum <guido@python.org>
parents: 25793
diff changeset
5931 wrap_binaryfunc, "**"),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5932 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5933 wrap_binaryfunc, "<<"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5934 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5935 wrap_binaryfunc, ">>"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5936 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5937 wrap_binaryfunc, "&"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5938 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5939 wrap_binaryfunc, "^"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5940 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5941 wrap_binaryfunc, "|"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5942 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5943 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5944 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5945 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5946 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5947 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5948 IBSLOT("__itruediv__", nb_inplace_true_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5949 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5950
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5951 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5952 "x.__str__() <==> str(x)"),
20737
00a581bdedeb When overriding __str__ or __repr__, set the tp_print slot to NULL.
Guido van Rossum <guido@python.org>
parents: 20725
diff changeset
5953 TPSLOT("__str__", tp_print, NULL, NULL, ""),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5954 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5955 "x.__repr__() <==> repr(x)"),
20737
00a581bdedeb When overriding __str__ or __repr__, set the tp_print slot to NULL.
Guido van Rossum <guido@python.org>
parents: 20725
diff changeset
5956 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5957 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5958 "x.__cmp__(y) <==> cmp(x,y)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5959 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5960 "x.__hash__() <==> hash(x)"),
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
5961 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
5962 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
5963 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5964 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5965 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5966 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5967 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5968 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5969 "x.__setattr__('name', value) <==> x.name = value"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5970 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5971 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5972 "x.__delattr__('name') <==> del x.name"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5973 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5974 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5975 "x.__lt__(y) <==> x<y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5976 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5977 "x.__le__(y) <==> x<=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5978 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5979 "x.__eq__(y) <==> x==y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5980 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5981 "x.__ne__(y) <==> x!=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5982 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5983 "x.__gt__(y) <==> x>y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5984 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5985 "x.__ge__(y) <==> x>=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5986 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5987 "x.__iter__() <==> iter(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5988 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5989 "x.next() -> the next value, or raise StopIteration"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5990 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5991 "descr.__get__(obj[, type]) -> value"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5992 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5993 "descr.__set__(obj, value)"),
24460
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
5994 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
2d77be41ee5f SF patch 588728 (Nathan Srebro).
Guido van Rossum <guido@python.org>
parents: 24427
diff changeset
5995 wrap_descr_delete, "descr.__delete__(obj)"),
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
5996 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
5997 "x.__init__(...) initializes x; "
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
5998 "see x.__class__.__doc__ for signature",
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
5999 PyWrapperFlag_KEYWORDS),
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
6000 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
24678
71dca7c76fa2 A modest speedup of object deallocation. call_finalizer() did rather
Guido van Rossum <guido@python.org>
parents: 24653
diff changeset
6001 TPSLOT("__del__", tp_del, slot_tp_del, 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
6002 {NULL}
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6003 };
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6004
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6005 /* Given a type pointer and an offset gotten from a slotdef entry, return a
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6006 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
6007 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
6008 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
6009 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
6010 static void **
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
6011 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
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 char *ptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
6014 long offset = 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
6015
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
6016 /* Note: this depends on the order of the members of PyHeapTypeObject! */
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6017 assert(offset >= 0);
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37759
diff changeset
6018 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37759
diff changeset
6019 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
37603
38acb91824fc Correct casts to char*.
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
6020 ptr = (char *)type->tp_as_sequence;
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
6021 offset -= offsetof(PyHeapTypeObject, as_sequence);
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
6022 }
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37759
diff changeset
6023 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
37603
38acb91824fc Correct casts to char*.
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
6024 ptr = (char *)type->tp_as_mapping;
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
6025 offset -= offsetof(PyHeapTypeObject, as_mapping);
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
6026 }
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37759
diff changeset
6027 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
37603
38acb91824fc Correct casts to char*.
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
6028 ptr = (char *)type->tp_as_number;
28219
7c8e8807aed2 - The extended type structure used for heap types (new-style
Guido van Rossum <guido@python.org>
parents: 28016
diff changeset
6029 offset -= offsetof(PyHeapTypeObject, as_number);
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6030 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6031 else {
37603
38acb91824fc Correct casts to char*.
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
6032 ptr = (char *)type;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6033 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6034 if (ptr != NULL)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6035 ptr += offset;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6036 return (void **)ptr;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6037 }
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
6038
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6039 /* 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
6040 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
6041 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
6042 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
6043 #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
6044
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6045 /* 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
6046 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
6047 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
6048 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
6049 {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6050 /* XXX Maybe this could be optimized more -- but is it worth it? */
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6051
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6052 /* pname and ptrs act as a little cache */
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6053 static PyObject *pname;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6054 static slotdef *ptrs[MAX_EQUIV];
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6055 slotdef *p, **pp;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6056 void **res, **ptr;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6057
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6058 if (pname != name) {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6059 /* Collect all slotdefs that match name into ptrs. */
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6060 pname = name;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6061 pp = ptrs;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6062 for (p = slotdefs; p->name_strobj; p++) {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6063 if (p->name_strobj == name)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6064 *pp++ = p;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6065 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6066 *pp = NULL;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6067 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6068
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6069 /* Look in all matching slots of the type; if exactly one of these has
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6070 a filled-in slot, return its value. Otherwise return NULL. */
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6071 res = NULL;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6072 for (pp = ptrs; *pp; pp++) {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6073 ptr = slotptr(type, (*pp)->offset);
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6074 if (ptr == NULL || *ptr == NULL)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6075 continue;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6076 if (res != NULL)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6077 return NULL;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6078 res = ptr;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6079 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6080 return res;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6081 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6082
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6083 /* 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
6084 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
6085 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
6086 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
6087 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
6088 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
6089 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
6090 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
6091 {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6092 PyObject *descr;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6093 PyWrapperDescrObject *d;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6094 void *generic = NULL, *specific = NULL;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6095 int use_generic = 0;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6096 int offset = p->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 void **ptr = slotptr(type, offset);
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6098
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6099 if (ptr == NULL) {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6100 do {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6101 ++p;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6102 } while (p->offset == offset);
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6103 return p;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6104 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6105 do {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6106 descr = _PyType_Lookup(type, p->name_strobj);
50754
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49741
diff changeset
6107 if (descr == NULL) {
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49741
diff changeset
6108 if (ptr == (void**)&type->tp_iternext) {
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49741
diff changeset
6109 specific = _PyObject_NextNotImplemented;
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49741
diff changeset
6110 }
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6111 continue;
50754
e9748db47635 #3720: Interpreter crashes when an evil iterator removes its own next function.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49741
diff changeset
6112 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6113 if (Py_TYPE(descr) == &PyWrapperDescr_Type) {
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6114 void **tptr = resolve_slotdups(type, p->name_strobj);
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6115 if (tptr == NULL || tptr == ptr)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6116 generic = p->function;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6117 d = (PyWrapperDescrObject *)descr;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6118 if (d->d_base->wrapper == p->wrapper &&
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6119 PyType_IsSubtype(type, d->d_type))
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6120 {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6121 if (specific == NULL ||
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6122 specific == d->d_wrapped)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6123 specific = d->d_wrapped;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6124 else
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6125 use_generic = 1;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6126 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6127 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6128 else if (Py_TYPE(descr) == &PyCFunction_Type &&
24685
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6129 PyCFunction_GET_FUNCTION(descr) ==
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6130 (PyCFunction)tp_new_wrapper &&
50828
27ba803bdea9 #4930: Slightly cleaner (and faster) code in type creation:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50754
diff changeset
6131 ptr == (void**)&type->tp_new)
24685
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6132 {
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6133 /* The __new__ wrapper is not a wrapper descriptor,
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6134 so must be special-cased differently.
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6135 If we don't do this, creating an instance will
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6136 always use slot_tp_new which will look up
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6137 __new__ in the MRO which will call tp_new_wrapper
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6138 which will look through the base classes looking
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6139 for a static base and call its tp_new (usually
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6140 PyType_GenericNew), after performing various
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6141 sanity checks and constructing a new argument
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6142 list. Cut all that nonsense short -- this speeds
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6143 up instance creation tremendously. */
29066
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28737
diff changeset
6144 specific = (void *)type->tp_new;
24685
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6145 /* XXX I'm not 100% sure that there isn't a hole
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6146 in this reasoning that requires additional
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6147 sanity checks. I'll buy the first person to
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6148 point out a bug in this reasoning a beer. */
36493a7c6253 Major speedup for new-style class creation. Turns out there was some
Guido van Rossum <guido@python.org>
parents: 24680
diff changeset
6149 }
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6150 else if (descr == Py_None &&
50828
27ba803bdea9 #4930: Slightly cleaner (and faster) code in type creation:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50754
diff changeset
6151 ptr == (void**)&type->tp_hash) {
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6152 /* We specifically allow __hash__ to be set to None
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6153 to prevent inheritance of the default
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6154 implementation from object.__hash__ */
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6155 specific = PyObject_HashNotImplemented;
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6156 }
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6157 else {
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6158 use_generic = 1;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6159 generic = p->function;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6160 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6161 } while ((++p)->offset == offset);
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6162 if (specific && !use_generic)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6163 *ptr = specific;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6164 else
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6165 *ptr = generic;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6166 return p;
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6167 }
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6168
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6169 /* 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
6170 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
6171 static int
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6172 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
6173 {
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6174 slotdef **pp = (slotdef **)data;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6175
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6176 for (; *pp; pp++)
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6177 update_one_slot(type, *pp);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6178 return 0;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6179 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6180
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6181 /* 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
6182 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
6183 static int
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6184 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
6185 {
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6186 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6187 int c = a->offset - b->offset;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6188 if (c != 0)
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6189 return c;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6190 else
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
6191 /* Cannot use a-b, as this gives off_t,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
6192 which may lose precision when converted to int. */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
6193 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
6194 }
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6195
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6196 /* 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
6197 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
6198 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
6199 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
6200 {
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6201 slotdef *p;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6202 static int initialized = 0;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6203
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6204 if (initialized)
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6205 return;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6206 for (p = slotdefs; p->name; p++) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
6207 p->name_strobj = PyString_InternFromString(p->name);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6208 if (!p->name_strobj)
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6209 Py_FatalError("Out of memory interning slotdef names");
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6210 }
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6211 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6212 slotdef_cmp);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6213 initialized = 1;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6214 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6215
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6216 /* 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
6217 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
6218 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
6219 {
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6220 slotdef *ptrs[MAX_EQUIV];
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6221 slotdef *p;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6222 slotdef **pp;
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6223 int offset;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6224
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
6225 /* Clear the VALID_VERSION flag of 'type' and all its
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
6226 subclasses. This could possibly be unified with the
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
6227 update_subclasses() recursion below, but carefully:
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
6228 they each have their own conditions on which to stop
387d48c3a0b3 Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44661
diff changeset
6229 recursing into subclasses. */
47358
93fe913fae55 #2989: add PyType_Modified().
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
6230 PyType_Modified(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
6231
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6232 init_slotdefs();
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6233 pp = ptrs;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6234 for (p = slotdefs; p->name; p++) {
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6235 /* XXX assume name is interned! */
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6236 if (p->name_strobj == name)
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6237 *pp++ = p;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6238 }
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6239 *pp = NULL;
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6240 for (pp = ptrs; *pp; pp++) {
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6241 p = *pp;
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6242 offset = p->offset;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6243 while (p > slotdefs && (p-1)->offset == offset)
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6244 --p;
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
6245 *pp = p;
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6246 }
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6247 if (ptrs[0] == NULL)
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6248 return 0; /* Not an attribute that affects any slots */
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6249 return update_subclasses(type, name,
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6250 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
6251 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
6252
22834
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6253 /* 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
6254 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
6255 dict. */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
6256 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
6257 fixup_slot_dispatchers(PyTypeObject *type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
6258 {
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
6259 slotdef *p;
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6260
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
6261 init_slotdefs();
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 for (p = slotdefs; p->name; )
0908729e94e0 A much revised version of SF patch 514662, by Naofumi Honda. This
Guido van Rossum <guido@python.org>
parents: 22804
diff changeset
6263 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
6264 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6265
26421
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6266 static void
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6267 update_all_slots(PyTypeObject* type)
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6268 {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6269 slotdef *p;
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6270
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6271 init_slotdefs();
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6272 for (p = slotdefs; p->name; p++) {
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6273 /* update_slot returns int but can't actually fail */
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6274 update_slot(type, p->name_strobj);
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6275 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6276 }
9d6aed2e60e2 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 26410
diff changeset
6277
28403
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6278 /* 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
6279 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
6280 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
6281
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6282 static int
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6283 update_subclasses(PyTypeObject *type, PyObject *name,
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6284 update_callback callback, void *data)
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6285 {
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6286 if (callback(type, data) < 0)
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6287 return -1;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6288 return recurse_down_subclasses(type, name, callback, data);
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6289 }
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 recurse_down_subclasses(PyTypeObject *type, PyObject *name,
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6293 update_callback callback, void *data)
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6294 {
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6295 PyTypeObject *subclass;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6296 PyObject *ref, *subclasses, *dict;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
6297 Py_ssize_t i, n;
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 subclasses = type->tp_subclasses;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6300 if (subclasses == NULL)
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6301 return 0;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6302 assert(PyList_Check(subclasses));
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6303 n = PyList_GET_SIZE(subclasses);
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6304 for (i = 0; i < n; i++) {
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6305 ref = PyList_GET_ITEM(subclasses, i);
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6306 assert(PyWeakref_CheckRef(ref));
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6307 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6308 assert(subclass != NULL);
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6309 if ((PyObject *)subclass == Py_None)
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6310 continue;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6311 assert(PyType_Check(subclass));
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6312 /* Avoid recursing down into unaffected classes */
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6313 dict = subclass->tp_dict;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6314 if (dict != NULL && PyDict_Check(dict) &&
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6315 PyDict_GetItem(dict, name) != NULL)
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6316 continue;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6317 if (update_subclasses(subclass, name, callback, data) < 0)
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6318 return -1;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6319 }
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6320 return 0;
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6321 }
63ed85290413 Refactoring: rename update_these_slots() into update_subclasses() and
Guido van Rossum <guido@python.org>
parents: 28396
diff changeset
6322
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6323 /* 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
6324 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
6325 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
6326 corresponding descriptors are added in the type's tp_dict dictionary
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6327 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
6328 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
6329 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
6330 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
6331 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
6332
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6333 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
6334 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
6335 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
6336 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
6337 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
6338 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
6339 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
6340 wins.
23830
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6341
ade1d56109a8 Hopefully this addresses the remaining issues of SF bugs 459235 and
Guido van Rossum <guido@python.org>
parents: 23756
diff changeset
6342 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
6343 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
6344 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
6345 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
6346 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
6347 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
6348 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
6349 (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
6350 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
6351 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
6352
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6353 static int
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6354 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
6355 {
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6356 PyObject *dict = type->tp_dict;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6357 slotdef *p;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6358 PyObject *descr;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6359 void **ptr;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6360
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6361 init_slotdefs();
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6362 for (p = slotdefs; p->name; p++) {
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6363 if (p->wrapper == NULL)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6364 continue;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6365 ptr = slotptr(type, p->offset);
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6366 if (!ptr || !*ptr)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6367 continue;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6368 if (PyDict_GetItem(dict, p->name_strobj))
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6369 continue;
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6370 if (*ptr == PyObject_HashNotImplemented) {
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6371 /* Classes may prevent the inheritance of the tp_hash
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6372 slot by storing PyObject_HashNotImplemented in it. Make it
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6373 visible as a None value for the __hash__ attribute. */
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6374 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6375 return -1;
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6376 }
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6377 else {
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6378 descr = PyDescr_NewWrapper(type, p, *ptr);
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6379 if (descr == NULL)
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6380 return -1;
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6381 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6382 return -1;
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6383 Py_DECREF(descr);
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47822
diff changeset
6384 }
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6385 }
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6386 if (type->tp_new != NULL) {
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6387 if (add_tp_new_wrapper(type) < 0)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6388 return -1;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6389 }
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6390 return 0;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6391 }
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
6392
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6393
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6394 /* Cooperative 'super' */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6395
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6396 typedef struct {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6397 PyObject_HEAD
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
6398 PyTypeObject *type;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6399 PyObject *obj;
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6400 PyTypeObject *obj_type;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6401 } superobject;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6402
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
6403 static PyMemberDef super_members[] = {
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
6404 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
6405 "the class invoking super()"},
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
6406 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
6407 "the instance invoking super(); may be None"},
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6408 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
30722
651fc8d98d99 Fix a bunch of typos in documentation, docstrings and comments.
Walter Dörwald <walter@livinglogic.de>
parents: 30683
diff changeset
6409 "the type of the instance invoking super(); may be None"},
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6410 {0}
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6411 };
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6412
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6413 static void
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6414 super_dealloc(PyObject *self)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6415 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6416 superobject *su = (superobject *)self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6417
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6418 _PyObject_GC_UNTRACK(self);
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6419 Py_XDECREF(su->obj);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6420 Py_XDECREF(su->type);
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6421 Py_XDECREF(su->obj_type);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6422 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
6423 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6424
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6425 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
6426 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
6427 {
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6428 superobject *su = (superobject *)self;
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6429
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6430 if (su->obj_type)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
6431 return PyString_FromFormat(
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
6432 "<super: <class '%s'>, <%s object>>",
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6433 su->type ? su->type->tp_name : "NULL",
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6434 su->obj_type->tp_name);
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6435 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
6436 return PyString_FromFormat(
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
6437 "<super: <class '%s'>, NULL>",
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6438 su->type ? su->type->tp_name : "NULL");
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6439 }
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6440
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6441 static PyObject *
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6442 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
6443 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6444 superobject *su = (superobject *)self;
28638
9be4103807f5 - super() no longer ignores data descriptors, except __class__. See
Guido van Rossum <guido@python.org>
parents: 28625
diff changeset
6445 int skip = su->obj_type == NULL;
9be4103807f5 - super() no longer ignores data descriptors, except __class__. See
Guido van Rossum <guido@python.org>
parents: 28625
diff changeset
6446
9be4103807f5 - super() no longer ignores data descriptors, except __class__. See
Guido van Rossum <guido@python.org>
parents: 28625
diff changeset
6447 if (!skip) {
9be4103807f5 - super() no longer ignores data descriptors, except __class__. See
Guido van Rossum <guido@python.org>
parents: 28625
diff changeset
6448 /* We want __class__ to return the class of the super object
9be4103807f5 - super() no longer ignores data descriptors, except __class__. See
Guido van Rossum <guido@python.org>
parents: 28625
diff changeset
6449 (i.e. super, or a subclass), not the class of su->obj. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
6450 skip = (PyString_Check(name) &&
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
6451 PyString_GET_SIZE(name) == 9 &&
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
6452 strcmp(PyString_AS_STRING(name), "__class__") == 0);
28638
9be4103807f5 - super() no longer ignores data descriptors, except __class__. See
Guido van Rossum <guido@python.org>
parents: 28625
diff changeset
6453 }
9be4103807f5 - super() no longer ignores data descriptors, except __class__. See
Guido van Rossum <guido@python.org>
parents: 28625
diff changeset
6454
9be4103807f5 - super() no longer ignores data descriptors, except __class__. See
Guido van Rossum <guido@python.org>
parents: 28625
diff changeset
6455 if (!skip) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
6456 PyObject *mro, *res, *tmp, *dict;
22770
314e1416aa5d SF patch 537536 by Phillip J. Eby, fix for SF bug 535444, super()
Guido van Rossum <guido@python.org>
parents: 22713
diff changeset
6457 PyTypeObject *starttype;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6458 descrgetfunc f;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36169
diff changeset
6459 Py_ssize_t i, n;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6460
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6461 starttype = su->obj_type;
22770
314e1416aa5d SF patch 537536 by Phillip J. Eby, fix for SF bug 535444, super()
Guido van Rossum <guido@python.org>
parents: 22713
diff changeset
6462 mro = starttype->tp_mro;
314e1416aa5d SF patch 537536 by Phillip J. Eby, fix for SF bug 535444, super()
Guido van Rossum <guido@python.org>
parents: 22713
diff changeset
6463
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6464 if (mro == NULL)
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6465 n = 0;
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6466 else {
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6467 assert(PyTuple_Check(mro));
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6468 n = PyTuple_GET_SIZE(mro);
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6469 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6470 for (i = 0; i < n; i++) {
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
6471 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6472 break;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6473 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6474 i++;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6475 res = NULL;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6476 for (; i < n; i++) {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6477 tmp = PyTuple_GET_ITEM(mro, i);
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
6478 if (PyType_Check(tmp))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
6479 dict = ((PyTypeObject *)tmp)->tp_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
6480 else if (PyClass_Check(tmp))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
6481 dict = ((PyClassObject *)tmp)->cl_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
6482 else
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
6483 continue;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
6484 res = PyDict_GetItem(dict, name);
28640
4824aab12549 Sigh. The crucial change was still missing from the previous
Guido van Rossum <guido@python.org>
parents: 28638
diff changeset
6485 if (res != NULL) {
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6486 Py_INCREF(res);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6487 f = Py_TYPE(res)->tp_descr_get;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6488 if (f != NULL) {
31800
37dc8c80ca7e Ensure super() lookup of descriptor from classmethod works (SF #743627)
Phillip J. Eby <pje@telecommunity.com>
parents: 31477
diff changeset
6489 tmp = f(res,
37dc8c80ca7e Ensure super() lookup of descriptor from classmethod works (SF #743627)
Phillip J. Eby <pje@telecommunity.com>
parents: 31477
diff changeset
6490 /* Only pass 'obj' param if
37dc8c80ca7e Ensure super() lookup of descriptor from classmethod works (SF #743627)
Phillip J. Eby <pje@telecommunity.com>
parents: 31477
diff changeset
6491 this is instance-mode super
37dc8c80ca7e Ensure super() lookup of descriptor from classmethod works (SF #743627)
Phillip J. Eby <pje@telecommunity.com>
parents: 31477
diff changeset
6492 (See SF ID #743627)
37dc8c80ca7e Ensure super() lookup of descriptor from classmethod works (SF #743627)
Phillip J. Eby <pje@telecommunity.com>
parents: 31477
diff changeset
6493 */
31813
8184c677713f Get rid of gcc warning.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31800
diff changeset
6494 (su->obj == (PyObject *)
8184c677713f Get rid of gcc warning.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31800
diff changeset
6495 su->obj_type
31800
37dc8c80ca7e Ensure super() lookup of descriptor from classmethod works (SF #743627)
Phillip J. Eby <pje@telecommunity.com>
parents: 31477
diff changeset
6496 ? (PyObject *)NULL
37dc8c80ca7e Ensure super() lookup of descriptor from classmethod works (SF #743627)
Phillip J. Eby <pje@telecommunity.com>
parents: 31477
diff changeset
6497 : su->obj),
22774
02fddf7c25c2 Fold some long lines. Delete blank initial line.
Guido van Rossum <guido@python.org>
parents: 22770
diff changeset
6498 (PyObject *)starttype);
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6499 Py_DECREF(res);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6500 res = tmp;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6501 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6502 return res;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6503 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6504 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6505 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6506 return PyObject_GenericGetAttr(self, name);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6507 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6508
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6509 static PyTypeObject *
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6510 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
6511 {
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6512 /* Check that a super() call makes sense. Return a type object.
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6513
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6514 obj can be a new-style class, or an instance of one:
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6515
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6516 - If it is a class, it must be a subclass of 'type'. This case is
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6517 used for class methods; the return value is obj.
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6518
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6519 - If it is an instance, it must be an instance of 'type'. This is
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6520 the normal case; the return value is obj.__class__.
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6521
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6522 But... when obj is an instance, we want to allow for the case where
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6523 Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6524 This will allow using super() with a proxy for obj.
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6525 */
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6526
27942
9255fbde43f3 The recent changes to super(), in particular supercheck(), broke when
Guido van Rossum <guido@python.org>
parents: 27866
diff changeset
6527 /* Check for first bullet above (special case) */
9255fbde43f3 The recent changes to super(), in particular supercheck(), broke when
Guido van Rossum <guido@python.org>
parents: 27866
diff changeset
6528 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
9255fbde43f3 The recent changes to super(), in particular supercheck(), broke when
Guido van Rossum <guido@python.org>
parents: 27866
diff changeset
6529 Py_INCREF(obj);
9255fbde43f3 The recent changes to super(), in particular supercheck(), broke when
Guido van Rossum <guido@python.org>
parents: 27866
diff changeset
6530 return (PyTypeObject *)obj;
9255fbde43f3 The recent changes to super(), in particular supercheck(), broke when
Guido van Rossum <guido@python.org>
parents: 27866
diff changeset
6531 }
9255fbde43f3 The recent changes to super(), in particular supercheck(), broke when
Guido van Rossum <guido@python.org>
parents: 27866
diff changeset
6532
9255fbde43f3 The recent changes to super(), in particular supercheck(), broke when
Guido van Rossum <guido@python.org>
parents: 27866
diff changeset
6533 /* Normal case */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6534 if (PyType_IsSubtype(Py_TYPE(obj), type)) {
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6535 Py_INCREF(Py_TYPE(obj));
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6536 return Py_TYPE(obj);
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6537 }
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6538 else {
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6539 /* Try the slow way */
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6540 static PyObject *class_str = NULL;
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6541 PyObject *class_attr;
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6542
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6543 if (class_str == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
6544 class_str = PyString_FromString("__class__");
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6545 if (class_str == NULL)
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6546 return NULL;
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6547 }
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6548
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6549 class_attr = PyObject_GetAttr(obj, class_str);
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6550
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6551 if (class_attr != NULL &&
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6552 PyType_Check(class_attr) &&
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6553 (PyTypeObject *)class_attr != Py_TYPE(obj))
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6554 {
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6555 int ok = PyType_IsSubtype(
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6556 (PyTypeObject *)class_attr, type);
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6557 if (ok)
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6558 return (PyTypeObject *)class_attr;
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6559 }
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6560
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6561 if (class_attr == NULL)
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6562 PyErr_Clear();
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6563 else
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6564 Py_DECREF(class_attr);
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6565 }
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6566
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6567 PyErr_SetString(PyExc_TypeError,
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6568 "super(type, obj): "
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6569 "obj must be an instance or subtype of type");
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6570 return NULL;
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6571 }
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6572
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6573 static PyObject *
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6574 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
6575 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6576 superobject *su = (superobject *)self;
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
6577 superobject *newobj;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6578
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6579 if (obj == NULL || obj == Py_None || su->obj != NULL) {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6580 /* Not binding to an object, or already bound */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6581 Py_INCREF(self);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6582 return self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6583 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6584 if (Py_TYPE(su) != &PySuper_Type)
34958
c91e5e31c5ff Fixed a quite misleading comment: a "not" should not have been there.
Armin Rigo <arigo@tunes.org>
parents: 34668
diff changeset
6585 /* If su is an instance of a (strict) subclass of super,
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6586 call its type */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43958
diff changeset
6587 return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6588 su->type, obj, NULL);
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6589 else {
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6590 /* Inline the common case */
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6591 PyTypeObject *obj_type = supercheck(su->type, obj);
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6592 if (obj_type == NULL)
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6593 return NULL;
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
6594 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6595 NULL, NULL);
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
6596 if (newobj == NULL)
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6597 return NULL;
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6598 Py_INCREF(su->type);
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6599 Py_INCREF(obj);
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
6600 newobj->type = su->type;
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
6601 newobj->obj = obj;
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
6602 newobj->obj_type = obj_type;
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
6603 return (PyObject *)newobj;
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
6604 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6605 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6606
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6607 static int
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6608 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
6609 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6610 superobject *su = (superobject *)self;
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
6611 PyTypeObject *type;
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
6612 PyObject *obj = NULL;
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6613 PyTypeObject *obj_type = NULL;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6614
39921
49fbddfb1217 Patch #1567691: super() and new.instancemethod() now don't accept
Georg Brandl <georg@python.org>
parents: 39464
diff changeset
6615 if (!_PyArg_NoKeywords("super", kwds))
49fbddfb1217 Patch #1567691: super() and new.instancemethod() now don't accept
Georg Brandl <georg@python.org>
parents: 39464
diff changeset
6616 return -1;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6617 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6618 return -1;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6619 if (obj == Py_None)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6620 obj = NULL;
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6621 if (obj != NULL) {
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6622 obj_type = supercheck(type, obj);
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6623 if (obj_type == NULL)
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6624 return -1;
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6625 Py_INCREF(obj);
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6626 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6627 Py_INCREF(type);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6628 su->type = type;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6629 su->obj = obj;
27831
6b5696541ba4 Implement another useful feature for proxies: in super(X, x), x may
Guido van Rossum <guido@python.org>
parents: 27821
diff changeset
6630 su->obj_type = obj_type;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6631 return 0;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6632 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6633
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23830
diff changeset
6634 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
6635 "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
6636 "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
6637 "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
6638 "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
6639 "class C(B):\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6640 " def meth(self, arg):\n"
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6641 " 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
6642
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6643 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
6644 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
6645 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6646 superobject *su = (superobject *)self;
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
6647
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
6648 Py_VISIT(su->obj);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
6649 Py_VISIT(su->type);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37748
diff changeset
6650 Py_VISIT(su->obj_type);
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6651
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6652 return 0;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6653 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6654
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6655 PyTypeObject PySuper_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41816
diff changeset
6656 PyVarObject_HEAD_INIT(&PyType_Type, 0)
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6657 "super", /* tp_name */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6658 sizeof(superobject), /* tp_basicsize */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6659 0, /* tp_itemsize */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6660 /* methods */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6661 super_dealloc, /* tp_dealloc */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6662 0, /* tp_print */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6663 0, /* tp_getattr */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6664 0, /* tp_setattr */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6665 0, /* tp_compare */
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6666 super_repr, /* tp_repr */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6667 0, /* tp_as_number */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6668 0, /* tp_as_sequence */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6669 0, /* tp_as_mapping */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6670 0, /* tp_hash */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6671 0, /* tp_call */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6672 0, /* tp_str */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6673 super_getattro, /* tp_getattro */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6674 0, /* tp_setattro */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6675 0, /* tp_as_buffer */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6676 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
6677 Py_TPFLAGS_BASETYPE, /* tp_flags */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6678 super_doc, /* tp_doc */
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6679 super_traverse, /* tp_traverse */
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6680 0, /* tp_clear */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6681 0, /* tp_richcompare */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6682 0, /* tp_weaklistoffset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6683 0, /* tp_iter */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6684 0, /* tp_iternext */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6685 0, /* tp_methods */
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
6686 super_members, /* tp_members */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6687 0, /* tp_getset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6688 0, /* tp_base */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6689 0, /* tp_dict */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6690 super_descr_get, /* tp_descr_get */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6691 0, /* tp_descr_set */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6692 0, /* tp_dictoffset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6693 super_init, /* tp_init */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6694 PyType_GenericAlloc, /* tp_alloc */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
6695 PyType_GenericNew, /* tp_new */
40974
3cabbfa43984 whitespace normalization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40908
diff changeset
6696 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
6697 };