blob: c6d6fc8ec3d8741243a6f1b692a7f53586c7806d [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Type object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Guido van Rossumcd16bf62007-06-13 18:07:49 +00004#include "frameobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum9923ffe2002-06-04 19:52:53 +00007#include <ctype.h>
8
Christian Heimesa62da1d2008-01-12 19:39:10 +00009
10/* Support type attribute cache */
11
12/* The cache can keep references to the names alive for longer than
13 they normally would. This is why the maximum size is limited to
14 MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
15 strings are used as attribute names. */
16#define MCACHE_MAX_ATTR_SIZE 100
17#define MCACHE_SIZE_EXP 10
18#define MCACHE_HASH(version, name_hash) \
19 (((unsigned int)(version) * (unsigned int)(name_hash)) \
20 >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
21#define MCACHE_HASH_METHOD(type, name) \
22 MCACHE_HASH((type)->tp_version_tag, \
Georg Brandl1bcf35a2008-05-25 09:32:09 +000023 ((PyUnicodeObject *)(name))->hash)
Christian Heimesa62da1d2008-01-12 19:39:10 +000024#define MCACHE_CACHEABLE_NAME(name) \
Georg Brandl1bcf35a2008-05-25 09:32:09 +000025 PyUnicode_CheckExact(name) && \
26 PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
Christian Heimesa62da1d2008-01-12 19:39:10 +000027
28struct method_cache_entry {
29 unsigned int version;
30 PyObject *name; /* reference to exactly a str or None */
31 PyObject *value; /* borrowed */
32};
33
34static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
35static unsigned int next_version_tag = 0;
Christian Heimes26855632008-01-27 23:50:43 +000036
37unsigned int
38PyType_ClearCache(void)
39{
40 Py_ssize_t i;
41 unsigned int cur_version_tag = next_version_tag - 1;
42
43 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
44 method_cache[i].version = 0;
45 Py_CLEAR(method_cache[i].name);
46 method_cache[i].value = NULL;
47 }
48 next_version_tag = 0;
49 /* mark all version tags as invalid */
Georg Brandlf08a9dd2008-06-10 16:57:31 +000050 PyType_Modified(&PyBaseObject_Type);
Christian Heimes26855632008-01-27 23:50:43 +000051 return cur_version_tag;
52}
Christian Heimesa62da1d2008-01-12 19:39:10 +000053
Georg Brandlf08a9dd2008-06-10 16:57:31 +000054void
55PyType_Modified(PyTypeObject *type)
Christian Heimesa62da1d2008-01-12 19:39:10 +000056{
57 /* Invalidate any cached data for the specified type and all
58 subclasses. This function is called after the base
59 classes, mro, or attributes of the type are altered.
60
61 Invariants:
62
63 - Py_TPFLAGS_VALID_VERSION_TAG is never set if
64 Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
65 objects coming from non-recompiled extension modules)
66
67 - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
68 it must first be set on all super types.
69
70 This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
71 type (so it must first clear it on all subclasses). The
72 tp_version_tag value is meaningless unless this flag is set.
73 We don't assign new version tags eagerly, but only as
74 needed.
75 */
76 PyObject *raw, *ref;
77 Py_ssize_t i, n;
78
Christian Heimes412dc9c2008-01-27 18:55:54 +000079 if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
Christian Heimesa62da1d2008-01-12 19:39:10 +000080 return;
81
82 raw = type->tp_subclasses;
83 if (raw != NULL) {
84 n = PyList_GET_SIZE(raw);
85 for (i = 0; i < n; i++) {
86 ref = PyList_GET_ITEM(raw, i);
87 ref = PyWeakref_GET_OBJECT(ref);
88 if (ref != Py_None) {
Georg Brandlf08a9dd2008-06-10 16:57:31 +000089 PyType_Modified((PyTypeObject *)ref);
Christian Heimesa62da1d2008-01-12 19:39:10 +000090 }
91 }
92 }
93 type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
94}
95
96static void
97type_mro_modified(PyTypeObject *type, PyObject *bases) {
98 /*
99 Check that all base classes or elements of the mro of type are
100 able to be cached. This function is called after the base
101 classes or mro of the type are altered.
102
103 Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
104 inherits from an old-style class, either directly or if it
105 appears in the MRO of a new-style class. No support either for
106 custom MROs that include types that are not officially super
107 types.
108
109 Called from mro_internal, which will subsequently be called on
110 each subclass when their mro is recursively updated.
111 */
112 Py_ssize_t i, n;
113 int clear = 0;
114
Christian Heimes412dc9c2008-01-27 18:55:54 +0000115 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
Christian Heimesa62da1d2008-01-12 19:39:10 +0000116 return;
117
118 n = PyTuple_GET_SIZE(bases);
119 for (i = 0; i < n; i++) {
120 PyObject *b = PyTuple_GET_ITEM(bases, i);
121 PyTypeObject *cls;
122
123 if (!PyType_Check(b) ) {
124 clear = 1;
125 break;
126 }
127
128 cls = (PyTypeObject *)b;
129
130 if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
131 !PyType_IsSubtype(type, cls)) {
132 clear = 1;
133 break;
134 }
135 }
136
137 if (clear)
138 type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
139 Py_TPFLAGS_VALID_VERSION_TAG);
140}
141
142static int
143assign_version_tag(PyTypeObject *type)
144{
145 /* Ensure that the tp_version_tag is valid and set
146 Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
147 must first be done on all super classes. Return 0 if this
148 cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
149 */
150 Py_ssize_t i, n;
151 PyObject *bases;
152
153 if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
154 return 1;
155 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
156 return 0;
157 if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
158 return 0;
159
160 type->tp_version_tag = next_version_tag++;
161 /* for stress-testing: next_version_tag &= 0xFF; */
162
163 if (type->tp_version_tag == 0) {
164 /* wrap-around or just starting Python - clear the whole
165 cache by filling names with references to Py_None.
166 Values are also set to NULL for added protection, as they
167 are borrowed reference */
168 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
169 method_cache[i].value = NULL;
170 Py_XDECREF(method_cache[i].name);
171 method_cache[i].name = Py_None;
172 Py_INCREF(Py_None);
173 }
174 /* mark all version tags as invalid */
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000175 PyType_Modified(&PyBaseObject_Type);
Christian Heimesa62da1d2008-01-12 19:39:10 +0000176 return 1;
177 }
178 bases = type->tp_bases;
179 n = PyTuple_GET_SIZE(bases);
180 for (i = 0; i < n; i++) {
181 PyObject *b = PyTuple_GET_ITEM(bases, i);
182 assert(PyType_Check(b));
183 if (!assign_version_tag((PyTypeObject *)b))
184 return 0;
185 }
186 type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
187 return 1;
188}
189
190
Guido van Rossum6f799372001-09-20 20:46:19 +0000191static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000192 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
193 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
194 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
Guido van Rossum9676b222001-08-17 20:32:36 +0000195 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000196 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
197 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
198 {"__dictoffset__", T_LONG,
199 offsetof(PyTypeObject, tp_dictoffset), READONLY},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
201 {0}
202};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +0000205type_name(PyTypeObject *type, void *context)
206{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000207 const char *s;
Guido van Rossumc3542212001-08-16 09:18:56 +0000208
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000209 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Guido van Rossume5c691a2003-03-07 15:13:17 +0000210 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
Tim Petersea7f75d2002-12-07 21:39:16 +0000211
Georg Brandlc255c7b2006-02-20 22:27:28 +0000212 Py_INCREF(et->ht_name);
213 return et->ht_name;
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000214 }
215 else {
216 s = strrchr(type->tp_name, '.');
217 if (s == NULL)
218 s = type->tp_name;
219 else
220 s++;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000221 return PyUnicode_FromString(s);
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000222 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000223}
224
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000225static int
226type_set_name(PyTypeObject *type, PyObject *value, void *context)
227{
Guido van Rossume5c691a2003-03-07 15:13:17 +0000228 PyHeapTypeObject* et;
Neal Norwitz80e7f272007-08-26 06:45:23 +0000229 char *tp_name;
Guido van Rossume845c0f2007-11-02 23:07:07 +0000230 PyObject *tmp;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000231
232 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
233 PyErr_Format(PyExc_TypeError,
234 "can't set %s.__name__", type->tp_name);
235 return -1;
236 }
237 if (!value) {
238 PyErr_Format(PyExc_TypeError,
239 "can't delete %s.__name__", type->tp_name);
240 return -1;
241 }
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000242 if (!PyUnicode_Check(value)) {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000243 PyErr_Format(PyExc_TypeError,
244 "can only assign string to %s.__name__, not '%s'",
Christian Heimes90aa7642007-12-19 02:45:37 +0000245 type->tp_name, Py_TYPE(value)->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000246 return -1;
247 }
Guido van Rossume845c0f2007-11-02 23:07:07 +0000248
249 /* Check absence of null characters */
250 tmp = PyUnicode_FromStringAndSize("\0", 1);
251 if (tmp == NULL)
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000252 return -1;
Guido van Rossume845c0f2007-11-02 23:07:07 +0000253 if (PyUnicode_Contains(value, tmp) != 0) {
254 Py_DECREF(tmp);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000255 PyErr_Format(PyExc_ValueError,
256 "__name__ must not contain null bytes");
257 return -1;
258 }
Guido van Rossume845c0f2007-11-02 23:07:07 +0000259 Py_DECREF(tmp);
260
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000261 tp_name = _PyUnicode_AsString(value);
Guido van Rossume845c0f2007-11-02 23:07:07 +0000262 if (tp_name == NULL)
263 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000264
Guido van Rossume5c691a2003-03-07 15:13:17 +0000265 et = (PyHeapTypeObject*)type;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000266
267 Py_INCREF(value);
268
Georg Brandlc255c7b2006-02-20 22:27:28 +0000269 Py_DECREF(et->ht_name);
270 et->ht_name = value;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000271
Neal Norwitz80e7f272007-08-26 06:45:23 +0000272 type->tp_name = tp_name;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000273
274 return 0;
275}
276
Guido van Rossumc3542212001-08-16 09:18:56 +0000277static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000278type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000279{
Guido van Rossumc3542212001-08-16 09:18:56 +0000280 PyObject *mod;
281 char *s;
282
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000283 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
284 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Anthony Baxter3ecdb252004-06-11 14:41:18 +0000285 if (!mod) {
286 PyErr_Format(PyExc_AttributeError, "__module__");
287 return 0;
288 }
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000289 Py_XINCREF(mod);
Guido van Rossumc3542212001-08-16 09:18:56 +0000290 return mod;
291 }
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000292 else {
293 s = strrchr(type->tp_name, '.');
294 if (s != NULL)
Martin v. Löwis5b222132007-06-10 09:51:05 +0000295 return PyUnicode_FromStringAndSize(
Thomas Wouters89f507f2006-12-13 04:49:30 +0000296 type->tp_name, (Py_ssize_t)(s - type->tp_name));
Georg Brandl1a3284e2007-12-02 09:40:06 +0000297 return PyUnicode_FromString("builtins");
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000298 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000299}
300
Guido van Rossum3926a632001-09-25 16:25:58 +0000301static int
302type_set_module(PyTypeObject *type, PyObject *value, void *context)
303{
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000304 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
Guido van Rossum3926a632001-09-25 16:25:58 +0000305 PyErr_Format(PyExc_TypeError,
306 "can't set %s.__module__", type->tp_name);
307 return -1;
308 }
309 if (!value) {
310 PyErr_Format(PyExc_TypeError,
311 "can't delete %s.__module__", type->tp_name);
312 return -1;
313 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000314
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000315 PyType_Modified(type);
Christian Heimesa62da1d2008-01-12 19:39:10 +0000316
Guido van Rossum3926a632001-09-25 16:25:58 +0000317 return PyDict_SetItemString(type->tp_dict, "__module__", value);
318}
319
Tim Peters6d6c1a32001-08-02 04:15:00 +0000320static PyObject *
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000321type_abstractmethods(PyTypeObject *type, void *context)
322{
323 PyObject *mod = PyDict_GetItemString(type->tp_dict,
324 "__abstractmethods__");
325 if (!mod) {
326 PyErr_Format(PyExc_AttributeError, "__abstractmethods__");
327 return NULL;
328 }
329 Py_XINCREF(mod);
330 return mod;
331}
332
333static int
334type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
335{
336 /* __abstractmethods__ should only be set once on a type, in
337 abc.ABCMeta.__new__, so this function doesn't do anything
338 special to update subclasses.
339 */
340 int res = PyDict_SetItemString(type->tp_dict,
341 "__abstractmethods__", value);
342 if (res == 0) {
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000343 PyType_Modified(type);
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000344 if (value && PyObject_IsTrue(value)) {
345 type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
346 }
347 else {
348 type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
349 }
350 }
351 return res;
352}
353
354static PyObject *
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000355type_get_bases(PyTypeObject *type, void *context)
356{
357 Py_INCREF(type->tp_bases);
358 return type->tp_bases;
359}
360
361static PyTypeObject *best_base(PyObject *);
362static int mro_internal(PyTypeObject *);
363static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
364static int add_subclass(PyTypeObject*, PyTypeObject*);
365static void remove_subclass(PyTypeObject *, PyTypeObject *);
366static void update_all_slots(PyTypeObject *);
367
Guido van Rossum8d24ee92003-03-24 23:49:49 +0000368typedef int (*update_callback)(PyTypeObject *, void *);
369static int update_subclasses(PyTypeObject *type, PyObject *name,
370 update_callback callback, void *data);
371static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
372 update_callback callback, void *data);
373
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000374static int
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000375mro_subclasses(PyTypeObject *type, PyObject* temp)
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000376{
377 PyTypeObject *subclass;
378 PyObject *ref, *subclasses, *old_mro;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379 Py_ssize_t i, n;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000380
381 subclasses = type->tp_subclasses;
382 if (subclasses == NULL)
383 return 0;
384 assert(PyList_Check(subclasses));
385 n = PyList_GET_SIZE(subclasses);
386 for (i = 0; i < n; i++) {
387 ref = PyList_GET_ITEM(subclasses, i);
388 assert(PyWeakref_CheckRef(ref));
389 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
390 assert(subclass != NULL);
391 if ((PyObject *)subclass == Py_None)
392 continue;
393 assert(PyType_Check(subclass));
394 old_mro = subclass->tp_mro;
395 if (mro_internal(subclass) < 0) {
396 subclass->tp_mro = old_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000397 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000398 }
399 else {
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000400 PyObject* tuple;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000401 tuple = PyTuple_Pack(2, subclass, old_mro);
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000402 Py_DECREF(old_mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000403 if (!tuple)
404 return -1;
405 if (PyList_Append(temp, tuple) < 0)
406 return -1;
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000407 Py_DECREF(tuple);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000408 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000409 if (mro_subclasses(subclass, temp) < 0)
410 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000411 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000412 return 0;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000413}
414
415static int
416type_set_bases(PyTypeObject *type, PyObject *value, void *context)
417{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000418 Py_ssize_t i;
419 int r = 0;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000420 PyObject *ob, *temp;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000421 PyTypeObject *new_base, *old_base;
422 PyObject *old_bases, *old_mro;
423
424 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
425 PyErr_Format(PyExc_TypeError,
426 "can't set %s.__bases__", type->tp_name);
427 return -1;
428 }
429 if (!value) {
430 PyErr_Format(PyExc_TypeError,
431 "can't delete %s.__bases__", type->tp_name);
432 return -1;
433 }
434 if (!PyTuple_Check(value)) {
435 PyErr_Format(PyExc_TypeError,
436 "can only assign tuple to %s.__bases__, not %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000437 type->tp_name, Py_TYPE(value)->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000438 return -1;
439 }
Guido van Rossum3bbc0ee2002-12-13 17:49:38 +0000440 if (PyTuple_GET_SIZE(value) == 0) {
441 PyErr_Format(PyExc_TypeError,
442 "can only assign non-empty tuple to %s.__bases__, not ()",
443 type->tp_name);
444 return -1;
445 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000446 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
447 ob = PyTuple_GET_ITEM(value, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +0000448 if (!PyType_Check(ob)) {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000449 PyErr_Format(
450 PyExc_TypeError,
451 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
Christian Heimes90aa7642007-12-19 02:45:37 +0000452 type->tp_name, Py_TYPE(ob)->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000453 return -1;
454 }
Michael W. Hudsoncaf17be2002-11-27 10:24:44 +0000455 if (PyType_Check(ob)) {
456 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
457 PyErr_SetString(PyExc_TypeError,
458 "a __bases__ item causes an inheritance cycle");
459 return -1;
460 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000461 }
462 }
463
464 new_base = best_base(value);
465
466 if (!new_base) {
467 return -1;
468 }
469
470 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
471 return -1;
472
473 Py_INCREF(new_base);
474 Py_INCREF(value);
475
476 old_bases = type->tp_bases;
477 old_base = type->tp_base;
478 old_mro = type->tp_mro;
479
480 type->tp_bases = value;
481 type->tp_base = new_base;
482
483 if (mro_internal(type) < 0) {
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000484 goto bail;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000485 }
486
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000487 temp = PyList_New(0);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000488 if (!temp)
489 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000490
491 r = mro_subclasses(type, temp);
492
493 if (r < 0) {
494 for (i = 0; i < PyList_Size(temp); i++) {
495 PyTypeObject* cls;
496 PyObject* mro;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000497 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
498 "", 2, 2, &cls, &mro);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000499 Py_INCREF(mro);
500 ob = cls->tp_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000501 cls->tp_mro = mro;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000502 Py_DECREF(ob);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000503 }
504 Py_DECREF(temp);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000505 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000506 }
507
508 Py_DECREF(temp);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000509
510 /* any base that was in __bases__ but now isn't, we
Raymond Hettingera8285862002-12-14 17:17:56 +0000511 need to remove |type| from its tp_subclasses.
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000512 conversely, any class now in __bases__ that wasn't
Raymond Hettingera8285862002-12-14 17:17:56 +0000513 needs to have |type| added to its subclasses. */
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000514
515 /* for now, sod that: just remove from all old_bases,
516 add to all new_bases */
517
518 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
519 ob = PyTuple_GET_ITEM(old_bases, i);
520 if (PyType_Check(ob)) {
521 remove_subclass(
522 (PyTypeObject*)ob, type);
523 }
524 }
525
526 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
527 ob = PyTuple_GET_ITEM(value, i);
528 if (PyType_Check(ob)) {
529 if (add_subclass((PyTypeObject*)ob, type) < 0)
530 r = -1;
531 }
532 }
533
534 update_all_slots(type);
535
536 Py_DECREF(old_bases);
537 Py_DECREF(old_base);
538 Py_DECREF(old_mro);
539
540 return r;
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000541
542 bail:
Michael W. Hudsone723e452003-08-07 14:58:10 +0000543 Py_DECREF(type->tp_bases);
544 Py_DECREF(type->tp_base);
545 if (type->tp_mro != old_mro) {
546 Py_DECREF(type->tp_mro);
547 }
548
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000549 type->tp_bases = old_bases;
550 type->tp_base = old_base;
551 type->tp_mro = old_mro;
Tim Petersea7f75d2002-12-07 21:39:16 +0000552
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000553 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000554}
555
556static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000557type_dict(PyTypeObject *type, void *context)
558{
559 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 Py_INCREF(Py_None);
561 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000562 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000563 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000564}
565
Tim Peters24008312002-03-17 18:56:20 +0000566static PyObject *
567type_get_doc(PyTypeObject *type, void *context)
568{
569 PyObject *result;
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000570 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Neal Norwitza369c5a2007-08-25 07:41:59 +0000571 return PyUnicode_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +0000572 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000573 if (result == NULL) {
574 result = Py_None;
575 Py_INCREF(result);
576 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000577 else if (Py_TYPE(result)->tp_descr_get) {
578 result = Py_TYPE(result)->tp_descr_get(result, NULL,
Tim Peters2b858972002-04-18 04:12:28 +0000579 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000580 }
581 else {
582 Py_INCREF(result);
583 }
Tim Peters24008312002-03-17 18:56:20 +0000584 return result;
585}
586
Antoine Pitrouec569b72008-08-26 22:40:48 +0000587static PyObject *
588type___instancecheck__(PyObject *type, PyObject *inst)
589{
590 switch (_PyObject_RealIsInstance(inst, type)) {
591 case -1:
592 return NULL;
593 case 0:
594 Py_RETURN_FALSE;
595 default:
596 Py_RETURN_TRUE;
597 }
598}
599
600
601static PyObject *
602type_get_instancecheck(PyObject *type, void *context)
603{
604 static PyMethodDef ml = {"__instancecheck__",
605 type___instancecheck__, METH_O };
606 return PyCFunction_New(&ml, type);
607}
608
609static PyObject *
610type___subclasscheck__(PyObject *type, PyObject *inst)
611{
612 switch (_PyObject_RealIsSubclass(inst, type)) {
613 case -1:
614 return NULL;
615 case 0:
616 Py_RETURN_FALSE;
617 default:
618 Py_RETURN_TRUE;
619 }
620}
621
622static PyObject *
623type_get_subclasscheck(PyObject *type, void *context)
624{
625 static PyMethodDef ml = {"__subclasscheck__",
626 type___subclasscheck__, METH_O };
627 return PyCFunction_New(&ml, type);
628}
629
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000630static PyGetSetDef type_getsets[] = {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000631 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
632 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000633 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000634 {"__abstractmethods__", (getter)type_abstractmethods,
635 (setter)type_set_abstractmethods, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000637 {"__doc__", (getter)type_get_doc, NULL, NULL},
Antoine Pitrouec569b72008-08-26 22:40:48 +0000638 {"__instancecheck__", (getter)type_get_instancecheck, NULL, NULL},
639 {"__subclasscheck__", (getter)type_get_subclasscheck, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640 {0}
641};
642
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000646 PyObject *mod, *name, *rtn;
Guido van Rossumc3542212001-08-16 09:18:56 +0000647
648 mod = type_module(type, NULL);
649 if (mod == NULL)
650 PyErr_Clear();
Martin v. Löwis5b222132007-06-10 09:51:05 +0000651 else if (!PyUnicode_Check(mod)) {
Guido van Rossumc3542212001-08-16 09:18:56 +0000652 Py_DECREF(mod);
653 mod = NULL;
654 }
655 name = type_name(type, NULL);
656 if (name == NULL)
657 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000658
Georg Brandl1a3284e2007-12-02 09:40:06 +0000659 if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
Martin v. Löwis250ad612008-04-07 05:43:42 +0000660 rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
Guido van Rossumc3542212001-08-16 09:18:56 +0000661 else
Martin v. Löwis250ad612008-04-07 05:43:42 +0000662 rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000663
Guido van Rossumc3542212001-08-16 09:18:56 +0000664 Py_XDECREF(mod);
665 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000666 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667}
668
Tim Peters6d6c1a32001-08-02 04:15:00 +0000669static PyObject *
670type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
671{
672 PyObject *obj;
673
674 if (type->tp_new == NULL) {
675 PyErr_Format(PyExc_TypeError,
676 "cannot create '%.100s' instances",
677 type->tp_name);
678 return NULL;
679 }
680
Tim Peters3f996e72001-09-13 19:18:27 +0000681 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000682 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000683 /* Ugly exception: when the call was type(something),
684 don't call tp_init on the result. */
685 if (type == &PyType_Type &&
686 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
687 (kwds == NULL ||
688 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
689 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000690 /* If the returned object is not an instance of type,
691 it won't be initialized. */
Christian Heimes90aa7642007-12-19 02:45:37 +0000692 if (!PyType_IsSubtype(Py_TYPE(obj), type))
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000693 return obj;
Christian Heimes90aa7642007-12-19 02:45:37 +0000694 type = Py_TYPE(obj);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000695 if (type->tp_init != NULL &&
Tim Peters6d6c1a32001-08-02 04:15:00 +0000696 type->tp_init(obj, args, kwds) < 0) {
697 Py_DECREF(obj);
698 obj = NULL;
699 }
700 }
701 return obj;
702}
703
704PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000705PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000707 PyObject *obj;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000708 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
709 /* note that we need to add one, for the sentinel */
Tim Peters406fe3b2001-10-06 19:04:01 +0000710
711 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000712 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000713 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714 obj = (PyObject *)PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000715
Neil Schemenauerc806c882001-08-29 23:54:54 +0000716 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000718
Neil Schemenauerc806c882001-08-29 23:54:54 +0000719 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000720
Tim Peters6d6c1a32001-08-02 04:15:00 +0000721 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
722 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000723
Tim Peters6d6c1a32001-08-02 04:15:00 +0000724 if (type->tp_itemsize == 0)
725 PyObject_INIT(obj, type);
726 else
727 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000728
Tim Peters6d6c1a32001-08-02 04:15:00 +0000729 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000730 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731 return obj;
732}
733
734PyObject *
735PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
736{
737 return type->tp_alloc(type, 0);
738}
739
Guido van Rossum9475a232001-10-05 20:51:39 +0000740/* Helpers for subtyping */
741
742static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000743traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
744{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000745 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000746 PyMemberDef *mp;
747
Christian Heimes90aa7642007-12-19 02:45:37 +0000748 n = Py_SIZE(type);
Guido van Rossume5c691a2003-03-07 15:13:17 +0000749 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000750 for (i = 0; i < n; i++, mp++) {
751 if (mp->type == T_OBJECT_EX) {
752 char *addr = (char *)self + mp->offset;
753 PyObject *obj = *(PyObject **)addr;
754 if (obj != NULL) {
755 int err = visit(obj, arg);
756 if (err)
757 return err;
758 }
759 }
760 }
761 return 0;
762}
763
764static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000765subtype_traverse(PyObject *self, visitproc visit, void *arg)
766{
767 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000768 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000769
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000770 /* Find the nearest base with a different tp_traverse,
771 and traverse slots while we're at it */
Christian Heimes90aa7642007-12-19 02:45:37 +0000772 type = Py_TYPE(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000773 base = type;
774 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000775 if (Py_SIZE(base)) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000776 int err = traverse_slots(base, self, visit, arg);
777 if (err)
778 return err;
779 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000780 base = base->tp_base;
781 assert(base);
782 }
783
784 if (type->tp_dictoffset != base->tp_dictoffset) {
785 PyObject **dictptr = _PyObject_GetDictPtr(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000786 if (dictptr && *dictptr)
787 Py_VISIT(*dictptr);
Guido van Rossum9475a232001-10-05 20:51:39 +0000788 }
789
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossuma3862092002-06-10 15:24:42 +0000791 /* For a heaptype, the instances count as references
Guido van Rossumd8faa362007-04-27 19:54:29 +0000792 to the type. Traverse the type so the collector
Guido van Rossuma3862092002-06-10 15:24:42 +0000793 can find cycles involving this link. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000794 Py_VISIT(type);
Guido van Rossuma3862092002-06-10 15:24:42 +0000795
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000796 if (basetraverse)
797 return basetraverse(self, visit, arg);
798 return 0;
799}
800
801static void
802clear_slots(PyTypeObject *type, PyObject *self)
803{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000804 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000805 PyMemberDef *mp;
806
Christian Heimes90aa7642007-12-19 02:45:37 +0000807 n = Py_SIZE(type);
Guido van Rossume5c691a2003-03-07 15:13:17 +0000808 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000809 for (i = 0; i < n; i++, mp++) {
810 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
811 char *addr = (char *)self + mp->offset;
812 PyObject *obj = *(PyObject **)addr;
813 if (obj != NULL) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000814 *(PyObject **)addr = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815 Py_DECREF(obj);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000816 }
817 }
818 }
819}
820
821static int
822subtype_clear(PyObject *self)
823{
824 PyTypeObject *type, *base;
825 inquiry baseclear;
826
827 /* Find the nearest base with a different tp_clear
828 and clear slots while we're at it */
Christian Heimes90aa7642007-12-19 02:45:37 +0000829 type = Py_TYPE(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000830 base = type;
831 while ((baseclear = base->tp_clear) == subtype_clear) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000832 if (Py_SIZE(base))
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000833 clear_slots(base, self);
834 base = base->tp_base;
835 assert(base);
836 }
837
Guido van Rossuma3862092002-06-10 15:24:42 +0000838 /* There's no need to clear the instance dict (if any);
839 the collector will call its tp_clear handler. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000840
841 if (baseclear)
842 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000843 return 0;
844}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000845
846static void
847subtype_dealloc(PyObject *self)
848{
Guido van Rossum14227b42001-12-06 02:35:58 +0000849 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000850 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000851
Guido van Rossum22b13872002-08-06 21:41:44 +0000852 /* Extract the type; we expect it to be a heap type */
Christian Heimes90aa7642007-12-19 02:45:37 +0000853 type = Py_TYPE(self);
Guido van Rossum22b13872002-08-06 21:41:44 +0000854 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000855
Guido van Rossum22b13872002-08-06 21:41:44 +0000856 /* Test whether the type has GC exactly once */
857
858 if (!PyType_IS_GC(type)) {
859 /* It's really rare to find a dynamic type that doesn't have
860 GC; it can only happen when deriving from 'object' and not
861 adding any slots or instance variables. This allows
862 certain simplifications: there's no need to call
863 clear_slots(), or DECREF the dict, or clear weakrefs. */
864
865 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000866 if (type->tp_del) {
867 type->tp_del(self);
868 if (self->ob_refcnt > 0)
869 return;
870 }
Guido van Rossum22b13872002-08-06 21:41:44 +0000871
872 /* Find the nearest base with a different tp_dealloc */
873 base = type;
874 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000875 assert(Py_SIZE(base) == 0);
Guido van Rossum22b13872002-08-06 21:41:44 +0000876 base = base->tp_base;
877 assert(base);
878 }
879
880 /* Call the base tp_dealloc() */
881 assert(basedealloc);
882 basedealloc(self);
883
884 /* Can't reference self beyond this point */
885 Py_DECREF(type);
886
887 /* Done */
888 return;
889 }
890
891 /* We get here only if the type has GC */
892
893 /* UnTrack and re-Track around the trashcan macro, alas */
Andrew M. Kuchlingc9172d32003-02-06 15:22:49 +0000894 /* See explanation at end of function for full disclosure */
Guido van Rossum0906e072002-08-07 20:42:09 +0000895 PyObject_GC_UnTrack(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000896 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000897 Py_TRASHCAN_SAFE_BEGIN(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000898 --_PyTrash_delete_nesting;
Tim Petersf7f9e992003-11-13 21:59:32 +0000899 /* DO NOT restore GC tracking at this point. weakref callbacks
900 * (if any, and whether directly here or indirectly in something we
901 * call) may trigger GC, and if self is tracked at that point, it
902 * will look like trash to GC and GC will try to delete self again.
Tim Petersadd09b42003-11-12 20:43:28 +0000903 */
Guido van Rossum22b13872002-08-06 21:41:44 +0000904
Guido van Rossum59195fd2003-06-13 20:54:40 +0000905 /* Find the nearest base with a different tp_dealloc */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000906 base = type;
907 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908 base = base->tp_base;
909 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000910 }
911
Guido van Rossumd8faa362007-04-27 19:54:29 +0000912 /* If we added a weaklist, we clear it. Do this *before* calling
Guido van Rossum59195fd2003-06-13 20:54:40 +0000913 the finalizer (__del__), clearing slots, or clearing the instance
914 dict. */
915
Guido van Rossum1987c662003-05-29 14:29:23 +0000916 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
917 PyObject_ClearWeakRefs(self);
918
919 /* Maybe call finalizer; exit early if resurrected */
920 if (type->tp_del) {
Tim Petersf7f9e992003-11-13 21:59:32 +0000921 _PyObject_GC_TRACK(self);
Guido van Rossum1987c662003-05-29 14:29:23 +0000922 type->tp_del(self);
923 if (self->ob_refcnt > 0)
Tim Petersf7f9e992003-11-13 21:59:32 +0000924 goto endlabel; /* resurrected */
925 else
926 _PyObject_GC_UNTRACK(self);
Thomas Woutersb2137042007-02-01 18:02:27 +0000927 /* New weakrefs could be created during the finalizer call.
928 If this occurs, clear them out without calling their
929 finalizers since they might rely on part of the object
930 being finalized that has already been destroyed. */
931 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
932 /* Modeled after GET_WEAKREFS_LISTPTR() */
933 PyWeakReference **list = (PyWeakReference **) \
934 PyObject_GET_WEAKREFS_LISTPTR(self);
935 while (*list)
936 _PyWeakref_ClearRef(*list);
937 }
Guido van Rossum1987c662003-05-29 14:29:23 +0000938 }
939
Guido van Rossum59195fd2003-06-13 20:54:40 +0000940 /* Clear slots up to the nearest base with a different tp_dealloc */
941 base = type;
942 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000943 if (Py_SIZE(base))
Guido van Rossum59195fd2003-06-13 20:54:40 +0000944 clear_slots(base, self);
945 base = base->tp_base;
946 assert(base);
947 }
948
Tim Peters6d6c1a32001-08-02 04:15:00 +0000949 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000950 if (type->tp_dictoffset && !base->tp_dictoffset) {
951 PyObject **dictptr = _PyObject_GetDictPtr(self);
952 if (dictptr != NULL) {
953 PyObject *dict = *dictptr;
954 if (dict != NULL) {
955 Py_DECREF(dict);
956 *dictptr = NULL;
957 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958 }
959 }
960
Tim Peters0bd743c2003-11-13 22:50:00 +0000961 /* Call the base tp_dealloc(); first retrack self if
962 * basedealloc knows about gc.
963 */
964 if (PyType_IS_GC(base))
965 _PyObject_GC_TRACK(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000966 assert(basedealloc);
967 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000968
969 /* Can't reference self beyond this point */
Guido van Rossum22b13872002-08-06 21:41:44 +0000970 Py_DECREF(type);
971
Guido van Rossum0906e072002-08-07 20:42:09 +0000972 endlabel:
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000973 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000974 Py_TRASHCAN_SAFE_END(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000975 --_PyTrash_delete_nesting;
976
977 /* Explanation of the weirdness around the trashcan macros:
978
979 Q. What do the trashcan macros do?
980
981 A. Read the comment titled "Trashcan mechanism" in object.h.
982 For one, this explains why there must be a call to GC-untrack
Guido van Rossumd8faa362007-04-27 19:54:29 +0000983 before the trashcan begin macro. Without understanding the
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000984 trashcan code, the answers to the following questions don't make
985 sense.
986
987 Q. Why do we GC-untrack before the trashcan and then immediately
988 GC-track again afterward?
989
990 A. In the case that the base class is GC-aware, the base class
Guido van Rossumd8faa362007-04-27 19:54:29 +0000991 probably GC-untracks the object. If it does that using the
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000992 UNTRACK macro, this will crash when the object is already
993 untracked. Because we don't know what the base class does, the
994 only safe thing is to make sure the object is tracked when we
995 call the base class dealloc. But... The trashcan begin macro
996 requires that the object is *untracked* before it is called. So
997 the dance becomes:
998
Guido van Rossumd8faa362007-04-27 19:54:29 +0000999 GC untrack
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001000 trashcan begin
1001 GC track
1002
Guido van Rossumd8faa362007-04-27 19:54:29 +00001003 Q. Why did the last question say "immediately GC-track again"?
1004 It's nowhere near immediately.
Tim Petersf7f9e992003-11-13 21:59:32 +00001005
Guido van Rossumd8faa362007-04-27 19:54:29 +00001006 A. Because the code *used* to re-track immediately. Bad Idea.
1007 self has a refcount of 0, and if gc ever gets its hands on it
1008 (which can happen if any weakref callback gets invoked), it
1009 looks like trash to gc too, and gc also tries to delete self
1010 then. But we're already deleting self. Double dealloction is
1011 a subtle disaster.
Tim Petersf7f9e992003-11-13 21:59:32 +00001012
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001013 Q. Why the bizarre (net-zero) manipulation of
1014 _PyTrash_delete_nesting around the trashcan macros?
1015
1016 A. Some base classes (e.g. list) also use the trashcan mechanism.
1017 The following scenario used to be possible:
1018
1019 - suppose the trashcan level is one below the trashcan limit
1020
1021 - subtype_dealloc() is called
1022
1023 - the trashcan limit is not yet reached, so the trashcan level
Guido van Rossumd8faa362007-04-27 19:54:29 +00001024 is incremented and the code between trashcan begin and end is
1025 executed
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001026
1027 - this destroys much of the object's contents, including its
Guido van Rossumd8faa362007-04-27 19:54:29 +00001028 slots and __dict__
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001029
1030 - basedealloc() is called; this is really list_dealloc(), or
Guido van Rossumd8faa362007-04-27 19:54:29 +00001031 some other type which also uses the trashcan macros
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001032
1033 - the trashcan limit is now reached, so the object is put on the
Guido van Rossumd8faa362007-04-27 19:54:29 +00001034 trashcan's to-be-deleted-later list
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001035
1036 - basedealloc() returns
1037
1038 - subtype_dealloc() decrefs the object's type
1039
1040 - subtype_dealloc() returns
1041
1042 - later, the trashcan code starts deleting the objects from its
Guido van Rossumd8faa362007-04-27 19:54:29 +00001043 to-be-deleted-later list
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001044
1045 - subtype_dealloc() is called *AGAIN* for the same object
1046
1047 - at the very least (if the destroyed slots and __dict__ don't
Guido van Rossumd8faa362007-04-27 19:54:29 +00001048 cause problems) the object's type gets decref'ed a second
1049 time, which is *BAD*!!!
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001050
1051 The remedy is to make sure that if the code between trashcan
1052 begin and end in subtype_dealloc() is called, the code between
1053 trashcan begin and end in basedealloc() will also be called.
1054 This is done by decrementing the level after passing into the
1055 trashcan block, and incrementing it just before leaving the
1056 block.
1057
1058 But now it's possible that a chain of objects consisting solely
1059 of objects whose deallocator is subtype_dealloc() will defeat
1060 the trashcan mechanism completely: the decremented level means
Guido van Rossumd8faa362007-04-27 19:54:29 +00001061 that the effective level never reaches the limit. Therefore, we
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001062 *increment* the level *before* entering the trashcan block, and
1063 matchingly decrement it after leaving. This means the trashcan
1064 code will trigger a little early, but that's no big deal.
1065
1066 Q. Are there any live examples of code in need of all this
1067 complexity?
1068
1069 A. Yes. See SF bug 668433 for code that crashed (when Python was
1070 compiled in debug mode) before the trashcan level manipulations
1071 were added. For more discussion, see SF patches 581742, 575073
1072 and bug 574207.
1073 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001074}
1075
Jeremy Hylton938ace62002-07-17 16:30:39 +00001076static PyTypeObject *solid_base(PyTypeObject *type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001077
Tim Peters6d6c1a32001-08-02 04:15:00 +00001078/* type test with subclassing support */
1079
1080int
1081PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
1082{
1083 PyObject *mro;
1084
1085 mro = a->tp_mro;
1086 if (mro != NULL) {
1087 /* Deal with multiple inheritance without recursion
1088 by walking the MRO tuple */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001089 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001090 assert(PyTuple_Check(mro));
1091 n = PyTuple_GET_SIZE(mro);
1092 for (i = 0; i < n; i++) {
1093 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
1094 return 1;
1095 }
1096 return 0;
1097 }
1098 else {
1099 /* a is not completely initilized yet; follow tp_base */
1100 do {
1101 if (a == b)
1102 return 1;
1103 a = a->tp_base;
1104 } while (a != NULL);
1105 return b == &PyBaseObject_Type;
1106 }
1107}
1108
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001109/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +00001110 without looking in the instance dictionary
1111 (so we can't use PyObject_GetAttr) but still binding
Guido van Rossumd8faa362007-04-27 19:54:29 +00001112 it to the instance. The arguments are the object,
Guido van Rossum60718732001-08-28 17:47:51 +00001113 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001114 static variable used to cache the interned Python string.
1115
1116 Two variants:
1117
1118 - lookup_maybe() returns NULL without raising an exception
1119 when the _PyType_Lookup() call fails;
1120
1121 - lookup_method() always raises an exception upon errors.
1122*/
Guido van Rossum60718732001-08-28 17:47:51 +00001123
1124static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001125lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +00001126{
1127 PyObject *res;
1128
1129 if (*attrobj == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001130 *attrobj = PyUnicode_InternFromString(attrstr);
Guido van Rossum60718732001-08-28 17:47:51 +00001131 if (*attrobj == NULL)
1132 return NULL;
1133 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001134 res = _PyType_Lookup(Py_TYPE(self), *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001135 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +00001136 descrgetfunc f;
Christian Heimes90aa7642007-12-19 02:45:37 +00001137 if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
Guido van Rossum60718732001-08-28 17:47:51 +00001138 Py_INCREF(res);
1139 else
Christian Heimes90aa7642007-12-19 02:45:37 +00001140 res = f(res, self, (PyObject *)(Py_TYPE(self)));
Guido van Rossum60718732001-08-28 17:47:51 +00001141 }
1142 return res;
1143}
1144
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001145static PyObject *
1146lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
1147{
1148 PyObject *res = lookup_maybe(self, attrstr, attrobj);
1149 if (res == NULL && !PyErr_Occurred())
1150 PyErr_SetObject(PyExc_AttributeError, *attrobj);
1151 return res;
1152}
1153
Guido van Rossum2730b132001-08-28 18:22:14 +00001154/* A variation of PyObject_CallMethod that uses lookup_method()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001155 instead of PyObject_GetAttrString(). This uses the same convention
Guido van Rossum2730b132001-08-28 18:22:14 +00001156 as lookup_method to cache the interned name string object. */
1157
Neil Schemenauerf23473f2001-10-21 22:28:58 +00001158static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +00001159call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1160{
1161 va_list va;
1162 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +00001163 va_start(va, format);
1164
Guido van Rossumda21c012001-10-03 00:50:18 +00001165 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001166 if (func == NULL) {
1167 va_end(va);
1168 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +00001169 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001170 return NULL;
1171 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001172
1173 if (format && *format)
1174 args = Py_VaBuildValue(format, va);
1175 else
1176 args = PyTuple_New(0);
1177
1178 va_end(va);
1179
1180 if (args == NULL)
1181 return NULL;
1182
1183 assert(PyTuple_Check(args));
1184 retval = PyObject_Call(func, args, NULL);
1185
1186 Py_DECREF(args);
1187 Py_DECREF(func);
1188
1189 return retval;
1190}
1191
1192/* Clone of call_method() that returns NotImplemented when the lookup fails. */
1193
Neil Schemenauerf23473f2001-10-21 22:28:58 +00001194static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001195call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1196{
1197 va_list va;
1198 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001199 va_start(va, format);
1200
Guido van Rossumda21c012001-10-03 00:50:18 +00001201 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +00001202 if (func == NULL) {
1203 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001204 if (!PyErr_Occurred()) {
1205 Py_INCREF(Py_NotImplemented);
1206 return Py_NotImplemented;
1207 }
Guido van Rossum717ce002001-09-14 16:58:08 +00001208 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +00001209 }
1210
1211 if (format && *format)
1212 args = Py_VaBuildValue(format, va);
1213 else
1214 args = PyTuple_New(0);
1215
1216 va_end(va);
1217
Guido van Rossum717ce002001-09-14 16:58:08 +00001218 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00001219 return NULL;
1220
Guido van Rossum717ce002001-09-14 16:58:08 +00001221 assert(PyTuple_Check(args));
1222 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +00001223
1224 Py_DECREF(args);
1225 Py_DECREF(func);
1226
1227 return retval;
1228}
1229
Tim Petersea7f75d2002-12-07 21:39:16 +00001230/*
Guido van Rossum1f121312002-11-14 19:49:16 +00001231 Method resolution order algorithm C3 described in
1232 "A Monotonic Superclass Linearization for Dylan",
1233 by Kim Barrett, Bob Cassel, Paul Haahr,
Tim Petersea7f75d2002-12-07 21:39:16 +00001234 David A. Moon, Keith Playford, and P. Tucker Withington.
Guido van Rossum1f121312002-11-14 19:49:16 +00001235 (OOPSLA 1996)
1236
Guido van Rossum98f33732002-11-25 21:36:54 +00001237 Some notes about the rules implied by C3:
1238
Tim Petersea7f75d2002-12-07 21:39:16 +00001239 No duplicate bases.
Guido van Rossum98f33732002-11-25 21:36:54 +00001240 It isn't legal to repeat a class in a list of base classes.
1241
1242 The next three properties are the 3 constraints in "C3".
1243
Tim Petersea7f75d2002-12-07 21:39:16 +00001244 Local precendece order.
Guido van Rossum98f33732002-11-25 21:36:54 +00001245 If A precedes B in C's MRO, then A will precede B in the MRO of all
1246 subclasses of C.
1247
1248 Monotonicity.
1249 The MRO of a class must be an extension without reordering of the
1250 MRO of each of its superclasses.
1251
1252 Extended Precedence Graph (EPG).
1253 Linearization is consistent if there is a path in the EPG from
1254 each class to all its successors in the linearization. See
1255 the paper for definition of EPG.
Guido van Rossum1f121312002-11-14 19:49:16 +00001256 */
1257
Tim Petersea7f75d2002-12-07 21:39:16 +00001258static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001259tail_contains(PyObject *list, int whence, PyObject *o) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001260 Py_ssize_t j, size;
Guido van Rossum1f121312002-11-14 19:49:16 +00001261 size = PyList_GET_SIZE(list);
1262
1263 for (j = whence+1; j < size; j++) {
1264 if (PyList_GET_ITEM(list, j) == o)
1265 return 1;
1266 }
1267 return 0;
1268}
1269
Guido van Rossum98f33732002-11-25 21:36:54 +00001270static PyObject *
1271class_name(PyObject *cls)
1272{
1273 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1274 if (name == NULL) {
1275 PyErr_Clear();
1276 Py_XDECREF(name);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001277 name = PyObject_Repr(cls);
Guido van Rossum98f33732002-11-25 21:36:54 +00001278 }
1279 if (name == NULL)
1280 return NULL;
Martin v. Löwis9b9905b2007-06-10 21:13:34 +00001281 if (!PyUnicode_Check(name)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001282 Py_DECREF(name);
1283 return NULL;
1284 }
1285 return name;
1286}
1287
1288static int
1289check_duplicates(PyObject *list)
1290{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001291 Py_ssize_t i, j, n;
Guido van Rossum98f33732002-11-25 21:36:54 +00001292 /* Let's use a quadratic time algorithm,
1293 assuming that the bases lists is short.
1294 */
1295 n = PyList_GET_SIZE(list);
1296 for (i = 0; i < n; i++) {
1297 PyObject *o = PyList_GET_ITEM(list, i);
1298 for (j = i + 1; j < n; j++) {
1299 if (PyList_GET_ITEM(list, j) == o) {
1300 o = class_name(o);
1301 PyErr_Format(PyExc_TypeError,
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001302 "duplicate base class %.400s",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001303 o ? _PyUnicode_AsString(o) : "?");
Guido van Rossum98f33732002-11-25 21:36:54 +00001304 Py_XDECREF(o);
1305 return -1;
1306 }
1307 }
1308 }
1309 return 0;
1310}
1311
1312/* Raise a TypeError for an MRO order disagreement.
1313
1314 It's hard to produce a good error message. In the absence of better
1315 insight into error reporting, report the classes that were candidates
Guido van Rossumd8faa362007-04-27 19:54:29 +00001316 to be put next into the MRO. There is some conflict between the
Guido van Rossum98f33732002-11-25 21:36:54 +00001317 order in which they should be put in the MRO, but it's hard to
1318 diagnose what constraint can't be satisfied.
1319*/
1320
1321static void
1322set_mro_error(PyObject *to_merge, int *remain)
1323{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001324 Py_ssize_t i, n, off, to_merge_size;
Guido van Rossum98f33732002-11-25 21:36:54 +00001325 char buf[1000];
1326 PyObject *k, *v;
1327 PyObject *set = PyDict_New();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001328 if (!set) return;
Guido van Rossum98f33732002-11-25 21:36:54 +00001329
1330 to_merge_size = PyList_GET_SIZE(to_merge);
1331 for (i = 0; i < to_merge_size; i++) {
1332 PyObject *L = PyList_GET_ITEM(to_merge, i);
1333 if (remain[i] < PyList_GET_SIZE(L)) {
1334 PyObject *c = PyList_GET_ITEM(L, remain[i]);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335 if (PyDict_SetItem(set, c, Py_None) < 0) {
1336 Py_DECREF(set);
Guido van Rossum98f33732002-11-25 21:36:54 +00001337 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001338 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001339 }
1340 }
1341 n = PyDict_Size(set);
1342
Raymond Hettingerf394df42003-04-06 19:13:41 +00001343 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1344consistent method resolution\norder (MRO) for bases");
Guido van Rossum98f33732002-11-25 21:36:54 +00001345 i = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001346 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001347 PyObject *name = class_name(k);
1348 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001349 name ? _PyUnicode_AsString(name) : "?");
Guido van Rossum98f33732002-11-25 21:36:54 +00001350 Py_XDECREF(name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001351 if (--n && (size_t)(off+1) < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001352 buf[off++] = ',';
1353 buf[off] = '\0';
1354 }
1355 }
1356 PyErr_SetString(PyExc_TypeError, buf);
1357 Py_DECREF(set);
1358}
1359
Tim Petersea7f75d2002-12-07 21:39:16 +00001360static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001361pmerge(PyObject *acc, PyObject* to_merge) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001362 Py_ssize_t i, j, to_merge_size, empty_cnt;
Guido van Rossum1f121312002-11-14 19:49:16 +00001363 int *remain;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001364 int ok;
Tim Petersea7f75d2002-12-07 21:39:16 +00001365
Guido van Rossum1f121312002-11-14 19:49:16 +00001366 to_merge_size = PyList_GET_SIZE(to_merge);
1367
Guido van Rossum98f33732002-11-25 21:36:54 +00001368 /* remain stores an index into each sublist of to_merge.
1369 remain[i] is the index of the next base in to_merge[i]
1370 that is not included in acc.
1371 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001372 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
Guido van Rossum1f121312002-11-14 19:49:16 +00001373 if (remain == NULL)
1374 return -1;
1375 for (i = 0; i < to_merge_size; i++)
1376 remain[i] = 0;
1377
1378 again:
1379 empty_cnt = 0;
1380 for (i = 0; i < to_merge_size; i++) {
1381 PyObject *candidate;
Tim Petersea7f75d2002-12-07 21:39:16 +00001382
Guido van Rossum1f121312002-11-14 19:49:16 +00001383 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1384
1385 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1386 empty_cnt++;
1387 continue;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001388 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001389
Guido van Rossum98f33732002-11-25 21:36:54 +00001390 /* Choose next candidate for MRO.
1391
1392 The input sequences alone can determine the choice.
1393 If not, choose the class which appears in the MRO
1394 of the earliest direct superclass of the new class.
1395 */
1396
Guido van Rossum1f121312002-11-14 19:49:16 +00001397 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1398 for (j = 0; j < to_merge_size; j++) {
1399 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum98f33732002-11-25 21:36:54 +00001400 if (tail_contains(j_lst, remain[j], candidate)) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001401 goto skip; /* continue outer loop */
Guido van Rossum98f33732002-11-25 21:36:54 +00001402 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001403 }
1404 ok = PyList_Append(acc, candidate);
1405 if (ok < 0) {
1406 PyMem_Free(remain);
1407 return -1;
1408 }
1409 for (j = 0; j < to_merge_size; j++) {
1410 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum768158c2002-12-31 16:33:01 +00001411 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1412 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001413 remain[j]++;
1414 }
1415 }
1416 goto again;
Tim Peters9a6b8d82002-11-14 23:22:33 +00001417 skip: ;
Guido van Rossum1f121312002-11-14 19:49:16 +00001418 }
1419
Guido van Rossum98f33732002-11-25 21:36:54 +00001420 if (empty_cnt == to_merge_size) {
1421 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001422 return 0;
Guido van Rossum98f33732002-11-25 21:36:54 +00001423 }
1424 set_mro_error(to_merge, remain);
1425 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001426 return -1;
1427}
1428
Tim Peters6d6c1a32001-08-02 04:15:00 +00001429static PyObject *
1430mro_implementation(PyTypeObject *type)
1431{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001432 Py_ssize_t i, n;
1433 int ok;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001434 PyObject *bases, *result;
Guido van Rossum1f121312002-11-14 19:49:16 +00001435 PyObject *to_merge, *bases_aslist;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001436
Christian Heimes412dc9c2008-01-27 18:55:54 +00001437 if (type->tp_dict == NULL) {
1438 if (PyType_Ready(type) < 0)
Guido van Rossum63517572002-06-18 16:44:57 +00001439 return NULL;
1440 }
1441
Guido van Rossum98f33732002-11-25 21:36:54 +00001442 /* Find a superclass linearization that honors the constraints
1443 of the explicit lists of bases and the constraints implied by
Tim Petersea7f75d2002-12-07 21:39:16 +00001444 each base class.
Guido van Rossum98f33732002-11-25 21:36:54 +00001445
1446 to_merge is a list of lists, where each list is a superclass
1447 linearization implied by a base class. The last element of
1448 to_merge is the declared list of bases.
1449 */
1450
Tim Peters6d6c1a32001-08-02 04:15:00 +00001451 bases = type->tp_bases;
1452 n = PyTuple_GET_SIZE(bases);
Guido van Rossum1f121312002-11-14 19:49:16 +00001453
1454 to_merge = PyList_New(n+1);
1455 if (to_merge == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001456 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001457
Tim Peters6d6c1a32001-08-02 04:15:00 +00001458 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001459 PyObject *base = PyTuple_GET_ITEM(bases, i);
1460 PyObject *parentMRO;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001461 parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001462 if (parentMRO == NULL) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001463 Py_DECREF(to_merge);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001464 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001465 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001466
1467 PyList_SET_ITEM(to_merge, i, parentMRO);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001468 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001469
1470 bases_aslist = PySequence_List(bases);
1471 if (bases_aslist == NULL) {
1472 Py_DECREF(to_merge);
1473 return NULL;
1474 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001475 /* This is just a basic sanity check. */
1476 if (check_duplicates(bases_aslist) < 0) {
1477 Py_DECREF(to_merge);
1478 Py_DECREF(bases_aslist);
1479 return NULL;
1480 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001481 PyList_SET_ITEM(to_merge, n, bases_aslist);
1482
1483 result = Py_BuildValue("[O]", (PyObject *)type);
1484 if (result == NULL) {
1485 Py_DECREF(to_merge);
1486 return NULL;
1487 }
1488
1489 ok = pmerge(result, to_merge);
1490 Py_DECREF(to_merge);
1491 if (ok < 0) {
1492 Py_DECREF(result);
1493 return NULL;
1494 }
1495
Tim Peters6d6c1a32001-08-02 04:15:00 +00001496 return result;
1497}
1498
1499static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001500mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001501{
1502 PyTypeObject *type = (PyTypeObject *)self;
1503
Tim Peters6d6c1a32001-08-02 04:15:00 +00001504 return mro_implementation(type);
1505}
1506
1507static int
1508mro_internal(PyTypeObject *type)
1509{
1510 PyObject *mro, *result, *tuple;
Armin Rigo037d1e02005-12-29 17:07:39 +00001511 int checkit = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001512
Christian Heimes90aa7642007-12-19 02:45:37 +00001513 if (Py_TYPE(type) == &PyType_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001514 result = mro_implementation(type);
1515 }
1516 else {
Guido van Rossum60718732001-08-28 17:47:51 +00001517 static PyObject *mro_str;
Armin Rigo037d1e02005-12-29 17:07:39 +00001518 checkit = 1;
Guido van Rossum60718732001-08-28 17:47:51 +00001519 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001520 if (mro == NULL)
1521 return -1;
1522 result = PyObject_CallObject(mro, NULL);
1523 Py_DECREF(mro);
1524 }
1525 if (result == NULL)
1526 return -1;
1527 tuple = PySequence_Tuple(result);
1528 Py_DECREF(result);
Armin Rigo037d1e02005-12-29 17:07:39 +00001529 if (tuple == NULL)
1530 return -1;
1531 if (checkit) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001532 Py_ssize_t i, len;
Armin Rigo037d1e02005-12-29 17:07:39 +00001533 PyObject *cls;
1534 PyTypeObject *solid;
1535
1536 solid = solid_base(type);
1537
1538 len = PyTuple_GET_SIZE(tuple);
1539
1540 for (i = 0; i < len; i++) {
1541 PyTypeObject *t;
1542 cls = PyTuple_GET_ITEM(tuple, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001543 if (!PyType_Check(cls)) {
Armin Rigo037d1e02005-12-29 17:07:39 +00001544 PyErr_Format(PyExc_TypeError,
1545 "mro() returned a non-class ('%.500s')",
Christian Heimes90aa7642007-12-19 02:45:37 +00001546 Py_TYPE(cls)->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001547 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001548 return -1;
1549 }
1550 t = (PyTypeObject*)cls;
1551 if (!PyType_IsSubtype(solid, solid_base(t))) {
1552 PyErr_Format(PyExc_TypeError,
1553 "mro() returned base with unsuitable layout ('%.500s')",
1554 t->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001555 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001556 return -1;
1557 }
1558 }
1559 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001560 type->tp_mro = tuple;
Christian Heimesa62da1d2008-01-12 19:39:10 +00001561
1562 type_mro_modified(type, type->tp_mro);
1563 /* corner case: the old-style super class might have been hidden
1564 from the custom MRO */
1565 type_mro_modified(type, type->tp_bases);
1566
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001567 PyType_Modified(type);
Christian Heimesa62da1d2008-01-12 19:39:10 +00001568
Tim Peters6d6c1a32001-08-02 04:15:00 +00001569 return 0;
1570}
1571
1572
1573/* Calculate the best base amongst multiple base classes.
1574 This is the first one that's on the path to the "solid base". */
1575
1576static PyTypeObject *
1577best_base(PyObject *bases)
1578{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001579 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001580 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +00001581 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001582
1583 assert(PyTuple_Check(bases));
1584 n = PyTuple_GET_SIZE(bases);
1585 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +00001586 base = NULL;
1587 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001588 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001589 base_proto = PyTuple_GET_ITEM(bases, i);
Tim Petersa91e9642001-11-14 23:32:33 +00001590 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001591 PyErr_SetString(
1592 PyExc_TypeError,
1593 "bases must be types");
1594 return NULL;
1595 }
Tim Petersa91e9642001-11-14 23:32:33 +00001596 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001597 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001598 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001599 return NULL;
1600 }
1601 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +00001602 if (winner == NULL) {
1603 winner = candidate;
1604 base = base_i;
1605 }
1606 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001607 ;
1608 else if (PyType_IsSubtype(candidate, winner)) {
1609 winner = candidate;
1610 base = base_i;
1611 }
1612 else {
1613 PyErr_SetString(
1614 PyExc_TypeError,
1615 "multiple bases have "
1616 "instance lay-out conflict");
1617 return NULL;
1618 }
1619 }
Guido van Rossume54616c2001-12-14 04:19:56 +00001620 if (base == NULL)
1621 PyErr_SetString(PyExc_TypeError,
1622 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001623 return base;
1624}
1625
1626static int
1627extra_ivars(PyTypeObject *type, PyTypeObject *base)
1628{
Neil Schemenauerc806c882001-08-29 23:54:54 +00001629 size_t t_size = type->tp_basicsize;
1630 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001631
Guido van Rossum9676b222001-08-17 20:32:36 +00001632 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001633 if (type->tp_itemsize || base->tp_itemsize) {
1634 /* If itemsize is involved, stricter rules */
1635 return t_size != b_size ||
1636 type->tp_itemsize != base->tp_itemsize;
1637 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001638 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
Guido van Rossum360e4b82007-05-14 22:51:27 +00001639 type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
1640 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossum9676b222001-08-17 20:32:36 +00001641 t_size -= sizeof(PyObject *);
1642 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
Guido van Rossum360e4b82007-05-14 22:51:27 +00001643 type->tp_dictoffset + sizeof(PyObject *) == t_size &&
1644 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossum9676b222001-08-17 20:32:36 +00001645 t_size -= sizeof(PyObject *);
1646
1647 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001648}
1649
1650static PyTypeObject *
1651solid_base(PyTypeObject *type)
1652{
1653 PyTypeObject *base;
1654
1655 if (type->tp_base)
1656 base = solid_base(type->tp_base);
1657 else
1658 base = &PyBaseObject_Type;
1659 if (extra_ivars(type, base))
1660 return type;
1661 else
1662 return base;
1663}
1664
Jeremy Hylton938ace62002-07-17 16:30:39 +00001665static void object_dealloc(PyObject *);
1666static int object_init(PyObject *, PyObject *, PyObject *);
1667static int update_slot(PyTypeObject *, PyObject *);
1668static void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001669
Guido van Rossum360e4b82007-05-14 22:51:27 +00001670/*
1671 * Helpers for __dict__ descriptor. We don't want to expose the dicts
1672 * inherited from various builtin types. The builtin base usually provides
1673 * its own __dict__ descriptor, so we use that when we can.
1674 */
1675static PyTypeObject *
1676get_builtin_base_with_dict(PyTypeObject *type)
1677{
1678 while (type->tp_base != NULL) {
1679 if (type->tp_dictoffset != 0 &&
1680 !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1681 return type;
1682 type = type->tp_base;
1683 }
1684 return NULL;
1685}
1686
1687static PyObject *
1688get_dict_descriptor(PyTypeObject *type)
1689{
1690 static PyObject *dict_str;
1691 PyObject *descr;
1692
1693 if (dict_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001694 dict_str = PyUnicode_InternFromString("__dict__");
Guido van Rossum360e4b82007-05-14 22:51:27 +00001695 if (dict_str == NULL)
1696 return NULL;
1697 }
1698 descr = _PyType_Lookup(type, dict_str);
1699 if (descr == NULL || !PyDescr_IsData(descr))
1700 return NULL;
1701
1702 return descr;
1703}
1704
1705static void
1706raise_dict_descr_error(PyObject *obj)
1707{
1708 PyErr_Format(PyExc_TypeError,
1709 "this __dict__ descriptor does not support "
Christian Heimes90aa7642007-12-19 02:45:37 +00001710 "'%.200s' objects", Py_TYPE(obj)->tp_name);
Guido van Rossum360e4b82007-05-14 22:51:27 +00001711}
1712
Tim Peters6d6c1a32001-08-02 04:15:00 +00001713static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001714subtype_dict(PyObject *obj, void *context)
1715{
Guido van Rossum360e4b82007-05-14 22:51:27 +00001716 PyObject **dictptr;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001717 PyObject *dict;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001718 PyTypeObject *base;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001719
Christian Heimes90aa7642007-12-19 02:45:37 +00001720 base = get_builtin_base_with_dict(Py_TYPE(obj));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001721 if (base != NULL) {
1722 descrgetfunc func;
1723 PyObject *descr = get_dict_descriptor(base);
1724 if (descr == NULL) {
1725 raise_dict_descr_error(obj);
1726 return NULL;
1727 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001728 func = Py_TYPE(descr)->tp_descr_get;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001729 if (func == NULL) {
1730 raise_dict_descr_error(obj);
1731 return NULL;
1732 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001733 return func(descr, obj, (PyObject *)(Py_TYPE(obj)));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001734 }
1735
1736 dictptr = _PyObject_GetDictPtr(obj);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001737 if (dictptr == NULL) {
1738 PyErr_SetString(PyExc_AttributeError,
1739 "This object has no __dict__");
1740 return NULL;
1741 }
1742 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +00001743 if (dict == NULL)
1744 *dictptr = dict = PyDict_New();
1745 Py_XINCREF(dict);
1746 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001747}
1748
Guido van Rossum6661be32001-10-26 04:26:12 +00001749static int
1750subtype_setdict(PyObject *obj, PyObject *value, void *context)
1751{
Guido van Rossum360e4b82007-05-14 22:51:27 +00001752 PyObject **dictptr;
Guido van Rossum6661be32001-10-26 04:26:12 +00001753 PyObject *dict;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001754 PyTypeObject *base;
Guido van Rossum6661be32001-10-26 04:26:12 +00001755
Christian Heimes90aa7642007-12-19 02:45:37 +00001756 base = get_builtin_base_with_dict(Py_TYPE(obj));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001757 if (base != NULL) {
1758 descrsetfunc func;
1759 PyObject *descr = get_dict_descriptor(base);
1760 if (descr == NULL) {
1761 raise_dict_descr_error(obj);
1762 return -1;
1763 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001764 func = Py_TYPE(descr)->tp_descr_set;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001765 if (func == NULL) {
1766 raise_dict_descr_error(obj);
1767 return -1;
1768 }
1769 return func(descr, obj, value);
1770 }
1771
1772 dictptr = _PyObject_GetDictPtr(obj);
Guido van Rossum6661be32001-10-26 04:26:12 +00001773 if (dictptr == NULL) {
1774 PyErr_SetString(PyExc_AttributeError,
1775 "This object has no __dict__");
1776 return -1;
1777 }
Guido van Rossumd331cb52001-12-05 19:46:42 +00001778 if (value != NULL && !PyDict_Check(value)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001779 PyErr_Format(PyExc_TypeError,
1780 "__dict__ must be set to a dictionary, "
Christian Heimes90aa7642007-12-19 02:45:37 +00001781 "not a '%.200s'", Py_TYPE(value)->tp_name);
Guido van Rossum6661be32001-10-26 04:26:12 +00001782 return -1;
1783 }
1784 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +00001785 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +00001786 *dictptr = value;
1787 Py_XDECREF(dict);
1788 return 0;
1789}
1790
Guido van Rossumad47da02002-08-12 19:05:44 +00001791static PyObject *
1792subtype_getweakref(PyObject *obj, void *context)
1793{
1794 PyObject **weaklistptr;
1795 PyObject *result;
1796
Christian Heimes90aa7642007-12-19 02:45:37 +00001797 if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001798 PyErr_SetString(PyExc_AttributeError,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001799 "This object has no __weakref__");
Guido van Rossumad47da02002-08-12 19:05:44 +00001800 return NULL;
1801 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001802 assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
1803 assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
1804 (size_t)(Py_TYPE(obj)->tp_basicsize));
Guido van Rossumad47da02002-08-12 19:05:44 +00001805 weaklistptr = (PyObject **)
Christian Heimes90aa7642007-12-19 02:45:37 +00001806 ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
Guido van Rossumad47da02002-08-12 19:05:44 +00001807 if (*weaklistptr == NULL)
1808 result = Py_None;
1809 else
1810 result = *weaklistptr;
1811 Py_INCREF(result);
1812 return result;
1813}
1814
Guido van Rossum373c7412003-01-07 13:41:37 +00001815/* Three variants on the subtype_getsets list. */
1816
1817static PyGetSetDef subtype_getsets_full[] = {
Guido van Rossumad47da02002-08-12 19:05:44 +00001818 {"__dict__", subtype_dict, subtype_setdict,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001819 PyDoc_STR("dictionary for instance variables (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001820 {"__weakref__", subtype_getweakref, NULL,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001821 PyDoc_STR("list of weak references to the object (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001822 {0}
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001823};
1824
Guido van Rossum373c7412003-01-07 13:41:37 +00001825static PyGetSetDef subtype_getsets_dict_only[] = {
1826 {"__dict__", subtype_dict, subtype_setdict,
1827 PyDoc_STR("dictionary for instance variables (if defined)")},
1828 {0}
1829};
1830
1831static PyGetSetDef subtype_getsets_weakref_only[] = {
1832 {"__weakref__", subtype_getweakref, NULL,
1833 PyDoc_STR("list of weak references to the object (if defined)")},
1834 {0}
1835};
1836
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001837static int
1838valid_identifier(PyObject *s)
1839{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001840 if (!PyUnicode_Check(s)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001841 PyErr_Format(PyExc_TypeError,
1842 "__slots__ items must be strings, not '%.200s'",
Christian Heimes90aa7642007-12-19 02:45:37 +00001843 Py_TYPE(s)->tp_name);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001844 return 0;
1845 }
Georg Brandlf4780d02007-08-30 18:29:48 +00001846 if (!PyUnicode_IsIdentifier(s)) {
1847 PyErr_SetString(PyExc_TypeError,
1848 "__slots__ must be identifiers");
1849 return 0;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001850 }
1851 return 1;
1852}
1853
Guido van Rossumd8faa362007-04-27 19:54:29 +00001854/* Forward */
1855static int
1856object_init(PyObject *self, PyObject *args, PyObject *kwds);
1857
1858static int
1859type_init(PyObject *cls, PyObject *args, PyObject *kwds)
1860{
1861 int res;
1862
1863 assert(args != NULL && PyTuple_Check(args));
1864 assert(kwds == NULL || PyDict_Check(kwds));
1865
1866 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
1867 PyErr_SetString(PyExc_TypeError,
1868 "type.__init__() takes no keyword arguments");
1869 return -1;
1870 }
1871
1872 if (args != NULL && PyTuple_Check(args) &&
1873 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
1874 PyErr_SetString(PyExc_TypeError,
1875 "type.__init__() takes 1 or 3 arguments");
1876 return -1;
1877 }
1878
1879 /* Call object.__init__(self) now. */
1880 /* XXX Could call super(type, cls).__init__() but what's the point? */
1881 args = PyTuple_GetSlice(args, 0, 0);
1882 res = object_init(cls, args, NULL);
1883 Py_DECREF(args);
1884 return res;
1885}
1886
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001887static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001888type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1889{
1890 PyObject *name, *bases, *dict;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001891 static char *kwlist[] = {"name", "bases", "dict", 0};
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001892 PyObject *slots, *tmp, *newslots;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001893 PyTypeObject *type, *base, *tmptype, *winner;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001894 PyHeapTypeObject *et;
Guido van Rossum6f799372001-09-20 20:46:19 +00001895 PyMemberDef *mp;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001896 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
Guido van Rossumad47da02002-08-12 19:05:44 +00001897 int j, may_add_dict, may_add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001898
Tim Peters3abca122001-10-27 19:37:48 +00001899 assert(args != NULL && PyTuple_Check(args));
1900 assert(kwds == NULL || PyDict_Check(kwds));
1901
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001902 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +00001903 {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001904 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1905 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
Tim Peters3abca122001-10-27 19:37:48 +00001906
1907 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1908 PyObject *x = PyTuple_GET_ITEM(args, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00001909 Py_INCREF(Py_TYPE(x));
1910 return (PyObject *) Py_TYPE(x);
Tim Peters3abca122001-10-27 19:37:48 +00001911 }
1912
1913 /* SF bug 475327 -- if that didn't trigger, we need 3
1914 arguments. but PyArg_ParseTupleAndKeywords below may give
1915 a msg saying type() needs exactly 3. */
1916 if (nargs + nkwds != 3) {
1917 PyErr_SetString(PyExc_TypeError,
1918 "type() takes 1 or 3 arguments");
1919 return NULL;
1920 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001921 }
1922
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001923 /* Check arguments: (name, bases, dict) */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001924 if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001925 &name,
1926 &PyTuple_Type, &bases,
1927 &PyDict_Type, &dict))
1928 return NULL;
1929
1930 /* Determine the proper metatype to deal with this,
1931 and check for metatype conflicts while we're at it.
1932 Note that if some other metatype wins to contract,
1933 it's possible that its instances are not types. */
1934 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001935 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001936 for (i = 0; i < nbases; i++) {
1937 tmp = PyTuple_GET_ITEM(bases, i);
Christian Heimes90aa7642007-12-19 02:45:37 +00001938 tmptype = Py_TYPE(tmp);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001939 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001940 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001941 if (PyType_IsSubtype(tmptype, winner)) {
1942 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001943 continue;
1944 }
1945 PyErr_SetString(PyExc_TypeError,
Guido van Rossum636688d2003-04-23 12:07:22 +00001946 "metaclass conflict: "
1947 "the metaclass of a derived class "
1948 "must be a (non-strict) subclass "
1949 "of the metaclasses of all its bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001950 return NULL;
1951 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001952 if (winner != metatype) {
1953 if (winner->tp_new != type_new) /* Pass it to the winner */
1954 return winner->tp_new(winner, args, kwds);
1955 metatype = winner;
1956 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001957
1958 /* Adjust for empty tuple bases */
1959 if (nbases == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001960 bases = PyTuple_Pack(1, &PyBaseObject_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001961 if (bases == NULL)
1962 return NULL;
1963 nbases = 1;
1964 }
1965 else
1966 Py_INCREF(bases);
1967
1968 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1969
1970 /* Calculate best base, and check that all bases are type objects */
1971 base = best_base(bases);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001972 if (base == NULL) {
1973 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001974 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001975 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001976 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1977 PyErr_Format(PyExc_TypeError,
1978 "type '%.100s' is not an acceptable base type",
1979 base->tp_name);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001980 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001981 return NULL;
1982 }
1983
Tim Peters6d6c1a32001-08-02 04:15:00 +00001984 /* Check for a __slots__ sequence variable in dict, and count it */
1985 slots = PyDict_GetItemString(dict, "__slots__");
1986 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001987 add_dict = 0;
1988 add_weak = 0;
Guido van Rossumad47da02002-08-12 19:05:44 +00001989 may_add_dict = base->tp_dictoffset == 0;
1990 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1991 if (slots == NULL) {
1992 if (may_add_dict) {
1993 add_dict++;
1994 }
1995 if (may_add_weak) {
1996 add_weak++;
1997 }
1998 }
1999 else {
2000 /* Have slots */
2001
Tim Peters6d6c1a32001-08-02 04:15:00 +00002002 /* Make it into a tuple */
Neal Norwitz80e7f272007-08-26 06:45:23 +00002003 if (PyUnicode_Check(slots))
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002004 slots = PyTuple_Pack(1, slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002005 else
2006 slots = PySequence_Tuple(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002007 if (slots == NULL) {
2008 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002009 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002010 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002011 assert(PyTuple_Check(slots));
2012
2013 /* Are slots allowed? */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002014 nslots = PyTuple_GET_SIZE(slots);
Jeremy Hylton1c7a0ea2003-07-16 16:08:23 +00002015 if (nslots > 0 && base->tp_itemsize != 0) {
Guido van Rossumc4141872001-08-30 04:43:35 +00002016 PyErr_Format(PyExc_TypeError,
2017 "nonempty __slots__ "
2018 "not supported for subtype of '%s'",
2019 base->tp_name);
Guido van Rossumad47da02002-08-12 19:05:44 +00002020 bad_slots:
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002021 Py_DECREF(bases);
Guido van Rossumad47da02002-08-12 19:05:44 +00002022 Py_DECREF(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00002023 return NULL;
2024 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002025
2026 /* Check for valid slot names and two special cases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002027 for (i = 0; i < nslots; i++) {
Guido van Rossumad47da02002-08-12 19:05:44 +00002028 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
Guido van Rossumad47da02002-08-12 19:05:44 +00002029 if (!valid_identifier(tmp))
2030 goto bad_slots;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002031 assert(PyUnicode_Check(tmp));
2032 if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00002033 if (!may_add_dict || add_dict) {
2034 PyErr_SetString(PyExc_TypeError,
2035 "__dict__ slot disallowed: "
2036 "we already got one");
2037 goto bad_slots;
2038 }
2039 add_dict++;
2040 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002041 if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00002042 if (!may_add_weak || add_weak) {
2043 PyErr_SetString(PyExc_TypeError,
2044 "__weakref__ slot disallowed: "
2045 "either we already got one, "
2046 "or __itemsize__ != 0");
2047 goto bad_slots;
2048 }
2049 add_weak++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002050 }
2051 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002052
Guido van Rossumd8faa362007-04-27 19:54:29 +00002053 /* Copy slots into a list, mangle names and sort them.
2054 Sorted names are needed for __class__ assignment.
2055 Convert them back to tuple at the end.
2056 */
2057 newslots = PyList_New(nslots - add_dict - add_weak);
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002058 if (newslots == NULL)
Guido van Rossumad47da02002-08-12 19:05:44 +00002059 goto bad_slots;
2060 for (i = j = 0; i < nslots; i++) {
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002061 tmp = PyTuple_GET_ITEM(slots, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002062 if ((add_dict &&
2063 PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) ||
2064 (add_weak &&
2065 PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0))
Guido van Rossumad47da02002-08-12 19:05:44 +00002066 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 tmp =_Py_Mangle(name, tmp);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002068 if (!tmp)
2069 goto bad_slots;
2070 PyList_SET_ITEM(newslots, j, tmp);
Guido van Rossumad47da02002-08-12 19:05:44 +00002071 j++;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002072 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002073 assert(j == nslots - add_dict - add_weak);
2074 nslots = j;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002075 Py_DECREF(slots);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002076 if (PyList_Sort(newslots) == -1) {
2077 Py_DECREF(bases);
2078 Py_DECREF(newslots);
2079 return NULL;
2080 }
2081 slots = PyList_AsTuple(newslots);
2082 Py_DECREF(newslots);
2083 if (slots == NULL) {
2084 Py_DECREF(bases);
2085 return NULL;
2086 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002087
Guido van Rossumad47da02002-08-12 19:05:44 +00002088 /* Secondary bases may provide weakrefs or dict */
2089 if (nbases > 1 &&
2090 ((may_add_dict && !add_dict) ||
2091 (may_add_weak && !add_weak))) {
2092 for (i = 0; i < nbases; i++) {
2093 tmp = PyTuple_GET_ITEM(bases, i);
2094 if (tmp == (PyObject *)base)
2095 continue; /* Skip primary base */
Guido van Rossumad47da02002-08-12 19:05:44 +00002096 assert(PyType_Check(tmp));
2097 tmptype = (PyTypeObject *)tmp;
2098 if (may_add_dict && !add_dict &&
2099 tmptype->tp_dictoffset != 0)
2100 add_dict++;
2101 if (may_add_weak && !add_weak &&
2102 tmptype->tp_weaklistoffset != 0)
2103 add_weak++;
2104 if (may_add_dict && !add_dict)
2105 continue;
2106 if (may_add_weak && !add_weak)
2107 continue;
2108 /* Nothing more to check */
2109 break;
2110 }
2111 }
Guido van Rossum9676b222001-08-17 20:32:36 +00002112 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002113
2114 /* XXX From here until type is safely allocated,
2115 "return NULL" may leak slots! */
2116
2117 /* Allocate the type object */
2118 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
Guido van Rossumad47da02002-08-12 19:05:44 +00002119 if (type == NULL) {
2120 Py_XDECREF(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002121 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002122 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00002123 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002124
2125 /* Keep name and slots alive in the extended type object */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002126 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002127 Py_INCREF(name);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002128 et->ht_name = name;
2129 et->ht_slots = slots;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002130
Guido van Rossumdc91b992001-08-08 22:26:22 +00002131 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002132 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2133 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00002134 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2135 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00002136
Guido van Rossumdc91b992001-08-08 22:26:22 +00002137 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002138 type->tp_as_number = &et->as_number;
2139 type->tp_as_sequence = &et->as_sequence;
2140 type->tp_as_mapping = &et->as_mapping;
2141 type->tp_as_buffer = &et->as_buffer;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002142 type->tp_name = _PyUnicode_AsString(name);
Neal Norwitz80e7f272007-08-26 06:45:23 +00002143 if (!type->tp_name) {
2144 Py_DECREF(type);
2145 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002146 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002147
2148 /* Set tp_base and tp_bases */
2149 type->tp_bases = bases;
2150 Py_INCREF(base);
2151 type->tp_base = base;
2152
Guido van Rossum687ae002001-10-15 22:03:32 +00002153 /* Initialize tp_dict from passed-in dict */
2154 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002155 if (dict == NULL) {
2156 Py_DECREF(type);
2157 return NULL;
2158 }
2159
Guido van Rossumc3542212001-08-16 09:18:56 +00002160 /* Set __module__ in the dict */
2161 if (PyDict_GetItemString(dict, "__module__") == NULL) {
2162 tmp = PyEval_GetGlobals();
2163 if (tmp != NULL) {
2164 tmp = PyDict_GetItemString(tmp, "__name__");
2165 if (tmp != NULL) {
2166 if (PyDict_SetItemString(dict, "__module__",
2167 tmp) < 0)
2168 return NULL;
2169 }
2170 }
2171 }
2172
Tim Peters2f93e282001-10-04 05:27:00 +00002173 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00002174 and is a string. The __doc__ accessor will first look for tp_doc;
2175 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00002176 */
2177 {
2178 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002179 if (doc != NULL && PyUnicode_Check(doc)) {
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002180 Py_ssize_t len;
2181 char *doc_str;
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002182 char *tp_doc;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002183
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002184 doc_str = _PyUnicode_AsStringAndSize(doc, &len);
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002185 if (doc_str == NULL) {
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002186 Py_DECREF(type);
2187 return NULL;
Tim Peters2f93e282001-10-04 05:27:00 +00002188 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002189 if ((Py_ssize_t)strlen(doc_str) != len) {
2190 PyErr_SetString(PyExc_TypeError,
2191 "__doc__ contains null-bytes");
2192 Py_DECREF(type);
2193 return NULL;
2194 }
2195 tp_doc = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002196 if (tp_doc == NULL) {
2197 Py_DECREF(type);
2198 return NULL;
Neal Norwitza369c5a2007-08-25 07:41:59 +00002199 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002200 memcpy(tp_doc, doc_str, len + 1);
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002201 type->tp_doc = tp_doc;
Tim Peters2f93e282001-10-04 05:27:00 +00002202 }
2203 }
2204
Tim Peters6d6c1a32001-08-02 04:15:00 +00002205 /* Special-case __new__: if it's a plain function,
2206 make it a static function */
2207 tmp = PyDict_GetItemString(dict, "__new__");
2208 if (tmp != NULL && PyFunction_Check(tmp)) {
2209 tmp = PyStaticMethod_New(tmp);
2210 if (tmp == NULL) {
2211 Py_DECREF(type);
2212 return NULL;
2213 }
2214 PyDict_SetItemString(dict, "__new__", tmp);
2215 Py_DECREF(tmp);
2216 }
2217
2218 /* Add descriptors for custom slots from __slots__, or for __dict__ */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002219 mp = PyHeapType_GET_MEMBERS(et);
Neil Schemenauerc806c882001-08-29 23:54:54 +00002220 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002221 if (slots != NULL) {
2222 for (i = 0; i < nslots; i++, mp++) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002223 mp->name = _PyUnicode_AsString(
Tim Peters6d6c1a32001-08-02 04:15:00 +00002224 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00002225 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002226 mp->offset = slotoffset;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002227
2228 /* __dict__ and __weakref__ are already filtered out */
2229 assert(strcmp(mp->name, "__dict__") != 0);
2230 assert(strcmp(mp->name, "__weakref__") != 0);
2231
Tim Peters6d6c1a32001-08-02 04:15:00 +00002232 slotoffset += sizeof(PyObject *);
2233 }
2234 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002235 if (add_dict) {
2236 if (base->tp_itemsize)
2237 type->tp_dictoffset = -(long)sizeof(PyObject *);
2238 else
2239 type->tp_dictoffset = slotoffset;
2240 slotoffset += sizeof(PyObject *);
2241 }
2242 if (add_weak) {
2243 assert(!base->tp_itemsize);
2244 type->tp_weaklistoffset = slotoffset;
2245 slotoffset += sizeof(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002246 }
2247 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00002248 type->tp_itemsize = base->tp_itemsize;
Guido van Rossume5c691a2003-03-07 15:13:17 +00002249 type->tp_members = PyHeapType_GET_MEMBERS(et);
Guido van Rossum373c7412003-01-07 13:41:37 +00002250
2251 if (type->tp_weaklistoffset && type->tp_dictoffset)
2252 type->tp_getset = subtype_getsets_full;
2253 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
2254 type->tp_getset = subtype_getsets_weakref_only;
2255 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
2256 type->tp_getset = subtype_getsets_dict_only;
2257 else
2258 type->tp_getset = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002259
2260 /* Special case some slots */
2261 if (type->tp_dictoffset != 0 || nslots > 0) {
2262 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
2263 type->tp_getattro = PyObject_GenericGetAttr;
2264 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
2265 type->tp_setattro = PyObject_GenericSetAttr;
2266 }
2267 type->tp_dealloc = subtype_dealloc;
2268
Guido van Rossum9475a232001-10-05 20:51:39 +00002269 /* Enable GC unless there are really no instance variables possible */
2270 if (!(type->tp_basicsize == sizeof(PyObject) &&
2271 type->tp_itemsize == 0))
2272 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2273
Tim Peters6d6c1a32001-08-02 04:15:00 +00002274 /* Always override allocation strategy to use regular heap */
2275 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00002276 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002277 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00002278 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00002279 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00002280 }
2281 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002282 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002283
2284 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002285 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286 Py_DECREF(type);
2287 return NULL;
2288 }
2289
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002290 /* Put the proper slots in place */
2291 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00002292
Tim Peters6d6c1a32001-08-02 04:15:00 +00002293 return (PyObject *)type;
2294}
2295
2296/* Internal API to look for a name through the MRO.
2297 This returns a borrowed reference, and doesn't set an exception! */
2298PyObject *
2299_PyType_Lookup(PyTypeObject *type, PyObject *name)
2300{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002301 Py_ssize_t i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00002302 PyObject *mro, *res, *base, *dict;
Christian Heimesa62da1d2008-01-12 19:39:10 +00002303 unsigned int h;
2304
2305 if (MCACHE_CACHEABLE_NAME(name) &&
Christian Heimes412dc9c2008-01-27 18:55:54 +00002306 PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
Christian Heimesa62da1d2008-01-12 19:39:10 +00002307 /* fast path */
2308 h = MCACHE_HASH_METHOD(type, name);
2309 if (method_cache[h].version == type->tp_version_tag &&
2310 method_cache[h].name == name)
2311 return method_cache[h].value;
2312 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002313
Guido van Rossum687ae002001-10-15 22:03:32 +00002314 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002315 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00002316
2317 /* If mro is NULL, the type is either not yet initialized
2318 by PyType_Ready(), or already cleared by type_clear().
2319 Either way the safest thing to do is to return NULL. */
2320 if (mro == NULL)
2321 return NULL;
2322
Christian Heimesa62da1d2008-01-12 19:39:10 +00002323 res = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002324 assert(PyTuple_Check(mro));
2325 n = PyTuple_GET_SIZE(mro);
2326 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002327 base = PyTuple_GET_ITEM(mro, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002328 assert(PyType_Check(base));
2329 dict = ((PyTypeObject *)base)->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002330 assert(dict && PyDict_Check(dict));
2331 res = PyDict_GetItem(dict, name);
2332 if (res != NULL)
Christian Heimesa62da1d2008-01-12 19:39:10 +00002333 break;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002334 }
Christian Heimesa62da1d2008-01-12 19:39:10 +00002335
2336 if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
2337 h = MCACHE_HASH_METHOD(type, name);
2338 method_cache[h].version = type->tp_version_tag;
2339 method_cache[h].value = res; /* borrowed */
2340 Py_INCREF(name);
2341 Py_DECREF(method_cache[h].name);
2342 method_cache[h].name = name;
2343 }
2344 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002345}
2346
2347/* This is similar to PyObject_GenericGetAttr(),
2348 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2349static PyObject *
2350type_getattro(PyTypeObject *type, PyObject *name)
2351{
Christian Heimes90aa7642007-12-19 02:45:37 +00002352 PyTypeObject *metatype = Py_TYPE(type);
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002353 PyObject *meta_attribute, *attribute;
2354 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002355
2356 /* Initialize this type (we'll assume the metatype is initialized) */
2357 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002358 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002359 return NULL;
2360 }
2361
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002362 /* No readable descriptor found yet */
2363 meta_get = NULL;
Tim Peters34592512002-07-11 06:23:50 +00002364
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002365 /* Look for the attribute in the metatype */
2366 meta_attribute = _PyType_Lookup(metatype, name);
2367
2368 if (meta_attribute != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +00002369 meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
Tim Peters34592512002-07-11 06:23:50 +00002370
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002371 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2372 /* Data descriptors implement tp_descr_set to intercept
2373 * writes. Assume the attribute is not overridden in
2374 * type's tp_dict (and bases): call the descriptor now.
2375 */
2376 return meta_get(meta_attribute, (PyObject *)type,
2377 (PyObject *)metatype);
2378 }
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002379 Py_INCREF(meta_attribute);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002380 }
2381
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002382 /* No data descriptor found on metatype. Look in tp_dict of this
2383 * type and its bases */
2384 attribute = _PyType_Lookup(type, name);
2385 if (attribute != NULL) {
2386 /* Implement descriptor functionality, if any */
Christian Heimes90aa7642007-12-19 02:45:37 +00002387 descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002388
2389 Py_XDECREF(meta_attribute);
2390
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002391 if (local_get != NULL) {
2392 /* NULL 2nd argument indicates the descriptor was
2393 * found on the target object itself (or a base) */
2394 return local_get(attribute, (PyObject *)NULL,
2395 (PyObject *)type);
2396 }
Tim Peters34592512002-07-11 06:23:50 +00002397
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002398 Py_INCREF(attribute);
2399 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002400 }
2401
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002402 /* No attribute found in local __dict__ (or bases): use the
2403 * descriptor from the metatype, if any */
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002404 if (meta_get != NULL) {
2405 PyObject *res;
2406 res = meta_get(meta_attribute, (PyObject *)type,
2407 (PyObject *)metatype);
2408 Py_DECREF(meta_attribute);
2409 return res;
2410 }
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002411
2412 /* If an ordinary attribute was found on the metatype, return it now */
2413 if (meta_attribute != NULL) {
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002414 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002415 }
2416
2417 /* Give up */
2418 PyErr_Format(PyExc_AttributeError,
Walter Dörwald75163602007-06-11 15:47:13 +00002419 "type object '%.50s' has no attribute '%U'",
2420 type->tp_name, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002421 return NULL;
2422}
2423
2424static int
2425type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2426{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002427 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2428 PyErr_Format(
2429 PyExc_TypeError,
2430 "can't set attributes of built-in/extension type '%s'",
2431 type->tp_name);
2432 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002433 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002434 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2435 return -1;
2436 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002437}
2438
2439static void
2440type_dealloc(PyTypeObject *type)
2441{
Guido van Rossume5c691a2003-03-07 15:13:17 +00002442 PyHeapTypeObject *et;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002443
2444 /* Assert this is a heap-allocated type object */
2445 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002446 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00002447 PyObject_ClearWeakRefs((PyObject *)type);
Guido van Rossume5c691a2003-03-07 15:13:17 +00002448 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002449 Py_XDECREF(type->tp_base);
2450 Py_XDECREF(type->tp_dict);
2451 Py_XDECREF(type->tp_bases);
2452 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00002453 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00002454 Py_XDECREF(type->tp_subclasses);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002455 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2456 * of most other objects. It's okay to cast it to char *.
2457 */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002458 PyObject_Free((char *)type->tp_doc);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002459 Py_XDECREF(et->ht_name);
2460 Py_XDECREF(et->ht_slots);
Christian Heimes90aa7642007-12-19 02:45:37 +00002461 Py_TYPE(type)->tp_free((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002462}
2463
Guido van Rossum1c450732001-10-08 15:18:27 +00002464static PyObject *
2465type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2466{
2467 PyObject *list, *raw, *ref;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002468 Py_ssize_t i, n;
Guido van Rossum1c450732001-10-08 15:18:27 +00002469
2470 list = PyList_New(0);
2471 if (list == NULL)
2472 return NULL;
2473 raw = type->tp_subclasses;
2474 if (raw == NULL)
2475 return list;
2476 assert(PyList_Check(raw));
2477 n = PyList_GET_SIZE(raw);
2478 for (i = 0; i < n; i++) {
2479 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00002480 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00002481 ref = PyWeakref_GET_OBJECT(ref);
2482 if (ref != Py_None) {
2483 if (PyList_Append(list, ref) < 0) {
2484 Py_DECREF(list);
2485 return NULL;
2486 }
2487 }
2488 }
2489 return list;
2490}
2491
Guido van Rossum47374822007-08-02 16:48:17 +00002492static PyObject *
2493type_prepare(PyObject *self, PyObject *args, PyObject *kwds)
2494{
2495 return PyDict_New();
2496}
2497
Tim Peters6d6c1a32001-08-02 04:15:00 +00002498static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002499 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002500 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
Guido van Rossum1c450732001-10-08 15:18:27 +00002501 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002502 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
Guido van Rossum47374822007-08-02 16:48:17 +00002503 {"__prepare__", (PyCFunction)type_prepare,
2504 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
2505 PyDoc_STR("__prepare__() -> dict\n"
2506 "used to create the namespace for the class statement")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002507 {0}
2508};
2509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002510PyDoc_STRVAR(type_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002511"type(object) -> the object's type\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002512"type(name, bases, dict) -> a new type");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002513
Guido van Rossum048eb752001-10-02 21:24:57 +00002514static int
2515type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2516{
Guido van Rossuma3862092002-06-10 15:24:42 +00002517 /* Because of type_is_gc(), the collector only calls this
2518 for heaptypes. */
2519 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002520
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002521 Py_VISIT(type->tp_dict);
2522 Py_VISIT(type->tp_cache);
2523 Py_VISIT(type->tp_mro);
2524 Py_VISIT(type->tp_bases);
2525 Py_VISIT(type->tp_base);
Guido van Rossuma3862092002-06-10 15:24:42 +00002526
2527 /* There's no need to visit type->tp_subclasses or
Georg Brandlc255c7b2006-02-20 22:27:28 +00002528 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
Guido van Rossuma3862092002-06-10 15:24:42 +00002529 in cycles; tp_subclasses is a list of weak references,
2530 and slots is a tuple of strings. */
Guido van Rossum048eb752001-10-02 21:24:57 +00002531
Guido van Rossum048eb752001-10-02 21:24:57 +00002532 return 0;
2533}
2534
2535static int
2536type_clear(PyTypeObject *type)
2537{
Guido van Rossuma3862092002-06-10 15:24:42 +00002538 /* Because of type_is_gc(), the collector only calls this
2539 for heaptypes. */
2540 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002541
Guido van Rossuma3862092002-06-10 15:24:42 +00002542 /* The only field we need to clear is tp_mro, which is part of a
2543 hard cycle (its first element is the class itself) that won't
2544 be broken otherwise (it's a tuple and tuples don't have a
2545 tp_clear handler). None of the other fields need to be
2546 cleared, and here's why:
Guido van Rossum048eb752001-10-02 21:24:57 +00002547
Guido van Rossuma3862092002-06-10 15:24:42 +00002548 tp_dict:
2549 It is a dict, so the collector will call its tp_clear.
2550
2551 tp_cache:
2552 Not used; if it were, it would be a dict.
2553
2554 tp_bases, tp_base:
2555 If these are involved in a cycle, there must be at least
2556 one other, mutable object in the cycle, e.g. a base
2557 class's dict; the cycle will be broken that way.
2558
2559 tp_subclasses:
2560 A list of weak references can't be part of a cycle; and
2561 lists have their own tp_clear.
2562
Guido van Rossume5c691a2003-03-07 15:13:17 +00002563 slots (in PyHeapTypeObject):
Guido van Rossuma3862092002-06-10 15:24:42 +00002564 A tuple of strings can't be part of a cycle.
2565 */
2566
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002567 Py_CLEAR(type->tp_mro);
Guido van Rossum048eb752001-10-02 21:24:57 +00002568
2569 return 0;
2570}
2571
2572static int
2573type_is_gc(PyTypeObject *type)
2574{
2575 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2576}
2577
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002578PyTypeObject PyType_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002579 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002580 "type", /* tp_name */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002581 sizeof(PyHeapTypeObject), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00002582 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002583 (destructor)type_dealloc, /* tp_dealloc */
2584 0, /* tp_print */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002585 0, /* tp_getattr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002586 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002587 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002588 (reprfunc)type_repr, /* tp_repr */
2589 0, /* tp_as_number */
2590 0, /* tp_as_sequence */
2591 0, /* tp_as_mapping */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002592 0, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002593 (ternaryfunc)type_call, /* tp_call */
2594 0, /* tp_str */
2595 (getattrofunc)type_getattro, /* tp_getattro */
2596 (setattrofunc)type_setattro, /* tp_setattro */
2597 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00002598 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00002599 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002600 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00002601 (traverseproc)type_traverse, /* tp_traverse */
2602 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002603 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00002604 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002605 0, /* tp_iter */
2606 0, /* tp_iternext */
2607 type_methods, /* tp_methods */
2608 type_members, /* tp_members */
2609 type_getsets, /* tp_getset */
2610 0, /* tp_base */
2611 0, /* tp_dict */
2612 0, /* tp_descr_get */
2613 0, /* tp_descr_set */
2614 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002615 type_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002616 0, /* tp_alloc */
2617 type_new, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002618 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00002619 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002620};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002621
2622
2623/* The base type of all types (eventually)... except itself. */
2624
Guido van Rossumd8faa362007-04-27 19:54:29 +00002625/* You may wonder why object.__new__() only complains about arguments
2626 when object.__init__() is not overridden, and vice versa.
2627
2628 Consider the use cases:
2629
2630 1. When neither is overridden, we want to hear complaints about
2631 excess (i.e., any) arguments, since their presence could
2632 indicate there's a bug.
2633
2634 2. When defining an Immutable type, we are likely to override only
2635 __new__(), since __init__() is called too late to initialize an
2636 Immutable object. Since __new__() defines the signature for the
2637 type, it would be a pain to have to override __init__() just to
2638 stop it from complaining about excess arguments.
2639
2640 3. When defining a Mutable type, we are likely to override only
2641 __init__(). So here the converse reasoning applies: we don't
2642 want to have to override __new__() just to stop it from
2643 complaining.
2644
2645 4. When __init__() is overridden, and the subclass __init__() calls
2646 object.__init__(), the latter should complain about excess
2647 arguments; ditto for __new__().
2648
2649 Use cases 2 and 3 make it unattractive to unconditionally check for
2650 excess arguments. The best solution that addresses all four use
2651 cases is as follows: __init__() complains about excess arguments
2652 unless __new__() is overridden and __init__() is not overridden
2653 (IOW, if __init__() is overridden or __new__() is not overridden);
2654 symmetrically, __new__() complains about excess arguments unless
2655 __init__() is overridden and __new__() is not overridden
2656 (IOW, if __new__() is overridden or __init__() is not overridden).
2657
2658 However, for backwards compatibility, this breaks too much code.
2659 Therefore, in 2.6, we'll *warn* about excess arguments when both
2660 methods are overridden; for all other cases we'll use the above
2661 rules.
2662
2663*/
2664
2665/* Forward */
2666static PyObject *
2667object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2668
2669static int
2670excess_args(PyObject *args, PyObject *kwds)
2671{
2672 return PyTuple_GET_SIZE(args) ||
2673 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
2674}
2675
Tim Peters6d6c1a32001-08-02 04:15:00 +00002676static int
2677object_init(PyObject *self, PyObject *args, PyObject *kwds)
2678{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002679 int err = 0;
2680 if (excess_args(args, kwds)) {
Christian Heimes90aa7642007-12-19 02:45:37 +00002681 PyTypeObject *type = Py_TYPE(self);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002682 if (type->tp_init != object_init &&
2683 type->tp_new != object_new)
2684 {
2685 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2686 "object.__init__() takes no parameters",
2687 1);
2688 }
2689 else if (type->tp_init != object_init ||
2690 type->tp_new == object_new)
2691 {
2692 PyErr_SetString(PyExc_TypeError,
2693 "object.__init__() takes no parameters");
2694 err = -1;
2695 }
2696 }
2697 return err;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002698}
2699
Guido van Rossum298e4212003-02-13 16:30:16 +00002700static PyObject *
2701object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2702{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002703 int err = 0;
2704 if (excess_args(args, kwds)) {
2705 if (type->tp_new != object_new &&
2706 type->tp_init != object_init)
2707 {
2708 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2709 "object.__new__() takes no parameters",
2710 1);
2711 }
2712 else if (type->tp_new != object_new ||
2713 type->tp_init == object_init)
2714 {
2715 PyErr_SetString(PyExc_TypeError,
2716 "object.__new__() takes no parameters");
2717 err = -1;
2718 }
Guido van Rossum298e4212003-02-13 16:30:16 +00002719 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002720 if (err < 0)
2721 return NULL;
Christian Heimes9e7f1d22008-02-28 12:27:11 +00002722
2723 if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
2724 static PyObject *comma = NULL;
2725 PyObject *abstract_methods = NULL;
2726 PyObject *builtins;
2727 PyObject *sorted;
2728 PyObject *sorted_methods = NULL;
2729 PyObject *joined = NULL;
2730
2731 /* Compute ", ".join(sorted(type.__abstractmethods__))
2732 into joined. */
2733 abstract_methods = type_abstractmethods(type, NULL);
2734 if (abstract_methods == NULL)
2735 goto error;
2736 builtins = PyEval_GetBuiltins();
2737 if (builtins == NULL)
2738 goto error;
2739 sorted = PyDict_GetItemString(builtins, "sorted");
2740 if (sorted == NULL)
2741 goto error;
2742 sorted_methods = PyObject_CallFunctionObjArgs(sorted,
2743 abstract_methods,
2744 NULL);
2745 if (sorted_methods == NULL)
2746 goto error;
2747 if (comma == NULL) {
2748 comma = PyUnicode_InternFromString(", ");
2749 if (comma == NULL)
2750 goto error;
2751 }
2752 joined = PyObject_CallMethod(comma, "join",
2753 "O", sorted_methods);
2754 if (joined == NULL)
2755 goto error;
2756
2757 PyErr_Format(PyExc_TypeError,
2758 "Can't instantiate abstract class %s "
2759 "with abstract methods %U",
2760 type->tp_name,
2761 joined);
2762 error:
2763 Py_XDECREF(joined);
2764 Py_XDECREF(sorted_methods);
2765 Py_XDECREF(abstract_methods);
2766 return NULL;
2767 }
Guido van Rossum298e4212003-02-13 16:30:16 +00002768 return type->tp_alloc(type, 0);
2769}
2770
Tim Peters6d6c1a32001-08-02 04:15:00 +00002771static void
2772object_dealloc(PyObject *self)
2773{
Christian Heimes90aa7642007-12-19 02:45:37 +00002774 Py_TYPE(self)->tp_free(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002775}
2776
Guido van Rossum8e248182001-08-12 05:17:56 +00002777static PyObject *
2778object_repr(PyObject *self)
2779{
Guido van Rossum76e69632001-08-16 18:52:43 +00002780 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00002781 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002782
Christian Heimes90aa7642007-12-19 02:45:37 +00002783 type = Py_TYPE(self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002784 mod = type_module(type, NULL);
2785 if (mod == NULL)
2786 PyErr_Clear();
Martin v. Löwis5b222132007-06-10 09:51:05 +00002787 else if (!PyUnicode_Check(mod)) {
Guido van Rossum76e69632001-08-16 18:52:43 +00002788 Py_DECREF(mod);
2789 mod = NULL;
2790 }
2791 name = type_name(type, NULL);
2792 if (name == NULL)
2793 return NULL;
Georg Brandl1a3284e2007-12-02 09:40:06 +00002794 if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
Walter Dörwald4dbd01b2007-06-11 14:03:45 +00002795 rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002796 else
Walter Dörwald1ab83302007-05-18 17:15:44 +00002797 rtn = PyUnicode_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002798 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002799 Py_XDECREF(mod);
2800 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00002801 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002802}
2803
Guido van Rossumb8f63662001-08-15 23:57:02 +00002804static PyObject *
2805object_str(PyObject *self)
2806{
2807 unaryfunc f;
2808
Christian Heimes90aa7642007-12-19 02:45:37 +00002809 f = Py_TYPE(self)->tp_repr;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002810 if (f == NULL)
2811 f = object_repr;
2812 return f(self);
2813}
2814
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002815static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002816object_richcompare(PyObject *self, PyObject *other, int op)
2817{
2818 PyObject *res;
2819
2820 switch (op) {
2821
2822 case Py_EQ:
Guido van Rossumab078dd2008-01-06 00:09:11 +00002823 /* Return NotImplemented instead of False, so if two
2824 objects are compared, both get a chance at the
2825 comparison. See issue #1393. */
2826 res = (self == other) ? Py_True : Py_NotImplemented;
Guido van Rossum6b18a5b2007-03-29 20:49:57 +00002827 Py_INCREF(res);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002828 break;
2829
2830 case Py_NE:
Guido van Rossume27dc722007-03-27 22:37:34 +00002831 /* By default, != returns the opposite of ==,
2832 unless the latter returns NotImplemented. */
2833 res = PyObject_RichCompare(self, other, Py_EQ);
2834 if (res != NULL && res != Py_NotImplemented) {
2835 int ok = PyObject_IsTrue(res);
2836 Py_DECREF(res);
2837 if (ok < 0)
2838 res = NULL;
2839 else {
2840 if (ok)
2841 res = Py_False;
2842 else
2843 res = Py_True;
2844 Py_INCREF(res);
2845 }
2846 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002847 break;
2848
2849 default:
2850 res = Py_NotImplemented;
Guido van Rossum6b18a5b2007-03-29 20:49:57 +00002851 Py_INCREF(res);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002852 break;
2853 }
2854
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002855 return res;
2856}
2857
2858static PyObject *
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002859object_get_class(PyObject *self, void *closure)
2860{
Christian Heimes90aa7642007-12-19 02:45:37 +00002861 Py_INCREF(Py_TYPE(self));
2862 return (PyObject *)(Py_TYPE(self));
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002863}
2864
2865static int
2866equiv_structs(PyTypeObject *a, PyTypeObject *b)
2867{
2868 return a == b ||
2869 (a != NULL &&
2870 b != NULL &&
2871 a->tp_basicsize == b->tp_basicsize &&
2872 a->tp_itemsize == b->tp_itemsize &&
2873 a->tp_dictoffset == b->tp_dictoffset &&
2874 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2875 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2876 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2877}
2878
2879static int
2880same_slots_added(PyTypeObject *a, PyTypeObject *b)
2881{
2882 PyTypeObject *base = a->tp_base;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002883 Py_ssize_t size;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002884 PyObject *slots_a, *slots_b;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002885
2886 if (base != b->tp_base)
2887 return 0;
2888 if (equiv_structs(a, base) && equiv_structs(b, base))
2889 return 1;
2890 size = base->tp_basicsize;
2891 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2892 size += sizeof(PyObject *);
2893 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2894 size += sizeof(PyObject *);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002895
2896 /* Check slots compliance */
2897 slots_a = ((PyHeapTypeObject *)a)->ht_slots;
2898 slots_b = ((PyHeapTypeObject *)b)->ht_slots;
2899 if (slots_a && slots_b) {
Mark Dickinsonf02e0aa2009-02-01 12:13:56 +00002900 if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002901 return 0;
2902 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
2903 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002904 return size == a->tp_basicsize && size == b->tp_basicsize;
2905}
2906
2907static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002908compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002909{
2910 PyTypeObject *newbase, *oldbase;
2911
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002912 if (newto->tp_dealloc != oldto->tp_dealloc ||
2913 newto->tp_free != oldto->tp_free)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002914 {
2915 PyErr_Format(PyExc_TypeError,
2916 "%s assignment: "
2917 "'%s' deallocator differs from '%s'",
2918 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002919 newto->tp_name,
2920 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002921 return 0;
2922 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002923 newbase = newto;
2924 oldbase = oldto;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002925 while (equiv_structs(newbase, newbase->tp_base))
2926 newbase = newbase->tp_base;
2927 while (equiv_structs(oldbase, oldbase->tp_base))
2928 oldbase = oldbase->tp_base;
2929 if (newbase != oldbase &&
2930 (newbase->tp_base != oldbase->tp_base ||
2931 !same_slots_added(newbase, oldbase))) {
2932 PyErr_Format(PyExc_TypeError,
2933 "%s assignment: "
2934 "'%s' object layout differs from '%s'",
2935 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002936 newto->tp_name,
2937 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002938 return 0;
2939 }
Tim Petersea7f75d2002-12-07 21:39:16 +00002940
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002941 return 1;
2942}
2943
2944static int
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002945object_set_class(PyObject *self, PyObject *value, void *closure)
2946{
Christian Heimes90aa7642007-12-19 02:45:37 +00002947 PyTypeObject *oldto = Py_TYPE(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002948 PyTypeObject *newto;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002949
Guido van Rossumb6b89422002-04-15 01:03:30 +00002950 if (value == NULL) {
2951 PyErr_SetString(PyExc_TypeError,
2952 "can't delete __class__ attribute");
2953 return -1;
2954 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002955 if (!PyType_Check(value)) {
2956 PyErr_Format(PyExc_TypeError,
2957 "__class__ must be set to new-style class, not '%s' object",
Christian Heimes90aa7642007-12-19 02:45:37 +00002958 Py_TYPE(value)->tp_name);
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002959 return -1;
2960 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002961 newto = (PyTypeObject *)value;
2962 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2963 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
Guido van Rossum40af8892002-08-10 05:42:07 +00002964 {
2965 PyErr_Format(PyExc_TypeError,
2966 "__class__ assignment: only for heap types");
2967 return -1;
2968 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002969 if (compatible_for_assignment(newto, oldto, "__class__")) {
2970 Py_INCREF(newto);
Christian Heimes90aa7642007-12-19 02:45:37 +00002971 Py_TYPE(self) = newto;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002972 Py_DECREF(oldto);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002973 return 0;
2974 }
2975 else {
Guido van Rossum9ee4b942002-05-24 18:47:47 +00002976 return -1;
2977 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002978}
2979
2980static PyGetSetDef object_getsets[] = {
2981 {"__class__", object_get_class, object_set_class,
Neal Norwitz858e34f2002-08-13 17:18:45 +00002982 PyDoc_STR("the object's class")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002983 {0}
2984};
2985
Guido van Rossumc53f0092003-02-18 22:05:12 +00002986
Guido van Rossum036f9992003-02-21 22:02:54 +00002987/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002988 We fall back to helpers in copyreg for:
Guido van Rossum036f9992003-02-21 22:02:54 +00002989 - pickle protocols < 2
2990 - calculating the list of slot names (done only once per class)
2991 - the __newobj__ function (which is used as a token but never called)
2992*/
2993
2994static PyObject *
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002995import_copyreg(void)
Guido van Rossum036f9992003-02-21 22:02:54 +00002996{
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002997 static PyObject *copyreg_str;
Guido van Rossum3926a632001-09-25 16:25:58 +00002998
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002999 if (!copyreg_str) {
3000 copyreg_str = PyUnicode_InternFromString("copyreg");
3001 if (copyreg_str == NULL)
Guido van Rossum3926a632001-09-25 16:25:58 +00003002 return NULL;
3003 }
Guido van Rossum036f9992003-02-21 22:02:54 +00003004
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003005 return PyImport_Import(copyreg_str);
Guido van Rossum036f9992003-02-21 22:02:54 +00003006}
3007
3008static PyObject *
3009slotnames(PyObject *cls)
3010{
3011 PyObject *clsdict;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003012 PyObject *copyreg;
Guido van Rossum036f9992003-02-21 22:02:54 +00003013 PyObject *slotnames;
3014
3015 if (!PyType_Check(cls)) {
3016 Py_INCREF(Py_None);
3017 return Py_None;
3018 }
3019
3020 clsdict = ((PyTypeObject *)cls)->tp_dict;
3021 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
Armin Rigoec862b92005-09-24 22:58:41 +00003022 if (slotnames != NULL && PyList_Check(slotnames)) {
Guido van Rossum036f9992003-02-21 22:02:54 +00003023 Py_INCREF(slotnames);
3024 return slotnames;
3025 }
3026
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003027 copyreg = import_copyreg();
3028 if (copyreg == NULL)
Guido van Rossum036f9992003-02-21 22:02:54 +00003029 return NULL;
3030
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003031 slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
3032 Py_DECREF(copyreg);
Guido van Rossum036f9992003-02-21 22:02:54 +00003033 if (slotnames != NULL &&
3034 slotnames != Py_None &&
3035 !PyList_Check(slotnames))
3036 {
3037 PyErr_SetString(PyExc_TypeError,
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003038 "copyreg._slotnames didn't return a list or None");
Guido van Rossum036f9992003-02-21 22:02:54 +00003039 Py_DECREF(slotnames);
3040 slotnames = NULL;
3041 }
3042
3043 return slotnames;
3044}
3045
3046static PyObject *
3047reduce_2(PyObject *obj)
3048{
3049 PyObject *cls, *getnewargs;
3050 PyObject *args = NULL, *args2 = NULL;
3051 PyObject *getstate = NULL, *state = NULL, *names = NULL;
3052 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003053 PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003054 Py_ssize_t i, n;
Guido van Rossum036f9992003-02-21 22:02:54 +00003055
3056 cls = PyObject_GetAttrString(obj, "__class__");
3057 if (cls == NULL)
3058 return NULL;
3059
3060 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
3061 if (getnewargs != NULL) {
3062 args = PyObject_CallObject(getnewargs, NULL);
3063 Py_DECREF(getnewargs);
3064 if (args != NULL && !PyTuple_Check(args)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003065 PyErr_Format(PyExc_TypeError,
3066 "__getnewargs__ should return a tuple, "
Christian Heimes90aa7642007-12-19 02:45:37 +00003067 "not '%.200s'", Py_TYPE(args)->tp_name);
Guido van Rossum036f9992003-02-21 22:02:54 +00003068 goto end;
3069 }
3070 }
3071 else {
3072 PyErr_Clear();
3073 args = PyTuple_New(0);
3074 }
3075 if (args == NULL)
3076 goto end;
3077
3078 getstate = PyObject_GetAttrString(obj, "__getstate__");
3079 if (getstate != NULL) {
3080 state = PyObject_CallObject(getstate, NULL);
3081 Py_DECREF(getstate);
Neal Norwitze2fdc612003-06-08 13:19:58 +00003082 if (state == NULL)
3083 goto end;
Guido van Rossum036f9992003-02-21 22:02:54 +00003084 }
3085 else {
Jim Fulton8a1a5942004-02-08 04:21:26 +00003086 PyErr_Clear();
Guido van Rossum036f9992003-02-21 22:02:54 +00003087 state = PyObject_GetAttrString(obj, "__dict__");
3088 if (state == NULL) {
3089 PyErr_Clear();
3090 state = Py_None;
3091 Py_INCREF(state);
3092 }
3093 names = slotnames(cls);
3094 if (names == NULL)
3095 goto end;
3096 if (names != Py_None) {
3097 assert(PyList_Check(names));
3098 slots = PyDict_New();
3099 if (slots == NULL)
3100 goto end;
3101 n = 0;
3102 /* Can't pre-compute the list size; the list
3103 is stored on the class so accessible to other
3104 threads, which may be run by DECREF */
3105 for (i = 0; i < PyList_GET_SIZE(names); i++) {
3106 PyObject *name, *value;
3107 name = PyList_GET_ITEM(names, i);
3108 value = PyObject_GetAttr(obj, name);
3109 if (value == NULL)
3110 PyErr_Clear();
3111 else {
3112 int err = PyDict_SetItem(slots, name,
3113 value);
3114 Py_DECREF(value);
3115 if (err)
3116 goto end;
3117 n++;
3118 }
3119 }
3120 if (n) {
3121 state = Py_BuildValue("(NO)", state, slots);
3122 if (state == NULL)
3123 goto end;
3124 }
3125 }
3126 }
3127
3128 if (!PyList_Check(obj)) {
3129 listitems = Py_None;
3130 Py_INCREF(listitems);
3131 }
3132 else {
3133 listitems = PyObject_GetIter(obj);
3134 if (listitems == NULL)
3135 goto end;
3136 }
3137
3138 if (!PyDict_Check(obj)) {
3139 dictitems = Py_None;
3140 Py_INCREF(dictitems);
3141 }
3142 else {
Guido van Rossumcc2b0162007-02-11 06:12:03 +00003143 PyObject *items = PyObject_CallMethod(obj, "items", "");
3144 if (items == NULL)
3145 goto end;
3146 dictitems = PyObject_GetIter(items);
3147 Py_DECREF(items);
Guido van Rossum036f9992003-02-21 22:02:54 +00003148 if (dictitems == NULL)
3149 goto end;
3150 }
3151
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003152 copyreg = import_copyreg();
3153 if (copyreg == NULL)
Guido van Rossum036f9992003-02-21 22:02:54 +00003154 goto end;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003155 newobj = PyObject_GetAttrString(copyreg, "__newobj__");
Guido van Rossum036f9992003-02-21 22:02:54 +00003156 if (newobj == NULL)
3157 goto end;
3158
3159 n = PyTuple_GET_SIZE(args);
3160 args2 = PyTuple_New(n+1);
3161 if (args2 == NULL)
3162 goto end;
3163 PyTuple_SET_ITEM(args2, 0, cls);
3164 cls = NULL;
3165 for (i = 0; i < n; i++) {
3166 PyObject *v = PyTuple_GET_ITEM(args, i);
3167 Py_INCREF(v);
3168 PyTuple_SET_ITEM(args2, i+1, v);
3169 }
3170
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003171 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
Guido van Rossum036f9992003-02-21 22:02:54 +00003172
3173 end:
3174 Py_XDECREF(cls);
3175 Py_XDECREF(args);
3176 Py_XDECREF(args2);
Jeremy Hyltond06483c2003-04-09 21:01:42 +00003177 Py_XDECREF(slots);
Guido van Rossum036f9992003-02-21 22:02:54 +00003178 Py_XDECREF(state);
3179 Py_XDECREF(names);
3180 Py_XDECREF(listitems);
3181 Py_XDECREF(dictitems);
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003182 Py_XDECREF(copyreg);
Guido van Rossum036f9992003-02-21 22:02:54 +00003183 Py_XDECREF(newobj);
3184 return res;
3185}
3186
Guido van Rossumd8faa362007-04-27 19:54:29 +00003187/*
3188 * There were two problems when object.__reduce__ and object.__reduce_ex__
3189 * were implemented in the same function:
3190 * - trying to pickle an object with a custom __reduce__ method that
3191 * fell back to object.__reduce__ in certain circumstances led to
3192 * infinite recursion at Python level and eventual RuntimeError.
3193 * - Pickling objects that lied about their type by overwriting the
3194 * __class__ descriptor could lead to infinite recursion at C level
3195 * and eventual segfault.
3196 *
3197 * Because of backwards compatibility, the two methods still have to
3198 * behave in the same way, even if this is not required by the pickle
3199 * protocol. This common functionality was moved to the _common_reduce
3200 * function.
3201 */
3202static PyObject *
3203_common_reduce(PyObject *self, int proto)
3204{
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003205 PyObject *copyreg, *res;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003206
3207 if (proto >= 2)
3208 return reduce_2(self);
3209
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003210 copyreg = import_copyreg();
3211 if (!copyreg)
Guido van Rossumd8faa362007-04-27 19:54:29 +00003212 return NULL;
3213
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003214 res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
3215 Py_DECREF(copyreg);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003216
3217 return res;
3218}
3219
3220static PyObject *
3221object_reduce(PyObject *self, PyObject *args)
3222{
3223 int proto = 0;
3224
3225 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
3226 return NULL;
3227
3228 return _common_reduce(self, proto);
3229}
3230
Guido van Rossum036f9992003-02-21 22:02:54 +00003231static PyObject *
3232object_reduce_ex(PyObject *self, PyObject *args)
3233{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003234 PyObject *reduce, *res;
Guido van Rossum036f9992003-02-21 22:02:54 +00003235 int proto = 0;
3236
3237 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
3238 return NULL;
3239
3240 reduce = PyObject_GetAttrString(self, "__reduce__");
3241 if (reduce == NULL)
3242 PyErr_Clear();
3243 else {
3244 PyObject *cls, *clsreduce, *objreduce;
3245 int override;
3246 cls = PyObject_GetAttrString(self, "__class__");
3247 if (cls == NULL) {
3248 Py_DECREF(reduce);
3249 return NULL;
3250 }
3251 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
3252 Py_DECREF(cls);
3253 if (clsreduce == NULL) {
3254 Py_DECREF(reduce);
3255 return NULL;
3256 }
3257 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
3258 "__reduce__");
3259 override = (clsreduce != objreduce);
3260 Py_DECREF(clsreduce);
3261 if (override) {
3262 res = PyObject_CallObject(reduce, NULL);
3263 Py_DECREF(reduce);
3264 return res;
3265 }
3266 else
3267 Py_DECREF(reduce);
3268 }
3269
Guido van Rossumd8faa362007-04-27 19:54:29 +00003270 return _common_reduce(self, proto);
Guido van Rossum3926a632001-09-25 16:25:58 +00003271}
3272
Christian Heimes9e7f1d22008-02-28 12:27:11 +00003273static PyObject *
3274object_subclasshook(PyObject *cls, PyObject *args)
3275{
3276 Py_INCREF(Py_NotImplemented);
3277 return Py_NotImplemented;
3278}
3279
3280PyDoc_STRVAR(object_subclasshook_doc,
3281"Abstract classes can override this to customize issubclass().\n"
3282"\n"
3283"This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
3284"It should return True, False or NotImplemented. If it returns\n"
3285"NotImplemented, the normal algorithm is used. Otherwise, it\n"
3286"overrides the normal algorithm (and the outcome is cached).\n");
Eric Smith8c663262007-08-25 02:26:07 +00003287
3288/*
3289 from PEP 3101, this code implements:
3290
3291 class object:
3292 def __format__(self, format_spec):
3293 return format(str(self), format_spec)
3294*/
3295static PyObject *
3296object_format(PyObject *self, PyObject *args)
3297{
3298 PyObject *format_spec;
3299 PyObject *self_as_str = NULL;
3300 PyObject *result = NULL;
3301 PyObject *format_meth = NULL;
3302
Eric Smithfc6e8fe2008-01-11 00:17:22 +00003303 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
Eric Smith8c663262007-08-25 02:26:07 +00003304 return NULL;
Eric Smith8c663262007-08-25 02:26:07 +00003305
Thomas Heller519a0422007-11-15 20:48:54 +00003306 self_as_str = PyObject_Str(self);
Eric Smith8c663262007-08-25 02:26:07 +00003307 if (self_as_str != NULL) {
3308 /* find the format function */
3309 format_meth = PyObject_GetAttrString(self_as_str, "__format__");
3310 if (format_meth != NULL) {
3311 /* and call it */
3312 result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL);
3313 }
3314 }
3315
3316 Py_XDECREF(self_as_str);
3317 Py_XDECREF(format_meth);
3318
3319 return result;
3320}
3321
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003322static PyObject *
3323object_sizeof(PyObject *self, PyObject *args)
3324{
3325 Py_ssize_t res, isize;
3326
3327 res = 0;
3328 isize = self->ob_type->tp_itemsize;
3329 if (isize > 0)
3330 res = Py_SIZE(self->ob_type) * isize;
3331 res += self->ob_type->tp_basicsize;
3332
3333 return PyLong_FromSsize_t(res);
3334}
3335
Guido van Rossum3926a632001-09-25 16:25:58 +00003336static PyMethodDef object_methods[] = {
Guido van Rossumc53f0092003-02-18 22:05:12 +00003337 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
3338 PyDoc_STR("helper for pickle")},
Guido van Rossumd8faa362007-04-27 19:54:29 +00003339 {"__reduce__", object_reduce, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003340 PyDoc_STR("helper for pickle")},
Christian Heimes9e7f1d22008-02-28 12:27:11 +00003341 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
3342 object_subclasshook_doc},
Eric Smith8c663262007-08-25 02:26:07 +00003343 {"__format__", object_format, METH_VARARGS,
3344 PyDoc_STR("default object formatter")},
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003345 {"__sizeof__", object_sizeof, METH_NOARGS,
3346 PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
Guido van Rossum3926a632001-09-25 16:25:58 +00003347 {0}
3348};
3349
Guido van Rossum036f9992003-02-21 22:02:54 +00003350
Tim Peters6d6c1a32001-08-02 04:15:00 +00003351PyTypeObject PyBaseObject_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003352 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003353 "object", /* tp_name */
3354 sizeof(PyObject), /* tp_basicsize */
3355 0, /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003356 object_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003357 0, /* tp_print */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003358 0, /* tp_getattr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003359 0, /* tp_setattr */
3360 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003361 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003362 0, /* tp_as_number */
3363 0, /* tp_as_sequence */
3364 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003365 (hashfunc)_Py_HashPointer, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003366 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003367 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003368 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00003369 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003370 0, /* tp_as_buffer */
3371 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003372 PyDoc_STR("The most base type"), /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373 0, /* tp_traverse */
3374 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003375 object_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003376 0, /* tp_weaklistoffset */
3377 0, /* tp_iter */
3378 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00003379 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00003380 0, /* tp_members */
3381 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003382 0, /* tp_base */
3383 0, /* tp_dict */
3384 0, /* tp_descr_get */
3385 0, /* tp_descr_set */
3386 0, /* tp_dictoffset */
3387 object_init, /* tp_init */
3388 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossum298e4212003-02-13 16:30:16 +00003389 object_new, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003390 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003391};
3392
3393
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003394/* Add the methods from tp_methods to the __dict__ in a type object */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003395
3396static int
3397add_methods(PyTypeObject *type, PyMethodDef *meth)
3398{
Guido van Rossum687ae002001-10-15 22:03:32 +00003399 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003400
3401 for (; meth->ml_name != NULL; meth++) {
3402 PyObject *descr;
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003403 if (PyDict_GetItemString(dict, meth->ml_name) &&
3404 !(meth->ml_flags & METH_COEXIST))
3405 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00003406 if (meth->ml_flags & METH_CLASS) {
3407 if (meth->ml_flags & METH_STATIC) {
3408 PyErr_SetString(PyExc_ValueError,
3409 "method cannot be both class and static");
3410 return -1;
3411 }
Tim Petersbca1cbc2002-12-09 22:56:13 +00003412 descr = PyDescr_NewClassMethod(type, meth);
Fred Drake7bf97152002-03-28 05:33:33 +00003413 }
3414 else if (meth->ml_flags & METH_STATIC) {
Guido van Rossum9af48ff2003-02-11 17:12:46 +00003415 PyObject *cfunc = PyCFunction_New(meth, NULL);
3416 if (cfunc == NULL)
3417 return -1;
3418 descr = PyStaticMethod_New(cfunc);
3419 Py_DECREF(cfunc);
Fred Drake7bf97152002-03-28 05:33:33 +00003420 }
3421 else {
3422 descr = PyDescr_NewMethod(type, meth);
3423 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003424 if (descr == NULL)
3425 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00003426 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003427 return -1;
3428 Py_DECREF(descr);
3429 }
3430 return 0;
3431}
3432
3433static int
Guido van Rossum6f799372001-09-20 20:46:19 +00003434add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003435{
Guido van Rossum687ae002001-10-15 22:03:32 +00003436 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003437
3438 for (; memb->name != NULL; memb++) {
3439 PyObject *descr;
3440 if (PyDict_GetItemString(dict, memb->name))
3441 continue;
3442 descr = PyDescr_NewMember(type, memb);
3443 if (descr == NULL)
3444 return -1;
3445 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
3446 return -1;
3447 Py_DECREF(descr);
3448 }
3449 return 0;
3450}
3451
3452static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00003453add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003454{
Guido van Rossum687ae002001-10-15 22:03:32 +00003455 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003456
3457 for (; gsp->name != NULL; gsp++) {
3458 PyObject *descr;
3459 if (PyDict_GetItemString(dict, gsp->name))
3460 continue;
3461 descr = PyDescr_NewGetSet(type, gsp);
3462
3463 if (descr == NULL)
3464 return -1;
3465 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
3466 return -1;
3467 Py_DECREF(descr);
3468 }
3469 return 0;
3470}
3471
Guido van Rossum13d52f02001-08-10 21:24:08 +00003472static void
3473inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003474{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003475 Py_ssize_t oldsize, newsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003476
Guido van Rossum13d52f02001-08-10 21:24:08 +00003477 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00003478 oldsize = base->tp_basicsize;
3479 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
3480 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3481 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00003482 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00003483 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003484 if (type->tp_traverse == NULL)
3485 type->tp_traverse = base->tp_traverse;
3486 if (type->tp_clear == NULL)
3487 type->tp_clear = base->tp_clear;
3488 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003489 {
Guido van Rossumf884b742001-12-17 17:14:22 +00003490 /* The condition below could use some explanation.
3491 It appears that tp_new is not inherited for static types
3492 whose base class is 'object'; this seems to be a precaution
3493 so that old extension types don't suddenly become
3494 callable (object.__new__ wouldn't insure the invariants
3495 that the extension type's own factory function ensures).
3496 Heap types, of course, are under our control, so they do
3497 inherit tp_new; static extension types that specify some
3498 other built-in type as the default are considered
3499 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00003500 if (base != &PyBaseObject_Type ||
3501 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3502 if (type->tp_new == NULL)
3503 type->tp_new = base->tp_new;
3504 }
3505 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00003506 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00003507
3508 /* Copy other non-function slots */
3509
3510#undef COPYVAL
3511#define COPYVAL(SLOT) \
3512 if (type->SLOT == 0) type->SLOT = base->SLOT
3513
3514 COPYVAL(tp_itemsize);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003515 COPYVAL(tp_weaklistoffset);
3516 COPYVAL(tp_dictoffset);
Thomas Wouters27d517b2007-02-25 20:39:11 +00003517
3518 /* Setup fast subclass flags */
3519 if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
3520 type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
3521 else if (PyType_IsSubtype(base, &PyType_Type))
3522 type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
3523 else if (PyType_IsSubtype(base, &PyLong_Type))
3524 type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
Christian Heimes72b710a2008-05-26 13:28:38 +00003525 else if (PyType_IsSubtype(base, &PyBytes_Type))
3526 type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS;
Thomas Wouters27d517b2007-02-25 20:39:11 +00003527 else if (PyType_IsSubtype(base, &PyUnicode_Type))
3528 type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
3529 else if (PyType_IsSubtype(base, &PyTuple_Type))
3530 type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
3531 else if (PyType_IsSubtype(base, &PyList_Type))
3532 type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
3533 else if (PyType_IsSubtype(base, &PyDict_Type))
3534 type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003535}
3536
Guido van Rossumf5243f02008-01-01 04:06:48 +00003537static char *hash_name_op[] = {
Guido van Rossum38938152006-08-21 23:36:26 +00003538 "__eq__",
Guido van Rossum38938152006-08-21 23:36:26 +00003539 "__hash__",
Guido van Rossumf5243f02008-01-01 04:06:48 +00003540 NULL
Guido van Rossum38938152006-08-21 23:36:26 +00003541};
3542
3543static int
Guido van Rossumf5243f02008-01-01 04:06:48 +00003544overrides_hash(PyTypeObject *type)
Guido van Rossum38938152006-08-21 23:36:26 +00003545{
Guido van Rossumf5243f02008-01-01 04:06:48 +00003546 char **p;
Guido van Rossum38938152006-08-21 23:36:26 +00003547 PyObject *dict = type->tp_dict;
3548
3549 assert(dict != NULL);
Guido van Rossumf5243f02008-01-01 04:06:48 +00003550 for (p = hash_name_op; *p; p++) {
3551 if (PyDict_GetItemString(dict, *p) != NULL)
Guido van Rossum38938152006-08-21 23:36:26 +00003552 return 1;
3553 }
3554 return 0;
3555}
3556
Guido van Rossum13d52f02001-08-10 21:24:08 +00003557static void
3558inherit_slots(PyTypeObject *type, PyTypeObject *base)
3559{
3560 PyTypeObject *basebase;
3561
3562#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00003563#undef COPYSLOT
3564#undef COPYNUM
3565#undef COPYSEQ
3566#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00003567#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00003568
3569#define SLOTDEFINED(SLOT) \
3570 (base->SLOT != 0 && \
3571 (basebase == NULL || base->SLOT != basebase->SLOT))
3572
Tim Peters6d6c1a32001-08-02 04:15:00 +00003573#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00003574 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00003575
3576#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3577#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3578#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00003579#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003580
Guido van Rossum13d52f02001-08-10 21:24:08 +00003581 /* This won't inherit indirect slots (from tp_as_number etc.)
3582 if type doesn't provide the space. */
3583
3584 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3585 basebase = base->tp_base;
3586 if (basebase->tp_as_number == NULL)
3587 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003588 COPYNUM(nb_add);
3589 COPYNUM(nb_subtract);
3590 COPYNUM(nb_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003591 COPYNUM(nb_remainder);
3592 COPYNUM(nb_divmod);
3593 COPYNUM(nb_power);
3594 COPYNUM(nb_negative);
3595 COPYNUM(nb_positive);
3596 COPYNUM(nb_absolute);
Jack Diederich4dafcc42006-11-28 19:15:13 +00003597 COPYNUM(nb_bool);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003598 COPYNUM(nb_invert);
3599 COPYNUM(nb_lshift);
3600 COPYNUM(nb_rshift);
3601 COPYNUM(nb_and);
3602 COPYNUM(nb_xor);
3603 COPYNUM(nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003604 COPYNUM(nb_int);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003605 COPYNUM(nb_float);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003606 COPYNUM(nb_inplace_add);
3607 COPYNUM(nb_inplace_subtract);
3608 COPYNUM(nb_inplace_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003609 COPYNUM(nb_inplace_remainder);
3610 COPYNUM(nb_inplace_power);
3611 COPYNUM(nb_inplace_lshift);
3612 COPYNUM(nb_inplace_rshift);
3613 COPYNUM(nb_inplace_and);
3614 COPYNUM(nb_inplace_xor);
3615 COPYNUM(nb_inplace_or);
Neal Norwitzbcc0db82006-03-24 08:14:36 +00003616 COPYNUM(nb_true_divide);
3617 COPYNUM(nb_floor_divide);
3618 COPYNUM(nb_inplace_true_divide);
3619 COPYNUM(nb_inplace_floor_divide);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003620 COPYNUM(nb_index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003621 }
3622
Guido van Rossum13d52f02001-08-10 21:24:08 +00003623 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3624 basebase = base->tp_base;
3625 if (basebase->tp_as_sequence == NULL)
3626 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003627 COPYSEQ(sq_length);
3628 COPYSEQ(sq_concat);
3629 COPYSEQ(sq_repeat);
3630 COPYSEQ(sq_item);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003631 COPYSEQ(sq_ass_item);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003632 COPYSEQ(sq_contains);
3633 COPYSEQ(sq_inplace_concat);
3634 COPYSEQ(sq_inplace_repeat);
3635 }
3636
Guido van Rossum13d52f02001-08-10 21:24:08 +00003637 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3638 basebase = base->tp_base;
3639 if (basebase->tp_as_mapping == NULL)
3640 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003641 COPYMAP(mp_length);
3642 COPYMAP(mp_subscript);
3643 COPYMAP(mp_ass_subscript);
3644 }
3645
Tim Petersfc57ccb2001-10-12 02:38:24 +00003646 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3647 basebase = base->tp_base;
3648 if (basebase->tp_as_buffer == NULL)
3649 basebase = NULL;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00003650 COPYBUF(bf_getbuffer);
3651 COPYBUF(bf_releasebuffer);
Tim Petersfc57ccb2001-10-12 02:38:24 +00003652 }
3653
Guido van Rossum13d52f02001-08-10 21:24:08 +00003654 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003655
Tim Peters6d6c1a32001-08-02 04:15:00 +00003656 COPYSLOT(tp_dealloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003657 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3658 type->tp_getattr = base->tp_getattr;
3659 type->tp_getattro = base->tp_getattro;
3660 }
3661 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3662 type->tp_setattr = base->tp_setattr;
3663 type->tp_setattro = base->tp_setattro;
3664 }
Mark Dickinsonc008a172009-02-01 13:59:22 +00003665 /* tp_compare is ignored, see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003666 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003667 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003668 COPYSLOT(tp_call);
3669 COPYSLOT(tp_str);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003670 {
Guido van Rossum38938152006-08-21 23:36:26 +00003671 /* Copy comparison-related slots only when
3672 not overriding them anywhere */
Mark Dickinsonc008a172009-02-01 13:59:22 +00003673 if (type->tp_richcompare == NULL &&
Guido van Rossum38938152006-08-21 23:36:26 +00003674 type->tp_hash == NULL &&
Guido van Rossumf5243f02008-01-01 04:06:48 +00003675 !overrides_hash(type))
Guido van Rossumb8f63662001-08-15 23:57:02 +00003676 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003677 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003678 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003679 }
3680 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003681 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003682 COPYSLOT(tp_iter);
3683 COPYSLOT(tp_iternext);
3684 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003685 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003686 COPYSLOT(tp_descr_get);
3687 COPYSLOT(tp_descr_set);
3688 COPYSLOT(tp_dictoffset);
3689 COPYSLOT(tp_init);
3690 COPYSLOT(tp_alloc);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00003691 COPYSLOT(tp_is_gc);
Tim Peters3cfe7542003-05-21 21:29:48 +00003692 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3693 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3694 /* They agree about gc. */
3695 COPYSLOT(tp_free);
3696 }
3697 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3698 type->tp_free == NULL &&
Neal Norwitz30d1c512007-08-19 22:48:23 +00003699 base->tp_free == PyObject_Free) {
Tim Peters3cfe7542003-05-21 21:29:48 +00003700 /* A bit of magic to plug in the correct default
3701 * tp_free function when a derived class adds gc,
3702 * didn't define tp_free, and the base uses the
3703 * default non-gc tp_free.
3704 */
3705 type->tp_free = PyObject_GC_Del;
3706 }
3707 /* else they didn't agree about gc, and there isn't something
3708 * obvious to be done -- the type is on its own.
3709 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003710 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003711}
3712
Jeremy Hylton938ace62002-07-17 16:30:39 +00003713static int add_operators(PyTypeObject *);
Guido van Rossum13d52f02001-08-10 21:24:08 +00003714
Tim Peters6d6c1a32001-08-02 04:15:00 +00003715int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003716PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003717{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003718 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003719 PyTypeObject *base;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003720 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003721
Guido van Rossumcab05802002-06-10 15:29:03 +00003722 if (type->tp_flags & Py_TPFLAGS_READY) {
3723 assert(type->tp_dict != NULL);
Guido van Rossumd614f972001-08-10 17:39:49 +00003724 return 0;
Guido van Rossumcab05802002-06-10 15:29:03 +00003725 }
Guido van Rossumd614f972001-08-10 17:39:49 +00003726 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00003727
3728 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003729
Tim Peters36eb4df2003-03-23 03:33:13 +00003730#ifdef Py_TRACE_REFS
3731 /* PyType_Ready is the closest thing we have to a choke point
3732 * for type objects, so is the best place I can think of to try
3733 * to get type objects into the doubly-linked list of all objects.
3734 * Still, not all type objects go thru PyType_Ready.
3735 */
Tim Peters7571a0f2003-03-23 17:52:28 +00003736 _Py_AddToAllObjects((PyObject *)type, 0);
Tim Peters36eb4df2003-03-23 03:33:13 +00003737#endif
3738
Tim Peters6d6c1a32001-08-02 04:15:00 +00003739 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3740 base = type->tp_base;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003741 if (base == NULL && type != &PyBaseObject_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003742 base = type->tp_base = &PyBaseObject_Type;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003743 Py_INCREF(base);
3744 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003745
Guido van Rossumd8faa362007-04-27 19:54:29 +00003746 /* Now the only way base can still be NULL is if type is
3747 * &PyBaseObject_Type.
3748 */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003749
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003750 /* Initialize the base class */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003751 if (base != NULL && base->tp_dict == NULL) {
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003752 if (PyType_Ready(base) < 0)
3753 goto error;
3754 }
3755
Guido van Rossumd8faa362007-04-27 19:54:29 +00003756 /* Initialize ob_type if NULL. This means extensions that want to be
Guido van Rossum0986d822002-04-08 01:38:42 +00003757 compilable separately on Windows can call PyType_Ready() instead of
3758 initializing the ob_type field of their type objects. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003759 /* The test for base != NULL is really unnecessary, since base is only
3760 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3761 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3762 know that. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003763 if (Py_TYPE(type) == NULL && base != NULL)
3764 Py_TYPE(type) = Py_TYPE(base);
Guido van Rossum0986d822002-04-08 01:38:42 +00003765
Tim Peters6d6c1a32001-08-02 04:15:00 +00003766 /* Initialize tp_bases */
3767 bases = type->tp_bases;
3768 if (bases == NULL) {
3769 if (base == NULL)
3770 bases = PyTuple_New(0);
3771 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003772 bases = PyTuple_Pack(1, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003773 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003774 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003775 type->tp_bases = bases;
3776 }
3777
Guido van Rossum687ae002001-10-15 22:03:32 +00003778 /* Initialize tp_dict */
3779 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003780 if (dict == NULL) {
3781 dict = PyDict_New();
3782 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003783 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00003784 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003785 }
3786
Guido van Rossum687ae002001-10-15 22:03:32 +00003787 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003788 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003789 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003790 if (type->tp_methods != NULL) {
3791 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003792 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003793 }
3794 if (type->tp_members != NULL) {
3795 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003796 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003797 }
3798 if (type->tp_getset != NULL) {
3799 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003800 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003801 }
3802
Tim Peters6d6c1a32001-08-02 04:15:00 +00003803 /* Calculate method resolution order */
3804 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00003805 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003806 }
3807
Guido van Rossum13d52f02001-08-10 21:24:08 +00003808 /* Inherit special flags from dominant base */
3809 if (type->tp_base != NULL)
3810 inherit_special(type, type->tp_base);
3811
Tim Peters6d6c1a32001-08-02 04:15:00 +00003812 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003813 bases = type->tp_mro;
3814 assert(bases != NULL);
3815 assert(PyTuple_Check(bases));
3816 n = PyTuple_GET_SIZE(bases);
3817 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003818 PyObject *b = PyTuple_GET_ITEM(bases, i);
3819 if (PyType_Check(b))
3820 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003821 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003822
Tim Peters3cfe7542003-05-21 21:29:48 +00003823 /* Sanity check for tp_free. */
3824 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3825 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003826 /* This base class needs to call tp_free, but doesn't have
3827 * one, or its tp_free is for non-gc'ed objects.
3828 */
Tim Peters3cfe7542003-05-21 21:29:48 +00003829 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3830 "gc and is a base type but has inappropriate "
3831 "tp_free slot",
3832 type->tp_name);
3833 goto error;
3834 }
3835
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003836 /* if the type dictionary doesn't contain a __doc__, set it from
3837 the tp_doc slot.
3838 */
3839 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3840 if (type->tp_doc != NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +00003841 PyObject *doc = PyUnicode_FromString(type->tp_doc);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003842 if (doc == NULL)
3843 goto error;
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003844 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3845 Py_DECREF(doc);
3846 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00003847 PyDict_SetItemString(type->tp_dict,
3848 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003849 }
3850 }
3851
Guido van Rossum38938152006-08-21 23:36:26 +00003852 /* Hack for tp_hash and __hash__.
3853 If after all that, tp_hash is still NULL, and __hash__ is not in
Nick Coghland1abd252008-07-15 15:46:38 +00003854 tp_dict, set tp_hash to PyObject_HashNotImplemented and
3855 tp_dict['__hash__'] equal to None.
Guido van Rossum38938152006-08-21 23:36:26 +00003856 This signals that __hash__ is not inherited.
3857 */
3858 if (type->tp_hash == NULL) {
3859 if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
3860 if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
3861 goto error;
Nick Coghland1abd252008-07-15 15:46:38 +00003862 type->tp_hash = PyObject_HashNotImplemented;
Guido van Rossum38938152006-08-21 23:36:26 +00003863 }
3864 }
3865
Guido van Rossum13d52f02001-08-10 21:24:08 +00003866 /* Some more special stuff */
3867 base = type->tp_base;
3868 if (base != NULL) {
3869 if (type->tp_as_number == NULL)
3870 type->tp_as_number = base->tp_as_number;
3871 if (type->tp_as_sequence == NULL)
3872 type->tp_as_sequence = base->tp_as_sequence;
3873 if (type->tp_as_mapping == NULL)
3874 type->tp_as_mapping = base->tp_as_mapping;
Guido van Rossumeea47182003-02-11 20:39:59 +00003875 if (type->tp_as_buffer == NULL)
3876 type->tp_as_buffer = base->tp_as_buffer;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003877 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003878
Guido van Rossum1c450732001-10-08 15:18:27 +00003879 /* Link into each base class's list of subclasses */
3880 bases = type->tp_bases;
3881 n = PyTuple_GET_SIZE(bases);
3882 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003883 PyObject *b = PyTuple_GET_ITEM(bases, i);
3884 if (PyType_Check(b) &&
3885 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00003886 goto error;
3887 }
3888
Mark Dickinsonc008a172009-02-01 13:59:22 +00003889 /* Check reserved slots */
3890 if (type->tp_compare) {
3891 PyErr_Format(PyExc_TypeError,
3892 "type %s has tp_compare",
3893 type->tp_name);
3894 }
3895
Guido van Rossum13d52f02001-08-10 21:24:08 +00003896 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00003897 assert(type->tp_dict != NULL);
3898 type->tp_flags =
3899 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003900 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00003901
3902 error:
3903 type->tp_flags &= ~Py_TPFLAGS_READYING;
3904 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003905}
3906
Guido van Rossum1c450732001-10-08 15:18:27 +00003907static int
3908add_subclass(PyTypeObject *base, PyTypeObject *type)
3909{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003910 Py_ssize_t i;
3911 int result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003912 PyObject *list, *ref, *newobj;
Guido van Rossum1c450732001-10-08 15:18:27 +00003913
3914 list = base->tp_subclasses;
3915 if (list == NULL) {
3916 base->tp_subclasses = list = PyList_New(0);
3917 if (list == NULL)
3918 return -1;
3919 }
3920 assert(PyList_Check(list));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003921 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
Guido van Rossum1c450732001-10-08 15:18:27 +00003922 i = PyList_GET_SIZE(list);
3923 while (--i >= 0) {
3924 ref = PyList_GET_ITEM(list, i);
3925 assert(PyWeakref_CheckRef(ref));
Guido van Rossum3930bc32002-10-18 13:51:49 +00003926 if (PyWeakref_GET_OBJECT(ref) == Py_None)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003927 return PyList_SetItem(list, i, newobj);
Guido van Rossum1c450732001-10-08 15:18:27 +00003928 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003929 result = PyList_Append(list, newobj);
3930 Py_DECREF(newobj);
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003931 return result;
Guido van Rossum1c450732001-10-08 15:18:27 +00003932}
3933
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003934static void
3935remove_subclass(PyTypeObject *base, PyTypeObject *type)
3936{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003937 Py_ssize_t i;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003938 PyObject *list, *ref;
3939
3940 list = base->tp_subclasses;
3941 if (list == NULL) {
3942 return;
3943 }
3944 assert(PyList_Check(list));
3945 i = PyList_GET_SIZE(list);
3946 while (--i >= 0) {
3947 ref = PyList_GET_ITEM(list, i);
3948 assert(PyWeakref_CheckRef(ref));
3949 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3950 /* this can't fail, right? */
3951 PySequence_DelItem(list, i);
3952 return;
3953 }
3954 }
3955}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003956
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003957static int
3958check_num_args(PyObject *ob, int n)
3959{
3960 if (!PyTuple_CheckExact(ob)) {
3961 PyErr_SetString(PyExc_SystemError,
3962 "PyArg_UnpackTuple() argument list is not a tuple");
3963 return 0;
3964 }
3965 if (n == PyTuple_GET_SIZE(ob))
3966 return 1;
3967 PyErr_Format(
3968 PyExc_TypeError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00003969 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003970 return 0;
3971}
3972
Tim Peters6d6c1a32001-08-02 04:15:00 +00003973/* Generic wrappers for overloadable 'operators' such as __getitem__ */
3974
3975/* There's a wrapper *function* for each distinct function typedef used
Guido van Rossumd8faa362007-04-27 19:54:29 +00003976 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
Tim Peters6d6c1a32001-08-02 04:15:00 +00003977 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3978 Most tables have only one entry; the tables for binary operators have two
3979 entries, one regular and one with reversed arguments. */
3980
3981static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003982wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003983{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003984 lenfunc func = (lenfunc)wrapped;
3985 Py_ssize_t res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003986
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003987 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003988 return NULL;
3989 res = (*func)(self);
3990 if (res == -1 && PyErr_Occurred())
3991 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00003992 return PyLong_FromLong((long)res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003993}
3994
Tim Peters6d6c1a32001-08-02 04:15:00 +00003995static PyObject *
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003996wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3997{
3998 inquiry func = (inquiry)wrapped;
3999 int res;
4000
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004001 if (!check_num_args(args, 0))
Raymond Hettingerf34f2642003-10-11 17:29:04 +00004002 return NULL;
4003 res = (*func)(self);
4004 if (res == -1 && PyErr_Occurred())
4005 return NULL;
4006 return PyBool_FromLong((long)res);
4007}
4008
4009static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004010wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
4011{
4012 binaryfunc func = (binaryfunc)wrapped;
4013 PyObject *other;
4014
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004015 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004016 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004017 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004018 return (*func)(self, other);
4019}
4020
4021static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00004022wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
4023{
4024 binaryfunc func = (binaryfunc)wrapped;
4025 PyObject *other;
4026
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004027 if (!check_num_args(args, 1))
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00004028 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004029 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00004030 return (*func)(self, other);
4031}
4032
4033static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004034wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4035{
4036 binaryfunc func = (binaryfunc)wrapped;
4037 PyObject *other;
4038
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004039 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004040 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004041 other = PyTuple_GET_ITEM(args, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00004042 if (!PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00004043 Py_INCREF(Py_NotImplemented);
4044 return Py_NotImplemented;
4045 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004046 return (*func)(other, self);
4047}
4048
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004049static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004050wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
4051{
4052 ternaryfunc func = (ternaryfunc)wrapped;
4053 PyObject *other;
4054 PyObject *third = Py_None;
4055
4056 /* Note: This wrapper only works for __pow__() */
4057
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004058 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004059 return NULL;
4060 return (*func)(self, other, third);
4061}
4062
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00004063static PyObject *
4064wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4065{
4066 ternaryfunc func = (ternaryfunc)wrapped;
4067 PyObject *other;
4068 PyObject *third = Py_None;
4069
4070 /* Note: This wrapper only works for __pow__() */
4071
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004072 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00004073 return NULL;
4074 return (*func)(other, self, third);
4075}
4076
Tim Peters6d6c1a32001-08-02 04:15:00 +00004077static PyObject *
4078wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
4079{
4080 unaryfunc func = (unaryfunc)wrapped;
4081
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004082 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004083 return NULL;
4084 return (*func)(self);
4085}
4086
Tim Peters6d6c1a32001-08-02 04:15:00 +00004087static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004088wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004089{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004090 ssizeargfunc func = (ssizeargfunc)wrapped;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004091 PyObject* o;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004092 Py_ssize_t i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004093
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004094 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
4095 return NULL;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004096 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004097 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00004098 return NULL;
4099 return (*func)(self, i);
4100}
4101
Martin v. Löwis18e16552006-02-15 17:27:45 +00004102static Py_ssize_t
Guido van Rossum5d815f32001-08-17 21:57:47 +00004103getindex(PyObject *self, PyObject *arg)
4104{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004105 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004106
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004107 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004108 if (i == -1 && PyErr_Occurred())
4109 return -1;
4110 if (i < 0) {
Christian Heimes90aa7642007-12-19 02:45:37 +00004111 PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004112 if (sq && sq->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00004113 Py_ssize_t n = (*sq->sq_length)(self);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004114 if (n < 0)
4115 return -1;
4116 i += n;
4117 }
4118 }
4119 return i;
4120}
4121
4122static PyObject *
4123wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
4124{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004125 ssizeargfunc func = (ssizeargfunc)wrapped;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004126 PyObject *arg;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004127 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004128
Guido van Rossumf4593e02001-10-03 12:09:30 +00004129 if (PyTuple_GET_SIZE(args) == 1) {
4130 arg = PyTuple_GET_ITEM(args, 0);
4131 i = getindex(self, arg);
4132 if (i == -1 && PyErr_Occurred())
4133 return NULL;
4134 return (*func)(self, i);
4135 }
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004136 check_num_args(args, 1);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004137 assert(PyErr_Occurred());
4138 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004139}
4140
Tim Peters6d6c1a32001-08-02 04:15:00 +00004141static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00004142wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004143{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004144 ssizeobjargproc func = (ssizeobjargproc)wrapped;
4145 Py_ssize_t i;
4146 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004147 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004148
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004149 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
Guido van Rossum5d815f32001-08-17 21:57:47 +00004150 return NULL;
4151 i = getindex(self, arg);
4152 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00004153 return NULL;
4154 res = (*func)(self, i, value);
4155 if (res == -1 && PyErr_Occurred())
4156 return NULL;
4157 Py_INCREF(Py_None);
4158 return Py_None;
4159}
4160
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004161static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00004162wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004163{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004164 ssizeobjargproc func = (ssizeobjargproc)wrapped;
4165 Py_ssize_t i;
4166 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004167 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004168
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004169 if (!check_num_args(args, 1))
Guido van Rossum5d815f32001-08-17 21:57:47 +00004170 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004171 arg = PyTuple_GET_ITEM(args, 0);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004172 i = getindex(self, arg);
4173 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004174 return NULL;
4175 res = (*func)(self, i, NULL);
4176 if (res == -1 && PyErr_Occurred())
4177 return NULL;
4178 Py_INCREF(Py_None);
4179 return Py_None;
4180}
4181
Tim Peters6d6c1a32001-08-02 04:15:00 +00004182/* XXX objobjproc is a misnomer; should be objargpred */
4183static PyObject *
4184wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
4185{
4186 objobjproc func = (objobjproc)wrapped;
4187 int res;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00004188 PyObject *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004189
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004190 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004191 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004192 value = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004193 res = (*func)(self, value);
4194 if (res == -1 && PyErr_Occurred())
4195 return NULL;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00004196 else
4197 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004198}
4199
Tim Peters6d6c1a32001-08-02 04:15:00 +00004200static PyObject *
4201wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
4202{
4203 objobjargproc func = (objobjargproc)wrapped;
4204 int res;
4205 PyObject *key, *value;
4206
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004207 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004208 return NULL;
4209 res = (*func)(self, key, value);
4210 if (res == -1 && PyErr_Occurred())
4211 return NULL;
4212 Py_INCREF(Py_None);
4213 return Py_None;
4214}
4215
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004216static PyObject *
4217wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
4218{
4219 objobjargproc func = (objobjargproc)wrapped;
4220 int res;
4221 PyObject *key;
4222
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004223 if (!check_num_args(args, 1))
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004224 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004225 key = PyTuple_GET_ITEM(args, 0);
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004226 res = (*func)(self, key, NULL);
4227 if (res == -1 && PyErr_Occurred())
4228 return NULL;
4229 Py_INCREF(Py_None);
4230 return Py_None;
4231}
4232
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004233/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
Guido van Rossum52b27052003-04-15 20:05:10 +00004234 This is called the Carlo Verre hack after its discoverer. */
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004235static int
4236hackcheck(PyObject *self, setattrofunc func, char *what)
4237{
Christian Heimes90aa7642007-12-19 02:45:37 +00004238 PyTypeObject *type = Py_TYPE(self);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004239 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
4240 type = type->tp_base;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004241 /* If type is NULL now, this is a really weird type.
4242 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00004243 if (type && type->tp_setattro != func) {
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004244 PyErr_Format(PyExc_TypeError,
4245 "can't apply this %s to %s object",
4246 what,
4247 type->tp_name);
4248 return 0;
4249 }
4250 return 1;
4251}
4252
Tim Peters6d6c1a32001-08-02 04:15:00 +00004253static PyObject *
4254wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
4255{
4256 setattrofunc func = (setattrofunc)wrapped;
4257 int res;
4258 PyObject *name, *value;
4259
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004260 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004261 return NULL;
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004262 if (!hackcheck(self, func, "__setattr__"))
4263 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004264 res = (*func)(self, name, value);
4265 if (res < 0)
4266 return NULL;
4267 Py_INCREF(Py_None);
4268 return Py_None;
4269}
4270
4271static PyObject *
4272wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
4273{
4274 setattrofunc func = (setattrofunc)wrapped;
4275 int res;
4276 PyObject *name;
4277
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004278 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004279 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004280 name = PyTuple_GET_ITEM(args, 0);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004281 if (!hackcheck(self, func, "__delattr__"))
4282 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004283 res = (*func)(self, name, NULL);
4284 if (res < 0)
4285 return NULL;
4286 Py_INCREF(Py_None);
4287 return Py_None;
4288}
4289
Tim Peters6d6c1a32001-08-02 04:15:00 +00004290static PyObject *
4291wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
4292{
4293 hashfunc func = (hashfunc)wrapped;
4294 long res;
4295
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004296 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004297 return NULL;
4298 res = (*func)(self);
4299 if (res == -1 && PyErr_Occurred())
4300 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00004301 return PyLong_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004302}
4303
Tim Peters6d6c1a32001-08-02 04:15:00 +00004304static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00004305wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004306{
4307 ternaryfunc func = (ternaryfunc)wrapped;
4308
Guido van Rossumc8e56452001-10-22 00:43:43 +00004309 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004310}
4311
Tim Peters6d6c1a32001-08-02 04:15:00 +00004312static PyObject *
4313wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
4314{
4315 richcmpfunc func = (richcmpfunc)wrapped;
4316 PyObject *other;
4317
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004318 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004319 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004320 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004321 return (*func)(self, other, op);
4322}
4323
4324#undef RICHCMP_WRAPPER
4325#define RICHCMP_WRAPPER(NAME, OP) \
4326static PyObject * \
4327richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4328{ \
4329 return wrap_richcmpfunc(self, args, wrapped, OP); \
4330}
4331
Jack Jansen8e938b42001-08-08 15:29:49 +00004332RICHCMP_WRAPPER(lt, Py_LT)
4333RICHCMP_WRAPPER(le, Py_LE)
4334RICHCMP_WRAPPER(eq, Py_EQ)
4335RICHCMP_WRAPPER(ne, Py_NE)
4336RICHCMP_WRAPPER(gt, Py_GT)
4337RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004338
Tim Peters6d6c1a32001-08-02 04:15:00 +00004339static PyObject *
4340wrap_next(PyObject *self, PyObject *args, void *wrapped)
4341{
4342 unaryfunc func = (unaryfunc)wrapped;
4343 PyObject *res;
4344
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004345 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004346 return NULL;
4347 res = (*func)(self);
4348 if (res == NULL && !PyErr_Occurred())
4349 PyErr_SetNone(PyExc_StopIteration);
4350 return res;
4351}
4352
Tim Peters6d6c1a32001-08-02 04:15:00 +00004353static PyObject *
4354wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
4355{
4356 descrgetfunc func = (descrgetfunc)wrapped;
4357 PyObject *obj;
4358 PyObject *type = NULL;
4359
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004360 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004361 return NULL;
Guido van Rossum82ed25c2003-02-11 16:25:43 +00004362 if (obj == Py_None)
4363 obj = NULL;
4364 if (type == Py_None)
4365 type = NULL;
4366 if (type == NULL &&obj == NULL) {
4367 PyErr_SetString(PyExc_TypeError,
4368 "__get__(None, None) is invalid");
4369 return NULL;
4370 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004371 return (*func)(self, obj, type);
4372}
4373
Tim Peters6d6c1a32001-08-02 04:15:00 +00004374static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004375wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004376{
4377 descrsetfunc func = (descrsetfunc)wrapped;
4378 PyObject *obj, *value;
4379 int ret;
4380
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004381 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004382 return NULL;
4383 ret = (*func)(self, obj, value);
4384 if (ret < 0)
4385 return NULL;
4386 Py_INCREF(Py_None);
4387 return Py_None;
4388}
Guido van Rossum22b13872002-08-06 21:41:44 +00004389
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004390static PyObject *
4391wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
4392{
4393 descrsetfunc func = (descrsetfunc)wrapped;
4394 PyObject *obj;
4395 int ret;
4396
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004397 if (!check_num_args(args, 1))
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004398 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004399 obj = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004400 ret = (*func)(self, obj, NULL);
4401 if (ret < 0)
4402 return NULL;
4403 Py_INCREF(Py_None);
4404 return Py_None;
4405}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004406
Tim Peters6d6c1a32001-08-02 04:15:00 +00004407static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00004408wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004409{
4410 initproc func = (initproc)wrapped;
4411
Guido van Rossumc8e56452001-10-22 00:43:43 +00004412 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004413 return NULL;
4414 Py_INCREF(Py_None);
4415 return Py_None;
4416}
4417
Tim Peters6d6c1a32001-08-02 04:15:00 +00004418static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004419tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004420{
Barry Warsaw60f01882001-08-22 19:24:42 +00004421 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004422 PyObject *arg0, *res;
4423
4424 if (self == NULL || !PyType_Check(self))
4425 Py_FatalError("__new__() called with non-type 'self'");
4426 type = (PyTypeObject *)self;
4427 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004428 PyErr_Format(PyExc_TypeError,
4429 "%s.__new__(): not enough arguments",
4430 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004431 return NULL;
4432 }
4433 arg0 = PyTuple_GET_ITEM(args, 0);
4434 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004435 PyErr_Format(PyExc_TypeError,
4436 "%s.__new__(X): X is not a type object (%s)",
4437 type->tp_name,
Christian Heimes90aa7642007-12-19 02:45:37 +00004438 Py_TYPE(arg0)->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004439 return NULL;
4440 }
4441 subtype = (PyTypeObject *)arg0;
4442 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004443 PyErr_Format(PyExc_TypeError,
4444 "%s.__new__(%s): %s is not a subtype of %s",
4445 type->tp_name,
4446 subtype->tp_name,
4447 subtype->tp_name,
4448 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004449 return NULL;
4450 }
Barry Warsaw60f01882001-08-22 19:24:42 +00004451
4452 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00004453 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00004454 most derived base that's not a heap type is this type. */
4455 staticbase = subtype;
4456 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4457 staticbase = staticbase->tp_base;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004458 /* If staticbase is NULL now, it is a really weird type.
4459 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00004460 if (staticbase && staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004461 PyErr_Format(PyExc_TypeError,
4462 "%s.__new__(%s) is not safe, use %s.__new__()",
4463 type->tp_name,
4464 subtype->tp_name,
4465 staticbase == NULL ? "?" : staticbase->tp_name);
4466 return NULL;
4467 }
4468
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004469 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4470 if (args == NULL)
4471 return NULL;
4472 res = type->tp_new(subtype, args, kwds);
4473 Py_DECREF(args);
4474 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004475}
4476
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004477static struct PyMethodDef tp_new_methoddef[] = {
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004478 {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00004479 PyDoc_STR("T.__new__(S, ...) -> "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004480 "a new object with type S, a subtype of T")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00004481 {0}
4482};
4483
4484static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004485add_tp_new_wrapper(PyTypeObject *type)
4486{
Guido van Rossumf040ede2001-08-07 16:40:56 +00004487 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004488
Guido van Rossum687ae002001-10-15 22:03:32 +00004489 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00004490 return 0;
4491 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004492 if (func == NULL)
4493 return -1;
Raymond Hettinger8d726ee2004-06-25 22:24:35 +00004494 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
Raymond Hettingerd56cbe52004-06-25 22:17:39 +00004495 Py_DECREF(func);
4496 return -1;
4497 }
4498 Py_DECREF(func);
4499 return 0;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004500}
4501
Guido van Rossumf040ede2001-08-07 16:40:56 +00004502/* Slot wrappers that call the corresponding __foo__ slot. See comments
4503 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004504
Guido van Rossumdc91b992001-08-08 22:26:22 +00004505#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004506static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004507FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004508{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00004509 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004510 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004511}
4512
Guido van Rossumdc91b992001-08-08 22:26:22 +00004513#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004514static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004515FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004516{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004517 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004518 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004519}
4520
Guido van Rossumcd118802003-01-06 22:57:47 +00004521/* Boolean helper for SLOT1BINFULL().
4522 right.__class__ is a nontrivial subclass of left.__class__. */
4523static int
4524method_is_overloaded(PyObject *left, PyObject *right, char *name)
4525{
4526 PyObject *a, *b;
4527 int ok;
4528
Christian Heimes90aa7642007-12-19 02:45:37 +00004529 b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
Guido van Rossumcd118802003-01-06 22:57:47 +00004530 if (b == NULL) {
4531 PyErr_Clear();
4532 /* If right doesn't have it, it's not overloaded */
4533 return 0;
4534 }
4535
Christian Heimes90aa7642007-12-19 02:45:37 +00004536 a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
Guido van Rossumcd118802003-01-06 22:57:47 +00004537 if (a == NULL) {
4538 PyErr_Clear();
4539 Py_DECREF(b);
4540 /* If right has it but left doesn't, it's overloaded */
4541 return 1;
4542 }
4543
4544 ok = PyObject_RichCompareBool(a, b, Py_NE);
4545 Py_DECREF(a);
4546 Py_DECREF(b);
4547 if (ok < 0) {
4548 PyErr_Clear();
4549 return 0;
4550 }
4551
4552 return ok;
4553}
4554
Guido van Rossumdc91b992001-08-08 22:26:22 +00004555
4556#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004557static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004558FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004559{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004560 static PyObject *cache_str, *rcache_str; \
Christian Heimes90aa7642007-12-19 02:45:37 +00004561 int do_other = Py_TYPE(self) != Py_TYPE(other) && \
4562 Py_TYPE(other)->tp_as_number != NULL && \
4563 Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
4564 if (Py_TYPE(self)->tp_as_number != NULL && \
4565 Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004566 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00004567 if (do_other && \
Christian Heimes90aa7642007-12-19 02:45:37 +00004568 PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
Guido van Rossumcd118802003-01-06 22:57:47 +00004569 method_is_overloaded(self, other, ROPSTR)) { \
Guido van Rossum55f20992001-10-01 17:18:22 +00004570 r = call_maybe( \
4571 other, ROPSTR, &rcache_str, "(O)", self); \
4572 if (r != Py_NotImplemented) \
4573 return r; \
4574 Py_DECREF(r); \
4575 do_other = 0; \
4576 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004577 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004578 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004579 if (r != Py_NotImplemented || \
Christian Heimes90aa7642007-12-19 02:45:37 +00004580 Py_TYPE(other) == Py_TYPE(self)) \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004581 return r; \
4582 Py_DECREF(r); \
4583 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00004584 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004585 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004586 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004587 } \
4588 Py_INCREF(Py_NotImplemented); \
4589 return Py_NotImplemented; \
4590}
4591
4592#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4593 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4594
4595#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4596static PyObject * \
4597FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4598{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004599 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004600 return call_method(self, OPSTR, &cache_str, \
4601 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004602}
4603
Martin v. Löwis18e16552006-02-15 17:27:45 +00004604static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +00004605slot_sq_length(PyObject *self)
4606{
Guido van Rossum2730b132001-08-28 18:22:14 +00004607 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004608 PyObject *res = call_method(self, "__len__", &len_str, "()");
Martin v. Löwis18e16552006-02-15 17:27:45 +00004609 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004610
4611 if (res == NULL)
4612 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00004613 len = PyLong_AsSsize_t(res);
Guido van Rossum26111622001-10-01 16:42:49 +00004614 Py_DECREF(res);
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004615 if (len < 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004616 if (!PyErr_Occurred())
4617 PyErr_SetString(PyExc_ValueError,
4618 "__len__() should return >= 0");
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004619 return -1;
4620 }
Guido van Rossum26111622001-10-01 16:42:49 +00004621 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004622}
4623
Guido van Rossumf4593e02001-10-03 12:09:30 +00004624/* Super-optimized version of slot_sq_item.
4625 Other slots could do the same... */
4626static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00004627slot_sq_item(PyObject *self, Py_ssize_t i)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004628{
4629 static PyObject *getitem_str;
4630 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4631 descrgetfunc f;
4632
4633 if (getitem_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00004634 getitem_str = PyUnicode_InternFromString("__getitem__");
Guido van Rossumf4593e02001-10-03 12:09:30 +00004635 if (getitem_str == NULL)
4636 return NULL;
4637 }
Christian Heimes90aa7642007-12-19 02:45:37 +00004638 func = _PyType_Lookup(Py_TYPE(self), getitem_str);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004639 if (func != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +00004640 if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004641 Py_INCREF(func);
Neal Norwitz673cd822002-10-18 16:33:13 +00004642 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00004643 func = f(func, self, (PyObject *)(Py_TYPE(self)));
Neal Norwitz673cd822002-10-18 16:33:13 +00004644 if (func == NULL) {
4645 return NULL;
4646 }
4647 }
Christian Heimes217cfd12007-12-02 14:31:20 +00004648 ival = PyLong_FromSsize_t(i);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004649 if (ival != NULL) {
4650 args = PyTuple_New(1);
4651 if (args != NULL) {
4652 PyTuple_SET_ITEM(args, 0, ival);
4653 retval = PyObject_Call(func, args, NULL);
4654 Py_XDECREF(args);
4655 Py_XDECREF(func);
4656 return retval;
4657 }
4658 }
4659 }
4660 else {
4661 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4662 }
4663 Py_XDECREF(args);
4664 Py_XDECREF(ival);
4665 Py_XDECREF(func);
4666 return NULL;
4667}
4668
Tim Peters6d6c1a32001-08-02 04:15:00 +00004669static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004670slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004671{
4672 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004673 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004674
4675 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004676 res = call_method(self, "__delitem__", &delitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004677 "(n)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004678 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004679 res = call_method(self, "__setitem__", &setitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004680 "(nO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004681 if (res == NULL)
4682 return -1;
4683 Py_DECREF(res);
4684 return 0;
4685}
4686
4687static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00004688slot_sq_contains(PyObject *self, PyObject *value)
4689{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004690 PyObject *func, *res, *args;
Tim Petersbf9b2442003-03-23 05:35:36 +00004691 int result = -1;
4692
Guido van Rossum60718732001-08-28 17:47:51 +00004693 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004694
Guido van Rossum55f20992001-10-01 17:18:22 +00004695 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004696 if (func != NULL) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004697 args = PyTuple_Pack(1, value);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004698 if (args == NULL)
4699 res = NULL;
4700 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004701 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004702 Py_DECREF(args);
4703 }
4704 Py_DECREF(func);
Tim Petersbf9b2442003-03-23 05:35:36 +00004705 if (res != NULL) {
4706 result = PyObject_IsTrue(res);
4707 Py_DECREF(res);
4708 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004709 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004710 else if (! PyErr_Occurred()) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00004711 /* Possible results: -1 and 1 */
4712 result = (int)_PySequence_IterSearch(self, value,
Tim Petersbf9b2442003-03-23 05:35:36 +00004713 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004714 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004715 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004716}
4717
Tim Peters6d6c1a32001-08-02 04:15:00 +00004718#define slot_mp_length slot_sq_length
4719
Guido van Rossumdc91b992001-08-08 22:26:22 +00004720SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004721
4722static int
4723slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4724{
4725 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004726 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004727
4728 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004729 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004730 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004731 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004732 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004733 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004734 if (res == NULL)
4735 return -1;
4736 Py_DECREF(res);
4737 return 0;
4738}
4739
Guido van Rossumdc91b992001-08-08 22:26:22 +00004740SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4741SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4742SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004743SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4744SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4745
Jeremy Hylton938ace62002-07-17 16:30:39 +00004746static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
Guido van Rossumdc91b992001-08-08 22:26:22 +00004747
4748SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4749 nb_power, "__pow__", "__rpow__")
4750
4751static PyObject *
4752slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4753{
Guido van Rossum2730b132001-08-28 18:22:14 +00004754 static PyObject *pow_str;
4755
Guido van Rossumdc91b992001-08-08 22:26:22 +00004756 if (modulus == Py_None)
4757 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00004758 /* Three-arg power doesn't use __rpow__. But ternary_op
4759 can call this when the second argument's type uses
4760 slot_nb_power, so check before calling self.__pow__. */
Christian Heimes90aa7642007-12-19 02:45:37 +00004761 if (Py_TYPE(self)->tp_as_number != NULL &&
4762 Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
Guido van Rossum23094982002-06-10 14:30:43 +00004763 return call_method(self, "__pow__", &pow_str,
4764 "(OO)", other, modulus);
4765 }
4766 Py_INCREF(Py_NotImplemented);
4767 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00004768}
4769
4770SLOT0(slot_nb_negative, "__neg__")
4771SLOT0(slot_nb_positive, "__pos__")
4772SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004773
4774static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00004775slot_nb_bool(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004776{
Tim Petersea7f75d2002-12-07 21:39:16 +00004777 PyObject *func, *args;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004778 static PyObject *bool_str, *len_str;
Tim Petersea7f75d2002-12-07 21:39:16 +00004779 int result = -1;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004780 int from_len = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004781
Jack Diederich4dafcc42006-11-28 19:15:13 +00004782 func = lookup_maybe(self, "__bool__", &bool_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004783 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00004784 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00004785 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00004786 func = lookup_maybe(self, "__len__", &len_str);
Tim Petersea7f75d2002-12-07 21:39:16 +00004787 if (func == NULL)
4788 return PyErr_Occurred() ? -1 : 1;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004789 from_len = 1;
Tim Petersea7f75d2002-12-07 21:39:16 +00004790 }
4791 args = PyTuple_New(0);
4792 if (args != NULL) {
4793 PyObject *temp = PyObject_Call(func, args, NULL);
4794 Py_DECREF(args);
4795 if (temp != NULL) {
Jack Diederich4dafcc42006-11-28 19:15:13 +00004796 if (from_len) {
4797 /* enforced by slot_nb_len */
Jeremy Hylton090a3492003-06-27 16:46:45 +00004798 result = PyObject_IsTrue(temp);
Jack Diederich4dafcc42006-11-28 19:15:13 +00004799 }
4800 else if (PyBool_Check(temp)) {
4801 result = PyObject_IsTrue(temp);
4802 }
Jeremy Hylton090a3492003-06-27 16:46:45 +00004803 else {
4804 PyErr_Format(PyExc_TypeError,
Jack Diederich4dafcc42006-11-28 19:15:13 +00004805 "__bool__ should return "
4806 "bool, returned %s",
Christian Heimes90aa7642007-12-19 02:45:37 +00004807 Py_TYPE(temp)->tp_name);
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004808 result = -1;
Jeremy Hylton090a3492003-06-27 16:46:45 +00004809 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004810 Py_DECREF(temp);
Guido van Rossum55f20992001-10-01 17:18:22 +00004811 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004812 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004813 Py_DECREF(func);
Tim Petersea7f75d2002-12-07 21:39:16 +00004814 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004815}
4816
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004817
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004818static PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004819slot_nb_index(PyObject *self)
4820{
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004821 static PyObject *index_str;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004822 return call_method(self, "__index__", &index_str, "()");
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004823}
4824
4825
Guido van Rossumdc91b992001-08-08 22:26:22 +00004826SLOT0(slot_nb_invert, "__invert__")
4827SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4828SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4829SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4830SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4831SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004832
Guido van Rossumdc91b992001-08-08 22:26:22 +00004833SLOT0(slot_nb_int, "__int__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004834SLOT0(slot_nb_float, "__float__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004835SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4836SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4837SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004838SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
Thomas Wouterscf297e42007-02-23 15:07:44 +00004839/* Can't use SLOT1 here, because nb_inplace_power is ternary */
4840static PyObject *
4841slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
4842{
4843 static PyObject *cache_str;
4844 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
4845}
Guido van Rossumdc91b992001-08-08 22:26:22 +00004846SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4847SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4848SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4849SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4850SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4851SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4852 "__floordiv__", "__rfloordiv__")
4853SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4854SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4855SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004856
Guido van Rossumb8f63662001-08-15 23:57:02 +00004857static PyObject *
4858slot_tp_repr(PyObject *self)
4859{
4860 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004861 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004862
Guido van Rossum60718732001-08-28 17:47:51 +00004863 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004864 if (func != NULL) {
4865 res = PyEval_CallObject(func, NULL);
4866 Py_DECREF(func);
4867 return res;
4868 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00004869 PyErr_Clear();
Walter Dörwald1ab83302007-05-18 17:15:44 +00004870 return PyUnicode_FromFormat("<%s object at %p>",
Christian Heimes90aa7642007-12-19 02:45:37 +00004871 Py_TYPE(self)->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004872}
4873
4874static PyObject *
4875slot_tp_str(PyObject *self)
4876{
4877 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004878 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004879
Guido van Rossum60718732001-08-28 17:47:51 +00004880 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004881 if (func != NULL) {
4882 res = PyEval_CallObject(func, NULL);
4883 Py_DECREF(func);
4884 return res;
4885 }
4886 else {
Walter Dörwald1ab83302007-05-18 17:15:44 +00004887 PyObject *ress;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004888 PyErr_Clear();
Walter Dörwald1ab83302007-05-18 17:15:44 +00004889 res = slot_tp_repr(self);
4890 if (!res)
4891 return NULL;
4892 ress = _PyUnicode_AsDefaultEncodedString(res, NULL);
4893 Py_DECREF(res);
4894 return ress;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004895 }
4896}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004897
4898static long
4899slot_tp_hash(PyObject *self)
4900{
Guido van Rossum4011a242006-08-17 23:09:57 +00004901 PyObject *func, *res;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004902 static PyObject *hash_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004903 long h;
4904
Guido van Rossum60718732001-08-28 17:47:51 +00004905 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004906
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004907 if (func == Py_None) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004908 Py_DECREF(func);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004909 func = NULL;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004910 }
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004911
4912 if (func == NULL) {
Nick Coghland1abd252008-07-15 15:46:38 +00004913 return PyObject_HashNotImplemented(self);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004914 }
4915
Guido van Rossum4011a242006-08-17 23:09:57 +00004916 res = PyEval_CallObject(func, NULL);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004917 Py_DECREF(func);
4918 if (res == NULL)
4919 return -1;
4920 if (PyLong_Check(res))
4921 h = PyLong_Type.tp_hash(res);
4922 else
Christian Heimes217cfd12007-12-02 14:31:20 +00004923 h = PyLong_AsLong(res);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004924 Py_DECREF(res);
4925 if (h == -1 && !PyErr_Occurred())
4926 h = -2;
4927 return h;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004928}
4929
4930static PyObject *
4931slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4932{
Guido van Rossum60718732001-08-28 17:47:51 +00004933 static PyObject *call_str;
4934 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004935 PyObject *res;
4936
4937 if (meth == NULL)
4938 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004939
Tim Peters6d6c1a32001-08-02 04:15:00 +00004940 res = PyObject_Call(meth, args, kwds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004941
Tim Peters6d6c1a32001-08-02 04:15:00 +00004942 Py_DECREF(meth);
4943 return res;
4944}
4945
Guido van Rossum14a6f832001-10-17 13:59:09 +00004946/* There are two slot dispatch functions for tp_getattro.
4947
4948 - slot_tp_getattro() is used when __getattribute__ is overridden
4949 but no __getattr__ hook is present;
4950
4951 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4952
Guido van Rossumc334df52002-04-04 23:44:47 +00004953 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4954 detects the absence of __getattr__ and then installs the simpler slot if
4955 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00004956
Tim Peters6d6c1a32001-08-02 04:15:00 +00004957static PyObject *
4958slot_tp_getattro(PyObject *self, PyObject *name)
4959{
Guido van Rossum14a6f832001-10-17 13:59:09 +00004960 static PyObject *getattribute_str = NULL;
4961 return call_method(self, "__getattribute__", &getattribute_str,
4962 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004963}
4964
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004965static PyObject *
Benjamin Peterson9262b842008-11-17 22:45:50 +00004966call_attribute(PyObject *self, PyObject *attr, PyObject *name)
4967{
4968 PyObject *res, *descr = NULL;
4969 descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
4970
4971 if (f != NULL) {
4972 descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
4973 if (descr == NULL)
4974 return NULL;
4975 else
4976 attr = descr;
4977 }
4978 res = PyObject_CallFunctionObjArgs(attr, name, NULL);
4979 Py_XDECREF(descr);
4980 return res;
4981}
4982
4983static PyObject *
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004984slot_tp_getattr_hook(PyObject *self, PyObject *name)
4985{
Christian Heimes90aa7642007-12-19 02:45:37 +00004986 PyTypeObject *tp = Py_TYPE(self);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004987 PyObject *getattr, *getattribute, *res;
4988 static PyObject *getattribute_str = NULL;
4989 static PyObject *getattr_str = NULL;
4990
4991 if (getattr_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00004992 getattr_str = PyUnicode_InternFromString("__getattr__");
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004993 if (getattr_str == NULL)
4994 return NULL;
4995 }
4996 if (getattribute_str == NULL) {
4997 getattribute_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00004998 PyUnicode_InternFromString("__getattribute__");
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004999 if (getattribute_str == NULL)
5000 return NULL;
5001 }
Benjamin Peterson9262b842008-11-17 22:45:50 +00005002 /* speed hack: we could use lookup_maybe, but that would resolve the
5003 method fully for each attribute lookup for classes with
5004 __getattr__, even when the attribute is present. So we use
5005 _PyType_Lookup and create the method only when needed, with
5006 call_attribute. */
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005007 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00005008 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005009 /* No __getattr__ hook: use a simpler dispatcher */
5010 tp->tp_getattro = slot_tp_getattro;
5011 return slot_tp_getattro(self, name);
5012 }
Benjamin Peterson9262b842008-11-17 22:45:50 +00005013 Py_INCREF(getattr);
5014 /* speed hack: we could use lookup_maybe, but that would resolve the
5015 method fully for each attribute lookup for classes with
5016 __getattr__, even when self has the default __getattribute__
5017 method. So we use _PyType_Lookup and create the method only when
5018 needed, with call_attribute. */
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005019 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00005020 if (getattribute == NULL ||
Christian Heimes90aa7642007-12-19 02:45:37 +00005021 (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
Guido van Rossum14a6f832001-10-17 13:59:09 +00005022 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
5023 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005024 res = PyObject_GenericGetAttr(self, name);
Benjamin Peterson9262b842008-11-17 22:45:50 +00005025 else {
5026 Py_INCREF(getattribute);
5027 res = call_attribute(self, getattribute, name);
5028 Py_DECREF(getattribute);
5029 }
Guido van Rossum14a6f832001-10-17 13:59:09 +00005030 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005031 PyErr_Clear();
Benjamin Peterson9262b842008-11-17 22:45:50 +00005032 res = call_attribute(self, getattr, name);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005033 }
Benjamin Peterson9262b842008-11-17 22:45:50 +00005034 Py_DECREF(getattr);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005035 return res;
5036}
5037
Tim Peters6d6c1a32001-08-02 04:15:00 +00005038static int
5039slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
5040{
5041 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00005042 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005043
5044 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00005045 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005046 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005047 else
Guido van Rossum2730b132001-08-28 18:22:14 +00005048 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005049 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005050 if (res == NULL)
5051 return -1;
5052 Py_DECREF(res);
5053 return 0;
5054}
5055
Guido van Rossumf5243f02008-01-01 04:06:48 +00005056static char *name_op[] = {
5057 "__lt__",
5058 "__le__",
5059 "__eq__",
5060 "__ne__",
5061 "__gt__",
5062 "__ge__",
5063};
5064
Tim Peters6d6c1a32001-08-02 04:15:00 +00005065static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00005066half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005067{
Guido van Rossumb8f63662001-08-15 23:57:02 +00005068 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00005069 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00005070
Guido van Rossum60718732001-08-28 17:47:51 +00005071 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005072 if (func == NULL) {
5073 PyErr_Clear();
5074 Py_INCREF(Py_NotImplemented);
5075 return Py_NotImplemented;
5076 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005077 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005078 if (args == NULL)
5079 res = NULL;
5080 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00005081 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005082 Py_DECREF(args);
5083 }
5084 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005085 return res;
5086}
5087
Guido van Rossumb8f63662001-08-15 23:57:02 +00005088static PyObject *
5089slot_tp_richcompare(PyObject *self, PyObject *other, int op)
5090{
5091 PyObject *res;
5092
Christian Heimes90aa7642007-12-19 02:45:37 +00005093 if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00005094 res = half_richcompare(self, other, op);
5095 if (res != Py_NotImplemented)
5096 return res;
5097 Py_DECREF(res);
5098 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005099 if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
Tim Petersf4aca752004-09-23 02:39:37 +00005100 res = half_richcompare(other, self, _Py_SwappedOp[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005101 if (res != Py_NotImplemented) {
5102 return res;
5103 }
5104 Py_DECREF(res);
5105 }
5106 Py_INCREF(Py_NotImplemented);
5107 return Py_NotImplemented;
5108}
5109
5110static PyObject *
5111slot_tp_iter(PyObject *self)
5112{
5113 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00005114 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00005115
Guido van Rossum60718732001-08-28 17:47:51 +00005116 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005117 if (func != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +00005118 PyObject *args;
5119 args = res = PyTuple_New(0);
5120 if (args != NULL) {
5121 res = PyObject_Call(func, args, NULL);
5122 Py_DECREF(args);
5123 }
5124 Py_DECREF(func);
5125 return res;
Guido van Rossumb8f63662001-08-15 23:57:02 +00005126 }
5127 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00005128 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005129 if (func == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005130 PyErr_Format(PyExc_TypeError,
5131 "'%.200s' object is not iterable",
Christian Heimes90aa7642007-12-19 02:45:37 +00005132 Py_TYPE(self)->tp_name);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005133 return NULL;
5134 }
5135 Py_DECREF(func);
5136 return PySeqIter_New(self);
5137}
Tim Peters6d6c1a32001-08-02 04:15:00 +00005138
5139static PyObject *
5140slot_tp_iternext(PyObject *self)
5141{
Guido van Rossum2730b132001-08-28 18:22:14 +00005142 static PyObject *next_str;
Georg Brandla18af4e2007-04-21 15:47:16 +00005143 return call_method(self, "__next__", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00005144}
5145
Guido van Rossum1a493502001-08-17 16:47:50 +00005146static PyObject *
5147slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5148{
Christian Heimes90aa7642007-12-19 02:45:37 +00005149 PyTypeObject *tp = Py_TYPE(self);
Guido van Rossum1a493502001-08-17 16:47:50 +00005150 PyObject *get;
5151 static PyObject *get_str = NULL;
5152
5153 if (get_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005154 get_str = PyUnicode_InternFromString("__get__");
Guido van Rossum1a493502001-08-17 16:47:50 +00005155 if (get_str == NULL)
5156 return NULL;
5157 }
5158 get = _PyType_Lookup(tp, get_str);
5159 if (get == NULL) {
5160 /* Avoid further slowdowns */
5161 if (tp->tp_descr_get == slot_tp_descr_get)
5162 tp->tp_descr_get = NULL;
5163 Py_INCREF(self);
5164 return self;
5165 }
Guido van Rossum2c252392001-08-24 10:13:31 +00005166 if (obj == NULL)
5167 obj = Py_None;
5168 if (type == NULL)
5169 type = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00005170 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
Guido van Rossum1a493502001-08-17 16:47:50 +00005171}
Tim Peters6d6c1a32001-08-02 04:15:00 +00005172
5173static int
5174slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
5175{
Guido van Rossum2c252392001-08-24 10:13:31 +00005176 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00005177 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00005178
5179 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00005180 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005181 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00005182 else
Guido van Rossum2730b132001-08-28 18:22:14 +00005183 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005184 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005185 if (res == NULL)
5186 return -1;
5187 Py_DECREF(res);
5188 return 0;
5189}
5190
5191static int
5192slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
5193{
Guido van Rossum60718732001-08-28 17:47:51 +00005194 static PyObject *init_str;
5195 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005196 PyObject *res;
5197
5198 if (meth == NULL)
5199 return -1;
5200 res = PyObject_Call(meth, args, kwds);
5201 Py_DECREF(meth);
5202 if (res == NULL)
5203 return -1;
Raymond Hettingerb67cc802005-03-03 16:45:19 +00005204 if (res != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005205 PyErr_Format(PyExc_TypeError,
5206 "__init__() should return None, not '%.200s'",
Christian Heimes90aa7642007-12-19 02:45:37 +00005207 Py_TYPE(res)->tp_name);
Raymond Hettingerb67cc802005-03-03 16:45:19 +00005208 Py_DECREF(res);
5209 return -1;
5210 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00005211 Py_DECREF(res);
5212 return 0;
5213}
5214
5215static PyObject *
5216slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5217{
Guido van Rossum7bed2132002-08-08 21:57:53 +00005218 static PyObject *new_str;
5219 PyObject *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005220 PyObject *newargs, *x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005221 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005222
Guido van Rossum7bed2132002-08-08 21:57:53 +00005223 if (new_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005224 new_str = PyUnicode_InternFromString("__new__");
Guido van Rossum7bed2132002-08-08 21:57:53 +00005225 if (new_str == NULL)
5226 return NULL;
5227 }
5228 func = PyObject_GetAttr((PyObject *)type, new_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005229 if (func == NULL)
5230 return NULL;
5231 assert(PyTuple_Check(args));
5232 n = PyTuple_GET_SIZE(args);
5233 newargs = PyTuple_New(n+1);
5234 if (newargs == NULL)
5235 return NULL;
5236 Py_INCREF(type);
5237 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
5238 for (i = 0; i < n; i++) {
5239 x = PyTuple_GET_ITEM(args, i);
5240 Py_INCREF(x);
5241 PyTuple_SET_ITEM(newargs, i+1, x);
5242 }
5243 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00005244 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005245 Py_DECREF(func);
5246 return x;
5247}
5248
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005249static void
5250slot_tp_del(PyObject *self)
5251{
5252 static PyObject *del_str = NULL;
5253 PyObject *del, *res;
5254 PyObject *error_type, *error_value, *error_traceback;
5255
5256 /* Temporarily resurrect the object. */
5257 assert(self->ob_refcnt == 0);
5258 self->ob_refcnt = 1;
5259
5260 /* Save the current exception, if any. */
5261 PyErr_Fetch(&error_type, &error_value, &error_traceback);
5262
5263 /* Execute __del__ method, if any. */
5264 del = lookup_maybe(self, "__del__", &del_str);
5265 if (del != NULL) {
5266 res = PyEval_CallObject(del, NULL);
5267 if (res == NULL)
5268 PyErr_WriteUnraisable(del);
5269 else
5270 Py_DECREF(res);
5271 Py_DECREF(del);
5272 }
5273
5274 /* Restore the saved exception. */
5275 PyErr_Restore(error_type, error_value, error_traceback);
5276
5277 /* Undo the temporary resurrection; can't use DECREF here, it would
5278 * cause a recursive call.
5279 */
5280 assert(self->ob_refcnt > 0);
5281 if (--self->ob_refcnt == 0)
5282 return; /* this is the normal path out */
5283
5284 /* __del__ resurrected it! Make it look like the original Py_DECREF
5285 * never happened.
5286 */
5287 {
Martin v. Löwis725507b2006-03-07 12:08:51 +00005288 Py_ssize_t refcnt = self->ob_refcnt;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005289 _Py_NewReference(self);
5290 self->ob_refcnt = refcnt;
5291 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005292 assert(!PyType_IS_GC(Py_TYPE(self)) ||
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005293 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
Michael W. Hudson3f3b6682004-08-03 10:21:03 +00005294 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5295 * we need to undo that. */
5296 _Py_DEC_REFTOTAL;
5297 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5298 * chain, so no more to do there.
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005299 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5300 * _Py_NewReference bumped tp_allocs: both of those need to be
5301 * undone.
5302 */
5303#ifdef COUNT_ALLOCS
Christian Heimes90aa7642007-12-19 02:45:37 +00005304 --Py_TYPE(self)->tp_frees;
5305 --Py_TYPE(self)->tp_allocs;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005306#endif
5307}
5308
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005309
5310/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
Tim Petersbf9b2442003-03-23 05:35:36 +00005311 functions. The offsets here are relative to the 'PyHeapTypeObject'
Guido van Rossume5c691a2003-03-07 15:13:17 +00005312 structure, which incorporates the additional structures used for numbers,
5313 sequences and mappings.
5314 Note that multiple names may map to the same slot (e.g. __eq__,
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005315 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00005316 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
5317 terminated with an all-zero entry. (This table is further initialized and
5318 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005319
Guido van Rossum6d204072001-10-21 00:44:31 +00005320typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005321
5322#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00005323#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005324#undef ETSLOT
5325#undef SQSLOT
5326#undef MPSLOT
5327#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00005328#undef UNSLOT
5329#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005330#undef BINSLOT
5331#undef RBINSLOT
5332
Guido van Rossum6d204072001-10-21 00:44:31 +00005333#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005334 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5335 PyDoc_STR(DOC)}
Guido van Rossumc8e56452001-10-22 00:43:43 +00005336#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5337 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005338 PyDoc_STR(DOC), FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00005339#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Guido van Rossume5c691a2003-03-07 15:13:17 +00005340 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005341 PyDoc_STR(DOC)}
Guido van Rossum6d204072001-10-21 00:44:31 +00005342#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5343 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5344#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5345 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5346#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5347 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5348#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5349 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5350 "x." NAME "() <==> " DOC)
5351#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5352 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5353 "x." NAME "(y) <==> x" DOC "y")
5354#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5355 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5356 "x." NAME "(y) <==> x" DOC "y")
5357#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5358 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5359 "x." NAME "(y) <==> y" DOC "x")
Anthony Baxter56616992005-06-03 14:12:21 +00005360#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5361 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5362 "x." NAME "(y) <==> " DOC)
5363#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5364 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5365 "x." NAME "(y) <==> " DOC)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005366
5367static slotdef slotdefs[] = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00005368 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00005369 "x.__len__() <==> len(x)"),
Armin Rigofd163f92005-12-29 15:59:19 +00005370 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5371 The logic in abstract.c always falls back to nb_add/nb_multiply in
5372 this case. Defining both the nb_* and the sq_* slots to call the
5373 user-defined methods has unexpected side-effects, as shown by
5374 test_descr.notimplemented() */
5375 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005376 "x.__add__(y) <==> x+y"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005377 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005378 "x.__mul__(n) <==> x*n"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005379 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005380 "x.__rmul__(n) <==> n*x"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005381 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
5382 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005383 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00005384 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005385 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00005386 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005387 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
5388 "x.__contains__(y) <==> y in x"),
Armin Rigofd163f92005-12-29 15:59:19 +00005389 SQSLOT("__iadd__", sq_inplace_concat, NULL,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005390 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Armin Rigofd163f92005-12-29 15:59:19 +00005391 SQSLOT("__imul__", sq_inplace_repeat, NULL,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005392 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005393
Martin v. Löwis18e16552006-02-15 17:27:45 +00005394 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00005395 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00005396 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005397 wrap_binaryfunc,
5398 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005399 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005400 wrap_objobjargproc,
5401 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005402 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005403 wrap_delitem,
5404 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005405
Guido van Rossum6d204072001-10-21 00:44:31 +00005406 BINSLOT("__add__", nb_add, slot_nb_add,
5407 "+"),
5408 RBINSLOT("__radd__", nb_add, slot_nb_add,
5409 "+"),
5410 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5411 "-"),
5412 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5413 "-"),
5414 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5415 "*"),
5416 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5417 "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005418 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5419 "%"),
5420 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5421 "%"),
Anthony Baxter56616992005-06-03 14:12:21 +00005422 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00005423 "divmod(x, y)"),
Anthony Baxter56616992005-06-03 14:12:21 +00005424 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00005425 "divmod(y, x)"),
5426 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5427 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5428 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5429 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5430 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5431 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5432 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5433 "abs(x)"),
Jack Diederich4dafcc42006-11-28 19:15:13 +00005434 UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
Guido van Rossum6d204072001-10-21 00:44:31 +00005435 "x != 0"),
5436 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5437 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5438 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5439 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5440 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5441 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5442 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5443 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5444 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5445 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5446 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005447 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5448 "int(x)"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005449 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5450 "float(x)"),
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00005451 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005452 "x[y:z] <==> x[y.__index__():z.__index__()]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005453 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5454 wrap_binaryfunc, "+"),
5455 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5456 wrap_binaryfunc, "-"),
5457 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5458 wrap_binaryfunc, "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005459 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5460 wrap_binaryfunc, "%"),
5461 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
Guido van Rossum6e5680f2002-10-15 01:01:53 +00005462 wrap_binaryfunc, "**"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005463 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5464 wrap_binaryfunc, "<<"),
5465 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5466 wrap_binaryfunc, ">>"),
5467 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5468 wrap_binaryfunc, "&"),
5469 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5470 wrap_binaryfunc, "^"),
5471 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5472 wrap_binaryfunc, "|"),
5473 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5474 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5475 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5476 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5477 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5478 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5479 IBSLOT("__itruediv__", nb_inplace_true_divide,
5480 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005481
Guido van Rossum6d204072001-10-21 00:44:31 +00005482 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5483 "x.__str__() <==> str(x)"),
5484 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5485 "x.__repr__() <==> repr(x)"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005486 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5487 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005488 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5489 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005490 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00005491 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5492 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5493 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5494 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5495 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5496 "x.__setattr__('name', value) <==> x.name = value"),
5497 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5498 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5499 "x.__delattr__('name') <==> del x.name"),
5500 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5501 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5502 "x.__lt__(y) <==> x<y"),
5503 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5504 "x.__le__(y) <==> x<=y"),
5505 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5506 "x.__eq__(y) <==> x==y"),
5507 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5508 "x.__ne__(y) <==> x!=y"),
5509 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5510 "x.__gt__(y) <==> x>y"),
5511 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5512 "x.__ge__(y) <==> x>=y"),
5513 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5514 "x.__iter__() <==> iter(x)"),
Georg Brandla18af4e2007-04-21 15:47:16 +00005515 TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
5516 "x.__next__() <==> next(x)"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005517 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5518 "descr.__get__(obj[, type]) -> value"),
5519 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5520 "descr.__set__(obj, value)"),
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00005521 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5522 wrap_descr_delete, "descr.__delete__(obj)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005523 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00005524 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00005525 "see x.__class__.__doc__ for signature",
5526 PyWrapperFlag_KEYWORDS),
5527 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005528 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005529 {NULL}
5530};
5531
Guido van Rossumc334df52002-04-04 23:44:47 +00005532/* Given a type pointer and an offset gotten from a slotdef entry, return a
Guido van Rossumd8faa362007-04-27 19:54:29 +00005533 pointer to the actual slot. This is not quite the same as simply adding
Guido van Rossumc334df52002-04-04 23:44:47 +00005534 the offset to the type pointer, since it takes care to indirect through the
5535 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5536 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005537static void **
Martin v. Löwis18e16552006-02-15 17:27:45 +00005538slotptr(PyTypeObject *type, int ioffset)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005539{
5540 char *ptr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005541 long offset = ioffset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005542
Guido van Rossume5c691a2003-03-07 15:13:17 +00005543 /* Note: this depends on the order of the members of PyHeapTypeObject! */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005544 assert(offset >= 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005545 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5546 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5547 ptr = (char *)type->tp_as_sequence;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005548 offset -= offsetof(PyHeapTypeObject, as_sequence);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005549 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005550 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5551 ptr = (char *)type->tp_as_mapping;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005552 offset -= offsetof(PyHeapTypeObject, as_mapping);
Guido van Rossum09638c12002-06-13 19:17:46 +00005553 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005554 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
5555 ptr = (char *)type->tp_as_number;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005556 offset -= offsetof(PyHeapTypeObject, as_number);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005557 }
5558 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005559 ptr = (char *)type;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005560 }
5561 if (ptr != NULL)
5562 ptr += offset;
5563 return (void **)ptr;
5564}
Guido van Rossumf040ede2001-08-07 16:40:56 +00005565
Guido van Rossumc334df52002-04-04 23:44:47 +00005566/* Length of array of slotdef pointers used to store slots with the
5567 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5568 the same __name__, for any __name__. Since that's a static property, it is
5569 appropriate to declare fixed-size arrays for this. */
5570#define MAX_EQUIV 10
5571
5572/* Return a slot pointer for a given name, but ONLY if the attribute has
5573 exactly one slot function. The name must be an interned string. */
5574static void **
5575resolve_slotdups(PyTypeObject *type, PyObject *name)
5576{
5577 /* XXX Maybe this could be optimized more -- but is it worth it? */
5578
5579 /* pname and ptrs act as a little cache */
5580 static PyObject *pname;
5581 static slotdef *ptrs[MAX_EQUIV];
5582 slotdef *p, **pp;
5583 void **res, **ptr;
5584
5585 if (pname != name) {
5586 /* Collect all slotdefs that match name into ptrs. */
5587 pname = name;
5588 pp = ptrs;
5589 for (p = slotdefs; p->name_strobj; p++) {
5590 if (p->name_strobj == name)
5591 *pp++ = p;
5592 }
5593 *pp = NULL;
5594 }
5595
5596 /* Look in all matching slots of the type; if exactly one of these has
Guido van Rossumd8faa362007-04-27 19:54:29 +00005597 a filled-in slot, return its value. Otherwise return NULL. */
Guido van Rossumc334df52002-04-04 23:44:47 +00005598 res = NULL;
5599 for (pp = ptrs; *pp; pp++) {
5600 ptr = slotptr(type, (*pp)->offset);
5601 if (ptr == NULL || *ptr == NULL)
5602 continue;
5603 if (res != NULL)
5604 return NULL;
5605 res = ptr;
5606 }
5607 return res;
5608}
5609
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005610/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
Guido van Rossumc334df52002-04-04 23:44:47 +00005611 does some incredibly complex thinking and then sticks something into the
5612 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5613 interests, and then stores a generic wrapper or a specific function into
5614 the slot.) Return a pointer to the next slotdef with a different offset,
5615 because that's convenient for fixup_slot_dispatchers(). */
5616static slotdef *
5617update_one_slot(PyTypeObject *type, slotdef *p)
5618{
5619 PyObject *descr;
5620 PyWrapperDescrObject *d;
5621 void *generic = NULL, *specific = NULL;
5622 int use_generic = 0;
5623 int offset = p->offset;
5624 void **ptr = slotptr(type, offset);
5625
5626 if (ptr == NULL) {
5627 do {
5628 ++p;
5629 } while (p->offset == offset);
5630 return p;
5631 }
5632 do {
5633 descr = _PyType_Lookup(type, p->name_strobj);
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00005634 if (descr == NULL) {
5635 if (ptr == (void**)&type->tp_iternext) {
5636 specific = _PyObject_NextNotImplemented;
5637 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005638 continue;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00005639 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005640 if (Py_TYPE(descr) == &PyWrapperDescr_Type) {
Guido van Rossumc334df52002-04-04 23:44:47 +00005641 void **tptr = resolve_slotdups(type, p->name_strobj);
5642 if (tptr == NULL || tptr == ptr)
5643 generic = p->function;
5644 d = (PyWrapperDescrObject *)descr;
5645 if (d->d_base->wrapper == p->wrapper &&
5646 PyType_IsSubtype(type, d->d_type))
5647 {
5648 if (specific == NULL ||
5649 specific == d->d_wrapped)
5650 specific = d->d_wrapped;
5651 else
5652 use_generic = 1;
5653 }
5654 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005655 else if (Py_TYPE(descr) == &PyCFunction_Type &&
Guido van Rossum721f62e2002-08-09 02:14:34 +00005656 PyCFunction_GET_FUNCTION(descr) ==
5657 (PyCFunction)tp_new_wrapper &&
Benjamin Petersonb5479792009-01-18 22:10:38 +00005658 ptr == (void**)&type->tp_new)
Guido van Rossum721f62e2002-08-09 02:14:34 +00005659 {
5660 /* The __new__ wrapper is not a wrapper descriptor,
5661 so must be special-cased differently.
5662 If we don't do this, creating an instance will
5663 always use slot_tp_new which will look up
5664 __new__ in the MRO which will call tp_new_wrapper
5665 which will look through the base classes looking
5666 for a static base and call its tp_new (usually
5667 PyType_GenericNew), after performing various
5668 sanity checks and constructing a new argument
5669 list. Cut all that nonsense short -- this speeds
5670 up instance creation tremendously. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005671 specific = (void *)type->tp_new;
Guido van Rossum721f62e2002-08-09 02:14:34 +00005672 /* XXX I'm not 100% sure that there isn't a hole
5673 in this reasoning that requires additional
5674 sanity checks. I'll buy the first person to
5675 point out a bug in this reasoning a beer. */
5676 }
Nick Coghland1abd252008-07-15 15:46:38 +00005677 else if (descr == Py_None &&
Benjamin Petersonb5479792009-01-18 22:10:38 +00005678 ptr == (void**)&type->tp_hash) {
Nick Coghland1abd252008-07-15 15:46:38 +00005679 /* We specifically allow __hash__ to be set to None
5680 to prevent inheritance of the default
5681 implementation from object.__hash__ */
5682 specific = PyObject_HashNotImplemented;
5683 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005684 else {
5685 use_generic = 1;
5686 generic = p->function;
5687 }
5688 } while ((++p)->offset == offset);
5689 if (specific && !use_generic)
5690 *ptr = specific;
5691 else
5692 *ptr = generic;
5693 return p;
5694}
5695
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005696/* In the type, update the slots whose slotdefs are gathered in the pp array.
5697 This is a callback for update_subclasses(). */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005698static int
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005699update_slots_callback(PyTypeObject *type, void *data)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005700{
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005701 slotdef **pp = (slotdef **)data;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005702
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005703 for (; *pp; pp++)
Guido van Rossumc334df52002-04-04 23:44:47 +00005704 update_one_slot(type, *pp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005705 return 0;
5706}
5707
Guido van Rossumc334df52002-04-04 23:44:47 +00005708/* Comparison function for qsort() to compare slotdefs by their offset, and
5709 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005710static int
5711slotdef_cmp(const void *aa, const void *bb)
5712{
5713 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5714 int c = a->offset - b->offset;
5715 if (c != 0)
5716 return c;
5717 else
Martin v. Löwis18e16552006-02-15 17:27:45 +00005718 /* Cannot use a-b, as this gives off_t,
5719 which may lose precision when converted to int. */
5720 return (a > b) ? 1 : (a < b) ? -1 : 0;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005721}
5722
Guido van Rossumc334df52002-04-04 23:44:47 +00005723/* Initialize the slotdefs table by adding interned string objects for the
5724 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005725static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005726init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005727{
5728 slotdef *p;
5729 static int initialized = 0;
5730
5731 if (initialized)
5732 return;
5733 for (p = slotdefs; p->name; p++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005734 p->name_strobj = PyUnicode_InternFromString(p->name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005735 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00005736 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005737 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005738 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5739 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005740 initialized = 1;
5741}
5742
Guido van Rossumc334df52002-04-04 23:44:47 +00005743/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005744static int
5745update_slot(PyTypeObject *type, PyObject *name)
5746{
Guido van Rossumc334df52002-04-04 23:44:47 +00005747 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005748 slotdef *p;
5749 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005750 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005751
Christian Heimesa62da1d2008-01-12 19:39:10 +00005752 /* Clear the VALID_VERSION flag of 'type' and all its
5753 subclasses. This could possibly be unified with the
5754 update_subclasses() recursion below, but carefully:
5755 they each have their own conditions on which to stop
5756 recursing into subclasses. */
Georg Brandlf08a9dd2008-06-10 16:57:31 +00005757 PyType_Modified(type);
Christian Heimesa62da1d2008-01-12 19:39:10 +00005758
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005759 init_slotdefs();
5760 pp = ptrs;
5761 for (p = slotdefs; p->name; p++) {
5762 /* XXX assume name is interned! */
5763 if (p->name_strobj == name)
5764 *pp++ = p;
5765 }
5766 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005767 for (pp = ptrs; *pp; pp++) {
5768 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005769 offset = p->offset;
5770 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005771 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005772 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005773 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005774 if (ptrs[0] == NULL)
5775 return 0; /* Not an attribute that affects any slots */
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005776 return update_subclasses(type, name,
5777 update_slots_callback, (void *)ptrs);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005778}
5779
Guido van Rossumc334df52002-04-04 23:44:47 +00005780/* Store the proper functions in the slot dispatches at class (type)
5781 definition time, based upon which operations the class overrides in its
5782 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00005783static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005784fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005785{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005786 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005787
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005788 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00005789 for (p = slotdefs; p->name; )
5790 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005791}
Guido van Rossum705f0f52001-08-24 16:47:00 +00005792
Michael W. Hudson98bbc492002-11-26 14:47:27 +00005793static void
5794update_all_slots(PyTypeObject* type)
5795{
5796 slotdef *p;
5797
5798 init_slotdefs();
5799 for (p = slotdefs; p->name; p++) {
5800 /* update_slot returns int but can't actually fail */
5801 update_slot(type, p->name_strobj);
5802 }
5803}
5804
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005805/* recurse_down_subclasses() and update_subclasses() are mutually
5806 recursive functions to call a callback for all subclasses,
5807 but refraining from recursing into subclasses that define 'name'. */
5808
5809static int
5810update_subclasses(PyTypeObject *type, PyObject *name,
5811 update_callback callback, void *data)
5812{
5813 if (callback(type, data) < 0)
5814 return -1;
5815 return recurse_down_subclasses(type, name, callback, data);
5816}
5817
5818static int
5819recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5820 update_callback callback, void *data)
5821{
5822 PyTypeObject *subclass;
5823 PyObject *ref, *subclasses, *dict;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005824 Py_ssize_t i, n;
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005825
5826 subclasses = type->tp_subclasses;
5827 if (subclasses == NULL)
5828 return 0;
5829 assert(PyList_Check(subclasses));
5830 n = PyList_GET_SIZE(subclasses);
5831 for (i = 0; i < n; i++) {
5832 ref = PyList_GET_ITEM(subclasses, i);
5833 assert(PyWeakref_CheckRef(ref));
5834 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5835 assert(subclass != NULL);
5836 if ((PyObject *)subclass == Py_None)
5837 continue;
5838 assert(PyType_Check(subclass));
5839 /* Avoid recursing down into unaffected classes */
5840 dict = subclass->tp_dict;
5841 if (dict != NULL && PyDict_Check(dict) &&
5842 PyDict_GetItem(dict, name) != NULL)
5843 continue;
5844 if (update_subclasses(subclass, name, callback, data) < 0)
5845 return -1;
5846 }
5847 return 0;
5848}
5849
Guido van Rossum6d204072001-10-21 00:44:31 +00005850/* This function is called by PyType_Ready() to populate the type's
5851 dictionary with method descriptors for function slots. For each
Guido van Rossum09638c12002-06-13 19:17:46 +00005852 function slot (like tp_repr) that's defined in the type, one or more
5853 corresponding descriptors are added in the type's tp_dict dictionary
Guido van Rossumd8faa362007-04-27 19:54:29 +00005854 under the appropriate name (like __repr__). Some function slots
Guido van Rossum09638c12002-06-13 19:17:46 +00005855 cause more than one descriptor to be added (for example, the nb_add
5856 slot adds both __add__ and __radd__ descriptors) and some function
5857 slots compete for the same descriptor (for example both sq_item and
5858 mp_subscript generate a __getitem__ descriptor).
5859
Guido van Rossumd8faa362007-04-27 19:54:29 +00005860 In the latter case, the first slotdef entry encoutered wins. Since
Tim Petersbf9b2442003-03-23 05:35:36 +00005861 slotdef entries are sorted by the offset of the slot in the
Guido van Rossume5c691a2003-03-07 15:13:17 +00005862 PyHeapTypeObject, this gives us some control over disambiguating
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005863 between competing slots: the members of PyHeapTypeObject are listed
5864 from most general to least general, so the most general slot is
5865 preferred. In particular, because as_mapping comes before as_sequence,
5866 for a type that defines both mp_subscript and sq_item, mp_subscript
5867 wins.
Guido van Rossum09638c12002-06-13 19:17:46 +00005868
5869 This only adds new descriptors and doesn't overwrite entries in
5870 tp_dict that were previously defined. The descriptors contain a
5871 reference to the C function they must call, so that it's safe if they
5872 are copied into a subtype's __dict__ and the subtype has a different
5873 C function in its slot -- calling the method defined by the
5874 descriptor will call the C function that was used to create it,
5875 rather than the C function present in the slot when it is called.
5876 (This is important because a subtype may have a C function in the
5877 slot that calls the method from the dictionary, and we want to avoid
5878 infinite recursion here.) */
Guido van Rossum6d204072001-10-21 00:44:31 +00005879
5880static int
5881add_operators(PyTypeObject *type)
5882{
5883 PyObject *dict = type->tp_dict;
5884 slotdef *p;
5885 PyObject *descr;
5886 void **ptr;
5887
5888 init_slotdefs();
5889 for (p = slotdefs; p->name; p++) {
5890 if (p->wrapper == NULL)
5891 continue;
5892 ptr = slotptr(type, p->offset);
5893 if (!ptr || !*ptr)
5894 continue;
5895 if (PyDict_GetItem(dict, p->name_strobj))
5896 continue;
Nick Coghland1abd252008-07-15 15:46:38 +00005897 if (*ptr == PyObject_HashNotImplemented) {
5898 /* Classes may prevent the inheritance of the tp_hash
5899 slot by storing PyObject_HashNotImplemented in it. Make it
5900 visible as a None value for the __hash__ attribute. */
5901 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
5902 return -1;
5903 }
5904 else {
5905 descr = PyDescr_NewWrapper(type, p, *ptr);
5906 if (descr == NULL)
5907 return -1;
5908 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5909 return -1;
5910 Py_DECREF(descr);
5911 }
Guido van Rossum6d204072001-10-21 00:44:31 +00005912 }
5913 if (type->tp_new != NULL) {
5914 if (add_tp_new_wrapper(type) < 0)
5915 return -1;
5916 }
5917 return 0;
5918}
5919
Guido van Rossum705f0f52001-08-24 16:47:00 +00005920
5921/* Cooperative 'super' */
5922
5923typedef struct {
5924 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00005925 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005926 PyObject *obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005927 PyTypeObject *obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005928} superobject;
5929
Guido van Rossum6f799372001-09-20 20:46:19 +00005930static PyMemberDef super_members[] = {
5931 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5932 "the class invoking super()"},
5933 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5934 "the instance invoking super(); may be None"},
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005935 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005936 "the type of the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005937 {0}
5938};
5939
Guido van Rossum705f0f52001-08-24 16:47:00 +00005940static void
5941super_dealloc(PyObject *self)
5942{
5943 superobject *su = (superobject *)self;
5944
Guido van Rossum048eb752001-10-02 21:24:57 +00005945 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005946 Py_XDECREF(su->obj);
5947 Py_XDECREF(su->type);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005948 Py_XDECREF(su->obj_type);
Christian Heimes90aa7642007-12-19 02:45:37 +00005949 Py_TYPE(self)->tp_free(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005950}
5951
5952static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005953super_repr(PyObject *self)
5954{
5955 superobject *su = (superobject *)self;
5956
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005957 if (su->obj_type)
Walter Dörwald1ab83302007-05-18 17:15:44 +00005958 return PyUnicode_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005959 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005960 su->type ? su->type->tp_name : "NULL",
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005961 su->obj_type->tp_name);
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005962 else
Walter Dörwald1ab83302007-05-18 17:15:44 +00005963 return PyUnicode_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005964 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005965 su->type ? su->type->tp_name : "NULL");
5966}
5967
5968static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00005969super_getattro(PyObject *self, PyObject *name)
5970{
5971 superobject *su = (superobject *)self;
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005972 int skip = su->obj_type == NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005973
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005974 if (!skip) {
5975 /* We want __class__ to return the class of the super object
5976 (i.e. super, or a subclass), not the class of su->obj. */
Martin v. Löwis5b222132007-06-10 09:51:05 +00005977 skip = (PyUnicode_Check(name) &&
5978 PyUnicode_GET_SIZE(name) == 9 &&
5979 PyUnicode_CompareWithASCIIString(name, "__class__") == 0);
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005980 }
5981
5982 if (!skip) {
Tim Petersa91e9642001-11-14 23:32:33 +00005983 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005984 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005985 descrgetfunc f;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005986 Py_ssize_t i, n;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005987
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005988 starttype = su->obj_type;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005989 mro = starttype->tp_mro;
5990
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005991 if (mro == NULL)
5992 n = 0;
5993 else {
5994 assert(PyTuple_Check(mro));
5995 n = PyTuple_GET_SIZE(mro);
5996 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005997 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00005998 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00005999 break;
6000 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006001 i++;
6002 res = NULL;
6003 for (; i < n; i++) {
6004 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00006005 if (PyType_Check(tmp))
6006 dict = ((PyTypeObject *)tmp)->tp_dict;
Tim Petersa91e9642001-11-14 23:32:33 +00006007 else
6008 continue;
6009 res = PyDict_GetItem(dict, name);
Guido van Rossum6cc5bb62003-04-16 20:01:36 +00006010 if (res != NULL) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00006011 Py_INCREF(res);
Christian Heimes90aa7642007-12-19 02:45:37 +00006012 f = Py_TYPE(res)->tp_descr_get;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006013 if (f != NULL) {
Phillip J. Eby91a968a2004-03-25 02:19:34 +00006014 tmp = f(res,
6015 /* Only pass 'obj' param if
6016 this is instance-mode super
6017 (See SF ID #743627)
6018 */
Hye-Shik Changff365c92004-03-25 16:37:03 +00006019 (su->obj == (PyObject *)
6020 su->obj_type
Phillip J. Eby91a968a2004-03-25 02:19:34 +00006021 ? (PyObject *)NULL
6022 : su->obj),
Guido van Rossumd4641072002-04-03 02:13:37 +00006023 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00006024 Py_DECREF(res);
6025 res = tmp;
6026 }
6027 return res;
6028 }
6029 }
6030 }
6031 return PyObject_GenericGetAttr(self, name);
6032}
6033
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006034static PyTypeObject *
Guido van Rossum5b443c62001-12-03 15:38:28 +00006035supercheck(PyTypeObject *type, PyObject *obj)
6036{
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006037 /* Check that a super() call makes sense. Return a type object.
6038
6039 obj can be a new-style class, or an instance of one:
6040
Guido van Rossumd8faa362007-04-27 19:54:29 +00006041 - If it is a class, it must be a subclass of 'type'. This case is
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006042 used for class methods; the return value is obj.
6043
6044 - If it is an instance, it must be an instance of 'type'. This is
6045 the normal case; the return value is obj.__class__.
6046
6047 But... when obj is an instance, we want to allow for the case where
Christian Heimes90aa7642007-12-19 02:45:37 +00006048 Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006049 This will allow using super() with a proxy for obj.
6050 */
6051
Guido van Rossum8e80a722003-02-18 19:22:22 +00006052 /* Check for first bullet above (special case) */
6053 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
6054 Py_INCREF(obj);
6055 return (PyTypeObject *)obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006056 }
Guido van Rossum8e80a722003-02-18 19:22:22 +00006057
6058 /* Normal case */
Christian Heimes90aa7642007-12-19 02:45:37 +00006059 if (PyType_IsSubtype(Py_TYPE(obj), type)) {
6060 Py_INCREF(Py_TYPE(obj));
6061 return Py_TYPE(obj);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006062 }
6063 else {
6064 /* Try the slow way */
6065 static PyObject *class_str = NULL;
6066 PyObject *class_attr;
6067
6068 if (class_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00006069 class_str = PyUnicode_FromString("__class__");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006070 if (class_str == NULL)
6071 return NULL;
6072 }
6073
6074 class_attr = PyObject_GetAttr(obj, class_str);
6075
6076 if (class_attr != NULL &&
6077 PyType_Check(class_attr) &&
Christian Heimes90aa7642007-12-19 02:45:37 +00006078 (PyTypeObject *)class_attr != Py_TYPE(obj))
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006079 {
6080 int ok = PyType_IsSubtype(
6081 (PyTypeObject *)class_attr, type);
6082 if (ok)
6083 return (PyTypeObject *)class_attr;
6084 }
6085
6086 if (class_attr == NULL)
6087 PyErr_Clear();
6088 else
6089 Py_DECREF(class_attr);
6090 }
6091
Guido van Rossumd8faa362007-04-27 19:54:29 +00006092 PyErr_SetString(PyExc_TypeError,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006093 "super(type, obj): "
6094 "obj must be an instance or subtype of type");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006095 return NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00006096}
6097
Guido van Rossum705f0f52001-08-24 16:47:00 +00006098static PyObject *
6099super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
6100{
6101 superobject *su = (superobject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006102 superobject *newobj;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006103
6104 if (obj == NULL || obj == Py_None || su->obj != NULL) {
6105 /* Not binding to an object, or already bound */
6106 Py_INCREF(self);
6107 return self;
6108 }
Christian Heimes90aa7642007-12-19 02:45:37 +00006109 if (Py_TYPE(su) != &PySuper_Type)
Armin Rigo7726dc02005-05-15 15:32:08 +00006110 /* If su is an instance of a (strict) subclass of super,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006111 call its type */
Christian Heimes90aa7642007-12-19 02:45:37 +00006112 return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
Guido van Rossumd8faa362007-04-27 19:54:29 +00006113 su->type, obj, NULL);
Guido van Rossum5b443c62001-12-03 15:38:28 +00006114 else {
6115 /* Inline the common case */
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006116 PyTypeObject *obj_type = supercheck(su->type, obj);
6117 if (obj_type == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00006118 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006119 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006120 NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006121 if (newobj == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00006122 return NULL;
6123 Py_INCREF(su->type);
6124 Py_INCREF(obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006125 newobj->type = su->type;
6126 newobj->obj = obj;
6127 newobj->obj_type = obj_type;
6128 return (PyObject *)newobj;
Guido van Rossum5b443c62001-12-03 15:38:28 +00006129 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006130}
6131
6132static int
6133super_init(PyObject *self, PyObject *args, PyObject *kwds)
6134{
6135 superobject *su = (superobject *)self;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006136 PyTypeObject *type = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00006137 PyObject *obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006138 PyTypeObject *obj_type = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006139
Thomas Wouters89f507f2006-12-13 04:49:30 +00006140 if (!_PyArg_NoKeywords("super", kwds))
6141 return -1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006142 if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj))
Guido van Rossum705f0f52001-08-24 16:47:00 +00006143 return -1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006144
6145 if (type == NULL) {
6146 /* Call super(), without args -- fill in from __class__
6147 and first local variable on the stack. */
6148 PyFrameObject *f = PyThreadState_GET()->frame;
6149 PyCodeObject *co = f->f_code;
6150 int i, n;
6151 if (co == NULL) {
6152 PyErr_SetString(PyExc_SystemError,
6153 "super(): no code object");
6154 return -1;
6155 }
6156 if (co->co_argcount == 0) {
6157 PyErr_SetString(PyExc_SystemError,
6158 "super(): no arguments");
6159 return -1;
6160 }
6161 obj = f->f_localsplus[0];
6162 if (obj == NULL) {
6163 PyErr_SetString(PyExc_SystemError,
6164 "super(): arg[0] deleted");
6165 return -1;
6166 }
6167 if (co->co_freevars == NULL)
6168 n = 0;
6169 else {
6170 assert(PyTuple_Check(co->co_freevars));
6171 n = PyTuple_GET_SIZE(co->co_freevars);
6172 }
6173 for (i = 0; i < n; i++) {
6174 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
6175 assert(PyUnicode_Check(name));
6176 if (!PyUnicode_CompareWithASCIIString(name,
6177 "__class__")) {
Barry Warsaw91cc8fb2008-11-20 20:01:57 +00006178 Py_ssize_t index = co->co_nlocals +
6179 PyTuple_GET_SIZE(co->co_cellvars) + i;
6180 PyObject *cell = f->f_localsplus[index];
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006181 if (cell == NULL || !PyCell_Check(cell)) {
6182 PyErr_SetString(PyExc_SystemError,
6183 "super(): bad __class__ cell");
6184 return -1;
6185 }
6186 type = (PyTypeObject *) PyCell_GET(cell);
6187 if (type == NULL) {
6188 PyErr_SetString(PyExc_SystemError,
6189 "super(): empty __class__ cell");
6190 return -1;
6191 }
6192 if (!PyType_Check(type)) {
6193 PyErr_Format(PyExc_SystemError,
6194 "super(): __class__ is not a type (%s)",
Christian Heimes90aa7642007-12-19 02:45:37 +00006195 Py_TYPE(type)->tp_name);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006196 return -1;
6197 }
6198 break;
6199 }
6200 }
6201 if (type == NULL) {
6202 PyErr_SetString(PyExc_SystemError,
6203 "super(): __class__ cell not found");
6204 return -1;
6205 }
6206 }
6207
Guido van Rossum705f0f52001-08-24 16:47:00 +00006208 if (obj == Py_None)
6209 obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006210 if (obj != NULL) {
6211 obj_type = supercheck(type, obj);
6212 if (obj_type == NULL)
6213 return -1;
6214 Py_INCREF(obj);
6215 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006216 Py_INCREF(type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00006217 su->type = type;
6218 su->obj = obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006219 su->obj_type = obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006220 return 0;
6221}
6222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006223PyDoc_STRVAR(super_doc,
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006224"super() -> same as super(__class__, <first argument>)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00006225"super(type) -> unbound super object\n"
6226"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00006227"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00006228"Typical use to call a cooperative superclass method:\n"
6229"class C(B):\n"
6230" def meth(self, arg):\n"
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006231" super().meth(arg)\n"
6232"This works for class methods too:\n"
6233"class C(B):\n"
6234" @classmethod\n"
6235" def cmeth(cls, arg):\n"
6236" super().cmeth(arg)\n");
Guido van Rossum705f0f52001-08-24 16:47:00 +00006237
Guido van Rossum048eb752001-10-02 21:24:57 +00006238static int
6239super_traverse(PyObject *self, visitproc visit, void *arg)
6240{
6241 superobject *su = (superobject *)self;
Guido van Rossum048eb752001-10-02 21:24:57 +00006242
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006243 Py_VISIT(su->obj);
6244 Py_VISIT(su->type);
6245 Py_VISIT(su->obj_type);
Guido van Rossum048eb752001-10-02 21:24:57 +00006246
6247 return 0;
6248}
6249
Guido van Rossum705f0f52001-08-24 16:47:00 +00006250PyTypeObject PySuper_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00006251 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00006252 "super", /* tp_name */
6253 sizeof(superobject), /* tp_basicsize */
6254 0, /* tp_itemsize */
6255 /* methods */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006256 super_dealloc, /* tp_dealloc */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006257 0, /* tp_print */
6258 0, /* tp_getattr */
6259 0, /* tp_setattr */
6260 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00006261 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006262 0, /* tp_as_number */
6263 0, /* tp_as_sequence */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006264 0, /* tp_as_mapping */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006265 0, /* tp_hash */
6266 0, /* tp_call */
6267 0, /* tp_str */
6268 super_getattro, /* tp_getattro */
6269 0, /* tp_setattro */
6270 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00006271 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
6272 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006273 super_doc, /* tp_doc */
6274 super_traverse, /* tp_traverse */
6275 0, /* tp_clear */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006276 0, /* tp_richcompare */
6277 0, /* tp_weaklistoffset */
6278 0, /* tp_iter */
6279 0, /* tp_iternext */
6280 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00006281 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006282 0, /* tp_getset */
6283 0, /* tp_base */
6284 0, /* tp_dict */
6285 super_descr_get, /* tp_descr_get */
6286 0, /* tp_descr_set */
6287 0, /* tp_dictoffset */
6288 super_init, /* tp_init */
6289 PyType_GenericAlloc, /* tp_alloc */
6290 PyType_GenericNew, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006291 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006292};