blob: 616164e1ce7475ca291918b8e4ceeca5f049a978 [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
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000587static PyGetSetDef type_getsets[] = {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000588 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
589 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000590 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000591 {"__abstractmethods__", (getter)type_abstractmethods,
592 (setter)type_set_abstractmethods, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000593 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000594 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000595 {0}
596};
597
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000598static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000599type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000600{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000601 PyObject *mod, *name, *rtn;
Guido van Rossumc3542212001-08-16 09:18:56 +0000602
603 mod = type_module(type, NULL);
604 if (mod == NULL)
605 PyErr_Clear();
Martin v. Löwis5b222132007-06-10 09:51:05 +0000606 else if (!PyUnicode_Check(mod)) {
Guido van Rossumc3542212001-08-16 09:18:56 +0000607 Py_DECREF(mod);
608 mod = NULL;
609 }
610 name = type_name(type, NULL);
611 if (name == NULL)
612 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000613
Georg Brandl1a3284e2007-12-02 09:40:06 +0000614 if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
Martin v. Löwis250ad612008-04-07 05:43:42 +0000615 rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
Guido van Rossumc3542212001-08-16 09:18:56 +0000616 else
Martin v. Löwis250ad612008-04-07 05:43:42 +0000617 rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000618
Guido van Rossumc3542212001-08-16 09:18:56 +0000619 Py_XDECREF(mod);
620 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000621 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000622}
623
Tim Peters6d6c1a32001-08-02 04:15:00 +0000624static PyObject *
625type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
626{
627 PyObject *obj;
628
629 if (type->tp_new == NULL) {
630 PyErr_Format(PyExc_TypeError,
631 "cannot create '%.100s' instances",
632 type->tp_name);
633 return NULL;
634 }
635
Tim Peters3f996e72001-09-13 19:18:27 +0000636 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000638 /* Ugly exception: when the call was type(something),
639 don't call tp_init on the result. */
640 if (type == &PyType_Type &&
641 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
642 (kwds == NULL ||
643 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
644 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000645 /* If the returned object is not an instance of type,
646 it won't be initialized. */
Christian Heimes90aa7642007-12-19 02:45:37 +0000647 if (!PyType_IsSubtype(Py_TYPE(obj), type))
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000648 return obj;
Christian Heimes90aa7642007-12-19 02:45:37 +0000649 type = Py_TYPE(obj);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000650 if (type->tp_init != NULL &&
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651 type->tp_init(obj, args, kwds) < 0) {
652 Py_DECREF(obj);
653 obj = NULL;
654 }
655 }
656 return obj;
657}
658
659PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000661{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662 PyObject *obj;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000663 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
664 /* note that we need to add one, for the sentinel */
Tim Peters406fe3b2001-10-06 19:04:01 +0000665
666 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000667 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000668 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000669 obj = (PyObject *)PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000670
Neil Schemenauerc806c882001-08-29 23:54:54 +0000671 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000672 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000673
Neil Schemenauerc806c882001-08-29 23:54:54 +0000674 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000675
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
677 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000678
Tim Peters6d6c1a32001-08-02 04:15:00 +0000679 if (type->tp_itemsize == 0)
680 PyObject_INIT(obj, type);
681 else
682 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000683
Tim Peters6d6c1a32001-08-02 04:15:00 +0000684 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000685 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000686 return obj;
687}
688
689PyObject *
690PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
691{
692 return type->tp_alloc(type, 0);
693}
694
Guido van Rossum9475a232001-10-05 20:51:39 +0000695/* Helpers for subtyping */
696
697static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000698traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
699{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000700 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000701 PyMemberDef *mp;
702
Christian Heimes90aa7642007-12-19 02:45:37 +0000703 n = Py_SIZE(type);
Guido van Rossume5c691a2003-03-07 15:13:17 +0000704 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000705 for (i = 0; i < n; i++, mp++) {
706 if (mp->type == T_OBJECT_EX) {
707 char *addr = (char *)self + mp->offset;
708 PyObject *obj = *(PyObject **)addr;
709 if (obj != NULL) {
710 int err = visit(obj, arg);
711 if (err)
712 return err;
713 }
714 }
715 }
716 return 0;
717}
718
719static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000720subtype_traverse(PyObject *self, visitproc visit, void *arg)
721{
722 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000723 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000724
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000725 /* Find the nearest base with a different tp_traverse,
726 and traverse slots while we're at it */
Christian Heimes90aa7642007-12-19 02:45:37 +0000727 type = Py_TYPE(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000728 base = type;
729 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000730 if (Py_SIZE(base)) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000731 int err = traverse_slots(base, self, visit, arg);
732 if (err)
733 return err;
734 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000735 base = base->tp_base;
736 assert(base);
737 }
738
739 if (type->tp_dictoffset != base->tp_dictoffset) {
740 PyObject **dictptr = _PyObject_GetDictPtr(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741 if (dictptr && *dictptr)
742 Py_VISIT(*dictptr);
Guido van Rossum9475a232001-10-05 20:51:39 +0000743 }
744
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000745 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossuma3862092002-06-10 15:24:42 +0000746 /* For a heaptype, the instances count as references
Guido van Rossumd8faa362007-04-27 19:54:29 +0000747 to the type. Traverse the type so the collector
Guido van Rossuma3862092002-06-10 15:24:42 +0000748 can find cycles involving this link. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000749 Py_VISIT(type);
Guido van Rossuma3862092002-06-10 15:24:42 +0000750
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000751 if (basetraverse)
752 return basetraverse(self, visit, arg);
753 return 0;
754}
755
756static void
757clear_slots(PyTypeObject *type, PyObject *self)
758{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000759 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000760 PyMemberDef *mp;
761
Christian Heimes90aa7642007-12-19 02:45:37 +0000762 n = Py_SIZE(type);
Guido van Rossume5c691a2003-03-07 15:13:17 +0000763 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000764 for (i = 0; i < n; i++, mp++) {
765 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
766 char *addr = (char *)self + mp->offset;
767 PyObject *obj = *(PyObject **)addr;
768 if (obj != NULL) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000769 *(PyObject **)addr = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000770 Py_DECREF(obj);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000771 }
772 }
773 }
774}
775
776static int
777subtype_clear(PyObject *self)
778{
779 PyTypeObject *type, *base;
780 inquiry baseclear;
781
782 /* Find the nearest base with a different tp_clear
783 and clear slots while we're at it */
Christian Heimes90aa7642007-12-19 02:45:37 +0000784 type = Py_TYPE(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000785 base = type;
786 while ((baseclear = base->tp_clear) == subtype_clear) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000787 if (Py_SIZE(base))
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000788 clear_slots(base, self);
789 base = base->tp_base;
790 assert(base);
791 }
792
Guido van Rossuma3862092002-06-10 15:24:42 +0000793 /* There's no need to clear the instance dict (if any);
794 the collector will call its tp_clear handler. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000795
796 if (baseclear)
797 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000798 return 0;
799}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000800
801static void
802subtype_dealloc(PyObject *self)
803{
Guido van Rossum14227b42001-12-06 02:35:58 +0000804 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000805 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000806
Guido van Rossum22b13872002-08-06 21:41:44 +0000807 /* Extract the type; we expect it to be a heap type */
Christian Heimes90aa7642007-12-19 02:45:37 +0000808 type = Py_TYPE(self);
Guido van Rossum22b13872002-08-06 21:41:44 +0000809 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810
Guido van Rossum22b13872002-08-06 21:41:44 +0000811 /* Test whether the type has GC exactly once */
812
813 if (!PyType_IS_GC(type)) {
814 /* It's really rare to find a dynamic type that doesn't have
815 GC; it can only happen when deriving from 'object' and not
816 adding any slots or instance variables. This allows
817 certain simplifications: there's no need to call
818 clear_slots(), or DECREF the dict, or clear weakrefs. */
819
820 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000821 if (type->tp_del) {
822 type->tp_del(self);
823 if (self->ob_refcnt > 0)
824 return;
825 }
Guido van Rossum22b13872002-08-06 21:41:44 +0000826
827 /* Find the nearest base with a different tp_dealloc */
828 base = type;
829 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000830 assert(Py_SIZE(base) == 0);
Guido van Rossum22b13872002-08-06 21:41:44 +0000831 base = base->tp_base;
832 assert(base);
833 }
834
835 /* Call the base tp_dealloc() */
836 assert(basedealloc);
837 basedealloc(self);
838
839 /* Can't reference self beyond this point */
840 Py_DECREF(type);
841
842 /* Done */
843 return;
844 }
845
846 /* We get here only if the type has GC */
847
848 /* UnTrack and re-Track around the trashcan macro, alas */
Andrew M. Kuchlingc9172d32003-02-06 15:22:49 +0000849 /* See explanation at end of function for full disclosure */
Guido van Rossum0906e072002-08-07 20:42:09 +0000850 PyObject_GC_UnTrack(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000851 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000852 Py_TRASHCAN_SAFE_BEGIN(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000853 --_PyTrash_delete_nesting;
Tim Petersf7f9e992003-11-13 21:59:32 +0000854 /* DO NOT restore GC tracking at this point. weakref callbacks
855 * (if any, and whether directly here or indirectly in something we
856 * call) may trigger GC, and if self is tracked at that point, it
857 * will look like trash to GC and GC will try to delete self again.
Tim Petersadd09b42003-11-12 20:43:28 +0000858 */
Guido van Rossum22b13872002-08-06 21:41:44 +0000859
Guido van Rossum59195fd2003-06-13 20:54:40 +0000860 /* Find the nearest base with a different tp_dealloc */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000861 base = type;
862 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000863 base = base->tp_base;
864 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000865 }
866
Guido van Rossumd8faa362007-04-27 19:54:29 +0000867 /* If we added a weaklist, we clear it. Do this *before* calling
Guido van Rossum59195fd2003-06-13 20:54:40 +0000868 the finalizer (__del__), clearing slots, or clearing the instance
869 dict. */
870
Guido van Rossum1987c662003-05-29 14:29:23 +0000871 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
872 PyObject_ClearWeakRefs(self);
873
874 /* Maybe call finalizer; exit early if resurrected */
875 if (type->tp_del) {
Tim Petersf7f9e992003-11-13 21:59:32 +0000876 _PyObject_GC_TRACK(self);
Guido van Rossum1987c662003-05-29 14:29:23 +0000877 type->tp_del(self);
878 if (self->ob_refcnt > 0)
Tim Petersf7f9e992003-11-13 21:59:32 +0000879 goto endlabel; /* resurrected */
880 else
881 _PyObject_GC_UNTRACK(self);
Thomas Woutersb2137042007-02-01 18:02:27 +0000882 /* New weakrefs could be created during the finalizer call.
883 If this occurs, clear them out without calling their
884 finalizers since they might rely on part of the object
885 being finalized that has already been destroyed. */
886 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
887 /* Modeled after GET_WEAKREFS_LISTPTR() */
888 PyWeakReference **list = (PyWeakReference **) \
889 PyObject_GET_WEAKREFS_LISTPTR(self);
890 while (*list)
891 _PyWeakref_ClearRef(*list);
892 }
Guido van Rossum1987c662003-05-29 14:29:23 +0000893 }
894
Guido van Rossum59195fd2003-06-13 20:54:40 +0000895 /* Clear slots up to the nearest base with a different tp_dealloc */
896 base = type;
897 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000898 if (Py_SIZE(base))
Guido van Rossum59195fd2003-06-13 20:54:40 +0000899 clear_slots(base, self);
900 base = base->tp_base;
901 assert(base);
902 }
903
Tim Peters6d6c1a32001-08-02 04:15:00 +0000904 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000905 if (type->tp_dictoffset && !base->tp_dictoffset) {
906 PyObject **dictptr = _PyObject_GetDictPtr(self);
907 if (dictptr != NULL) {
908 PyObject *dict = *dictptr;
909 if (dict != NULL) {
910 Py_DECREF(dict);
911 *dictptr = NULL;
912 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000913 }
914 }
915
Tim Peters0bd743c2003-11-13 22:50:00 +0000916 /* Call the base tp_dealloc(); first retrack self if
917 * basedealloc knows about gc.
918 */
919 if (PyType_IS_GC(base))
920 _PyObject_GC_TRACK(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000921 assert(basedealloc);
922 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000923
924 /* Can't reference self beyond this point */
Guido van Rossum22b13872002-08-06 21:41:44 +0000925 Py_DECREF(type);
926
Guido van Rossum0906e072002-08-07 20:42:09 +0000927 endlabel:
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000928 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000929 Py_TRASHCAN_SAFE_END(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000930 --_PyTrash_delete_nesting;
931
932 /* Explanation of the weirdness around the trashcan macros:
933
934 Q. What do the trashcan macros do?
935
936 A. Read the comment titled "Trashcan mechanism" in object.h.
937 For one, this explains why there must be a call to GC-untrack
Guido van Rossumd8faa362007-04-27 19:54:29 +0000938 before the trashcan begin macro. Without understanding the
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000939 trashcan code, the answers to the following questions don't make
940 sense.
941
942 Q. Why do we GC-untrack before the trashcan and then immediately
943 GC-track again afterward?
944
945 A. In the case that the base class is GC-aware, the base class
Guido van Rossumd8faa362007-04-27 19:54:29 +0000946 probably GC-untracks the object. If it does that using the
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000947 UNTRACK macro, this will crash when the object is already
948 untracked. Because we don't know what the base class does, the
949 only safe thing is to make sure the object is tracked when we
950 call the base class dealloc. But... The trashcan begin macro
951 requires that the object is *untracked* before it is called. So
952 the dance becomes:
953
Guido van Rossumd8faa362007-04-27 19:54:29 +0000954 GC untrack
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000955 trashcan begin
956 GC track
957
Guido van Rossumd8faa362007-04-27 19:54:29 +0000958 Q. Why did the last question say "immediately GC-track again"?
959 It's nowhere near immediately.
Tim Petersf7f9e992003-11-13 21:59:32 +0000960
Guido van Rossumd8faa362007-04-27 19:54:29 +0000961 A. Because the code *used* to re-track immediately. Bad Idea.
962 self has a refcount of 0, and if gc ever gets its hands on it
963 (which can happen if any weakref callback gets invoked), it
964 looks like trash to gc too, and gc also tries to delete self
965 then. But we're already deleting self. Double dealloction is
966 a subtle disaster.
Tim Petersf7f9e992003-11-13 21:59:32 +0000967
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000968 Q. Why the bizarre (net-zero) manipulation of
969 _PyTrash_delete_nesting around the trashcan macros?
970
971 A. Some base classes (e.g. list) also use the trashcan mechanism.
972 The following scenario used to be possible:
973
974 - suppose the trashcan level is one below the trashcan limit
975
976 - subtype_dealloc() is called
977
978 - the trashcan limit is not yet reached, so the trashcan level
Guido van Rossumd8faa362007-04-27 19:54:29 +0000979 is incremented and the code between trashcan begin and end is
980 executed
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000981
982 - this destroys much of the object's contents, including its
Guido van Rossumd8faa362007-04-27 19:54:29 +0000983 slots and __dict__
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000984
985 - basedealloc() is called; this is really list_dealloc(), or
Guido van Rossumd8faa362007-04-27 19:54:29 +0000986 some other type which also uses the trashcan macros
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000987
988 - the trashcan limit is now reached, so the object is put on the
Guido van Rossumd8faa362007-04-27 19:54:29 +0000989 trashcan's to-be-deleted-later list
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000990
991 - basedealloc() returns
992
993 - subtype_dealloc() decrefs the object's type
994
995 - subtype_dealloc() returns
996
997 - later, the trashcan code starts deleting the objects from its
Guido van Rossumd8faa362007-04-27 19:54:29 +0000998 to-be-deleted-later list
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000999
1000 - subtype_dealloc() is called *AGAIN* for the same object
1001
1002 - at the very least (if the destroyed slots and __dict__ don't
Guido van Rossumd8faa362007-04-27 19:54:29 +00001003 cause problems) the object's type gets decref'ed a second
1004 time, which is *BAD*!!!
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001005
1006 The remedy is to make sure that if the code between trashcan
1007 begin and end in subtype_dealloc() is called, the code between
1008 trashcan begin and end in basedealloc() will also be called.
1009 This is done by decrementing the level after passing into the
1010 trashcan block, and incrementing it just before leaving the
1011 block.
1012
1013 But now it's possible that a chain of objects consisting solely
1014 of objects whose deallocator is subtype_dealloc() will defeat
1015 the trashcan mechanism completely: the decremented level means
Guido van Rossumd8faa362007-04-27 19:54:29 +00001016 that the effective level never reaches the limit. Therefore, we
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001017 *increment* the level *before* entering the trashcan block, and
1018 matchingly decrement it after leaving. This means the trashcan
1019 code will trigger a little early, but that's no big deal.
1020
1021 Q. Are there any live examples of code in need of all this
1022 complexity?
1023
1024 A. Yes. See SF bug 668433 for code that crashed (when Python was
1025 compiled in debug mode) before the trashcan level manipulations
1026 were added. For more discussion, see SF patches 581742, 575073
1027 and bug 574207.
1028 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001029}
1030
Jeremy Hylton938ace62002-07-17 16:30:39 +00001031static PyTypeObject *solid_base(PyTypeObject *type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032
Tim Peters6d6c1a32001-08-02 04:15:00 +00001033/* type test with subclassing support */
1034
1035int
1036PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
1037{
1038 PyObject *mro;
1039
1040 mro = a->tp_mro;
1041 if (mro != NULL) {
1042 /* Deal with multiple inheritance without recursion
1043 by walking the MRO tuple */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001044 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001045 assert(PyTuple_Check(mro));
1046 n = PyTuple_GET_SIZE(mro);
1047 for (i = 0; i < n; i++) {
1048 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
1049 return 1;
1050 }
1051 return 0;
1052 }
1053 else {
1054 /* a is not completely initilized yet; follow tp_base */
1055 do {
1056 if (a == b)
1057 return 1;
1058 a = a->tp_base;
1059 } while (a != NULL);
1060 return b == &PyBaseObject_Type;
1061 }
1062}
1063
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001064/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +00001065 without looking in the instance dictionary
1066 (so we can't use PyObject_GetAttr) but still binding
Guido van Rossumd8faa362007-04-27 19:54:29 +00001067 it to the instance. The arguments are the object,
Guido van Rossum60718732001-08-28 17:47:51 +00001068 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001069 static variable used to cache the interned Python string.
1070
1071 Two variants:
1072
1073 - lookup_maybe() returns NULL without raising an exception
1074 when the _PyType_Lookup() call fails;
1075
1076 - lookup_method() always raises an exception upon errors.
1077*/
Guido van Rossum60718732001-08-28 17:47:51 +00001078
1079static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001080lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +00001081{
1082 PyObject *res;
1083
1084 if (*attrobj == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001085 *attrobj = PyUnicode_InternFromString(attrstr);
Guido van Rossum60718732001-08-28 17:47:51 +00001086 if (*attrobj == NULL)
1087 return NULL;
1088 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001089 res = _PyType_Lookup(Py_TYPE(self), *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001090 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +00001091 descrgetfunc f;
Christian Heimes90aa7642007-12-19 02:45:37 +00001092 if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
Guido van Rossum60718732001-08-28 17:47:51 +00001093 Py_INCREF(res);
1094 else
Christian Heimes90aa7642007-12-19 02:45:37 +00001095 res = f(res, self, (PyObject *)(Py_TYPE(self)));
Guido van Rossum60718732001-08-28 17:47:51 +00001096 }
1097 return res;
1098}
1099
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001100static PyObject *
1101lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
1102{
1103 PyObject *res = lookup_maybe(self, attrstr, attrobj);
1104 if (res == NULL && !PyErr_Occurred())
1105 PyErr_SetObject(PyExc_AttributeError, *attrobj);
1106 return res;
1107}
1108
Guido van Rossum2730b132001-08-28 18:22:14 +00001109/* A variation of PyObject_CallMethod that uses lookup_method()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001110 instead of PyObject_GetAttrString(). This uses the same convention
Guido van Rossum2730b132001-08-28 18:22:14 +00001111 as lookup_method to cache the interned name string object. */
1112
Neil Schemenauerf23473f2001-10-21 22:28:58 +00001113static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +00001114call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1115{
1116 va_list va;
1117 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +00001118 va_start(va, format);
1119
Guido van Rossumda21c012001-10-03 00:50:18 +00001120 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001121 if (func == NULL) {
1122 va_end(va);
1123 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +00001124 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001125 return NULL;
1126 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001127
1128 if (format && *format)
1129 args = Py_VaBuildValue(format, va);
1130 else
1131 args = PyTuple_New(0);
1132
1133 va_end(va);
1134
1135 if (args == NULL)
1136 return NULL;
1137
1138 assert(PyTuple_Check(args));
1139 retval = PyObject_Call(func, args, NULL);
1140
1141 Py_DECREF(args);
1142 Py_DECREF(func);
1143
1144 return retval;
1145}
1146
1147/* Clone of call_method() that returns NotImplemented when the lookup fails. */
1148
Neil Schemenauerf23473f2001-10-21 22:28:58 +00001149static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001150call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1151{
1152 va_list va;
1153 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001154 va_start(va, format);
1155
Guido van Rossumda21c012001-10-03 00:50:18 +00001156 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +00001157 if (func == NULL) {
1158 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001159 if (!PyErr_Occurred()) {
1160 Py_INCREF(Py_NotImplemented);
1161 return Py_NotImplemented;
1162 }
Guido van Rossum717ce002001-09-14 16:58:08 +00001163 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +00001164 }
1165
1166 if (format && *format)
1167 args = Py_VaBuildValue(format, va);
1168 else
1169 args = PyTuple_New(0);
1170
1171 va_end(va);
1172
Guido van Rossum717ce002001-09-14 16:58:08 +00001173 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00001174 return NULL;
1175
Guido van Rossum717ce002001-09-14 16:58:08 +00001176 assert(PyTuple_Check(args));
1177 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +00001178
1179 Py_DECREF(args);
1180 Py_DECREF(func);
1181
1182 return retval;
1183}
1184
Tim Petersea7f75d2002-12-07 21:39:16 +00001185/*
Guido van Rossum1f121312002-11-14 19:49:16 +00001186 Method resolution order algorithm C3 described in
1187 "A Monotonic Superclass Linearization for Dylan",
1188 by Kim Barrett, Bob Cassel, Paul Haahr,
Tim Petersea7f75d2002-12-07 21:39:16 +00001189 David A. Moon, Keith Playford, and P. Tucker Withington.
Guido van Rossum1f121312002-11-14 19:49:16 +00001190 (OOPSLA 1996)
1191
Guido van Rossum98f33732002-11-25 21:36:54 +00001192 Some notes about the rules implied by C3:
1193
Tim Petersea7f75d2002-12-07 21:39:16 +00001194 No duplicate bases.
Guido van Rossum98f33732002-11-25 21:36:54 +00001195 It isn't legal to repeat a class in a list of base classes.
1196
1197 The next three properties are the 3 constraints in "C3".
1198
Tim Petersea7f75d2002-12-07 21:39:16 +00001199 Local precendece order.
Guido van Rossum98f33732002-11-25 21:36:54 +00001200 If A precedes B in C's MRO, then A will precede B in the MRO of all
1201 subclasses of C.
1202
1203 Monotonicity.
1204 The MRO of a class must be an extension without reordering of the
1205 MRO of each of its superclasses.
1206
1207 Extended Precedence Graph (EPG).
1208 Linearization is consistent if there is a path in the EPG from
1209 each class to all its successors in the linearization. See
1210 the paper for definition of EPG.
Guido van Rossum1f121312002-11-14 19:49:16 +00001211 */
1212
Tim Petersea7f75d2002-12-07 21:39:16 +00001213static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001214tail_contains(PyObject *list, int whence, PyObject *o) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001215 Py_ssize_t j, size;
Guido van Rossum1f121312002-11-14 19:49:16 +00001216 size = PyList_GET_SIZE(list);
1217
1218 for (j = whence+1; j < size; j++) {
1219 if (PyList_GET_ITEM(list, j) == o)
1220 return 1;
1221 }
1222 return 0;
1223}
1224
Guido van Rossum98f33732002-11-25 21:36:54 +00001225static PyObject *
1226class_name(PyObject *cls)
1227{
1228 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1229 if (name == NULL) {
1230 PyErr_Clear();
1231 Py_XDECREF(name);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001232 name = PyObject_Repr(cls);
Guido van Rossum98f33732002-11-25 21:36:54 +00001233 }
1234 if (name == NULL)
1235 return NULL;
Martin v. Löwis9b9905b2007-06-10 21:13:34 +00001236 if (!PyUnicode_Check(name)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001237 Py_DECREF(name);
1238 return NULL;
1239 }
1240 return name;
1241}
1242
1243static int
1244check_duplicates(PyObject *list)
1245{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001246 Py_ssize_t i, j, n;
Guido van Rossum98f33732002-11-25 21:36:54 +00001247 /* Let's use a quadratic time algorithm,
1248 assuming that the bases lists is short.
1249 */
1250 n = PyList_GET_SIZE(list);
1251 for (i = 0; i < n; i++) {
1252 PyObject *o = PyList_GET_ITEM(list, i);
1253 for (j = i + 1; j < n; j++) {
1254 if (PyList_GET_ITEM(list, j) == o) {
1255 o = class_name(o);
1256 PyErr_Format(PyExc_TypeError,
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001257 "duplicate base class %.400s",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001258 o ? _PyUnicode_AsString(o) : "?");
Guido van Rossum98f33732002-11-25 21:36:54 +00001259 Py_XDECREF(o);
1260 return -1;
1261 }
1262 }
1263 }
1264 return 0;
1265}
1266
1267/* Raise a TypeError for an MRO order disagreement.
1268
1269 It's hard to produce a good error message. In the absence of better
1270 insight into error reporting, report the classes that were candidates
Guido van Rossumd8faa362007-04-27 19:54:29 +00001271 to be put next into the MRO. There is some conflict between the
Guido van Rossum98f33732002-11-25 21:36:54 +00001272 order in which they should be put in the MRO, but it's hard to
1273 diagnose what constraint can't be satisfied.
1274*/
1275
1276static void
1277set_mro_error(PyObject *to_merge, int *remain)
1278{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001279 Py_ssize_t i, n, off, to_merge_size;
Guido van Rossum98f33732002-11-25 21:36:54 +00001280 char buf[1000];
1281 PyObject *k, *v;
1282 PyObject *set = PyDict_New();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001283 if (!set) return;
Guido van Rossum98f33732002-11-25 21:36:54 +00001284
1285 to_merge_size = PyList_GET_SIZE(to_merge);
1286 for (i = 0; i < to_merge_size; i++) {
1287 PyObject *L = PyList_GET_ITEM(to_merge, i);
1288 if (remain[i] < PyList_GET_SIZE(L)) {
1289 PyObject *c = PyList_GET_ITEM(L, remain[i]);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001290 if (PyDict_SetItem(set, c, Py_None) < 0) {
1291 Py_DECREF(set);
Guido van Rossum98f33732002-11-25 21:36:54 +00001292 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001293 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001294 }
1295 }
1296 n = PyDict_Size(set);
1297
Raymond Hettingerf394df42003-04-06 19:13:41 +00001298 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1299consistent method resolution\norder (MRO) for bases");
Guido van Rossum98f33732002-11-25 21:36:54 +00001300 i = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001301 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001302 PyObject *name = class_name(k);
1303 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001304 name ? _PyUnicode_AsString(name) : "?");
Guido van Rossum98f33732002-11-25 21:36:54 +00001305 Py_XDECREF(name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306 if (--n && (size_t)(off+1) < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001307 buf[off++] = ',';
1308 buf[off] = '\0';
1309 }
1310 }
1311 PyErr_SetString(PyExc_TypeError, buf);
1312 Py_DECREF(set);
1313}
1314
Tim Petersea7f75d2002-12-07 21:39:16 +00001315static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001316pmerge(PyObject *acc, PyObject* to_merge) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001317 Py_ssize_t i, j, to_merge_size, empty_cnt;
Guido van Rossum1f121312002-11-14 19:49:16 +00001318 int *remain;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001319 int ok;
Tim Petersea7f75d2002-12-07 21:39:16 +00001320
Guido van Rossum1f121312002-11-14 19:49:16 +00001321 to_merge_size = PyList_GET_SIZE(to_merge);
1322
Guido van Rossum98f33732002-11-25 21:36:54 +00001323 /* remain stores an index into each sublist of to_merge.
1324 remain[i] is the index of the next base in to_merge[i]
1325 that is not included in acc.
1326 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
Guido van Rossum1f121312002-11-14 19:49:16 +00001328 if (remain == NULL)
1329 return -1;
1330 for (i = 0; i < to_merge_size; i++)
1331 remain[i] = 0;
1332
1333 again:
1334 empty_cnt = 0;
1335 for (i = 0; i < to_merge_size; i++) {
1336 PyObject *candidate;
Tim Petersea7f75d2002-12-07 21:39:16 +00001337
Guido van Rossum1f121312002-11-14 19:49:16 +00001338 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1339
1340 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1341 empty_cnt++;
1342 continue;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001343 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001344
Guido van Rossum98f33732002-11-25 21:36:54 +00001345 /* Choose next candidate for MRO.
1346
1347 The input sequences alone can determine the choice.
1348 If not, choose the class which appears in the MRO
1349 of the earliest direct superclass of the new class.
1350 */
1351
Guido van Rossum1f121312002-11-14 19:49:16 +00001352 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1353 for (j = 0; j < to_merge_size; j++) {
1354 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum98f33732002-11-25 21:36:54 +00001355 if (tail_contains(j_lst, remain[j], candidate)) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001356 goto skip; /* continue outer loop */
Guido van Rossum98f33732002-11-25 21:36:54 +00001357 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001358 }
1359 ok = PyList_Append(acc, candidate);
1360 if (ok < 0) {
1361 PyMem_Free(remain);
1362 return -1;
1363 }
1364 for (j = 0; j < to_merge_size; j++) {
1365 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum768158c2002-12-31 16:33:01 +00001366 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1367 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001368 remain[j]++;
1369 }
1370 }
1371 goto again;
Tim Peters9a6b8d82002-11-14 23:22:33 +00001372 skip: ;
Guido van Rossum1f121312002-11-14 19:49:16 +00001373 }
1374
Guido van Rossum98f33732002-11-25 21:36:54 +00001375 if (empty_cnt == to_merge_size) {
1376 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001377 return 0;
Guido van Rossum98f33732002-11-25 21:36:54 +00001378 }
1379 set_mro_error(to_merge, remain);
1380 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001381 return -1;
1382}
1383
Tim Peters6d6c1a32001-08-02 04:15:00 +00001384static PyObject *
1385mro_implementation(PyTypeObject *type)
1386{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001387 Py_ssize_t i, n;
1388 int ok;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001389 PyObject *bases, *result;
Guido van Rossum1f121312002-11-14 19:49:16 +00001390 PyObject *to_merge, *bases_aslist;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001391
Christian Heimes412dc9c2008-01-27 18:55:54 +00001392 if (type->tp_dict == NULL) {
1393 if (PyType_Ready(type) < 0)
Guido van Rossum63517572002-06-18 16:44:57 +00001394 return NULL;
1395 }
1396
Guido van Rossum98f33732002-11-25 21:36:54 +00001397 /* Find a superclass linearization that honors the constraints
1398 of the explicit lists of bases and the constraints implied by
Tim Petersea7f75d2002-12-07 21:39:16 +00001399 each base class.
Guido van Rossum98f33732002-11-25 21:36:54 +00001400
1401 to_merge is a list of lists, where each list is a superclass
1402 linearization implied by a base class. The last element of
1403 to_merge is the declared list of bases.
1404 */
1405
Tim Peters6d6c1a32001-08-02 04:15:00 +00001406 bases = type->tp_bases;
1407 n = PyTuple_GET_SIZE(bases);
Guido van Rossum1f121312002-11-14 19:49:16 +00001408
1409 to_merge = PyList_New(n+1);
1410 if (to_merge == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001411 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001412
Tim Peters6d6c1a32001-08-02 04:15:00 +00001413 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001414 PyObject *base = PyTuple_GET_ITEM(bases, i);
1415 PyObject *parentMRO;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001416 parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001417 if (parentMRO == NULL) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001418 Py_DECREF(to_merge);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001419 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001420 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001421
1422 PyList_SET_ITEM(to_merge, i, parentMRO);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001423 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001424
1425 bases_aslist = PySequence_List(bases);
1426 if (bases_aslist == NULL) {
1427 Py_DECREF(to_merge);
1428 return NULL;
1429 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001430 /* This is just a basic sanity check. */
1431 if (check_duplicates(bases_aslist) < 0) {
1432 Py_DECREF(to_merge);
1433 Py_DECREF(bases_aslist);
1434 return NULL;
1435 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001436 PyList_SET_ITEM(to_merge, n, bases_aslist);
1437
1438 result = Py_BuildValue("[O]", (PyObject *)type);
1439 if (result == NULL) {
1440 Py_DECREF(to_merge);
1441 return NULL;
1442 }
1443
1444 ok = pmerge(result, to_merge);
1445 Py_DECREF(to_merge);
1446 if (ok < 0) {
1447 Py_DECREF(result);
1448 return NULL;
1449 }
1450
Tim Peters6d6c1a32001-08-02 04:15:00 +00001451 return result;
1452}
1453
1454static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001455mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001456{
1457 PyTypeObject *type = (PyTypeObject *)self;
1458
Tim Peters6d6c1a32001-08-02 04:15:00 +00001459 return mro_implementation(type);
1460}
1461
1462static int
1463mro_internal(PyTypeObject *type)
1464{
1465 PyObject *mro, *result, *tuple;
Armin Rigo037d1e02005-12-29 17:07:39 +00001466 int checkit = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001467
Christian Heimes90aa7642007-12-19 02:45:37 +00001468 if (Py_TYPE(type) == &PyType_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001469 result = mro_implementation(type);
1470 }
1471 else {
Guido van Rossum60718732001-08-28 17:47:51 +00001472 static PyObject *mro_str;
Armin Rigo037d1e02005-12-29 17:07:39 +00001473 checkit = 1;
Guido van Rossum60718732001-08-28 17:47:51 +00001474 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001475 if (mro == NULL)
1476 return -1;
1477 result = PyObject_CallObject(mro, NULL);
1478 Py_DECREF(mro);
1479 }
1480 if (result == NULL)
1481 return -1;
1482 tuple = PySequence_Tuple(result);
1483 Py_DECREF(result);
Armin Rigo037d1e02005-12-29 17:07:39 +00001484 if (tuple == NULL)
1485 return -1;
1486 if (checkit) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001487 Py_ssize_t i, len;
Armin Rigo037d1e02005-12-29 17:07:39 +00001488 PyObject *cls;
1489 PyTypeObject *solid;
1490
1491 solid = solid_base(type);
1492
1493 len = PyTuple_GET_SIZE(tuple);
1494
1495 for (i = 0; i < len; i++) {
1496 PyTypeObject *t;
1497 cls = PyTuple_GET_ITEM(tuple, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001498 if (!PyType_Check(cls)) {
Armin Rigo037d1e02005-12-29 17:07:39 +00001499 PyErr_Format(PyExc_TypeError,
1500 "mro() returned a non-class ('%.500s')",
Christian Heimes90aa7642007-12-19 02:45:37 +00001501 Py_TYPE(cls)->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001502 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001503 return -1;
1504 }
1505 t = (PyTypeObject*)cls;
1506 if (!PyType_IsSubtype(solid, solid_base(t))) {
1507 PyErr_Format(PyExc_TypeError,
1508 "mro() returned base with unsuitable layout ('%.500s')",
1509 t->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001510 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001511 return -1;
1512 }
1513 }
1514 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001515 type->tp_mro = tuple;
Christian Heimesa62da1d2008-01-12 19:39:10 +00001516
1517 type_mro_modified(type, type->tp_mro);
1518 /* corner case: the old-style super class might have been hidden
1519 from the custom MRO */
1520 type_mro_modified(type, type->tp_bases);
1521
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001522 PyType_Modified(type);
Christian Heimesa62da1d2008-01-12 19:39:10 +00001523
Tim Peters6d6c1a32001-08-02 04:15:00 +00001524 return 0;
1525}
1526
1527
1528/* Calculate the best base amongst multiple base classes.
1529 This is the first one that's on the path to the "solid base". */
1530
1531static PyTypeObject *
1532best_base(PyObject *bases)
1533{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001534 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001535 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +00001536 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001537
1538 assert(PyTuple_Check(bases));
1539 n = PyTuple_GET_SIZE(bases);
1540 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +00001541 base = NULL;
1542 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001543 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001544 base_proto = PyTuple_GET_ITEM(bases, i);
Tim Petersa91e9642001-11-14 23:32:33 +00001545 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001546 PyErr_SetString(
1547 PyExc_TypeError,
1548 "bases must be types");
1549 return NULL;
1550 }
Tim Petersa91e9642001-11-14 23:32:33 +00001551 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001552 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001553 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001554 return NULL;
1555 }
1556 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +00001557 if (winner == NULL) {
1558 winner = candidate;
1559 base = base_i;
1560 }
1561 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001562 ;
1563 else if (PyType_IsSubtype(candidate, winner)) {
1564 winner = candidate;
1565 base = base_i;
1566 }
1567 else {
1568 PyErr_SetString(
1569 PyExc_TypeError,
1570 "multiple bases have "
1571 "instance lay-out conflict");
1572 return NULL;
1573 }
1574 }
Guido van Rossume54616c2001-12-14 04:19:56 +00001575 if (base == NULL)
1576 PyErr_SetString(PyExc_TypeError,
1577 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001578 return base;
1579}
1580
1581static int
1582extra_ivars(PyTypeObject *type, PyTypeObject *base)
1583{
Neil Schemenauerc806c882001-08-29 23:54:54 +00001584 size_t t_size = type->tp_basicsize;
1585 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001586
Guido van Rossum9676b222001-08-17 20:32:36 +00001587 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001588 if (type->tp_itemsize || base->tp_itemsize) {
1589 /* If itemsize is involved, stricter rules */
1590 return t_size != b_size ||
1591 type->tp_itemsize != base->tp_itemsize;
1592 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001593 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
Guido van Rossum360e4b82007-05-14 22:51:27 +00001594 type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
1595 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossum9676b222001-08-17 20:32:36 +00001596 t_size -= sizeof(PyObject *);
1597 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
Guido van Rossum360e4b82007-05-14 22:51:27 +00001598 type->tp_dictoffset + sizeof(PyObject *) == t_size &&
1599 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossum9676b222001-08-17 20:32:36 +00001600 t_size -= sizeof(PyObject *);
1601
1602 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001603}
1604
1605static PyTypeObject *
1606solid_base(PyTypeObject *type)
1607{
1608 PyTypeObject *base;
1609
1610 if (type->tp_base)
1611 base = solid_base(type->tp_base);
1612 else
1613 base = &PyBaseObject_Type;
1614 if (extra_ivars(type, base))
1615 return type;
1616 else
1617 return base;
1618}
1619
Jeremy Hylton938ace62002-07-17 16:30:39 +00001620static void object_dealloc(PyObject *);
1621static int object_init(PyObject *, PyObject *, PyObject *);
1622static int update_slot(PyTypeObject *, PyObject *);
1623static void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001624
Guido van Rossum360e4b82007-05-14 22:51:27 +00001625/*
1626 * Helpers for __dict__ descriptor. We don't want to expose the dicts
1627 * inherited from various builtin types. The builtin base usually provides
1628 * its own __dict__ descriptor, so we use that when we can.
1629 */
1630static PyTypeObject *
1631get_builtin_base_with_dict(PyTypeObject *type)
1632{
1633 while (type->tp_base != NULL) {
1634 if (type->tp_dictoffset != 0 &&
1635 !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1636 return type;
1637 type = type->tp_base;
1638 }
1639 return NULL;
1640}
1641
1642static PyObject *
1643get_dict_descriptor(PyTypeObject *type)
1644{
1645 static PyObject *dict_str;
1646 PyObject *descr;
1647
1648 if (dict_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001649 dict_str = PyUnicode_InternFromString("__dict__");
Guido van Rossum360e4b82007-05-14 22:51:27 +00001650 if (dict_str == NULL)
1651 return NULL;
1652 }
1653 descr = _PyType_Lookup(type, dict_str);
1654 if (descr == NULL || !PyDescr_IsData(descr))
1655 return NULL;
1656
1657 return descr;
1658}
1659
1660static void
1661raise_dict_descr_error(PyObject *obj)
1662{
1663 PyErr_Format(PyExc_TypeError,
1664 "this __dict__ descriptor does not support "
Christian Heimes90aa7642007-12-19 02:45:37 +00001665 "'%.200s' objects", Py_TYPE(obj)->tp_name);
Guido van Rossum360e4b82007-05-14 22:51:27 +00001666}
1667
Tim Peters6d6c1a32001-08-02 04:15:00 +00001668static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001669subtype_dict(PyObject *obj, void *context)
1670{
Guido van Rossum360e4b82007-05-14 22:51:27 +00001671 PyObject **dictptr;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001672 PyObject *dict;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001673 PyTypeObject *base;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001674
Christian Heimes90aa7642007-12-19 02:45:37 +00001675 base = get_builtin_base_with_dict(Py_TYPE(obj));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001676 if (base != NULL) {
1677 descrgetfunc func;
1678 PyObject *descr = get_dict_descriptor(base);
1679 if (descr == NULL) {
1680 raise_dict_descr_error(obj);
1681 return NULL;
1682 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001683 func = Py_TYPE(descr)->tp_descr_get;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001684 if (func == NULL) {
1685 raise_dict_descr_error(obj);
1686 return NULL;
1687 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001688 return func(descr, obj, (PyObject *)(Py_TYPE(obj)));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001689 }
1690
1691 dictptr = _PyObject_GetDictPtr(obj);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001692 if (dictptr == NULL) {
1693 PyErr_SetString(PyExc_AttributeError,
1694 "This object has no __dict__");
1695 return NULL;
1696 }
1697 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +00001698 if (dict == NULL)
1699 *dictptr = dict = PyDict_New();
1700 Py_XINCREF(dict);
1701 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001702}
1703
Guido van Rossum6661be32001-10-26 04:26:12 +00001704static int
1705subtype_setdict(PyObject *obj, PyObject *value, void *context)
1706{
Guido van Rossum360e4b82007-05-14 22:51:27 +00001707 PyObject **dictptr;
Guido van Rossum6661be32001-10-26 04:26:12 +00001708 PyObject *dict;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001709 PyTypeObject *base;
Guido van Rossum6661be32001-10-26 04:26:12 +00001710
Christian Heimes90aa7642007-12-19 02:45:37 +00001711 base = get_builtin_base_with_dict(Py_TYPE(obj));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001712 if (base != NULL) {
1713 descrsetfunc func;
1714 PyObject *descr = get_dict_descriptor(base);
1715 if (descr == NULL) {
1716 raise_dict_descr_error(obj);
1717 return -1;
1718 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001719 func = Py_TYPE(descr)->tp_descr_set;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001720 if (func == NULL) {
1721 raise_dict_descr_error(obj);
1722 return -1;
1723 }
1724 return func(descr, obj, value);
1725 }
1726
1727 dictptr = _PyObject_GetDictPtr(obj);
Guido van Rossum6661be32001-10-26 04:26:12 +00001728 if (dictptr == NULL) {
1729 PyErr_SetString(PyExc_AttributeError,
1730 "This object has no __dict__");
1731 return -1;
1732 }
Guido van Rossumd331cb52001-12-05 19:46:42 +00001733 if (value != NULL && !PyDict_Check(value)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001734 PyErr_Format(PyExc_TypeError,
1735 "__dict__ must be set to a dictionary, "
Christian Heimes90aa7642007-12-19 02:45:37 +00001736 "not a '%.200s'", Py_TYPE(value)->tp_name);
Guido van Rossum6661be32001-10-26 04:26:12 +00001737 return -1;
1738 }
1739 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +00001740 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +00001741 *dictptr = value;
1742 Py_XDECREF(dict);
1743 return 0;
1744}
1745
Guido van Rossumad47da02002-08-12 19:05:44 +00001746static PyObject *
1747subtype_getweakref(PyObject *obj, void *context)
1748{
1749 PyObject **weaklistptr;
1750 PyObject *result;
1751
Christian Heimes90aa7642007-12-19 02:45:37 +00001752 if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001753 PyErr_SetString(PyExc_AttributeError,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001754 "This object has no __weakref__");
Guido van Rossumad47da02002-08-12 19:05:44 +00001755 return NULL;
1756 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001757 assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
1758 assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
1759 (size_t)(Py_TYPE(obj)->tp_basicsize));
Guido van Rossumad47da02002-08-12 19:05:44 +00001760 weaklistptr = (PyObject **)
Christian Heimes90aa7642007-12-19 02:45:37 +00001761 ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
Guido van Rossumad47da02002-08-12 19:05:44 +00001762 if (*weaklistptr == NULL)
1763 result = Py_None;
1764 else
1765 result = *weaklistptr;
1766 Py_INCREF(result);
1767 return result;
1768}
1769
Guido van Rossum373c7412003-01-07 13:41:37 +00001770/* Three variants on the subtype_getsets list. */
1771
1772static PyGetSetDef subtype_getsets_full[] = {
Guido van Rossumad47da02002-08-12 19:05:44 +00001773 {"__dict__", subtype_dict, subtype_setdict,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001774 PyDoc_STR("dictionary for instance variables (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001775 {"__weakref__", subtype_getweakref, NULL,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001776 PyDoc_STR("list of weak references to the object (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001777 {0}
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001778};
1779
Guido van Rossum373c7412003-01-07 13:41:37 +00001780static PyGetSetDef subtype_getsets_dict_only[] = {
1781 {"__dict__", subtype_dict, subtype_setdict,
1782 PyDoc_STR("dictionary for instance variables (if defined)")},
1783 {0}
1784};
1785
1786static PyGetSetDef subtype_getsets_weakref_only[] = {
1787 {"__weakref__", subtype_getweakref, NULL,
1788 PyDoc_STR("list of weak references to the object (if defined)")},
1789 {0}
1790};
1791
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001792static int
1793valid_identifier(PyObject *s)
1794{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001795 if (!PyUnicode_Check(s)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001796 PyErr_Format(PyExc_TypeError,
1797 "__slots__ items must be strings, not '%.200s'",
Christian Heimes90aa7642007-12-19 02:45:37 +00001798 Py_TYPE(s)->tp_name);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001799 return 0;
1800 }
Georg Brandlf4780d02007-08-30 18:29:48 +00001801 if (!PyUnicode_IsIdentifier(s)) {
1802 PyErr_SetString(PyExc_TypeError,
1803 "__slots__ must be identifiers");
1804 return 0;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001805 }
1806 return 1;
1807}
1808
Guido van Rossumd8faa362007-04-27 19:54:29 +00001809/* Forward */
1810static int
1811object_init(PyObject *self, PyObject *args, PyObject *kwds);
1812
1813static int
1814type_init(PyObject *cls, PyObject *args, PyObject *kwds)
1815{
1816 int res;
1817
1818 assert(args != NULL && PyTuple_Check(args));
1819 assert(kwds == NULL || PyDict_Check(kwds));
1820
1821 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
1822 PyErr_SetString(PyExc_TypeError,
1823 "type.__init__() takes no keyword arguments");
1824 return -1;
1825 }
1826
1827 if (args != NULL && PyTuple_Check(args) &&
1828 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
1829 PyErr_SetString(PyExc_TypeError,
1830 "type.__init__() takes 1 or 3 arguments");
1831 return -1;
1832 }
1833
1834 /* Call object.__init__(self) now. */
1835 /* XXX Could call super(type, cls).__init__() but what's the point? */
1836 args = PyTuple_GetSlice(args, 0, 0);
1837 res = object_init(cls, args, NULL);
1838 Py_DECREF(args);
1839 return res;
1840}
1841
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001842static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001843type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1844{
1845 PyObject *name, *bases, *dict;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001846 static char *kwlist[] = {"name", "bases", "dict", 0};
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001847 PyObject *slots, *tmp, *newslots;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001848 PyTypeObject *type, *base, *tmptype, *winner;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001849 PyHeapTypeObject *et;
Guido van Rossum6f799372001-09-20 20:46:19 +00001850 PyMemberDef *mp;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001851 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
Guido van Rossumad47da02002-08-12 19:05:44 +00001852 int j, may_add_dict, may_add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001853
Tim Peters3abca122001-10-27 19:37:48 +00001854 assert(args != NULL && PyTuple_Check(args));
1855 assert(kwds == NULL || PyDict_Check(kwds));
1856
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001857 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +00001858 {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001859 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1860 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
Tim Peters3abca122001-10-27 19:37:48 +00001861
1862 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1863 PyObject *x = PyTuple_GET_ITEM(args, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00001864 Py_INCREF(Py_TYPE(x));
1865 return (PyObject *) Py_TYPE(x);
Tim Peters3abca122001-10-27 19:37:48 +00001866 }
1867
1868 /* SF bug 475327 -- if that didn't trigger, we need 3
1869 arguments. but PyArg_ParseTupleAndKeywords below may give
1870 a msg saying type() needs exactly 3. */
1871 if (nargs + nkwds != 3) {
1872 PyErr_SetString(PyExc_TypeError,
1873 "type() takes 1 or 3 arguments");
1874 return NULL;
1875 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001876 }
1877
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001878 /* Check arguments: (name, bases, dict) */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001879 if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001880 &name,
1881 &PyTuple_Type, &bases,
1882 &PyDict_Type, &dict))
1883 return NULL;
1884
1885 /* Determine the proper metatype to deal with this,
1886 and check for metatype conflicts while we're at it.
1887 Note that if some other metatype wins to contract,
1888 it's possible that its instances are not types. */
1889 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001890 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001891 for (i = 0; i < nbases; i++) {
1892 tmp = PyTuple_GET_ITEM(bases, i);
Christian Heimes90aa7642007-12-19 02:45:37 +00001893 tmptype = Py_TYPE(tmp);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001894 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001895 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001896 if (PyType_IsSubtype(tmptype, winner)) {
1897 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001898 continue;
1899 }
1900 PyErr_SetString(PyExc_TypeError,
Guido van Rossum636688d2003-04-23 12:07:22 +00001901 "metaclass conflict: "
1902 "the metaclass of a derived class "
1903 "must be a (non-strict) subclass "
1904 "of the metaclasses of all its bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001905 return NULL;
1906 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001907 if (winner != metatype) {
1908 if (winner->tp_new != type_new) /* Pass it to the winner */
1909 return winner->tp_new(winner, args, kwds);
1910 metatype = winner;
1911 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001912
1913 /* Adjust for empty tuple bases */
1914 if (nbases == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001915 bases = PyTuple_Pack(1, &PyBaseObject_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001916 if (bases == NULL)
1917 return NULL;
1918 nbases = 1;
1919 }
1920 else
1921 Py_INCREF(bases);
1922
1923 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1924
1925 /* Calculate best base, and check that all bases are type objects */
1926 base = best_base(bases);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001927 if (base == NULL) {
1928 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001929 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001930 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001931 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1932 PyErr_Format(PyExc_TypeError,
1933 "type '%.100s' is not an acceptable base type",
1934 base->tp_name);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001935 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001936 return NULL;
1937 }
1938
Tim Peters6d6c1a32001-08-02 04:15:00 +00001939 /* Check for a __slots__ sequence variable in dict, and count it */
1940 slots = PyDict_GetItemString(dict, "__slots__");
1941 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001942 add_dict = 0;
1943 add_weak = 0;
Guido van Rossumad47da02002-08-12 19:05:44 +00001944 may_add_dict = base->tp_dictoffset == 0;
1945 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1946 if (slots == NULL) {
1947 if (may_add_dict) {
1948 add_dict++;
1949 }
1950 if (may_add_weak) {
1951 add_weak++;
1952 }
1953 }
1954 else {
1955 /* Have slots */
1956
Tim Peters6d6c1a32001-08-02 04:15:00 +00001957 /* Make it into a tuple */
Neal Norwitz80e7f272007-08-26 06:45:23 +00001958 if (PyUnicode_Check(slots))
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001959 slots = PyTuple_Pack(1, slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001960 else
1961 slots = PySequence_Tuple(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001962 if (slots == NULL) {
1963 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001964 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001965 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001966 assert(PyTuple_Check(slots));
1967
1968 /* Are slots allowed? */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001969 nslots = PyTuple_GET_SIZE(slots);
Jeremy Hylton1c7a0ea2003-07-16 16:08:23 +00001970 if (nslots > 0 && base->tp_itemsize != 0) {
Guido van Rossumc4141872001-08-30 04:43:35 +00001971 PyErr_Format(PyExc_TypeError,
1972 "nonempty __slots__ "
1973 "not supported for subtype of '%s'",
1974 base->tp_name);
Guido van Rossumad47da02002-08-12 19:05:44 +00001975 bad_slots:
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001976 Py_DECREF(bases);
Guido van Rossumad47da02002-08-12 19:05:44 +00001977 Py_DECREF(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00001978 return NULL;
1979 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001980
1981 /* Check for valid slot names and two special cases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001982 for (i = 0; i < nslots; i++) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001983 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
Guido van Rossumad47da02002-08-12 19:05:44 +00001984 if (!valid_identifier(tmp))
1985 goto bad_slots;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001986 assert(PyUnicode_Check(tmp));
1987 if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001988 if (!may_add_dict || add_dict) {
1989 PyErr_SetString(PyExc_TypeError,
1990 "__dict__ slot disallowed: "
1991 "we already got one");
1992 goto bad_slots;
1993 }
1994 add_dict++;
1995 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001996 if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001997 if (!may_add_weak || add_weak) {
1998 PyErr_SetString(PyExc_TypeError,
1999 "__weakref__ slot disallowed: "
2000 "either we already got one, "
2001 "or __itemsize__ != 0");
2002 goto bad_slots;
2003 }
2004 add_weak++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002005 }
2006 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002007
Guido van Rossumd8faa362007-04-27 19:54:29 +00002008 /* Copy slots into a list, mangle names and sort them.
2009 Sorted names are needed for __class__ assignment.
2010 Convert them back to tuple at the end.
2011 */
2012 newslots = PyList_New(nslots - add_dict - add_weak);
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002013 if (newslots == NULL)
Guido van Rossumad47da02002-08-12 19:05:44 +00002014 goto bad_slots;
2015 for (i = j = 0; i < nslots; i++) {
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002016 tmp = PyTuple_GET_ITEM(slots, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002017 if ((add_dict &&
2018 PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) ||
2019 (add_weak &&
2020 PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0))
Guido van Rossumad47da02002-08-12 19:05:44 +00002021 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 tmp =_Py_Mangle(name, tmp);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002023 if (!tmp)
2024 goto bad_slots;
2025 PyList_SET_ITEM(newslots, j, tmp);
Guido van Rossumad47da02002-08-12 19:05:44 +00002026 j++;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002027 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002028 assert(j == nslots - add_dict - add_weak);
2029 nslots = j;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002030 Py_DECREF(slots);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002031 if (PyList_Sort(newslots) == -1) {
2032 Py_DECREF(bases);
2033 Py_DECREF(newslots);
2034 return NULL;
2035 }
2036 slots = PyList_AsTuple(newslots);
2037 Py_DECREF(newslots);
2038 if (slots == NULL) {
2039 Py_DECREF(bases);
2040 return NULL;
2041 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002042
Guido van Rossumad47da02002-08-12 19:05:44 +00002043 /* Secondary bases may provide weakrefs or dict */
2044 if (nbases > 1 &&
2045 ((may_add_dict && !add_dict) ||
2046 (may_add_weak && !add_weak))) {
2047 for (i = 0; i < nbases; i++) {
2048 tmp = PyTuple_GET_ITEM(bases, i);
2049 if (tmp == (PyObject *)base)
2050 continue; /* Skip primary base */
Guido van Rossumad47da02002-08-12 19:05:44 +00002051 assert(PyType_Check(tmp));
2052 tmptype = (PyTypeObject *)tmp;
2053 if (may_add_dict && !add_dict &&
2054 tmptype->tp_dictoffset != 0)
2055 add_dict++;
2056 if (may_add_weak && !add_weak &&
2057 tmptype->tp_weaklistoffset != 0)
2058 add_weak++;
2059 if (may_add_dict && !add_dict)
2060 continue;
2061 if (may_add_weak && !add_weak)
2062 continue;
2063 /* Nothing more to check */
2064 break;
2065 }
2066 }
Guido van Rossum9676b222001-08-17 20:32:36 +00002067 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002068
2069 /* XXX From here until type is safely allocated,
2070 "return NULL" may leak slots! */
2071
2072 /* Allocate the type object */
2073 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
Guido van Rossumad47da02002-08-12 19:05:44 +00002074 if (type == NULL) {
2075 Py_XDECREF(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002076 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002077 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00002078 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002079
2080 /* Keep name and slots alive in the extended type object */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002081 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002082 Py_INCREF(name);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002083 et->ht_name = name;
2084 et->ht_slots = slots;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002085
Guido van Rossumdc91b992001-08-08 22:26:22 +00002086 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002087 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2088 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00002089 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2090 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00002091
Guido van Rossumdc91b992001-08-08 22:26:22 +00002092 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002093 type->tp_as_number = &et->as_number;
2094 type->tp_as_sequence = &et->as_sequence;
2095 type->tp_as_mapping = &et->as_mapping;
2096 type->tp_as_buffer = &et->as_buffer;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002097 type->tp_name = _PyUnicode_AsString(name);
Neal Norwitz80e7f272007-08-26 06:45:23 +00002098 if (!type->tp_name) {
2099 Py_DECREF(type);
2100 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002101 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002102
2103 /* Set tp_base and tp_bases */
2104 type->tp_bases = bases;
2105 Py_INCREF(base);
2106 type->tp_base = base;
2107
Guido van Rossum687ae002001-10-15 22:03:32 +00002108 /* Initialize tp_dict from passed-in dict */
2109 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002110 if (dict == NULL) {
2111 Py_DECREF(type);
2112 return NULL;
2113 }
2114
Guido van Rossumc3542212001-08-16 09:18:56 +00002115 /* Set __module__ in the dict */
2116 if (PyDict_GetItemString(dict, "__module__") == NULL) {
2117 tmp = PyEval_GetGlobals();
2118 if (tmp != NULL) {
2119 tmp = PyDict_GetItemString(tmp, "__name__");
2120 if (tmp != NULL) {
2121 if (PyDict_SetItemString(dict, "__module__",
2122 tmp) < 0)
2123 return NULL;
2124 }
2125 }
2126 }
2127
Tim Peters2f93e282001-10-04 05:27:00 +00002128 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00002129 and is a string. The __doc__ accessor will first look for tp_doc;
2130 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00002131 */
2132 {
2133 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002134 if (doc != NULL && PyUnicode_Check(doc)) {
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002135 Py_ssize_t len;
2136 char *doc_str;
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002137 char *tp_doc;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002138
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002139 doc_str = _PyUnicode_AsStringAndSize(doc, &len);
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002140 if (doc_str == NULL) {
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002141 Py_DECREF(type);
2142 return NULL;
Tim Peters2f93e282001-10-04 05:27:00 +00002143 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002144 if ((Py_ssize_t)strlen(doc_str) != len) {
2145 PyErr_SetString(PyExc_TypeError,
2146 "__doc__ contains null-bytes");
2147 Py_DECREF(type);
2148 return NULL;
2149 }
2150 tp_doc = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002151 if (tp_doc == NULL) {
2152 Py_DECREF(type);
2153 return NULL;
Neal Norwitza369c5a2007-08-25 07:41:59 +00002154 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002155 memcpy(tp_doc, doc_str, len + 1);
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002156 type->tp_doc = tp_doc;
Tim Peters2f93e282001-10-04 05:27:00 +00002157 }
2158 }
2159
Tim Peters6d6c1a32001-08-02 04:15:00 +00002160 /* Special-case __new__: if it's a plain function,
2161 make it a static function */
2162 tmp = PyDict_GetItemString(dict, "__new__");
2163 if (tmp != NULL && PyFunction_Check(tmp)) {
2164 tmp = PyStaticMethod_New(tmp);
2165 if (tmp == NULL) {
2166 Py_DECREF(type);
2167 return NULL;
2168 }
2169 PyDict_SetItemString(dict, "__new__", tmp);
2170 Py_DECREF(tmp);
2171 }
2172
2173 /* Add descriptors for custom slots from __slots__, or for __dict__ */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002174 mp = PyHeapType_GET_MEMBERS(et);
Neil Schemenauerc806c882001-08-29 23:54:54 +00002175 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002176 if (slots != NULL) {
2177 for (i = 0; i < nslots; i++, mp++) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002178 mp->name = _PyUnicode_AsString(
Tim Peters6d6c1a32001-08-02 04:15:00 +00002179 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00002180 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002181 mp->offset = slotoffset;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002182
2183 /* __dict__ and __weakref__ are already filtered out */
2184 assert(strcmp(mp->name, "__dict__") != 0);
2185 assert(strcmp(mp->name, "__weakref__") != 0);
2186
Tim Peters6d6c1a32001-08-02 04:15:00 +00002187 slotoffset += sizeof(PyObject *);
2188 }
2189 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002190 if (add_dict) {
2191 if (base->tp_itemsize)
2192 type->tp_dictoffset = -(long)sizeof(PyObject *);
2193 else
2194 type->tp_dictoffset = slotoffset;
2195 slotoffset += sizeof(PyObject *);
2196 }
2197 if (add_weak) {
2198 assert(!base->tp_itemsize);
2199 type->tp_weaklistoffset = slotoffset;
2200 slotoffset += sizeof(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002201 }
2202 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00002203 type->tp_itemsize = base->tp_itemsize;
Guido van Rossume5c691a2003-03-07 15:13:17 +00002204 type->tp_members = PyHeapType_GET_MEMBERS(et);
Guido van Rossum373c7412003-01-07 13:41:37 +00002205
2206 if (type->tp_weaklistoffset && type->tp_dictoffset)
2207 type->tp_getset = subtype_getsets_full;
2208 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
2209 type->tp_getset = subtype_getsets_weakref_only;
2210 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
2211 type->tp_getset = subtype_getsets_dict_only;
2212 else
2213 type->tp_getset = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002214
2215 /* Special case some slots */
2216 if (type->tp_dictoffset != 0 || nslots > 0) {
2217 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
2218 type->tp_getattro = PyObject_GenericGetAttr;
2219 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
2220 type->tp_setattro = PyObject_GenericSetAttr;
2221 }
2222 type->tp_dealloc = subtype_dealloc;
2223
Guido van Rossum9475a232001-10-05 20:51:39 +00002224 /* Enable GC unless there are really no instance variables possible */
2225 if (!(type->tp_basicsize == sizeof(PyObject) &&
2226 type->tp_itemsize == 0))
2227 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2228
Tim Peters6d6c1a32001-08-02 04:15:00 +00002229 /* Always override allocation strategy to use regular heap */
2230 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00002231 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002232 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00002233 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00002234 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00002235 }
2236 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002237 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002238
2239 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002240 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002241 Py_DECREF(type);
2242 return NULL;
2243 }
2244
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002245 /* Put the proper slots in place */
2246 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00002247
Tim Peters6d6c1a32001-08-02 04:15:00 +00002248 return (PyObject *)type;
2249}
2250
2251/* Internal API to look for a name through the MRO.
2252 This returns a borrowed reference, and doesn't set an exception! */
2253PyObject *
2254_PyType_Lookup(PyTypeObject *type, PyObject *name)
2255{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002256 Py_ssize_t i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00002257 PyObject *mro, *res, *base, *dict;
Christian Heimesa62da1d2008-01-12 19:39:10 +00002258 unsigned int h;
2259
2260 if (MCACHE_CACHEABLE_NAME(name) &&
Christian Heimes412dc9c2008-01-27 18:55:54 +00002261 PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
Christian Heimesa62da1d2008-01-12 19:39:10 +00002262 /* fast path */
2263 h = MCACHE_HASH_METHOD(type, name);
2264 if (method_cache[h].version == type->tp_version_tag &&
2265 method_cache[h].name == name)
2266 return method_cache[h].value;
2267 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002268
Guido van Rossum687ae002001-10-15 22:03:32 +00002269 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002270 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00002271
2272 /* If mro is NULL, the type is either not yet initialized
2273 by PyType_Ready(), or already cleared by type_clear().
2274 Either way the safest thing to do is to return NULL. */
2275 if (mro == NULL)
2276 return NULL;
2277
Christian Heimesa62da1d2008-01-12 19:39:10 +00002278 res = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002279 assert(PyTuple_Check(mro));
2280 n = PyTuple_GET_SIZE(mro);
2281 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002282 base = PyTuple_GET_ITEM(mro, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002283 assert(PyType_Check(base));
2284 dict = ((PyTypeObject *)base)->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002285 assert(dict && PyDict_Check(dict));
2286 res = PyDict_GetItem(dict, name);
2287 if (res != NULL)
Christian Heimesa62da1d2008-01-12 19:39:10 +00002288 break;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002289 }
Christian Heimesa62da1d2008-01-12 19:39:10 +00002290
2291 if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
2292 h = MCACHE_HASH_METHOD(type, name);
2293 method_cache[h].version = type->tp_version_tag;
2294 method_cache[h].value = res; /* borrowed */
2295 Py_INCREF(name);
2296 Py_DECREF(method_cache[h].name);
2297 method_cache[h].name = name;
2298 }
2299 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002300}
2301
2302/* This is similar to PyObject_GenericGetAttr(),
2303 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2304static PyObject *
2305type_getattro(PyTypeObject *type, PyObject *name)
2306{
Christian Heimes90aa7642007-12-19 02:45:37 +00002307 PyTypeObject *metatype = Py_TYPE(type);
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002308 PyObject *meta_attribute, *attribute;
2309 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002310
2311 /* Initialize this type (we'll assume the metatype is initialized) */
2312 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002313 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002314 return NULL;
2315 }
2316
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002317 /* No readable descriptor found yet */
2318 meta_get = NULL;
Tim Peters34592512002-07-11 06:23:50 +00002319
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002320 /* Look for the attribute in the metatype */
2321 meta_attribute = _PyType_Lookup(metatype, name);
2322
2323 if (meta_attribute != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +00002324 meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
Tim Peters34592512002-07-11 06:23:50 +00002325
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002326 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2327 /* Data descriptors implement tp_descr_set to intercept
2328 * writes. Assume the attribute is not overridden in
2329 * type's tp_dict (and bases): call the descriptor now.
2330 */
2331 return meta_get(meta_attribute, (PyObject *)type,
2332 (PyObject *)metatype);
2333 }
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002334 Py_INCREF(meta_attribute);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002335 }
2336
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002337 /* No data descriptor found on metatype. Look in tp_dict of this
2338 * type and its bases */
2339 attribute = _PyType_Lookup(type, name);
2340 if (attribute != NULL) {
2341 /* Implement descriptor functionality, if any */
Christian Heimes90aa7642007-12-19 02:45:37 +00002342 descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002343
2344 Py_XDECREF(meta_attribute);
2345
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002346 if (local_get != NULL) {
2347 /* NULL 2nd argument indicates the descriptor was
2348 * found on the target object itself (or a base) */
2349 return local_get(attribute, (PyObject *)NULL,
2350 (PyObject *)type);
2351 }
Tim Peters34592512002-07-11 06:23:50 +00002352
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002353 Py_INCREF(attribute);
2354 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002355 }
2356
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002357 /* No attribute found in local __dict__ (or bases): use the
2358 * descriptor from the metatype, if any */
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002359 if (meta_get != NULL) {
2360 PyObject *res;
2361 res = meta_get(meta_attribute, (PyObject *)type,
2362 (PyObject *)metatype);
2363 Py_DECREF(meta_attribute);
2364 return res;
2365 }
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002366
2367 /* If an ordinary attribute was found on the metatype, return it now */
2368 if (meta_attribute != NULL) {
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002369 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002370 }
2371
2372 /* Give up */
2373 PyErr_Format(PyExc_AttributeError,
Walter Dörwald75163602007-06-11 15:47:13 +00002374 "type object '%.50s' has no attribute '%U'",
2375 type->tp_name, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002376 return NULL;
2377}
2378
2379static int
2380type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2381{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002382 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2383 PyErr_Format(
2384 PyExc_TypeError,
2385 "can't set attributes of built-in/extension type '%s'",
2386 type->tp_name);
2387 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002388 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002389 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2390 return -1;
2391 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002392}
2393
2394static void
2395type_dealloc(PyTypeObject *type)
2396{
Guido van Rossume5c691a2003-03-07 15:13:17 +00002397 PyHeapTypeObject *et;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002398
2399 /* Assert this is a heap-allocated type object */
2400 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002401 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00002402 PyObject_ClearWeakRefs((PyObject *)type);
Guido van Rossume5c691a2003-03-07 15:13:17 +00002403 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002404 Py_XDECREF(type->tp_base);
2405 Py_XDECREF(type->tp_dict);
2406 Py_XDECREF(type->tp_bases);
2407 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00002408 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00002409 Py_XDECREF(type->tp_subclasses);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002410 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2411 * of most other objects. It's okay to cast it to char *.
2412 */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002413 PyObject_Free((char *)type->tp_doc);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002414 Py_XDECREF(et->ht_name);
2415 Py_XDECREF(et->ht_slots);
Christian Heimes90aa7642007-12-19 02:45:37 +00002416 Py_TYPE(type)->tp_free((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002417}
2418
Guido van Rossum1c450732001-10-08 15:18:27 +00002419static PyObject *
2420type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2421{
2422 PyObject *list, *raw, *ref;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002423 Py_ssize_t i, n;
Guido van Rossum1c450732001-10-08 15:18:27 +00002424
2425 list = PyList_New(0);
2426 if (list == NULL)
2427 return NULL;
2428 raw = type->tp_subclasses;
2429 if (raw == NULL)
2430 return list;
2431 assert(PyList_Check(raw));
2432 n = PyList_GET_SIZE(raw);
2433 for (i = 0; i < n; i++) {
2434 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00002435 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00002436 ref = PyWeakref_GET_OBJECT(ref);
2437 if (ref != Py_None) {
2438 if (PyList_Append(list, ref) < 0) {
2439 Py_DECREF(list);
2440 return NULL;
2441 }
2442 }
2443 }
2444 return list;
2445}
2446
Guido van Rossum47374822007-08-02 16:48:17 +00002447static PyObject *
2448type_prepare(PyObject *self, PyObject *args, PyObject *kwds)
2449{
2450 return PyDict_New();
2451}
2452
Tim Peters6d6c1a32001-08-02 04:15:00 +00002453static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002454 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002455 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
Guido van Rossum1c450732001-10-08 15:18:27 +00002456 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002457 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
Guido van Rossum47374822007-08-02 16:48:17 +00002458 {"__prepare__", (PyCFunction)type_prepare,
2459 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
2460 PyDoc_STR("__prepare__() -> dict\n"
2461 "used to create the namespace for the class statement")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002462 {0}
2463};
2464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002465PyDoc_STRVAR(type_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002466"type(object) -> the object's type\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002467"type(name, bases, dict) -> a new type");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002468
Guido van Rossum048eb752001-10-02 21:24:57 +00002469static int
2470type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2471{
Guido van Rossuma3862092002-06-10 15:24:42 +00002472 /* Because of type_is_gc(), the collector only calls this
2473 for heaptypes. */
2474 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002475
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002476 Py_VISIT(type->tp_dict);
2477 Py_VISIT(type->tp_cache);
2478 Py_VISIT(type->tp_mro);
2479 Py_VISIT(type->tp_bases);
2480 Py_VISIT(type->tp_base);
Guido van Rossuma3862092002-06-10 15:24:42 +00002481
2482 /* There's no need to visit type->tp_subclasses or
Georg Brandlc255c7b2006-02-20 22:27:28 +00002483 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
Guido van Rossuma3862092002-06-10 15:24:42 +00002484 in cycles; tp_subclasses is a list of weak references,
2485 and slots is a tuple of strings. */
Guido van Rossum048eb752001-10-02 21:24:57 +00002486
Guido van Rossum048eb752001-10-02 21:24:57 +00002487 return 0;
2488}
2489
2490static int
2491type_clear(PyTypeObject *type)
2492{
Guido van Rossuma3862092002-06-10 15:24:42 +00002493 /* Because of type_is_gc(), the collector only calls this
2494 for heaptypes. */
2495 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002496
Guido van Rossuma3862092002-06-10 15:24:42 +00002497 /* The only field we need to clear is tp_mro, which is part of a
2498 hard cycle (its first element is the class itself) that won't
2499 be broken otherwise (it's a tuple and tuples don't have a
2500 tp_clear handler). None of the other fields need to be
2501 cleared, and here's why:
Guido van Rossum048eb752001-10-02 21:24:57 +00002502
Guido van Rossuma3862092002-06-10 15:24:42 +00002503 tp_dict:
2504 It is a dict, so the collector will call its tp_clear.
2505
2506 tp_cache:
2507 Not used; if it were, it would be a dict.
2508
2509 tp_bases, tp_base:
2510 If these are involved in a cycle, there must be at least
2511 one other, mutable object in the cycle, e.g. a base
2512 class's dict; the cycle will be broken that way.
2513
2514 tp_subclasses:
2515 A list of weak references can't be part of a cycle; and
2516 lists have their own tp_clear.
2517
Guido van Rossume5c691a2003-03-07 15:13:17 +00002518 slots (in PyHeapTypeObject):
Guido van Rossuma3862092002-06-10 15:24:42 +00002519 A tuple of strings can't be part of a cycle.
2520 */
2521
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002522 Py_CLEAR(type->tp_mro);
Guido van Rossum048eb752001-10-02 21:24:57 +00002523
2524 return 0;
2525}
2526
2527static int
2528type_is_gc(PyTypeObject *type)
2529{
2530 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2531}
2532
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002533PyTypeObject PyType_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002534 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002535 "type", /* tp_name */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002536 sizeof(PyHeapTypeObject), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00002537 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002538 (destructor)type_dealloc, /* tp_dealloc */
2539 0, /* tp_print */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002540 0, /* tp_getattr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002541 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002542 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002543 (reprfunc)type_repr, /* tp_repr */
2544 0, /* tp_as_number */
2545 0, /* tp_as_sequence */
2546 0, /* tp_as_mapping */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002547 0, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002548 (ternaryfunc)type_call, /* tp_call */
2549 0, /* tp_str */
2550 (getattrofunc)type_getattro, /* tp_getattro */
2551 (setattrofunc)type_setattro, /* tp_setattro */
2552 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00002553 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00002554 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002555 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00002556 (traverseproc)type_traverse, /* tp_traverse */
2557 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002558 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00002559 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002560 0, /* tp_iter */
2561 0, /* tp_iternext */
2562 type_methods, /* tp_methods */
2563 type_members, /* tp_members */
2564 type_getsets, /* tp_getset */
2565 0, /* tp_base */
2566 0, /* tp_dict */
2567 0, /* tp_descr_get */
2568 0, /* tp_descr_set */
2569 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002570 type_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002571 0, /* tp_alloc */
2572 type_new, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002573 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00002574 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002575};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002576
2577
2578/* The base type of all types (eventually)... except itself. */
2579
Guido van Rossumd8faa362007-04-27 19:54:29 +00002580/* You may wonder why object.__new__() only complains about arguments
2581 when object.__init__() is not overridden, and vice versa.
2582
2583 Consider the use cases:
2584
2585 1. When neither is overridden, we want to hear complaints about
2586 excess (i.e., any) arguments, since their presence could
2587 indicate there's a bug.
2588
2589 2. When defining an Immutable type, we are likely to override only
2590 __new__(), since __init__() is called too late to initialize an
2591 Immutable object. Since __new__() defines the signature for the
2592 type, it would be a pain to have to override __init__() just to
2593 stop it from complaining about excess arguments.
2594
2595 3. When defining a Mutable type, we are likely to override only
2596 __init__(). So here the converse reasoning applies: we don't
2597 want to have to override __new__() just to stop it from
2598 complaining.
2599
2600 4. When __init__() is overridden, and the subclass __init__() calls
2601 object.__init__(), the latter should complain about excess
2602 arguments; ditto for __new__().
2603
2604 Use cases 2 and 3 make it unattractive to unconditionally check for
2605 excess arguments. The best solution that addresses all four use
2606 cases is as follows: __init__() complains about excess arguments
2607 unless __new__() is overridden and __init__() is not overridden
2608 (IOW, if __init__() is overridden or __new__() is not overridden);
2609 symmetrically, __new__() complains about excess arguments unless
2610 __init__() is overridden and __new__() is not overridden
2611 (IOW, if __new__() is overridden or __init__() is not overridden).
2612
2613 However, for backwards compatibility, this breaks too much code.
2614 Therefore, in 2.6, we'll *warn* about excess arguments when both
2615 methods are overridden; for all other cases we'll use the above
2616 rules.
2617
2618*/
2619
2620/* Forward */
2621static PyObject *
2622object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2623
2624static int
2625excess_args(PyObject *args, PyObject *kwds)
2626{
2627 return PyTuple_GET_SIZE(args) ||
2628 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
2629}
2630
Tim Peters6d6c1a32001-08-02 04:15:00 +00002631static int
2632object_init(PyObject *self, PyObject *args, PyObject *kwds)
2633{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002634 int err = 0;
2635 if (excess_args(args, kwds)) {
Christian Heimes90aa7642007-12-19 02:45:37 +00002636 PyTypeObject *type = Py_TYPE(self);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002637 if (type->tp_init != object_init &&
2638 type->tp_new != object_new)
2639 {
2640 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2641 "object.__init__() takes no parameters",
2642 1);
2643 }
2644 else if (type->tp_init != object_init ||
2645 type->tp_new == object_new)
2646 {
2647 PyErr_SetString(PyExc_TypeError,
2648 "object.__init__() takes no parameters");
2649 err = -1;
2650 }
2651 }
2652 return err;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002653}
2654
Guido van Rossum298e4212003-02-13 16:30:16 +00002655static PyObject *
2656object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2657{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002658 int err = 0;
2659 if (excess_args(args, kwds)) {
2660 if (type->tp_new != object_new &&
2661 type->tp_init != object_init)
2662 {
2663 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2664 "object.__new__() takes no parameters",
2665 1);
2666 }
2667 else if (type->tp_new != object_new ||
2668 type->tp_init == object_init)
2669 {
2670 PyErr_SetString(PyExc_TypeError,
2671 "object.__new__() takes no parameters");
2672 err = -1;
2673 }
Guido van Rossum298e4212003-02-13 16:30:16 +00002674 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002675 if (err < 0)
2676 return NULL;
Christian Heimes9e7f1d22008-02-28 12:27:11 +00002677
2678 if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
2679 static PyObject *comma = NULL;
2680 PyObject *abstract_methods = NULL;
2681 PyObject *builtins;
2682 PyObject *sorted;
2683 PyObject *sorted_methods = NULL;
2684 PyObject *joined = NULL;
2685
2686 /* Compute ", ".join(sorted(type.__abstractmethods__))
2687 into joined. */
2688 abstract_methods = type_abstractmethods(type, NULL);
2689 if (abstract_methods == NULL)
2690 goto error;
2691 builtins = PyEval_GetBuiltins();
2692 if (builtins == NULL)
2693 goto error;
2694 sorted = PyDict_GetItemString(builtins, "sorted");
2695 if (sorted == NULL)
2696 goto error;
2697 sorted_methods = PyObject_CallFunctionObjArgs(sorted,
2698 abstract_methods,
2699 NULL);
2700 if (sorted_methods == NULL)
2701 goto error;
2702 if (comma == NULL) {
2703 comma = PyUnicode_InternFromString(", ");
2704 if (comma == NULL)
2705 goto error;
2706 }
2707 joined = PyObject_CallMethod(comma, "join",
2708 "O", sorted_methods);
2709 if (joined == NULL)
2710 goto error;
2711
2712 PyErr_Format(PyExc_TypeError,
2713 "Can't instantiate abstract class %s "
2714 "with abstract methods %U",
2715 type->tp_name,
2716 joined);
2717 error:
2718 Py_XDECREF(joined);
2719 Py_XDECREF(sorted_methods);
2720 Py_XDECREF(abstract_methods);
2721 return NULL;
2722 }
Guido van Rossum298e4212003-02-13 16:30:16 +00002723 return type->tp_alloc(type, 0);
2724}
2725
Tim Peters6d6c1a32001-08-02 04:15:00 +00002726static void
2727object_dealloc(PyObject *self)
2728{
Christian Heimes90aa7642007-12-19 02:45:37 +00002729 Py_TYPE(self)->tp_free(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002730}
2731
Guido van Rossum8e248182001-08-12 05:17:56 +00002732static PyObject *
2733object_repr(PyObject *self)
2734{
Guido van Rossum76e69632001-08-16 18:52:43 +00002735 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00002736 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002737
Christian Heimes90aa7642007-12-19 02:45:37 +00002738 type = Py_TYPE(self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002739 mod = type_module(type, NULL);
2740 if (mod == NULL)
2741 PyErr_Clear();
Martin v. Löwis5b222132007-06-10 09:51:05 +00002742 else if (!PyUnicode_Check(mod)) {
Guido van Rossum76e69632001-08-16 18:52:43 +00002743 Py_DECREF(mod);
2744 mod = NULL;
2745 }
2746 name = type_name(type, NULL);
2747 if (name == NULL)
2748 return NULL;
Georg Brandl1a3284e2007-12-02 09:40:06 +00002749 if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
Walter Dörwald4dbd01b2007-06-11 14:03:45 +00002750 rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002751 else
Walter Dörwald1ab83302007-05-18 17:15:44 +00002752 rtn = PyUnicode_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002753 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002754 Py_XDECREF(mod);
2755 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00002756 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002757}
2758
Guido van Rossumb8f63662001-08-15 23:57:02 +00002759static PyObject *
2760object_str(PyObject *self)
2761{
2762 unaryfunc f;
2763
Christian Heimes90aa7642007-12-19 02:45:37 +00002764 f = Py_TYPE(self)->tp_repr;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002765 if (f == NULL)
2766 f = object_repr;
2767 return f(self);
2768}
2769
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002770static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002771object_richcompare(PyObject *self, PyObject *other, int op)
2772{
2773 PyObject *res;
2774
2775 switch (op) {
2776
2777 case Py_EQ:
Guido van Rossumab078dd2008-01-06 00:09:11 +00002778 /* Return NotImplemented instead of False, so if two
2779 objects are compared, both get a chance at the
2780 comparison. See issue #1393. */
2781 res = (self == other) ? Py_True : Py_NotImplemented;
Guido van Rossum6b18a5b2007-03-29 20:49:57 +00002782 Py_INCREF(res);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002783 break;
2784
2785 case Py_NE:
Guido van Rossume27dc722007-03-27 22:37:34 +00002786 /* By default, != returns the opposite of ==,
2787 unless the latter returns NotImplemented. */
2788 res = PyObject_RichCompare(self, other, Py_EQ);
2789 if (res != NULL && res != Py_NotImplemented) {
2790 int ok = PyObject_IsTrue(res);
2791 Py_DECREF(res);
2792 if (ok < 0)
2793 res = NULL;
2794 else {
2795 if (ok)
2796 res = Py_False;
2797 else
2798 res = Py_True;
2799 Py_INCREF(res);
2800 }
2801 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002802 break;
2803
2804 default:
2805 res = Py_NotImplemented;
Guido van Rossum6b18a5b2007-03-29 20:49:57 +00002806 Py_INCREF(res);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002807 break;
2808 }
2809
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002810 return res;
2811}
2812
2813static PyObject *
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002814object_get_class(PyObject *self, void *closure)
2815{
Christian Heimes90aa7642007-12-19 02:45:37 +00002816 Py_INCREF(Py_TYPE(self));
2817 return (PyObject *)(Py_TYPE(self));
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002818}
2819
2820static int
2821equiv_structs(PyTypeObject *a, PyTypeObject *b)
2822{
2823 return a == b ||
2824 (a != NULL &&
2825 b != NULL &&
2826 a->tp_basicsize == b->tp_basicsize &&
2827 a->tp_itemsize == b->tp_itemsize &&
2828 a->tp_dictoffset == b->tp_dictoffset &&
2829 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2830 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2831 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2832}
2833
2834static int
2835same_slots_added(PyTypeObject *a, PyTypeObject *b)
2836{
2837 PyTypeObject *base = a->tp_base;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002838 Py_ssize_t size;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002839 PyObject *slots_a, *slots_b;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002840
2841 if (base != b->tp_base)
2842 return 0;
2843 if (equiv_structs(a, base) && equiv_structs(b, base))
2844 return 1;
2845 size = base->tp_basicsize;
2846 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2847 size += sizeof(PyObject *);
2848 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2849 size += sizeof(PyObject *);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002850
2851 /* Check slots compliance */
2852 slots_a = ((PyHeapTypeObject *)a)->ht_slots;
2853 slots_b = ((PyHeapTypeObject *)b)->ht_slots;
2854 if (slots_a && slots_b) {
2855 if (PyObject_Compare(slots_a, slots_b) != 0)
2856 return 0;
2857 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
2858 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002859 return size == a->tp_basicsize && size == b->tp_basicsize;
2860}
2861
2862static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002863compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002864{
2865 PyTypeObject *newbase, *oldbase;
2866
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002867 if (newto->tp_dealloc != oldto->tp_dealloc ||
2868 newto->tp_free != oldto->tp_free)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002869 {
2870 PyErr_Format(PyExc_TypeError,
2871 "%s assignment: "
2872 "'%s' deallocator differs from '%s'",
2873 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002874 newto->tp_name,
2875 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002876 return 0;
2877 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002878 newbase = newto;
2879 oldbase = oldto;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002880 while (equiv_structs(newbase, newbase->tp_base))
2881 newbase = newbase->tp_base;
2882 while (equiv_structs(oldbase, oldbase->tp_base))
2883 oldbase = oldbase->tp_base;
2884 if (newbase != oldbase &&
2885 (newbase->tp_base != oldbase->tp_base ||
2886 !same_slots_added(newbase, oldbase))) {
2887 PyErr_Format(PyExc_TypeError,
2888 "%s assignment: "
2889 "'%s' object layout differs from '%s'",
2890 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002891 newto->tp_name,
2892 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002893 return 0;
2894 }
Tim Petersea7f75d2002-12-07 21:39:16 +00002895
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002896 return 1;
2897}
2898
2899static int
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002900object_set_class(PyObject *self, PyObject *value, void *closure)
2901{
Christian Heimes90aa7642007-12-19 02:45:37 +00002902 PyTypeObject *oldto = Py_TYPE(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002903 PyTypeObject *newto;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002904
Guido van Rossumb6b89422002-04-15 01:03:30 +00002905 if (value == NULL) {
2906 PyErr_SetString(PyExc_TypeError,
2907 "can't delete __class__ attribute");
2908 return -1;
2909 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002910 if (!PyType_Check(value)) {
2911 PyErr_Format(PyExc_TypeError,
2912 "__class__ must be set to new-style class, not '%s' object",
Christian Heimes90aa7642007-12-19 02:45:37 +00002913 Py_TYPE(value)->tp_name);
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002914 return -1;
2915 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002916 newto = (PyTypeObject *)value;
2917 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2918 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
Guido van Rossum40af8892002-08-10 05:42:07 +00002919 {
2920 PyErr_Format(PyExc_TypeError,
2921 "__class__ assignment: only for heap types");
2922 return -1;
2923 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002924 if (compatible_for_assignment(newto, oldto, "__class__")) {
2925 Py_INCREF(newto);
Christian Heimes90aa7642007-12-19 02:45:37 +00002926 Py_TYPE(self) = newto;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002927 Py_DECREF(oldto);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002928 return 0;
2929 }
2930 else {
Guido van Rossum9ee4b942002-05-24 18:47:47 +00002931 return -1;
2932 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002933}
2934
2935static PyGetSetDef object_getsets[] = {
2936 {"__class__", object_get_class, object_set_class,
Neal Norwitz858e34f2002-08-13 17:18:45 +00002937 PyDoc_STR("the object's class")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002938 {0}
2939};
2940
Guido van Rossumc53f0092003-02-18 22:05:12 +00002941
Guido van Rossum036f9992003-02-21 22:02:54 +00002942/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002943 We fall back to helpers in copyreg for:
Guido van Rossum036f9992003-02-21 22:02:54 +00002944 - pickle protocols < 2
2945 - calculating the list of slot names (done only once per class)
2946 - the __newobj__ function (which is used as a token but never called)
2947*/
2948
2949static PyObject *
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002950import_copyreg(void)
Guido van Rossum036f9992003-02-21 22:02:54 +00002951{
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002952 static PyObject *copyreg_str;
Guido van Rossum3926a632001-09-25 16:25:58 +00002953
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002954 if (!copyreg_str) {
2955 copyreg_str = PyUnicode_InternFromString("copyreg");
2956 if (copyreg_str == NULL)
Guido van Rossum3926a632001-09-25 16:25:58 +00002957 return NULL;
2958 }
Guido van Rossum036f9992003-02-21 22:02:54 +00002959
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002960 return PyImport_Import(copyreg_str);
Guido van Rossum036f9992003-02-21 22:02:54 +00002961}
2962
2963static PyObject *
2964slotnames(PyObject *cls)
2965{
2966 PyObject *clsdict;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002967 PyObject *copyreg;
Guido van Rossum036f9992003-02-21 22:02:54 +00002968 PyObject *slotnames;
2969
2970 if (!PyType_Check(cls)) {
2971 Py_INCREF(Py_None);
2972 return Py_None;
2973 }
2974
2975 clsdict = ((PyTypeObject *)cls)->tp_dict;
2976 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
Armin Rigoec862b92005-09-24 22:58:41 +00002977 if (slotnames != NULL && PyList_Check(slotnames)) {
Guido van Rossum036f9992003-02-21 22:02:54 +00002978 Py_INCREF(slotnames);
2979 return slotnames;
2980 }
2981
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002982 copyreg = import_copyreg();
2983 if (copyreg == NULL)
Guido van Rossum036f9992003-02-21 22:02:54 +00002984 return NULL;
2985
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002986 slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
2987 Py_DECREF(copyreg);
Guido van Rossum036f9992003-02-21 22:02:54 +00002988 if (slotnames != NULL &&
2989 slotnames != Py_None &&
2990 !PyList_Check(slotnames))
2991 {
2992 PyErr_SetString(PyExc_TypeError,
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002993 "copyreg._slotnames didn't return a list or None");
Guido van Rossum036f9992003-02-21 22:02:54 +00002994 Py_DECREF(slotnames);
2995 slotnames = NULL;
2996 }
2997
2998 return slotnames;
2999}
3000
3001static PyObject *
3002reduce_2(PyObject *obj)
3003{
3004 PyObject *cls, *getnewargs;
3005 PyObject *args = NULL, *args2 = NULL;
3006 PyObject *getstate = NULL, *state = NULL, *names = NULL;
3007 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003008 PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003009 Py_ssize_t i, n;
Guido van Rossum036f9992003-02-21 22:02:54 +00003010
3011 cls = PyObject_GetAttrString(obj, "__class__");
3012 if (cls == NULL)
3013 return NULL;
3014
3015 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
3016 if (getnewargs != NULL) {
3017 args = PyObject_CallObject(getnewargs, NULL);
3018 Py_DECREF(getnewargs);
3019 if (args != NULL && !PyTuple_Check(args)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003020 PyErr_Format(PyExc_TypeError,
3021 "__getnewargs__ should return a tuple, "
Christian Heimes90aa7642007-12-19 02:45:37 +00003022 "not '%.200s'", Py_TYPE(args)->tp_name);
Guido van Rossum036f9992003-02-21 22:02:54 +00003023 goto end;
3024 }
3025 }
3026 else {
3027 PyErr_Clear();
3028 args = PyTuple_New(0);
3029 }
3030 if (args == NULL)
3031 goto end;
3032
3033 getstate = PyObject_GetAttrString(obj, "__getstate__");
3034 if (getstate != NULL) {
3035 state = PyObject_CallObject(getstate, NULL);
3036 Py_DECREF(getstate);
Neal Norwitze2fdc612003-06-08 13:19:58 +00003037 if (state == NULL)
3038 goto end;
Guido van Rossum036f9992003-02-21 22:02:54 +00003039 }
3040 else {
Jim Fulton8a1a5942004-02-08 04:21:26 +00003041 PyErr_Clear();
Guido van Rossum036f9992003-02-21 22:02:54 +00003042 state = PyObject_GetAttrString(obj, "__dict__");
3043 if (state == NULL) {
3044 PyErr_Clear();
3045 state = Py_None;
3046 Py_INCREF(state);
3047 }
3048 names = slotnames(cls);
3049 if (names == NULL)
3050 goto end;
3051 if (names != Py_None) {
3052 assert(PyList_Check(names));
3053 slots = PyDict_New();
3054 if (slots == NULL)
3055 goto end;
3056 n = 0;
3057 /* Can't pre-compute the list size; the list
3058 is stored on the class so accessible to other
3059 threads, which may be run by DECREF */
3060 for (i = 0; i < PyList_GET_SIZE(names); i++) {
3061 PyObject *name, *value;
3062 name = PyList_GET_ITEM(names, i);
3063 value = PyObject_GetAttr(obj, name);
3064 if (value == NULL)
3065 PyErr_Clear();
3066 else {
3067 int err = PyDict_SetItem(slots, name,
3068 value);
3069 Py_DECREF(value);
3070 if (err)
3071 goto end;
3072 n++;
3073 }
3074 }
3075 if (n) {
3076 state = Py_BuildValue("(NO)", state, slots);
3077 if (state == NULL)
3078 goto end;
3079 }
3080 }
3081 }
3082
3083 if (!PyList_Check(obj)) {
3084 listitems = Py_None;
3085 Py_INCREF(listitems);
3086 }
3087 else {
3088 listitems = PyObject_GetIter(obj);
3089 if (listitems == NULL)
3090 goto end;
3091 }
3092
3093 if (!PyDict_Check(obj)) {
3094 dictitems = Py_None;
3095 Py_INCREF(dictitems);
3096 }
3097 else {
Guido van Rossumcc2b0162007-02-11 06:12:03 +00003098 PyObject *items = PyObject_CallMethod(obj, "items", "");
3099 if (items == NULL)
3100 goto end;
3101 dictitems = PyObject_GetIter(items);
3102 Py_DECREF(items);
Guido van Rossum036f9992003-02-21 22:02:54 +00003103 if (dictitems == NULL)
3104 goto end;
3105 }
3106
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003107 copyreg = import_copyreg();
3108 if (copyreg == NULL)
Guido van Rossum036f9992003-02-21 22:02:54 +00003109 goto end;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003110 newobj = PyObject_GetAttrString(copyreg, "__newobj__");
Guido van Rossum036f9992003-02-21 22:02:54 +00003111 if (newobj == NULL)
3112 goto end;
3113
3114 n = PyTuple_GET_SIZE(args);
3115 args2 = PyTuple_New(n+1);
3116 if (args2 == NULL)
3117 goto end;
3118 PyTuple_SET_ITEM(args2, 0, cls);
3119 cls = NULL;
3120 for (i = 0; i < n; i++) {
3121 PyObject *v = PyTuple_GET_ITEM(args, i);
3122 Py_INCREF(v);
3123 PyTuple_SET_ITEM(args2, i+1, v);
3124 }
3125
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003126 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
Guido van Rossum036f9992003-02-21 22:02:54 +00003127
3128 end:
3129 Py_XDECREF(cls);
3130 Py_XDECREF(args);
3131 Py_XDECREF(args2);
Jeremy Hyltond06483c2003-04-09 21:01:42 +00003132 Py_XDECREF(slots);
Guido van Rossum036f9992003-02-21 22:02:54 +00003133 Py_XDECREF(state);
3134 Py_XDECREF(names);
3135 Py_XDECREF(listitems);
3136 Py_XDECREF(dictitems);
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003137 Py_XDECREF(copyreg);
Guido van Rossum036f9992003-02-21 22:02:54 +00003138 Py_XDECREF(newobj);
3139 return res;
3140}
3141
Guido van Rossumd8faa362007-04-27 19:54:29 +00003142/*
3143 * There were two problems when object.__reduce__ and object.__reduce_ex__
3144 * were implemented in the same function:
3145 * - trying to pickle an object with a custom __reduce__ method that
3146 * fell back to object.__reduce__ in certain circumstances led to
3147 * infinite recursion at Python level and eventual RuntimeError.
3148 * - Pickling objects that lied about their type by overwriting the
3149 * __class__ descriptor could lead to infinite recursion at C level
3150 * and eventual segfault.
3151 *
3152 * Because of backwards compatibility, the two methods still have to
3153 * behave in the same way, even if this is not required by the pickle
3154 * protocol. This common functionality was moved to the _common_reduce
3155 * function.
3156 */
3157static PyObject *
3158_common_reduce(PyObject *self, int proto)
3159{
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003160 PyObject *copyreg, *res;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003161
3162 if (proto >= 2)
3163 return reduce_2(self);
3164
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003165 copyreg = import_copyreg();
3166 if (!copyreg)
Guido van Rossumd8faa362007-04-27 19:54:29 +00003167 return NULL;
3168
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003169 res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
3170 Py_DECREF(copyreg);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003171
3172 return res;
3173}
3174
3175static PyObject *
3176object_reduce(PyObject *self, PyObject *args)
3177{
3178 int proto = 0;
3179
3180 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
3181 return NULL;
3182
3183 return _common_reduce(self, proto);
3184}
3185
Guido van Rossum036f9992003-02-21 22:02:54 +00003186static PyObject *
3187object_reduce_ex(PyObject *self, PyObject *args)
3188{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003189 PyObject *reduce, *res;
Guido van Rossum036f9992003-02-21 22:02:54 +00003190 int proto = 0;
3191
3192 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
3193 return NULL;
3194
3195 reduce = PyObject_GetAttrString(self, "__reduce__");
3196 if (reduce == NULL)
3197 PyErr_Clear();
3198 else {
3199 PyObject *cls, *clsreduce, *objreduce;
3200 int override;
3201 cls = PyObject_GetAttrString(self, "__class__");
3202 if (cls == NULL) {
3203 Py_DECREF(reduce);
3204 return NULL;
3205 }
3206 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
3207 Py_DECREF(cls);
3208 if (clsreduce == NULL) {
3209 Py_DECREF(reduce);
3210 return NULL;
3211 }
3212 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
3213 "__reduce__");
3214 override = (clsreduce != objreduce);
3215 Py_DECREF(clsreduce);
3216 if (override) {
3217 res = PyObject_CallObject(reduce, NULL);
3218 Py_DECREF(reduce);
3219 return res;
3220 }
3221 else
3222 Py_DECREF(reduce);
3223 }
3224
Guido van Rossumd8faa362007-04-27 19:54:29 +00003225 return _common_reduce(self, proto);
Guido van Rossum3926a632001-09-25 16:25:58 +00003226}
3227
Christian Heimes9e7f1d22008-02-28 12:27:11 +00003228static PyObject *
3229object_subclasshook(PyObject *cls, PyObject *args)
3230{
3231 Py_INCREF(Py_NotImplemented);
3232 return Py_NotImplemented;
3233}
3234
3235PyDoc_STRVAR(object_subclasshook_doc,
3236"Abstract classes can override this to customize issubclass().\n"
3237"\n"
3238"This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
3239"It should return True, False or NotImplemented. If it returns\n"
3240"NotImplemented, the normal algorithm is used. Otherwise, it\n"
3241"overrides the normal algorithm (and the outcome is cached).\n");
Eric Smith8c663262007-08-25 02:26:07 +00003242
3243/*
3244 from PEP 3101, this code implements:
3245
3246 class object:
3247 def __format__(self, format_spec):
3248 return format(str(self), format_spec)
3249*/
3250static PyObject *
3251object_format(PyObject *self, PyObject *args)
3252{
3253 PyObject *format_spec;
3254 PyObject *self_as_str = NULL;
3255 PyObject *result = NULL;
3256 PyObject *format_meth = NULL;
3257
Eric Smithfc6e8fe2008-01-11 00:17:22 +00003258 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
Eric Smith8c663262007-08-25 02:26:07 +00003259 return NULL;
Eric Smith8c663262007-08-25 02:26:07 +00003260
Thomas Heller519a0422007-11-15 20:48:54 +00003261 self_as_str = PyObject_Str(self);
Eric Smith8c663262007-08-25 02:26:07 +00003262 if (self_as_str != NULL) {
3263 /* find the format function */
3264 format_meth = PyObject_GetAttrString(self_as_str, "__format__");
3265 if (format_meth != NULL) {
3266 /* and call it */
3267 result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL);
3268 }
3269 }
3270
3271 Py_XDECREF(self_as_str);
3272 Py_XDECREF(format_meth);
3273
3274 return result;
3275}
3276
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003277static PyObject *
3278object_sizeof(PyObject *self, PyObject *args)
3279{
3280 Py_ssize_t res, isize;
3281
3282 res = 0;
3283 isize = self->ob_type->tp_itemsize;
3284 if (isize > 0)
3285 res = Py_SIZE(self->ob_type) * isize;
3286 res += self->ob_type->tp_basicsize;
3287
3288 return PyLong_FromSsize_t(res);
3289}
3290
Guido van Rossum3926a632001-09-25 16:25:58 +00003291static PyMethodDef object_methods[] = {
Guido van Rossumc53f0092003-02-18 22:05:12 +00003292 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
3293 PyDoc_STR("helper for pickle")},
Guido van Rossumd8faa362007-04-27 19:54:29 +00003294 {"__reduce__", object_reduce, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003295 PyDoc_STR("helper for pickle")},
Christian Heimes9e7f1d22008-02-28 12:27:11 +00003296 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
3297 object_subclasshook_doc},
Eric Smith8c663262007-08-25 02:26:07 +00003298 {"__format__", object_format, METH_VARARGS,
3299 PyDoc_STR("default object formatter")},
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003300 {"__sizeof__", object_sizeof, METH_NOARGS,
3301 PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
Guido van Rossum3926a632001-09-25 16:25:58 +00003302 {0}
3303};
3304
Guido van Rossum036f9992003-02-21 22:02:54 +00003305
Tim Peters6d6c1a32001-08-02 04:15:00 +00003306PyTypeObject PyBaseObject_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003307 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003308 "object", /* tp_name */
3309 sizeof(PyObject), /* tp_basicsize */
3310 0, /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003311 object_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003312 0, /* tp_print */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003313 0, /* tp_getattr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003314 0, /* tp_setattr */
3315 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003316 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003317 0, /* tp_as_number */
3318 0, /* tp_as_sequence */
3319 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003320 (hashfunc)_Py_HashPointer, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003321 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003322 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003323 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00003324 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003325 0, /* tp_as_buffer */
3326 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003327 PyDoc_STR("The most base type"), /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003328 0, /* tp_traverse */
3329 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003330 object_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003331 0, /* tp_weaklistoffset */
3332 0, /* tp_iter */
3333 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00003334 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00003335 0, /* tp_members */
3336 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003337 0, /* tp_base */
3338 0, /* tp_dict */
3339 0, /* tp_descr_get */
3340 0, /* tp_descr_set */
3341 0, /* tp_dictoffset */
3342 object_init, /* tp_init */
3343 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossum298e4212003-02-13 16:30:16 +00003344 object_new, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003345 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003346};
3347
3348
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003349/* Add the methods from tp_methods to the __dict__ in a type object */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003350
3351static int
3352add_methods(PyTypeObject *type, PyMethodDef *meth)
3353{
Guido van Rossum687ae002001-10-15 22:03:32 +00003354 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003355
3356 for (; meth->ml_name != NULL; meth++) {
3357 PyObject *descr;
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003358 if (PyDict_GetItemString(dict, meth->ml_name) &&
3359 !(meth->ml_flags & METH_COEXIST))
3360 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00003361 if (meth->ml_flags & METH_CLASS) {
3362 if (meth->ml_flags & METH_STATIC) {
3363 PyErr_SetString(PyExc_ValueError,
3364 "method cannot be both class and static");
3365 return -1;
3366 }
Tim Petersbca1cbc2002-12-09 22:56:13 +00003367 descr = PyDescr_NewClassMethod(type, meth);
Fred Drake7bf97152002-03-28 05:33:33 +00003368 }
3369 else if (meth->ml_flags & METH_STATIC) {
Guido van Rossum9af48ff2003-02-11 17:12:46 +00003370 PyObject *cfunc = PyCFunction_New(meth, NULL);
3371 if (cfunc == NULL)
3372 return -1;
3373 descr = PyStaticMethod_New(cfunc);
3374 Py_DECREF(cfunc);
Fred Drake7bf97152002-03-28 05:33:33 +00003375 }
3376 else {
3377 descr = PyDescr_NewMethod(type, meth);
3378 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003379 if (descr == NULL)
3380 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00003381 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003382 return -1;
3383 Py_DECREF(descr);
3384 }
3385 return 0;
3386}
3387
3388static int
Guido van Rossum6f799372001-09-20 20:46:19 +00003389add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003390{
Guido van Rossum687ae002001-10-15 22:03:32 +00003391 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003392
3393 for (; memb->name != NULL; memb++) {
3394 PyObject *descr;
3395 if (PyDict_GetItemString(dict, memb->name))
3396 continue;
3397 descr = PyDescr_NewMember(type, memb);
3398 if (descr == NULL)
3399 return -1;
3400 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
3401 return -1;
3402 Py_DECREF(descr);
3403 }
3404 return 0;
3405}
3406
3407static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00003408add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003409{
Guido van Rossum687ae002001-10-15 22:03:32 +00003410 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003411
3412 for (; gsp->name != NULL; gsp++) {
3413 PyObject *descr;
3414 if (PyDict_GetItemString(dict, gsp->name))
3415 continue;
3416 descr = PyDescr_NewGetSet(type, gsp);
3417
3418 if (descr == NULL)
3419 return -1;
3420 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
3421 return -1;
3422 Py_DECREF(descr);
3423 }
3424 return 0;
3425}
3426
Guido van Rossum13d52f02001-08-10 21:24:08 +00003427static void
3428inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003429{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003430 Py_ssize_t oldsize, newsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003431
Guido van Rossum13d52f02001-08-10 21:24:08 +00003432 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00003433 oldsize = base->tp_basicsize;
3434 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
3435 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3436 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00003437 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00003438 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003439 if (type->tp_traverse == NULL)
3440 type->tp_traverse = base->tp_traverse;
3441 if (type->tp_clear == NULL)
3442 type->tp_clear = base->tp_clear;
3443 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003444 {
Guido van Rossumf884b742001-12-17 17:14:22 +00003445 /* The condition below could use some explanation.
3446 It appears that tp_new is not inherited for static types
3447 whose base class is 'object'; this seems to be a precaution
3448 so that old extension types don't suddenly become
3449 callable (object.__new__ wouldn't insure the invariants
3450 that the extension type's own factory function ensures).
3451 Heap types, of course, are under our control, so they do
3452 inherit tp_new; static extension types that specify some
3453 other built-in type as the default are considered
3454 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00003455 if (base != &PyBaseObject_Type ||
3456 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3457 if (type->tp_new == NULL)
3458 type->tp_new = base->tp_new;
3459 }
3460 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00003461 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00003462
3463 /* Copy other non-function slots */
3464
3465#undef COPYVAL
3466#define COPYVAL(SLOT) \
3467 if (type->SLOT == 0) type->SLOT = base->SLOT
3468
3469 COPYVAL(tp_itemsize);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003470 COPYVAL(tp_weaklistoffset);
3471 COPYVAL(tp_dictoffset);
Thomas Wouters27d517b2007-02-25 20:39:11 +00003472
3473 /* Setup fast subclass flags */
3474 if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
3475 type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
3476 else if (PyType_IsSubtype(base, &PyType_Type))
3477 type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
3478 else if (PyType_IsSubtype(base, &PyLong_Type))
3479 type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
Christian Heimes72b710a2008-05-26 13:28:38 +00003480 else if (PyType_IsSubtype(base, &PyBytes_Type))
3481 type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS;
Thomas Wouters27d517b2007-02-25 20:39:11 +00003482 else if (PyType_IsSubtype(base, &PyUnicode_Type))
3483 type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
3484 else if (PyType_IsSubtype(base, &PyTuple_Type))
3485 type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
3486 else if (PyType_IsSubtype(base, &PyList_Type))
3487 type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
3488 else if (PyType_IsSubtype(base, &PyDict_Type))
3489 type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003490}
3491
Guido van Rossumf5243f02008-01-01 04:06:48 +00003492static char *hash_name_op[] = {
Guido van Rossum38938152006-08-21 23:36:26 +00003493 "__eq__",
Guido van Rossum38938152006-08-21 23:36:26 +00003494 "__cmp__",
3495 "__hash__",
Guido van Rossumf5243f02008-01-01 04:06:48 +00003496 NULL
Guido van Rossum38938152006-08-21 23:36:26 +00003497};
3498
3499static int
Guido van Rossumf5243f02008-01-01 04:06:48 +00003500overrides_hash(PyTypeObject *type)
Guido van Rossum38938152006-08-21 23:36:26 +00003501{
Guido van Rossumf5243f02008-01-01 04:06:48 +00003502 char **p;
Guido van Rossum38938152006-08-21 23:36:26 +00003503 PyObject *dict = type->tp_dict;
3504
3505 assert(dict != NULL);
Guido van Rossumf5243f02008-01-01 04:06:48 +00003506 for (p = hash_name_op; *p; p++) {
3507 if (PyDict_GetItemString(dict, *p) != NULL)
Guido van Rossum38938152006-08-21 23:36:26 +00003508 return 1;
3509 }
3510 return 0;
3511}
3512
Guido van Rossum13d52f02001-08-10 21:24:08 +00003513static void
3514inherit_slots(PyTypeObject *type, PyTypeObject *base)
3515{
3516 PyTypeObject *basebase;
3517
3518#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00003519#undef COPYSLOT
3520#undef COPYNUM
3521#undef COPYSEQ
3522#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00003523#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00003524
3525#define SLOTDEFINED(SLOT) \
3526 (base->SLOT != 0 && \
3527 (basebase == NULL || base->SLOT != basebase->SLOT))
3528
Tim Peters6d6c1a32001-08-02 04:15:00 +00003529#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00003530 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00003531
3532#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3533#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3534#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00003535#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003536
Guido van Rossum13d52f02001-08-10 21:24:08 +00003537 /* This won't inherit indirect slots (from tp_as_number etc.)
3538 if type doesn't provide the space. */
3539
3540 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3541 basebase = base->tp_base;
3542 if (basebase->tp_as_number == NULL)
3543 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003544 COPYNUM(nb_add);
3545 COPYNUM(nb_subtract);
3546 COPYNUM(nb_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003547 COPYNUM(nb_remainder);
3548 COPYNUM(nb_divmod);
3549 COPYNUM(nb_power);
3550 COPYNUM(nb_negative);
3551 COPYNUM(nb_positive);
3552 COPYNUM(nb_absolute);
Jack Diederich4dafcc42006-11-28 19:15:13 +00003553 COPYNUM(nb_bool);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003554 COPYNUM(nb_invert);
3555 COPYNUM(nb_lshift);
3556 COPYNUM(nb_rshift);
3557 COPYNUM(nb_and);
3558 COPYNUM(nb_xor);
3559 COPYNUM(nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003560 COPYNUM(nb_int);
3561 COPYNUM(nb_long);
3562 COPYNUM(nb_float);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003563 COPYNUM(nb_inplace_add);
3564 COPYNUM(nb_inplace_subtract);
3565 COPYNUM(nb_inplace_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003566 COPYNUM(nb_inplace_remainder);
3567 COPYNUM(nb_inplace_power);
3568 COPYNUM(nb_inplace_lshift);
3569 COPYNUM(nb_inplace_rshift);
3570 COPYNUM(nb_inplace_and);
3571 COPYNUM(nb_inplace_xor);
3572 COPYNUM(nb_inplace_or);
Neal Norwitzbcc0db82006-03-24 08:14:36 +00003573 COPYNUM(nb_true_divide);
3574 COPYNUM(nb_floor_divide);
3575 COPYNUM(nb_inplace_true_divide);
3576 COPYNUM(nb_inplace_floor_divide);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003577 COPYNUM(nb_index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003578 }
3579
Guido van Rossum13d52f02001-08-10 21:24:08 +00003580 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3581 basebase = base->tp_base;
3582 if (basebase->tp_as_sequence == NULL)
3583 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003584 COPYSEQ(sq_length);
3585 COPYSEQ(sq_concat);
3586 COPYSEQ(sq_repeat);
3587 COPYSEQ(sq_item);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003588 COPYSEQ(sq_ass_item);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003589 COPYSEQ(sq_contains);
3590 COPYSEQ(sq_inplace_concat);
3591 COPYSEQ(sq_inplace_repeat);
3592 }
3593
Guido van Rossum13d52f02001-08-10 21:24:08 +00003594 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3595 basebase = base->tp_base;
3596 if (basebase->tp_as_mapping == NULL)
3597 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003598 COPYMAP(mp_length);
3599 COPYMAP(mp_subscript);
3600 COPYMAP(mp_ass_subscript);
3601 }
3602
Tim Petersfc57ccb2001-10-12 02:38:24 +00003603 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3604 basebase = base->tp_base;
3605 if (basebase->tp_as_buffer == NULL)
3606 basebase = NULL;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00003607 COPYBUF(bf_getbuffer);
3608 COPYBUF(bf_releasebuffer);
Tim Petersfc57ccb2001-10-12 02:38:24 +00003609 }
3610
Guido van Rossum13d52f02001-08-10 21:24:08 +00003611 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003612
Tim Peters6d6c1a32001-08-02 04:15:00 +00003613 COPYSLOT(tp_dealloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003614 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3615 type->tp_getattr = base->tp_getattr;
3616 type->tp_getattro = base->tp_getattro;
3617 }
3618 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3619 type->tp_setattr = base->tp_setattr;
3620 type->tp_setattro = base->tp_setattro;
3621 }
3622 /* tp_compare see tp_richcompare */
3623 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003624 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003625 COPYSLOT(tp_call);
3626 COPYSLOT(tp_str);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003627 {
Guido van Rossum38938152006-08-21 23:36:26 +00003628 /* Copy comparison-related slots only when
3629 not overriding them anywhere */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003630 if (type->tp_compare == NULL &&
3631 type->tp_richcompare == NULL &&
Guido van Rossum38938152006-08-21 23:36:26 +00003632 type->tp_hash == NULL &&
Guido van Rossumf5243f02008-01-01 04:06:48 +00003633 !overrides_hash(type))
Guido van Rossumb8f63662001-08-15 23:57:02 +00003634 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003635 type->tp_compare = base->tp_compare;
3636 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003637 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003638 }
3639 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003640 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003641 COPYSLOT(tp_iter);
3642 COPYSLOT(tp_iternext);
3643 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003644 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003645 COPYSLOT(tp_descr_get);
3646 COPYSLOT(tp_descr_set);
3647 COPYSLOT(tp_dictoffset);
3648 COPYSLOT(tp_init);
3649 COPYSLOT(tp_alloc);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00003650 COPYSLOT(tp_is_gc);
Tim Peters3cfe7542003-05-21 21:29:48 +00003651 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3652 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3653 /* They agree about gc. */
3654 COPYSLOT(tp_free);
3655 }
3656 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3657 type->tp_free == NULL &&
Neal Norwitz30d1c512007-08-19 22:48:23 +00003658 base->tp_free == PyObject_Free) {
Tim Peters3cfe7542003-05-21 21:29:48 +00003659 /* A bit of magic to plug in the correct default
3660 * tp_free function when a derived class adds gc,
3661 * didn't define tp_free, and the base uses the
3662 * default non-gc tp_free.
3663 */
3664 type->tp_free = PyObject_GC_Del;
3665 }
3666 /* else they didn't agree about gc, and there isn't something
3667 * obvious to be done -- the type is on its own.
3668 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003669 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003670}
3671
Jeremy Hylton938ace62002-07-17 16:30:39 +00003672static int add_operators(PyTypeObject *);
Guido van Rossum13d52f02001-08-10 21:24:08 +00003673
Tim Peters6d6c1a32001-08-02 04:15:00 +00003674int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003675PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003676{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003677 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003678 PyTypeObject *base;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003679 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003680
Guido van Rossumcab05802002-06-10 15:29:03 +00003681 if (type->tp_flags & Py_TPFLAGS_READY) {
3682 assert(type->tp_dict != NULL);
Guido van Rossumd614f972001-08-10 17:39:49 +00003683 return 0;
Guido van Rossumcab05802002-06-10 15:29:03 +00003684 }
Guido van Rossumd614f972001-08-10 17:39:49 +00003685 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00003686
3687 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003688
Tim Peters36eb4df2003-03-23 03:33:13 +00003689#ifdef Py_TRACE_REFS
3690 /* PyType_Ready is the closest thing we have to a choke point
3691 * for type objects, so is the best place I can think of to try
3692 * to get type objects into the doubly-linked list of all objects.
3693 * Still, not all type objects go thru PyType_Ready.
3694 */
Tim Peters7571a0f2003-03-23 17:52:28 +00003695 _Py_AddToAllObjects((PyObject *)type, 0);
Tim Peters36eb4df2003-03-23 03:33:13 +00003696#endif
3697
Tim Peters6d6c1a32001-08-02 04:15:00 +00003698 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3699 base = type->tp_base;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003700 if (base == NULL && type != &PyBaseObject_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003701 base = type->tp_base = &PyBaseObject_Type;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003702 Py_INCREF(base);
3703 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003704
Guido van Rossumd8faa362007-04-27 19:54:29 +00003705 /* Now the only way base can still be NULL is if type is
3706 * &PyBaseObject_Type.
3707 */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003708
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003709 /* Initialize the base class */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003710 if (base != NULL && base->tp_dict == NULL) {
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003711 if (PyType_Ready(base) < 0)
3712 goto error;
3713 }
3714
Guido van Rossumd8faa362007-04-27 19:54:29 +00003715 /* Initialize ob_type if NULL. This means extensions that want to be
Guido van Rossum0986d822002-04-08 01:38:42 +00003716 compilable separately on Windows can call PyType_Ready() instead of
3717 initializing the ob_type field of their type objects. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003718 /* The test for base != NULL is really unnecessary, since base is only
3719 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3720 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3721 know that. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003722 if (Py_TYPE(type) == NULL && base != NULL)
3723 Py_TYPE(type) = Py_TYPE(base);
Guido van Rossum0986d822002-04-08 01:38:42 +00003724
Tim Peters6d6c1a32001-08-02 04:15:00 +00003725 /* Initialize tp_bases */
3726 bases = type->tp_bases;
3727 if (bases == NULL) {
3728 if (base == NULL)
3729 bases = PyTuple_New(0);
3730 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003731 bases = PyTuple_Pack(1, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003732 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003733 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003734 type->tp_bases = bases;
3735 }
3736
Guido van Rossum687ae002001-10-15 22:03:32 +00003737 /* Initialize tp_dict */
3738 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003739 if (dict == NULL) {
3740 dict = PyDict_New();
3741 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003742 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00003743 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003744 }
3745
Guido van Rossum687ae002001-10-15 22:03:32 +00003746 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003747 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003748 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003749 if (type->tp_methods != NULL) {
3750 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003751 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003752 }
3753 if (type->tp_members != NULL) {
3754 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003755 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003756 }
3757 if (type->tp_getset != NULL) {
3758 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003759 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003760 }
3761
Tim Peters6d6c1a32001-08-02 04:15:00 +00003762 /* Calculate method resolution order */
3763 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00003764 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003765 }
3766
Guido van Rossum13d52f02001-08-10 21:24:08 +00003767 /* Inherit special flags from dominant base */
3768 if (type->tp_base != NULL)
3769 inherit_special(type, type->tp_base);
3770
Tim Peters6d6c1a32001-08-02 04:15:00 +00003771 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003772 bases = type->tp_mro;
3773 assert(bases != NULL);
3774 assert(PyTuple_Check(bases));
3775 n = PyTuple_GET_SIZE(bases);
3776 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003777 PyObject *b = PyTuple_GET_ITEM(bases, i);
3778 if (PyType_Check(b))
3779 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003780 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003781
Tim Peters3cfe7542003-05-21 21:29:48 +00003782 /* Sanity check for tp_free. */
3783 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3784 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003785 /* This base class needs to call tp_free, but doesn't have
3786 * one, or its tp_free is for non-gc'ed objects.
3787 */
Tim Peters3cfe7542003-05-21 21:29:48 +00003788 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3789 "gc and is a base type but has inappropriate "
3790 "tp_free slot",
3791 type->tp_name);
3792 goto error;
3793 }
3794
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003795 /* if the type dictionary doesn't contain a __doc__, set it from
3796 the tp_doc slot.
3797 */
3798 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3799 if (type->tp_doc != NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +00003800 PyObject *doc = PyUnicode_FromString(type->tp_doc);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003801 if (doc == NULL)
3802 goto error;
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003803 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3804 Py_DECREF(doc);
3805 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00003806 PyDict_SetItemString(type->tp_dict,
3807 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003808 }
3809 }
3810
Guido van Rossum38938152006-08-21 23:36:26 +00003811 /* Hack for tp_hash and __hash__.
3812 If after all that, tp_hash is still NULL, and __hash__ is not in
Nick Coghland1abd252008-07-15 15:46:38 +00003813 tp_dict, set tp_hash to PyObject_HashNotImplemented and
3814 tp_dict['__hash__'] equal to None.
Guido van Rossum38938152006-08-21 23:36:26 +00003815 This signals that __hash__ is not inherited.
3816 */
3817 if (type->tp_hash == NULL) {
3818 if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
3819 if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
3820 goto error;
Nick Coghland1abd252008-07-15 15:46:38 +00003821 type->tp_hash = PyObject_HashNotImplemented;
Guido van Rossum38938152006-08-21 23:36:26 +00003822 }
3823 }
3824
Guido van Rossum13d52f02001-08-10 21:24:08 +00003825 /* Some more special stuff */
3826 base = type->tp_base;
3827 if (base != NULL) {
3828 if (type->tp_as_number == NULL)
3829 type->tp_as_number = base->tp_as_number;
3830 if (type->tp_as_sequence == NULL)
3831 type->tp_as_sequence = base->tp_as_sequence;
3832 if (type->tp_as_mapping == NULL)
3833 type->tp_as_mapping = base->tp_as_mapping;
Guido van Rossumeea47182003-02-11 20:39:59 +00003834 if (type->tp_as_buffer == NULL)
3835 type->tp_as_buffer = base->tp_as_buffer;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003836 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003837
Guido van Rossum1c450732001-10-08 15:18:27 +00003838 /* Link into each base class's list of subclasses */
3839 bases = type->tp_bases;
3840 n = PyTuple_GET_SIZE(bases);
3841 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003842 PyObject *b = PyTuple_GET_ITEM(bases, i);
3843 if (PyType_Check(b) &&
3844 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00003845 goto error;
3846 }
3847
Guido van Rossum13d52f02001-08-10 21:24:08 +00003848 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00003849 assert(type->tp_dict != NULL);
3850 type->tp_flags =
3851 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003852 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00003853
3854 error:
3855 type->tp_flags &= ~Py_TPFLAGS_READYING;
3856 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003857}
3858
Guido van Rossum1c450732001-10-08 15:18:27 +00003859static int
3860add_subclass(PyTypeObject *base, PyTypeObject *type)
3861{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003862 Py_ssize_t i;
3863 int result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003864 PyObject *list, *ref, *newobj;
Guido van Rossum1c450732001-10-08 15:18:27 +00003865
3866 list = base->tp_subclasses;
3867 if (list == NULL) {
3868 base->tp_subclasses = list = PyList_New(0);
3869 if (list == NULL)
3870 return -1;
3871 }
3872 assert(PyList_Check(list));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003873 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
Guido van Rossum1c450732001-10-08 15:18:27 +00003874 i = PyList_GET_SIZE(list);
3875 while (--i >= 0) {
3876 ref = PyList_GET_ITEM(list, i);
3877 assert(PyWeakref_CheckRef(ref));
Guido van Rossum3930bc32002-10-18 13:51:49 +00003878 if (PyWeakref_GET_OBJECT(ref) == Py_None)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003879 return PyList_SetItem(list, i, newobj);
Guido van Rossum1c450732001-10-08 15:18:27 +00003880 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003881 result = PyList_Append(list, newobj);
3882 Py_DECREF(newobj);
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003883 return result;
Guido van Rossum1c450732001-10-08 15:18:27 +00003884}
3885
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003886static void
3887remove_subclass(PyTypeObject *base, PyTypeObject *type)
3888{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003889 Py_ssize_t i;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003890 PyObject *list, *ref;
3891
3892 list = base->tp_subclasses;
3893 if (list == NULL) {
3894 return;
3895 }
3896 assert(PyList_Check(list));
3897 i = PyList_GET_SIZE(list);
3898 while (--i >= 0) {
3899 ref = PyList_GET_ITEM(list, i);
3900 assert(PyWeakref_CheckRef(ref));
3901 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3902 /* this can't fail, right? */
3903 PySequence_DelItem(list, i);
3904 return;
3905 }
3906 }
3907}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003908
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003909static int
3910check_num_args(PyObject *ob, int n)
3911{
3912 if (!PyTuple_CheckExact(ob)) {
3913 PyErr_SetString(PyExc_SystemError,
3914 "PyArg_UnpackTuple() argument list is not a tuple");
3915 return 0;
3916 }
3917 if (n == PyTuple_GET_SIZE(ob))
3918 return 1;
3919 PyErr_Format(
3920 PyExc_TypeError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00003921 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003922 return 0;
3923}
3924
Tim Peters6d6c1a32001-08-02 04:15:00 +00003925/* Generic wrappers for overloadable 'operators' such as __getitem__ */
3926
3927/* There's a wrapper *function* for each distinct function typedef used
Guido van Rossumd8faa362007-04-27 19:54:29 +00003928 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
Tim Peters6d6c1a32001-08-02 04:15:00 +00003929 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3930 Most tables have only one entry; the tables for binary operators have two
3931 entries, one regular and one with reversed arguments. */
3932
3933static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003934wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003935{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003936 lenfunc func = (lenfunc)wrapped;
3937 Py_ssize_t res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003938
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003939 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003940 return NULL;
3941 res = (*func)(self);
3942 if (res == -1 && PyErr_Occurred())
3943 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00003944 return PyLong_FromLong((long)res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003945}
3946
Tim Peters6d6c1a32001-08-02 04:15:00 +00003947static PyObject *
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003948wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3949{
3950 inquiry func = (inquiry)wrapped;
3951 int res;
3952
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003953 if (!check_num_args(args, 0))
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003954 return NULL;
3955 res = (*func)(self);
3956 if (res == -1 && PyErr_Occurred())
3957 return NULL;
3958 return PyBool_FromLong((long)res);
3959}
3960
3961static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003962wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3963{
3964 binaryfunc func = (binaryfunc)wrapped;
3965 PyObject *other;
3966
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003967 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003968 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003969 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003970 return (*func)(self, other);
3971}
3972
3973static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003974wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3975{
3976 binaryfunc func = (binaryfunc)wrapped;
3977 PyObject *other;
3978
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003979 if (!check_num_args(args, 1))
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003980 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003981 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003982 return (*func)(self, other);
3983}
3984
3985static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003986wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3987{
3988 binaryfunc func = (binaryfunc)wrapped;
3989 PyObject *other;
3990
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003991 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003992 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003993 other = PyTuple_GET_ITEM(args, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003994 if (!PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003995 Py_INCREF(Py_NotImplemented);
3996 return Py_NotImplemented;
3997 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003998 return (*func)(other, self);
3999}
4000
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004001static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004002wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
4003{
4004 ternaryfunc func = (ternaryfunc)wrapped;
4005 PyObject *other;
4006 PyObject *third = Py_None;
4007
4008 /* Note: This wrapper only works for __pow__() */
4009
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004010 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004011 return NULL;
4012 return (*func)(self, other, third);
4013}
4014
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00004015static PyObject *
4016wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4017{
4018 ternaryfunc func = (ternaryfunc)wrapped;
4019 PyObject *other;
4020 PyObject *third = Py_None;
4021
4022 /* Note: This wrapper only works for __pow__() */
4023
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004024 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00004025 return NULL;
4026 return (*func)(other, self, third);
4027}
4028
Tim Peters6d6c1a32001-08-02 04:15:00 +00004029static PyObject *
4030wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
4031{
4032 unaryfunc func = (unaryfunc)wrapped;
4033
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004034 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004035 return NULL;
4036 return (*func)(self);
4037}
4038
Tim Peters6d6c1a32001-08-02 04:15:00 +00004039static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004040wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004041{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004042 ssizeargfunc func = (ssizeargfunc)wrapped;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004043 PyObject* o;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004044 Py_ssize_t i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004045
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004046 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
4047 return NULL;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004048 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004049 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00004050 return NULL;
4051 return (*func)(self, i);
4052}
4053
Martin v. Löwis18e16552006-02-15 17:27:45 +00004054static Py_ssize_t
Guido van Rossum5d815f32001-08-17 21:57:47 +00004055getindex(PyObject *self, PyObject *arg)
4056{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004057 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004058
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004059 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004060 if (i == -1 && PyErr_Occurred())
4061 return -1;
4062 if (i < 0) {
Christian Heimes90aa7642007-12-19 02:45:37 +00004063 PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004064 if (sq && sq->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00004065 Py_ssize_t n = (*sq->sq_length)(self);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004066 if (n < 0)
4067 return -1;
4068 i += n;
4069 }
4070 }
4071 return i;
4072}
4073
4074static PyObject *
4075wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
4076{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004077 ssizeargfunc func = (ssizeargfunc)wrapped;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004078 PyObject *arg;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004079 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004080
Guido van Rossumf4593e02001-10-03 12:09:30 +00004081 if (PyTuple_GET_SIZE(args) == 1) {
4082 arg = PyTuple_GET_ITEM(args, 0);
4083 i = getindex(self, arg);
4084 if (i == -1 && PyErr_Occurred())
4085 return NULL;
4086 return (*func)(self, i);
4087 }
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004088 check_num_args(args, 1);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004089 assert(PyErr_Occurred());
4090 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004091}
4092
Tim Peters6d6c1a32001-08-02 04:15:00 +00004093static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00004094wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004095{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004096 ssizeobjargproc func = (ssizeobjargproc)wrapped;
4097 Py_ssize_t i;
4098 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004099 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004100
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004101 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
Guido van Rossum5d815f32001-08-17 21:57:47 +00004102 return NULL;
4103 i = getindex(self, arg);
4104 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00004105 return NULL;
4106 res = (*func)(self, i, value);
4107 if (res == -1 && PyErr_Occurred())
4108 return NULL;
4109 Py_INCREF(Py_None);
4110 return Py_None;
4111}
4112
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004113static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00004114wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004115{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004116 ssizeobjargproc func = (ssizeobjargproc)wrapped;
4117 Py_ssize_t i;
4118 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004119 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004120
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004121 if (!check_num_args(args, 1))
Guido van Rossum5d815f32001-08-17 21:57:47 +00004122 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004123 arg = PyTuple_GET_ITEM(args, 0);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004124 i = getindex(self, arg);
4125 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004126 return NULL;
4127 res = (*func)(self, i, NULL);
4128 if (res == -1 && PyErr_Occurred())
4129 return NULL;
4130 Py_INCREF(Py_None);
4131 return Py_None;
4132}
4133
Tim Peters6d6c1a32001-08-02 04:15:00 +00004134/* XXX objobjproc is a misnomer; should be objargpred */
4135static PyObject *
4136wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
4137{
4138 objobjproc func = (objobjproc)wrapped;
4139 int res;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00004140 PyObject *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004141
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004142 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004143 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004144 value = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004145 res = (*func)(self, value);
4146 if (res == -1 && PyErr_Occurred())
4147 return NULL;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00004148 else
4149 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004150}
4151
Tim Peters6d6c1a32001-08-02 04:15:00 +00004152static PyObject *
4153wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
4154{
4155 objobjargproc func = (objobjargproc)wrapped;
4156 int res;
4157 PyObject *key, *value;
4158
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004159 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004160 return NULL;
4161 res = (*func)(self, key, value);
4162 if (res == -1 && PyErr_Occurred())
4163 return NULL;
4164 Py_INCREF(Py_None);
4165 return Py_None;
4166}
4167
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004168static PyObject *
4169wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
4170{
4171 objobjargproc func = (objobjargproc)wrapped;
4172 int res;
4173 PyObject *key;
4174
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004175 if (!check_num_args(args, 1))
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004176 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004177 key = PyTuple_GET_ITEM(args, 0);
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004178 res = (*func)(self, key, NULL);
4179 if (res == -1 && PyErr_Occurred())
4180 return NULL;
4181 Py_INCREF(Py_None);
4182 return Py_None;
4183}
4184
Tim Peters6d6c1a32001-08-02 04:15:00 +00004185static PyObject *
4186wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
4187{
4188 cmpfunc func = (cmpfunc)wrapped;
4189 int res;
4190 PyObject *other;
4191
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004192 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004193 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004194 other = PyTuple_GET_ITEM(args, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00004195 if (Py_TYPE(other)->tp_compare != func &&
4196 !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
Guido van Rossumceccae52001-09-18 20:03:57 +00004197 PyErr_Format(
4198 PyExc_TypeError,
4199 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
Christian Heimes90aa7642007-12-19 02:45:37 +00004200 Py_TYPE(self)->tp_name,
4201 Py_TYPE(self)->tp_name,
4202 Py_TYPE(other)->tp_name);
Guido van Rossumceccae52001-09-18 20:03:57 +00004203 return NULL;
4204 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004205 res = (*func)(self, other);
4206 if (PyErr_Occurred())
4207 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00004208 return PyLong_FromLong((long)res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004209}
4210
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004211/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
Guido van Rossum52b27052003-04-15 20:05:10 +00004212 This is called the Carlo Verre hack after its discoverer. */
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004213static int
4214hackcheck(PyObject *self, setattrofunc func, char *what)
4215{
Christian Heimes90aa7642007-12-19 02:45:37 +00004216 PyTypeObject *type = Py_TYPE(self);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004217 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
4218 type = type->tp_base;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004219 /* If type is NULL now, this is a really weird type.
4220 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00004221 if (type && type->tp_setattro != func) {
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004222 PyErr_Format(PyExc_TypeError,
4223 "can't apply this %s to %s object",
4224 what,
4225 type->tp_name);
4226 return 0;
4227 }
4228 return 1;
4229}
4230
Tim Peters6d6c1a32001-08-02 04:15:00 +00004231static PyObject *
4232wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
4233{
4234 setattrofunc func = (setattrofunc)wrapped;
4235 int res;
4236 PyObject *name, *value;
4237
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004238 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004239 return NULL;
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004240 if (!hackcheck(self, func, "__setattr__"))
4241 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004242 res = (*func)(self, name, value);
4243 if (res < 0)
4244 return NULL;
4245 Py_INCREF(Py_None);
4246 return Py_None;
4247}
4248
4249static PyObject *
4250wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
4251{
4252 setattrofunc func = (setattrofunc)wrapped;
4253 int res;
4254 PyObject *name;
4255
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004256 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004257 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004258 name = PyTuple_GET_ITEM(args, 0);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004259 if (!hackcheck(self, func, "__delattr__"))
4260 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004261 res = (*func)(self, name, NULL);
4262 if (res < 0)
4263 return NULL;
4264 Py_INCREF(Py_None);
4265 return Py_None;
4266}
4267
Tim Peters6d6c1a32001-08-02 04:15:00 +00004268static PyObject *
4269wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
4270{
4271 hashfunc func = (hashfunc)wrapped;
4272 long res;
4273
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004274 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004275 return NULL;
4276 res = (*func)(self);
4277 if (res == -1 && PyErr_Occurred())
4278 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00004279 return PyLong_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004280}
4281
Tim Peters6d6c1a32001-08-02 04:15:00 +00004282static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00004283wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004284{
4285 ternaryfunc func = (ternaryfunc)wrapped;
4286
Guido van Rossumc8e56452001-10-22 00:43:43 +00004287 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004288}
4289
Tim Peters6d6c1a32001-08-02 04:15:00 +00004290static PyObject *
4291wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
4292{
4293 richcmpfunc func = (richcmpfunc)wrapped;
4294 PyObject *other;
4295
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004296 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004297 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004298 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004299 return (*func)(self, other, op);
4300}
4301
4302#undef RICHCMP_WRAPPER
4303#define RICHCMP_WRAPPER(NAME, OP) \
4304static PyObject * \
4305richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4306{ \
4307 return wrap_richcmpfunc(self, args, wrapped, OP); \
4308}
4309
Jack Jansen8e938b42001-08-08 15:29:49 +00004310RICHCMP_WRAPPER(lt, Py_LT)
4311RICHCMP_WRAPPER(le, Py_LE)
4312RICHCMP_WRAPPER(eq, Py_EQ)
4313RICHCMP_WRAPPER(ne, Py_NE)
4314RICHCMP_WRAPPER(gt, Py_GT)
4315RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004316
Tim Peters6d6c1a32001-08-02 04:15:00 +00004317static PyObject *
4318wrap_next(PyObject *self, PyObject *args, void *wrapped)
4319{
4320 unaryfunc func = (unaryfunc)wrapped;
4321 PyObject *res;
4322
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004323 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004324 return NULL;
4325 res = (*func)(self);
4326 if (res == NULL && !PyErr_Occurred())
4327 PyErr_SetNone(PyExc_StopIteration);
4328 return res;
4329}
4330
Tim Peters6d6c1a32001-08-02 04:15:00 +00004331static PyObject *
4332wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
4333{
4334 descrgetfunc func = (descrgetfunc)wrapped;
4335 PyObject *obj;
4336 PyObject *type = NULL;
4337
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004338 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004339 return NULL;
Guido van Rossum82ed25c2003-02-11 16:25:43 +00004340 if (obj == Py_None)
4341 obj = NULL;
4342 if (type == Py_None)
4343 type = NULL;
4344 if (type == NULL &&obj == NULL) {
4345 PyErr_SetString(PyExc_TypeError,
4346 "__get__(None, None) is invalid");
4347 return NULL;
4348 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004349 return (*func)(self, obj, type);
4350}
4351
Tim Peters6d6c1a32001-08-02 04:15:00 +00004352static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004353wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004354{
4355 descrsetfunc func = (descrsetfunc)wrapped;
4356 PyObject *obj, *value;
4357 int ret;
4358
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004359 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004360 return NULL;
4361 ret = (*func)(self, obj, value);
4362 if (ret < 0)
4363 return NULL;
4364 Py_INCREF(Py_None);
4365 return Py_None;
4366}
Guido van Rossum22b13872002-08-06 21:41:44 +00004367
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004368static PyObject *
4369wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
4370{
4371 descrsetfunc func = (descrsetfunc)wrapped;
4372 PyObject *obj;
4373 int ret;
4374
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004375 if (!check_num_args(args, 1))
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004376 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004377 obj = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004378 ret = (*func)(self, obj, NULL);
4379 if (ret < 0)
4380 return NULL;
4381 Py_INCREF(Py_None);
4382 return Py_None;
4383}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004384
Tim Peters6d6c1a32001-08-02 04:15:00 +00004385static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00004386wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004387{
4388 initproc func = (initproc)wrapped;
4389
Guido van Rossumc8e56452001-10-22 00:43:43 +00004390 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004391 return NULL;
4392 Py_INCREF(Py_None);
4393 return Py_None;
4394}
4395
Tim Peters6d6c1a32001-08-02 04:15:00 +00004396static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004397tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004398{
Barry Warsaw60f01882001-08-22 19:24:42 +00004399 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004400 PyObject *arg0, *res;
4401
4402 if (self == NULL || !PyType_Check(self))
4403 Py_FatalError("__new__() called with non-type 'self'");
4404 type = (PyTypeObject *)self;
4405 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004406 PyErr_Format(PyExc_TypeError,
4407 "%s.__new__(): not enough arguments",
4408 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004409 return NULL;
4410 }
4411 arg0 = PyTuple_GET_ITEM(args, 0);
4412 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004413 PyErr_Format(PyExc_TypeError,
4414 "%s.__new__(X): X is not a type object (%s)",
4415 type->tp_name,
Christian Heimes90aa7642007-12-19 02:45:37 +00004416 Py_TYPE(arg0)->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004417 return NULL;
4418 }
4419 subtype = (PyTypeObject *)arg0;
4420 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004421 PyErr_Format(PyExc_TypeError,
4422 "%s.__new__(%s): %s is not a subtype of %s",
4423 type->tp_name,
4424 subtype->tp_name,
4425 subtype->tp_name,
4426 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004427 return NULL;
4428 }
Barry Warsaw60f01882001-08-22 19:24:42 +00004429
4430 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00004431 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00004432 most derived base that's not a heap type is this type. */
4433 staticbase = subtype;
4434 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4435 staticbase = staticbase->tp_base;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004436 /* If staticbase is NULL now, it is a really weird type.
4437 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00004438 if (staticbase && staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004439 PyErr_Format(PyExc_TypeError,
4440 "%s.__new__(%s) is not safe, use %s.__new__()",
4441 type->tp_name,
4442 subtype->tp_name,
4443 staticbase == NULL ? "?" : staticbase->tp_name);
4444 return NULL;
4445 }
4446
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004447 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4448 if (args == NULL)
4449 return NULL;
4450 res = type->tp_new(subtype, args, kwds);
4451 Py_DECREF(args);
4452 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004453}
4454
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004455static struct PyMethodDef tp_new_methoddef[] = {
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004456 {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00004457 PyDoc_STR("T.__new__(S, ...) -> "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004458 "a new object with type S, a subtype of T")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00004459 {0}
4460};
4461
4462static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004463add_tp_new_wrapper(PyTypeObject *type)
4464{
Guido van Rossumf040ede2001-08-07 16:40:56 +00004465 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004466
Guido van Rossum687ae002001-10-15 22:03:32 +00004467 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00004468 return 0;
4469 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004470 if (func == NULL)
4471 return -1;
Raymond Hettinger8d726ee2004-06-25 22:24:35 +00004472 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
Raymond Hettingerd56cbe52004-06-25 22:17:39 +00004473 Py_DECREF(func);
4474 return -1;
4475 }
4476 Py_DECREF(func);
4477 return 0;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004478}
4479
Guido van Rossumf040ede2001-08-07 16:40:56 +00004480/* Slot wrappers that call the corresponding __foo__ slot. See comments
4481 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004482
Guido van Rossumdc91b992001-08-08 22:26:22 +00004483#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004484static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004485FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004486{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00004487 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004488 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004489}
4490
Guido van Rossumdc91b992001-08-08 22:26:22 +00004491#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004492static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004493FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004494{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004495 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004496 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004497}
4498
Guido van Rossumcd118802003-01-06 22:57:47 +00004499/* Boolean helper for SLOT1BINFULL().
4500 right.__class__ is a nontrivial subclass of left.__class__. */
4501static int
4502method_is_overloaded(PyObject *left, PyObject *right, char *name)
4503{
4504 PyObject *a, *b;
4505 int ok;
4506
Christian Heimes90aa7642007-12-19 02:45:37 +00004507 b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
Guido van Rossumcd118802003-01-06 22:57:47 +00004508 if (b == NULL) {
4509 PyErr_Clear();
4510 /* If right doesn't have it, it's not overloaded */
4511 return 0;
4512 }
4513
Christian Heimes90aa7642007-12-19 02:45:37 +00004514 a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
Guido van Rossumcd118802003-01-06 22:57:47 +00004515 if (a == NULL) {
4516 PyErr_Clear();
4517 Py_DECREF(b);
4518 /* If right has it but left doesn't, it's overloaded */
4519 return 1;
4520 }
4521
4522 ok = PyObject_RichCompareBool(a, b, Py_NE);
4523 Py_DECREF(a);
4524 Py_DECREF(b);
4525 if (ok < 0) {
4526 PyErr_Clear();
4527 return 0;
4528 }
4529
4530 return ok;
4531}
4532
Guido van Rossumdc91b992001-08-08 22:26:22 +00004533
4534#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004535static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004536FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004537{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004538 static PyObject *cache_str, *rcache_str; \
Christian Heimes90aa7642007-12-19 02:45:37 +00004539 int do_other = Py_TYPE(self) != Py_TYPE(other) && \
4540 Py_TYPE(other)->tp_as_number != NULL && \
4541 Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
4542 if (Py_TYPE(self)->tp_as_number != NULL && \
4543 Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004544 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00004545 if (do_other && \
Christian Heimes90aa7642007-12-19 02:45:37 +00004546 PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
Guido van Rossumcd118802003-01-06 22:57:47 +00004547 method_is_overloaded(self, other, ROPSTR)) { \
Guido van Rossum55f20992001-10-01 17:18:22 +00004548 r = call_maybe( \
4549 other, ROPSTR, &rcache_str, "(O)", self); \
4550 if (r != Py_NotImplemented) \
4551 return r; \
4552 Py_DECREF(r); \
4553 do_other = 0; \
4554 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004555 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004556 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004557 if (r != Py_NotImplemented || \
Christian Heimes90aa7642007-12-19 02:45:37 +00004558 Py_TYPE(other) == Py_TYPE(self)) \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004559 return r; \
4560 Py_DECREF(r); \
4561 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00004562 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004563 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004564 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004565 } \
4566 Py_INCREF(Py_NotImplemented); \
4567 return Py_NotImplemented; \
4568}
4569
4570#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4571 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4572
4573#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4574static PyObject * \
4575FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4576{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004577 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004578 return call_method(self, OPSTR, &cache_str, \
4579 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004580}
4581
Martin v. Löwis18e16552006-02-15 17:27:45 +00004582static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +00004583slot_sq_length(PyObject *self)
4584{
Guido van Rossum2730b132001-08-28 18:22:14 +00004585 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004586 PyObject *res = call_method(self, "__len__", &len_str, "()");
Martin v. Löwis18e16552006-02-15 17:27:45 +00004587 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004588
4589 if (res == NULL)
4590 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00004591 len = PyLong_AsSsize_t(res);
Guido van Rossum26111622001-10-01 16:42:49 +00004592 Py_DECREF(res);
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004593 if (len < 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004594 if (!PyErr_Occurred())
4595 PyErr_SetString(PyExc_ValueError,
4596 "__len__() should return >= 0");
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004597 return -1;
4598 }
Guido van Rossum26111622001-10-01 16:42:49 +00004599 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004600}
4601
Guido van Rossumf4593e02001-10-03 12:09:30 +00004602/* Super-optimized version of slot_sq_item.
4603 Other slots could do the same... */
4604static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00004605slot_sq_item(PyObject *self, Py_ssize_t i)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004606{
4607 static PyObject *getitem_str;
4608 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4609 descrgetfunc f;
4610
4611 if (getitem_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00004612 getitem_str = PyUnicode_InternFromString("__getitem__");
Guido van Rossumf4593e02001-10-03 12:09:30 +00004613 if (getitem_str == NULL)
4614 return NULL;
4615 }
Christian Heimes90aa7642007-12-19 02:45:37 +00004616 func = _PyType_Lookup(Py_TYPE(self), getitem_str);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004617 if (func != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +00004618 if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004619 Py_INCREF(func);
Neal Norwitz673cd822002-10-18 16:33:13 +00004620 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00004621 func = f(func, self, (PyObject *)(Py_TYPE(self)));
Neal Norwitz673cd822002-10-18 16:33:13 +00004622 if (func == NULL) {
4623 return NULL;
4624 }
4625 }
Christian Heimes217cfd12007-12-02 14:31:20 +00004626 ival = PyLong_FromSsize_t(i);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004627 if (ival != NULL) {
4628 args = PyTuple_New(1);
4629 if (args != NULL) {
4630 PyTuple_SET_ITEM(args, 0, ival);
4631 retval = PyObject_Call(func, args, NULL);
4632 Py_XDECREF(args);
4633 Py_XDECREF(func);
4634 return retval;
4635 }
4636 }
4637 }
4638 else {
4639 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4640 }
4641 Py_XDECREF(args);
4642 Py_XDECREF(ival);
4643 Py_XDECREF(func);
4644 return NULL;
4645}
4646
Tim Peters6d6c1a32001-08-02 04:15:00 +00004647static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004648slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004649{
4650 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004651 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004652
4653 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004654 res = call_method(self, "__delitem__", &delitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004655 "(n)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004656 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004657 res = call_method(self, "__setitem__", &setitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004658 "(nO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004659 if (res == NULL)
4660 return -1;
4661 Py_DECREF(res);
4662 return 0;
4663}
4664
4665static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00004666slot_sq_contains(PyObject *self, PyObject *value)
4667{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004668 PyObject *func, *res, *args;
Tim Petersbf9b2442003-03-23 05:35:36 +00004669 int result = -1;
4670
Guido van Rossum60718732001-08-28 17:47:51 +00004671 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004672
Guido van Rossum55f20992001-10-01 17:18:22 +00004673 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004674 if (func != NULL) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004675 args = PyTuple_Pack(1, value);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004676 if (args == NULL)
4677 res = NULL;
4678 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004679 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004680 Py_DECREF(args);
4681 }
4682 Py_DECREF(func);
Tim Petersbf9b2442003-03-23 05:35:36 +00004683 if (res != NULL) {
4684 result = PyObject_IsTrue(res);
4685 Py_DECREF(res);
4686 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004687 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004688 else if (! PyErr_Occurred()) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00004689 /* Possible results: -1 and 1 */
4690 result = (int)_PySequence_IterSearch(self, value,
Tim Petersbf9b2442003-03-23 05:35:36 +00004691 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004692 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004693 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004694}
4695
Tim Peters6d6c1a32001-08-02 04:15:00 +00004696#define slot_mp_length slot_sq_length
4697
Guido van Rossumdc91b992001-08-08 22:26:22 +00004698SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004699
4700static int
4701slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4702{
4703 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004704 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004705
4706 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004707 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004708 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004709 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004710 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004711 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004712 if (res == NULL)
4713 return -1;
4714 Py_DECREF(res);
4715 return 0;
4716}
4717
Guido van Rossumdc91b992001-08-08 22:26:22 +00004718SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4719SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4720SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004721SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4722SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4723
Jeremy Hylton938ace62002-07-17 16:30:39 +00004724static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
Guido van Rossumdc91b992001-08-08 22:26:22 +00004725
4726SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4727 nb_power, "__pow__", "__rpow__")
4728
4729static PyObject *
4730slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4731{
Guido van Rossum2730b132001-08-28 18:22:14 +00004732 static PyObject *pow_str;
4733
Guido van Rossumdc91b992001-08-08 22:26:22 +00004734 if (modulus == Py_None)
4735 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00004736 /* Three-arg power doesn't use __rpow__. But ternary_op
4737 can call this when the second argument's type uses
4738 slot_nb_power, so check before calling self.__pow__. */
Christian Heimes90aa7642007-12-19 02:45:37 +00004739 if (Py_TYPE(self)->tp_as_number != NULL &&
4740 Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
Guido van Rossum23094982002-06-10 14:30:43 +00004741 return call_method(self, "__pow__", &pow_str,
4742 "(OO)", other, modulus);
4743 }
4744 Py_INCREF(Py_NotImplemented);
4745 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00004746}
4747
4748SLOT0(slot_nb_negative, "__neg__")
4749SLOT0(slot_nb_positive, "__pos__")
4750SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004751
4752static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00004753slot_nb_bool(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004754{
Tim Petersea7f75d2002-12-07 21:39:16 +00004755 PyObject *func, *args;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004756 static PyObject *bool_str, *len_str;
Tim Petersea7f75d2002-12-07 21:39:16 +00004757 int result = -1;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004758 int from_len = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004759
Jack Diederich4dafcc42006-11-28 19:15:13 +00004760 func = lookup_maybe(self, "__bool__", &bool_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004761 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00004762 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00004763 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00004764 func = lookup_maybe(self, "__len__", &len_str);
Tim Petersea7f75d2002-12-07 21:39:16 +00004765 if (func == NULL)
4766 return PyErr_Occurred() ? -1 : 1;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004767 from_len = 1;
Tim Petersea7f75d2002-12-07 21:39:16 +00004768 }
4769 args = PyTuple_New(0);
4770 if (args != NULL) {
4771 PyObject *temp = PyObject_Call(func, args, NULL);
4772 Py_DECREF(args);
4773 if (temp != NULL) {
Jack Diederich4dafcc42006-11-28 19:15:13 +00004774 if (from_len) {
4775 /* enforced by slot_nb_len */
Jeremy Hylton090a3492003-06-27 16:46:45 +00004776 result = PyObject_IsTrue(temp);
Jack Diederich4dafcc42006-11-28 19:15:13 +00004777 }
4778 else if (PyBool_Check(temp)) {
4779 result = PyObject_IsTrue(temp);
4780 }
Jeremy Hylton090a3492003-06-27 16:46:45 +00004781 else {
4782 PyErr_Format(PyExc_TypeError,
Jack Diederich4dafcc42006-11-28 19:15:13 +00004783 "__bool__ should return "
4784 "bool, returned %s",
Christian Heimes90aa7642007-12-19 02:45:37 +00004785 Py_TYPE(temp)->tp_name);
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004786 result = -1;
Jeremy Hylton090a3492003-06-27 16:46:45 +00004787 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004788 Py_DECREF(temp);
Guido van Rossum55f20992001-10-01 17:18:22 +00004789 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004790 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004791 Py_DECREF(func);
Tim Petersea7f75d2002-12-07 21:39:16 +00004792 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004793}
4794
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004795
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004796static PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004797slot_nb_index(PyObject *self)
4798{
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004799 static PyObject *index_str;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004800 return call_method(self, "__index__", &index_str, "()");
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004801}
4802
4803
Guido van Rossumdc91b992001-08-08 22:26:22 +00004804SLOT0(slot_nb_invert, "__invert__")
4805SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4806SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4807SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4808SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4809SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004810
Guido van Rossumdc91b992001-08-08 22:26:22 +00004811SLOT0(slot_nb_int, "__int__")
4812SLOT0(slot_nb_long, "__long__")
4813SLOT0(slot_nb_float, "__float__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004814SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4815SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4816SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004817SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
Thomas Wouterscf297e42007-02-23 15:07:44 +00004818/* Can't use SLOT1 here, because nb_inplace_power is ternary */
4819static PyObject *
4820slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
4821{
4822 static PyObject *cache_str;
4823 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
4824}
Guido van Rossumdc91b992001-08-08 22:26:22 +00004825SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4826SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4827SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4828SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4829SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4830SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4831 "__floordiv__", "__rfloordiv__")
4832SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4833SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4834SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004835
4836static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00004837half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004838{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004839 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004840 static PyObject *cmp_str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004841 Py_ssize_t c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004842
Guido van Rossum60718732001-08-28 17:47:51 +00004843 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004844 if (func == NULL) {
4845 PyErr_Clear();
4846 }
4847 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004848 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004849 if (args == NULL)
4850 res = NULL;
4851 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004852 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004853 Py_DECREF(args);
4854 }
Raymond Hettingerab5dae32002-06-24 13:08:16 +00004855 Py_DECREF(func);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004856 if (res != Py_NotImplemented) {
4857 if (res == NULL)
4858 return -2;
Christian Heimes217cfd12007-12-02 14:31:20 +00004859 c = PyLong_AsLong(res);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004860 Py_DECREF(res);
4861 if (c == -1 && PyErr_Occurred())
4862 return -2;
4863 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4864 }
4865 Py_DECREF(res);
4866 }
4867 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004868}
4869
Guido van Rossumab3b0342001-09-18 20:38:53 +00004870/* This slot is published for the benefit of try_3way_compare in object.c */
4871int
4872_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00004873{
4874 int c;
4875
Christian Heimes90aa7642007-12-19 02:45:37 +00004876 if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004877 c = half_compare(self, other);
4878 if (c <= 1)
4879 return c;
4880 }
Christian Heimes90aa7642007-12-19 02:45:37 +00004881 if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004882 c = half_compare(other, self);
4883 if (c < -1)
4884 return -2;
4885 if (c <= 1)
4886 return -c;
4887 }
4888 return (void *)self < (void *)other ? -1 :
4889 (void *)self > (void *)other ? 1 : 0;
4890}
4891
4892static PyObject *
4893slot_tp_repr(PyObject *self)
4894{
4895 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004896 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004897
Guido van Rossum60718732001-08-28 17:47:51 +00004898 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004899 if (func != NULL) {
4900 res = PyEval_CallObject(func, NULL);
4901 Py_DECREF(func);
4902 return res;
4903 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00004904 PyErr_Clear();
Walter Dörwald1ab83302007-05-18 17:15:44 +00004905 return PyUnicode_FromFormat("<%s object at %p>",
Christian Heimes90aa7642007-12-19 02:45:37 +00004906 Py_TYPE(self)->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004907}
4908
4909static PyObject *
4910slot_tp_str(PyObject *self)
4911{
4912 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004913 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004914
Guido van Rossum60718732001-08-28 17:47:51 +00004915 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004916 if (func != NULL) {
4917 res = PyEval_CallObject(func, NULL);
4918 Py_DECREF(func);
4919 return res;
4920 }
4921 else {
Walter Dörwald1ab83302007-05-18 17:15:44 +00004922 PyObject *ress;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004923 PyErr_Clear();
Walter Dörwald1ab83302007-05-18 17:15:44 +00004924 res = slot_tp_repr(self);
4925 if (!res)
4926 return NULL;
4927 ress = _PyUnicode_AsDefaultEncodedString(res, NULL);
4928 Py_DECREF(res);
4929 return ress;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004930 }
4931}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004932
4933static long
4934slot_tp_hash(PyObject *self)
4935{
Guido van Rossum4011a242006-08-17 23:09:57 +00004936 PyObject *func, *res;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004937 static PyObject *hash_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004938 long h;
4939
Guido van Rossum60718732001-08-28 17:47:51 +00004940 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004941
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004942 if (func == Py_None) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004943 Py_DECREF(func);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004944 func = NULL;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004945 }
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004946
4947 if (func == NULL) {
Nick Coghland1abd252008-07-15 15:46:38 +00004948 return PyObject_HashNotImplemented(self);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004949 }
4950
Guido van Rossum4011a242006-08-17 23:09:57 +00004951 res = PyEval_CallObject(func, NULL);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004952 Py_DECREF(func);
4953 if (res == NULL)
4954 return -1;
4955 if (PyLong_Check(res))
4956 h = PyLong_Type.tp_hash(res);
4957 else
Christian Heimes217cfd12007-12-02 14:31:20 +00004958 h = PyLong_AsLong(res);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004959 Py_DECREF(res);
4960 if (h == -1 && !PyErr_Occurred())
4961 h = -2;
4962 return h;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004963}
4964
4965static PyObject *
4966slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4967{
Guido van Rossum60718732001-08-28 17:47:51 +00004968 static PyObject *call_str;
4969 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004970 PyObject *res;
4971
4972 if (meth == NULL)
4973 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004974
Tim Peters6d6c1a32001-08-02 04:15:00 +00004975 res = PyObject_Call(meth, args, kwds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004976
Tim Peters6d6c1a32001-08-02 04:15:00 +00004977 Py_DECREF(meth);
4978 return res;
4979}
4980
Guido van Rossum14a6f832001-10-17 13:59:09 +00004981/* There are two slot dispatch functions for tp_getattro.
4982
4983 - slot_tp_getattro() is used when __getattribute__ is overridden
4984 but no __getattr__ hook is present;
4985
4986 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4987
Guido van Rossumc334df52002-04-04 23:44:47 +00004988 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4989 detects the absence of __getattr__ and then installs the simpler slot if
4990 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00004991
Tim Peters6d6c1a32001-08-02 04:15:00 +00004992static PyObject *
4993slot_tp_getattro(PyObject *self, PyObject *name)
4994{
Guido van Rossum14a6f832001-10-17 13:59:09 +00004995 static PyObject *getattribute_str = NULL;
4996 return call_method(self, "__getattribute__", &getattribute_str,
4997 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004998}
4999
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005000static PyObject *
5001slot_tp_getattr_hook(PyObject *self, PyObject *name)
5002{
Christian Heimes90aa7642007-12-19 02:45:37 +00005003 PyTypeObject *tp = Py_TYPE(self);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005004 PyObject *getattr, *getattribute, *res;
5005 static PyObject *getattribute_str = NULL;
5006 static PyObject *getattr_str = NULL;
5007
5008 if (getattr_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005009 getattr_str = PyUnicode_InternFromString("__getattr__");
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005010 if (getattr_str == NULL)
5011 return NULL;
5012 }
5013 if (getattribute_str == NULL) {
5014 getattribute_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00005015 PyUnicode_InternFromString("__getattribute__");
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005016 if (getattribute_str == NULL)
5017 return NULL;
5018 }
5019 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00005020 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005021 /* No __getattr__ hook: use a simpler dispatcher */
5022 tp->tp_getattro = slot_tp_getattro;
5023 return slot_tp_getattro(self, name);
5024 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005025 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00005026 if (getattribute == NULL ||
Christian Heimes90aa7642007-12-19 02:45:37 +00005027 (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
Guido van Rossum14a6f832001-10-17 13:59:09 +00005028 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
5029 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005030 res = PyObject_GenericGetAttr(self, name);
5031 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00005032 res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL);
Guido van Rossum14a6f832001-10-17 13:59:09 +00005033 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005034 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00005035 res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005036 }
5037 return res;
5038}
5039
Tim Peters6d6c1a32001-08-02 04:15:00 +00005040static int
5041slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
5042{
5043 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00005044 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005045
5046 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00005047 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005048 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005049 else
Guido van Rossum2730b132001-08-28 18:22:14 +00005050 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005051 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005052 if (res == NULL)
5053 return -1;
5054 Py_DECREF(res);
5055 return 0;
5056}
5057
Guido van Rossumf5243f02008-01-01 04:06:48 +00005058static char *name_op[] = {
5059 "__lt__",
5060 "__le__",
5061 "__eq__",
5062 "__ne__",
5063 "__gt__",
5064 "__ge__",
5065};
5066
Tim Peters6d6c1a32001-08-02 04:15:00 +00005067static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00005068half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005069{
Guido van Rossumb8f63662001-08-15 23:57:02 +00005070 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00005071 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00005072
Guido van Rossum60718732001-08-28 17:47:51 +00005073 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005074 if (func == NULL) {
5075 PyErr_Clear();
5076 Py_INCREF(Py_NotImplemented);
5077 return Py_NotImplemented;
5078 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005079 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005080 if (args == NULL)
5081 res = NULL;
5082 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00005083 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005084 Py_DECREF(args);
5085 }
5086 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005087 return res;
5088}
5089
Guido van Rossumb8f63662001-08-15 23:57:02 +00005090static PyObject *
5091slot_tp_richcompare(PyObject *self, PyObject *other, int op)
5092{
5093 PyObject *res;
5094
Christian Heimes90aa7642007-12-19 02:45:37 +00005095 if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00005096 res = half_richcompare(self, other, op);
5097 if (res != Py_NotImplemented)
5098 return res;
5099 Py_DECREF(res);
5100 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005101 if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
Tim Petersf4aca752004-09-23 02:39:37 +00005102 res = half_richcompare(other, self, _Py_SwappedOp[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005103 if (res != Py_NotImplemented) {
5104 return res;
5105 }
5106 Py_DECREF(res);
5107 }
5108 Py_INCREF(Py_NotImplemented);
5109 return Py_NotImplemented;
5110}
5111
5112static PyObject *
5113slot_tp_iter(PyObject *self)
5114{
5115 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00005116 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00005117
Guido van Rossum60718732001-08-28 17:47:51 +00005118 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005119 if (func != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +00005120 PyObject *args;
5121 args = res = PyTuple_New(0);
5122 if (args != NULL) {
5123 res = PyObject_Call(func, args, NULL);
5124 Py_DECREF(args);
5125 }
5126 Py_DECREF(func);
5127 return res;
Guido van Rossumb8f63662001-08-15 23:57:02 +00005128 }
5129 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00005130 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005131 if (func == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005132 PyErr_Format(PyExc_TypeError,
5133 "'%.200s' object is not iterable",
Christian Heimes90aa7642007-12-19 02:45:37 +00005134 Py_TYPE(self)->tp_name);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005135 return NULL;
5136 }
5137 Py_DECREF(func);
5138 return PySeqIter_New(self);
5139}
Tim Peters6d6c1a32001-08-02 04:15:00 +00005140
5141static PyObject *
5142slot_tp_iternext(PyObject *self)
5143{
Guido van Rossum2730b132001-08-28 18:22:14 +00005144 static PyObject *next_str;
Georg Brandla18af4e2007-04-21 15:47:16 +00005145 return call_method(self, "__next__", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00005146}
5147
Guido van Rossum1a493502001-08-17 16:47:50 +00005148static PyObject *
5149slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5150{
Christian Heimes90aa7642007-12-19 02:45:37 +00005151 PyTypeObject *tp = Py_TYPE(self);
Guido van Rossum1a493502001-08-17 16:47:50 +00005152 PyObject *get;
5153 static PyObject *get_str = NULL;
5154
5155 if (get_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005156 get_str = PyUnicode_InternFromString("__get__");
Guido van Rossum1a493502001-08-17 16:47:50 +00005157 if (get_str == NULL)
5158 return NULL;
5159 }
5160 get = _PyType_Lookup(tp, get_str);
5161 if (get == NULL) {
5162 /* Avoid further slowdowns */
5163 if (tp->tp_descr_get == slot_tp_descr_get)
5164 tp->tp_descr_get = NULL;
5165 Py_INCREF(self);
5166 return self;
5167 }
Guido van Rossum2c252392001-08-24 10:13:31 +00005168 if (obj == NULL)
5169 obj = Py_None;
5170 if (type == NULL)
5171 type = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00005172 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
Guido van Rossum1a493502001-08-17 16:47:50 +00005173}
Tim Peters6d6c1a32001-08-02 04:15:00 +00005174
5175static int
5176slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
5177{
Guido van Rossum2c252392001-08-24 10:13:31 +00005178 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00005179 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00005180
5181 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00005182 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005183 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00005184 else
Guido van Rossum2730b132001-08-28 18:22:14 +00005185 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005186 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005187 if (res == NULL)
5188 return -1;
5189 Py_DECREF(res);
5190 return 0;
5191}
5192
5193static int
5194slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
5195{
Guido van Rossum60718732001-08-28 17:47:51 +00005196 static PyObject *init_str;
5197 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005198 PyObject *res;
5199
5200 if (meth == NULL)
5201 return -1;
5202 res = PyObject_Call(meth, args, kwds);
5203 Py_DECREF(meth);
5204 if (res == NULL)
5205 return -1;
Raymond Hettingerb67cc802005-03-03 16:45:19 +00005206 if (res != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005207 PyErr_Format(PyExc_TypeError,
5208 "__init__() should return None, not '%.200s'",
Christian Heimes90aa7642007-12-19 02:45:37 +00005209 Py_TYPE(res)->tp_name);
Raymond Hettingerb67cc802005-03-03 16:45:19 +00005210 Py_DECREF(res);
5211 return -1;
5212 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00005213 Py_DECREF(res);
5214 return 0;
5215}
5216
5217static PyObject *
5218slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5219{
Guido van Rossum7bed2132002-08-08 21:57:53 +00005220 static PyObject *new_str;
5221 PyObject *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005222 PyObject *newargs, *x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005223 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005224
Guido van Rossum7bed2132002-08-08 21:57:53 +00005225 if (new_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005226 new_str = PyUnicode_InternFromString("__new__");
Guido van Rossum7bed2132002-08-08 21:57:53 +00005227 if (new_str == NULL)
5228 return NULL;
5229 }
5230 func = PyObject_GetAttr((PyObject *)type, new_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005231 if (func == NULL)
5232 return NULL;
5233 assert(PyTuple_Check(args));
5234 n = PyTuple_GET_SIZE(args);
5235 newargs = PyTuple_New(n+1);
5236 if (newargs == NULL)
5237 return NULL;
5238 Py_INCREF(type);
5239 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
5240 for (i = 0; i < n; i++) {
5241 x = PyTuple_GET_ITEM(args, i);
5242 Py_INCREF(x);
5243 PyTuple_SET_ITEM(newargs, i+1, x);
5244 }
5245 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00005246 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005247 Py_DECREF(func);
5248 return x;
5249}
5250
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005251static void
5252slot_tp_del(PyObject *self)
5253{
5254 static PyObject *del_str = NULL;
5255 PyObject *del, *res;
5256 PyObject *error_type, *error_value, *error_traceback;
5257
5258 /* Temporarily resurrect the object. */
5259 assert(self->ob_refcnt == 0);
5260 self->ob_refcnt = 1;
5261
5262 /* Save the current exception, if any. */
5263 PyErr_Fetch(&error_type, &error_value, &error_traceback);
5264
5265 /* Execute __del__ method, if any. */
5266 del = lookup_maybe(self, "__del__", &del_str);
5267 if (del != NULL) {
5268 res = PyEval_CallObject(del, NULL);
5269 if (res == NULL)
5270 PyErr_WriteUnraisable(del);
5271 else
5272 Py_DECREF(res);
5273 Py_DECREF(del);
5274 }
5275
5276 /* Restore the saved exception. */
5277 PyErr_Restore(error_type, error_value, error_traceback);
5278
5279 /* Undo the temporary resurrection; can't use DECREF here, it would
5280 * cause a recursive call.
5281 */
5282 assert(self->ob_refcnt > 0);
5283 if (--self->ob_refcnt == 0)
5284 return; /* this is the normal path out */
5285
5286 /* __del__ resurrected it! Make it look like the original Py_DECREF
5287 * never happened.
5288 */
5289 {
Martin v. Löwis725507b2006-03-07 12:08:51 +00005290 Py_ssize_t refcnt = self->ob_refcnt;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005291 _Py_NewReference(self);
5292 self->ob_refcnt = refcnt;
5293 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005294 assert(!PyType_IS_GC(Py_TYPE(self)) ||
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005295 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
Michael W. Hudson3f3b6682004-08-03 10:21:03 +00005296 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5297 * we need to undo that. */
5298 _Py_DEC_REFTOTAL;
5299 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5300 * chain, so no more to do there.
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005301 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5302 * _Py_NewReference bumped tp_allocs: both of those need to be
5303 * undone.
5304 */
5305#ifdef COUNT_ALLOCS
Christian Heimes90aa7642007-12-19 02:45:37 +00005306 --Py_TYPE(self)->tp_frees;
5307 --Py_TYPE(self)->tp_allocs;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005308#endif
5309}
5310
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005311
5312/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
Tim Petersbf9b2442003-03-23 05:35:36 +00005313 functions. The offsets here are relative to the 'PyHeapTypeObject'
Guido van Rossume5c691a2003-03-07 15:13:17 +00005314 structure, which incorporates the additional structures used for numbers,
5315 sequences and mappings.
5316 Note that multiple names may map to the same slot (e.g. __eq__,
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005317 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00005318 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
5319 terminated with an all-zero entry. (This table is further initialized and
5320 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005321
Guido van Rossum6d204072001-10-21 00:44:31 +00005322typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005323
5324#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00005325#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005326#undef ETSLOT
5327#undef SQSLOT
5328#undef MPSLOT
5329#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00005330#undef UNSLOT
5331#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005332#undef BINSLOT
5333#undef RBINSLOT
5334
Guido van Rossum6d204072001-10-21 00:44:31 +00005335#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005336 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5337 PyDoc_STR(DOC)}
Guido van Rossumc8e56452001-10-22 00:43:43 +00005338#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5339 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005340 PyDoc_STR(DOC), FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00005341#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Guido van Rossume5c691a2003-03-07 15:13:17 +00005342 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005343 PyDoc_STR(DOC)}
Guido van Rossum6d204072001-10-21 00:44:31 +00005344#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5345 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5346#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5347 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5348#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5349 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5350#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5351 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5352 "x." NAME "() <==> " DOC)
5353#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5354 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5355 "x." NAME "(y) <==> x" DOC "y")
5356#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5357 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5358 "x." NAME "(y) <==> x" DOC "y")
5359#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5360 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5361 "x." NAME "(y) <==> y" DOC "x")
Anthony Baxter56616992005-06-03 14:12:21 +00005362#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5363 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5364 "x." NAME "(y) <==> " DOC)
5365#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5366 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5367 "x." NAME "(y) <==> " DOC)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005368
5369static slotdef slotdefs[] = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00005370 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00005371 "x.__len__() <==> len(x)"),
Armin Rigofd163f92005-12-29 15:59:19 +00005372 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5373 The logic in abstract.c always falls back to nb_add/nb_multiply in
5374 this case. Defining both the nb_* and the sq_* slots to call the
5375 user-defined methods has unexpected side-effects, as shown by
5376 test_descr.notimplemented() */
5377 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005378 "x.__add__(y) <==> x+y"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005379 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005380 "x.__mul__(n) <==> x*n"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005381 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005382 "x.__rmul__(n) <==> n*x"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005383 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
5384 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005385 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00005386 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005387 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00005388 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005389 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
5390 "x.__contains__(y) <==> y in x"),
Armin Rigofd163f92005-12-29 15:59:19 +00005391 SQSLOT("__iadd__", sq_inplace_concat, NULL,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005392 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Armin Rigofd163f92005-12-29 15:59:19 +00005393 SQSLOT("__imul__", sq_inplace_repeat, NULL,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005394 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005395
Martin v. Löwis18e16552006-02-15 17:27:45 +00005396 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00005397 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00005398 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005399 wrap_binaryfunc,
5400 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005401 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005402 wrap_objobjargproc,
5403 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005404 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005405 wrap_delitem,
5406 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005407
Guido van Rossum6d204072001-10-21 00:44:31 +00005408 BINSLOT("__add__", nb_add, slot_nb_add,
5409 "+"),
5410 RBINSLOT("__radd__", nb_add, slot_nb_add,
5411 "+"),
5412 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5413 "-"),
5414 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5415 "-"),
5416 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5417 "*"),
5418 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5419 "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005420 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5421 "%"),
5422 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5423 "%"),
Anthony Baxter56616992005-06-03 14:12:21 +00005424 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00005425 "divmod(x, y)"),
Anthony Baxter56616992005-06-03 14:12:21 +00005426 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00005427 "divmod(y, x)"),
5428 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5429 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5430 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5431 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5432 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5433 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5434 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5435 "abs(x)"),
Jack Diederich4dafcc42006-11-28 19:15:13 +00005436 UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
Guido van Rossum6d204072001-10-21 00:44:31 +00005437 "x != 0"),
5438 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5439 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5440 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5441 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5442 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5443 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5444 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5445 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5446 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5447 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5448 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005449 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5450 "int(x)"),
5451 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
5452 "long(x)"),
5453 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5454 "float(x)"),
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00005455 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005456 "x[y:z] <==> x[y.__index__():z.__index__()]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005457 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5458 wrap_binaryfunc, "+"),
5459 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5460 wrap_binaryfunc, "-"),
5461 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5462 wrap_binaryfunc, "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005463 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5464 wrap_binaryfunc, "%"),
5465 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
Guido van Rossum6e5680f2002-10-15 01:01:53 +00005466 wrap_binaryfunc, "**"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005467 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5468 wrap_binaryfunc, "<<"),
5469 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5470 wrap_binaryfunc, ">>"),
5471 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5472 wrap_binaryfunc, "&"),
5473 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5474 wrap_binaryfunc, "^"),
5475 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5476 wrap_binaryfunc, "|"),
5477 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5478 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5479 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5480 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5481 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5482 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5483 IBSLOT("__itruediv__", nb_inplace_true_divide,
5484 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005485
Guido van Rossum6d204072001-10-21 00:44:31 +00005486 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5487 "x.__str__() <==> str(x)"),
5488 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5489 "x.__repr__() <==> repr(x)"),
5490 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5491 "x.__cmp__(y) <==> cmp(x,y)"),
5492 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5493 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005494 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5495 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005496 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00005497 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5498 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5499 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5500 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5501 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5502 "x.__setattr__('name', value) <==> x.name = value"),
5503 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5504 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5505 "x.__delattr__('name') <==> del x.name"),
5506 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5507 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5508 "x.__lt__(y) <==> x<y"),
5509 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5510 "x.__le__(y) <==> x<=y"),
5511 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5512 "x.__eq__(y) <==> x==y"),
5513 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5514 "x.__ne__(y) <==> x!=y"),
5515 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5516 "x.__gt__(y) <==> x>y"),
5517 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5518 "x.__ge__(y) <==> x>=y"),
5519 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5520 "x.__iter__() <==> iter(x)"),
Georg Brandla18af4e2007-04-21 15:47:16 +00005521 TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
5522 "x.__next__() <==> next(x)"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005523 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5524 "descr.__get__(obj[, type]) -> value"),
5525 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5526 "descr.__set__(obj, value)"),
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00005527 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5528 wrap_descr_delete, "descr.__delete__(obj)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005529 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00005530 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00005531 "see x.__class__.__doc__ for signature",
5532 PyWrapperFlag_KEYWORDS),
5533 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005534 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005535 {NULL}
5536};
5537
Guido van Rossumc334df52002-04-04 23:44:47 +00005538/* Given a type pointer and an offset gotten from a slotdef entry, return a
Guido van Rossumd8faa362007-04-27 19:54:29 +00005539 pointer to the actual slot. This is not quite the same as simply adding
Guido van Rossumc334df52002-04-04 23:44:47 +00005540 the offset to the type pointer, since it takes care to indirect through the
5541 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5542 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005543static void **
Martin v. Löwis18e16552006-02-15 17:27:45 +00005544slotptr(PyTypeObject *type, int ioffset)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005545{
5546 char *ptr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005547 long offset = ioffset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005548
Guido van Rossume5c691a2003-03-07 15:13:17 +00005549 /* Note: this depends on the order of the members of PyHeapTypeObject! */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005550 assert(offset >= 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005551 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5552 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5553 ptr = (char *)type->tp_as_sequence;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005554 offset -= offsetof(PyHeapTypeObject, as_sequence);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005555 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005556 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5557 ptr = (char *)type->tp_as_mapping;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005558 offset -= offsetof(PyHeapTypeObject, as_mapping);
Guido van Rossum09638c12002-06-13 19:17:46 +00005559 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005560 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
5561 ptr = (char *)type->tp_as_number;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005562 offset -= offsetof(PyHeapTypeObject, as_number);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005563 }
5564 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005565 ptr = (char *)type;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005566 }
5567 if (ptr != NULL)
5568 ptr += offset;
5569 return (void **)ptr;
5570}
Guido van Rossumf040ede2001-08-07 16:40:56 +00005571
Guido van Rossumc334df52002-04-04 23:44:47 +00005572/* Length of array of slotdef pointers used to store slots with the
5573 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5574 the same __name__, for any __name__. Since that's a static property, it is
5575 appropriate to declare fixed-size arrays for this. */
5576#define MAX_EQUIV 10
5577
5578/* Return a slot pointer for a given name, but ONLY if the attribute has
5579 exactly one slot function. The name must be an interned string. */
5580static void **
5581resolve_slotdups(PyTypeObject *type, PyObject *name)
5582{
5583 /* XXX Maybe this could be optimized more -- but is it worth it? */
5584
5585 /* pname and ptrs act as a little cache */
5586 static PyObject *pname;
5587 static slotdef *ptrs[MAX_EQUIV];
5588 slotdef *p, **pp;
5589 void **res, **ptr;
5590
5591 if (pname != name) {
5592 /* Collect all slotdefs that match name into ptrs. */
5593 pname = name;
5594 pp = ptrs;
5595 for (p = slotdefs; p->name_strobj; p++) {
5596 if (p->name_strobj == name)
5597 *pp++ = p;
5598 }
5599 *pp = NULL;
5600 }
5601
5602 /* Look in all matching slots of the type; if exactly one of these has
Guido van Rossumd8faa362007-04-27 19:54:29 +00005603 a filled-in slot, return its value. Otherwise return NULL. */
Guido van Rossumc334df52002-04-04 23:44:47 +00005604 res = NULL;
5605 for (pp = ptrs; *pp; pp++) {
5606 ptr = slotptr(type, (*pp)->offset);
5607 if (ptr == NULL || *ptr == NULL)
5608 continue;
5609 if (res != NULL)
5610 return NULL;
5611 res = ptr;
5612 }
5613 return res;
5614}
5615
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005616/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
Guido van Rossumc334df52002-04-04 23:44:47 +00005617 does some incredibly complex thinking and then sticks something into the
5618 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5619 interests, and then stores a generic wrapper or a specific function into
5620 the slot.) Return a pointer to the next slotdef with a different offset,
5621 because that's convenient for fixup_slot_dispatchers(). */
5622static slotdef *
5623update_one_slot(PyTypeObject *type, slotdef *p)
5624{
5625 PyObject *descr;
5626 PyWrapperDescrObject *d;
5627 void *generic = NULL, *specific = NULL;
5628 int use_generic = 0;
5629 int offset = p->offset;
5630 void **ptr = slotptr(type, offset);
5631
5632 if (ptr == NULL) {
5633 do {
5634 ++p;
5635 } while (p->offset == offset);
5636 return p;
5637 }
5638 do {
5639 descr = _PyType_Lookup(type, p->name_strobj);
5640 if (descr == NULL)
5641 continue;
Christian Heimes90aa7642007-12-19 02:45:37 +00005642 if (Py_TYPE(descr) == &PyWrapperDescr_Type) {
Guido van Rossumc334df52002-04-04 23:44:47 +00005643 void **tptr = resolve_slotdups(type, p->name_strobj);
5644 if (tptr == NULL || tptr == ptr)
5645 generic = p->function;
5646 d = (PyWrapperDescrObject *)descr;
5647 if (d->d_base->wrapper == p->wrapper &&
5648 PyType_IsSubtype(type, d->d_type))
5649 {
5650 if (specific == NULL ||
5651 specific == d->d_wrapped)
5652 specific = d->d_wrapped;
5653 else
5654 use_generic = 1;
5655 }
5656 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005657 else if (Py_TYPE(descr) == &PyCFunction_Type &&
Guido van Rossum721f62e2002-08-09 02:14:34 +00005658 PyCFunction_GET_FUNCTION(descr) ==
5659 (PyCFunction)tp_new_wrapper &&
5660 strcmp(p->name, "__new__") == 0)
5661 {
5662 /* The __new__ wrapper is not a wrapper descriptor,
5663 so must be special-cased differently.
5664 If we don't do this, creating an instance will
5665 always use slot_tp_new which will look up
5666 __new__ in the MRO which will call tp_new_wrapper
5667 which will look through the base classes looking
5668 for a static base and call its tp_new (usually
5669 PyType_GenericNew), after performing various
5670 sanity checks and constructing a new argument
5671 list. Cut all that nonsense short -- this speeds
5672 up instance creation tremendously. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005673 specific = (void *)type->tp_new;
Guido van Rossum721f62e2002-08-09 02:14:34 +00005674 /* XXX I'm not 100% sure that there isn't a hole
5675 in this reasoning that requires additional
5676 sanity checks. I'll buy the first person to
5677 point out a bug in this reasoning a beer. */
5678 }
Nick Coghland1abd252008-07-15 15:46:38 +00005679 else if (descr == Py_None &&
5680 strcmp(p->name, "__hash__") == 0) {
5681 /* We specifically allow __hash__ to be set to None
5682 to prevent inheritance of the default
5683 implementation from object.__hash__ */
5684 specific = PyObject_HashNotImplemented;
5685 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005686 else {
5687 use_generic = 1;
5688 generic = p->function;
5689 }
5690 } while ((++p)->offset == offset);
5691 if (specific && !use_generic)
5692 *ptr = specific;
5693 else
5694 *ptr = generic;
5695 return p;
5696}
5697
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005698/* In the type, update the slots whose slotdefs are gathered in the pp array.
5699 This is a callback for update_subclasses(). */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005700static int
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005701update_slots_callback(PyTypeObject *type, void *data)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005702{
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005703 slotdef **pp = (slotdef **)data;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005704
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005705 for (; *pp; pp++)
Guido van Rossumc334df52002-04-04 23:44:47 +00005706 update_one_slot(type, *pp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005707 return 0;
5708}
5709
Guido van Rossumc334df52002-04-04 23:44:47 +00005710/* Comparison function for qsort() to compare slotdefs by their offset, and
5711 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005712static int
5713slotdef_cmp(const void *aa, const void *bb)
5714{
5715 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5716 int c = a->offset - b->offset;
5717 if (c != 0)
5718 return c;
5719 else
Martin v. Löwis18e16552006-02-15 17:27:45 +00005720 /* Cannot use a-b, as this gives off_t,
5721 which may lose precision when converted to int. */
5722 return (a > b) ? 1 : (a < b) ? -1 : 0;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005723}
5724
Guido van Rossumc334df52002-04-04 23:44:47 +00005725/* Initialize the slotdefs table by adding interned string objects for the
5726 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005727static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005728init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005729{
5730 slotdef *p;
5731 static int initialized = 0;
5732
5733 if (initialized)
5734 return;
5735 for (p = slotdefs; p->name; p++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005736 p->name_strobj = PyUnicode_InternFromString(p->name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005737 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00005738 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005739 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005740 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5741 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005742 initialized = 1;
5743}
5744
Guido van Rossumc334df52002-04-04 23:44:47 +00005745/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005746static int
5747update_slot(PyTypeObject *type, PyObject *name)
5748{
Guido van Rossumc334df52002-04-04 23:44:47 +00005749 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005750 slotdef *p;
5751 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005752 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005753
Christian Heimesa62da1d2008-01-12 19:39:10 +00005754 /* Clear the VALID_VERSION flag of 'type' and all its
5755 subclasses. This could possibly be unified with the
5756 update_subclasses() recursion below, but carefully:
5757 they each have their own conditions on which to stop
5758 recursing into subclasses. */
Georg Brandlf08a9dd2008-06-10 16:57:31 +00005759 PyType_Modified(type);
Christian Heimesa62da1d2008-01-12 19:39:10 +00005760
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005761 init_slotdefs();
5762 pp = ptrs;
5763 for (p = slotdefs; p->name; p++) {
5764 /* XXX assume name is interned! */
5765 if (p->name_strobj == name)
5766 *pp++ = p;
5767 }
5768 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005769 for (pp = ptrs; *pp; pp++) {
5770 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005771 offset = p->offset;
5772 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005773 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005774 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005775 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005776 if (ptrs[0] == NULL)
5777 return 0; /* Not an attribute that affects any slots */
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005778 return update_subclasses(type, name,
5779 update_slots_callback, (void *)ptrs);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005780}
5781
Guido van Rossumc334df52002-04-04 23:44:47 +00005782/* Store the proper functions in the slot dispatches at class (type)
5783 definition time, based upon which operations the class overrides in its
5784 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00005785static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005786fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005787{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005788 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005789
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005790 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00005791 for (p = slotdefs; p->name; )
5792 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005793}
Guido van Rossum705f0f52001-08-24 16:47:00 +00005794
Michael W. Hudson98bbc492002-11-26 14:47:27 +00005795static void
5796update_all_slots(PyTypeObject* type)
5797{
5798 slotdef *p;
5799
5800 init_slotdefs();
5801 for (p = slotdefs; p->name; p++) {
5802 /* update_slot returns int but can't actually fail */
5803 update_slot(type, p->name_strobj);
5804 }
5805}
5806
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005807/* recurse_down_subclasses() and update_subclasses() are mutually
5808 recursive functions to call a callback for all subclasses,
5809 but refraining from recursing into subclasses that define 'name'. */
5810
5811static int
5812update_subclasses(PyTypeObject *type, PyObject *name,
5813 update_callback callback, void *data)
5814{
5815 if (callback(type, data) < 0)
5816 return -1;
5817 return recurse_down_subclasses(type, name, callback, data);
5818}
5819
5820static int
5821recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5822 update_callback callback, void *data)
5823{
5824 PyTypeObject *subclass;
5825 PyObject *ref, *subclasses, *dict;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005826 Py_ssize_t i, n;
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005827
5828 subclasses = type->tp_subclasses;
5829 if (subclasses == NULL)
5830 return 0;
5831 assert(PyList_Check(subclasses));
5832 n = PyList_GET_SIZE(subclasses);
5833 for (i = 0; i < n; i++) {
5834 ref = PyList_GET_ITEM(subclasses, i);
5835 assert(PyWeakref_CheckRef(ref));
5836 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5837 assert(subclass != NULL);
5838 if ((PyObject *)subclass == Py_None)
5839 continue;
5840 assert(PyType_Check(subclass));
5841 /* Avoid recursing down into unaffected classes */
5842 dict = subclass->tp_dict;
5843 if (dict != NULL && PyDict_Check(dict) &&
5844 PyDict_GetItem(dict, name) != NULL)
5845 continue;
5846 if (update_subclasses(subclass, name, callback, data) < 0)
5847 return -1;
5848 }
5849 return 0;
5850}
5851
Guido van Rossum6d204072001-10-21 00:44:31 +00005852/* This function is called by PyType_Ready() to populate the type's
5853 dictionary with method descriptors for function slots. For each
Guido van Rossum09638c12002-06-13 19:17:46 +00005854 function slot (like tp_repr) that's defined in the type, one or more
5855 corresponding descriptors are added in the type's tp_dict dictionary
Guido van Rossumd8faa362007-04-27 19:54:29 +00005856 under the appropriate name (like __repr__). Some function slots
Guido van Rossum09638c12002-06-13 19:17:46 +00005857 cause more than one descriptor to be added (for example, the nb_add
5858 slot adds both __add__ and __radd__ descriptors) and some function
5859 slots compete for the same descriptor (for example both sq_item and
5860 mp_subscript generate a __getitem__ descriptor).
5861
Guido van Rossumd8faa362007-04-27 19:54:29 +00005862 In the latter case, the first slotdef entry encoutered wins. Since
Tim Petersbf9b2442003-03-23 05:35:36 +00005863 slotdef entries are sorted by the offset of the slot in the
Guido van Rossume5c691a2003-03-07 15:13:17 +00005864 PyHeapTypeObject, this gives us some control over disambiguating
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005865 between competing slots: the members of PyHeapTypeObject are listed
5866 from most general to least general, so the most general slot is
5867 preferred. In particular, because as_mapping comes before as_sequence,
5868 for a type that defines both mp_subscript and sq_item, mp_subscript
5869 wins.
Guido van Rossum09638c12002-06-13 19:17:46 +00005870
5871 This only adds new descriptors and doesn't overwrite entries in
5872 tp_dict that were previously defined. The descriptors contain a
5873 reference to the C function they must call, so that it's safe if they
5874 are copied into a subtype's __dict__ and the subtype has a different
5875 C function in its slot -- calling the method defined by the
5876 descriptor will call the C function that was used to create it,
5877 rather than the C function present in the slot when it is called.
5878 (This is important because a subtype may have a C function in the
5879 slot that calls the method from the dictionary, and we want to avoid
5880 infinite recursion here.) */
Guido van Rossum6d204072001-10-21 00:44:31 +00005881
5882static int
5883add_operators(PyTypeObject *type)
5884{
5885 PyObject *dict = type->tp_dict;
5886 slotdef *p;
5887 PyObject *descr;
5888 void **ptr;
5889
5890 init_slotdefs();
5891 for (p = slotdefs; p->name; p++) {
5892 if (p->wrapper == NULL)
5893 continue;
5894 ptr = slotptr(type, p->offset);
5895 if (!ptr || !*ptr)
5896 continue;
5897 if (PyDict_GetItem(dict, p->name_strobj))
5898 continue;
Nick Coghland1abd252008-07-15 15:46:38 +00005899 if (*ptr == PyObject_HashNotImplemented) {
5900 /* Classes may prevent the inheritance of the tp_hash
5901 slot by storing PyObject_HashNotImplemented in it. Make it
5902 visible as a None value for the __hash__ attribute. */
5903 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
5904 return -1;
5905 }
5906 else {
5907 descr = PyDescr_NewWrapper(type, p, *ptr);
5908 if (descr == NULL)
5909 return -1;
5910 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5911 return -1;
5912 Py_DECREF(descr);
5913 }
Guido van Rossum6d204072001-10-21 00:44:31 +00005914 }
5915 if (type->tp_new != NULL) {
5916 if (add_tp_new_wrapper(type) < 0)
5917 return -1;
5918 }
5919 return 0;
5920}
5921
Guido van Rossum705f0f52001-08-24 16:47:00 +00005922
5923/* Cooperative 'super' */
5924
5925typedef struct {
5926 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00005927 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005928 PyObject *obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005929 PyTypeObject *obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005930} superobject;
5931
Guido van Rossum6f799372001-09-20 20:46:19 +00005932static PyMemberDef super_members[] = {
5933 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5934 "the class invoking super()"},
5935 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5936 "the instance invoking super(); may be None"},
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005937 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005938 "the type of the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005939 {0}
5940};
5941
Guido van Rossum705f0f52001-08-24 16:47:00 +00005942static void
5943super_dealloc(PyObject *self)
5944{
5945 superobject *su = (superobject *)self;
5946
Guido van Rossum048eb752001-10-02 21:24:57 +00005947 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005948 Py_XDECREF(su->obj);
5949 Py_XDECREF(su->type);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005950 Py_XDECREF(su->obj_type);
Christian Heimes90aa7642007-12-19 02:45:37 +00005951 Py_TYPE(self)->tp_free(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005952}
5953
5954static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005955super_repr(PyObject *self)
5956{
5957 superobject *su = (superobject *)self;
5958
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005959 if (su->obj_type)
Walter Dörwald1ab83302007-05-18 17:15:44 +00005960 return PyUnicode_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005961 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005962 su->type ? su->type->tp_name : "NULL",
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005963 su->obj_type->tp_name);
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005964 else
Walter Dörwald1ab83302007-05-18 17:15:44 +00005965 return PyUnicode_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005966 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005967 su->type ? su->type->tp_name : "NULL");
5968}
5969
5970static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00005971super_getattro(PyObject *self, PyObject *name)
5972{
5973 superobject *su = (superobject *)self;
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005974 int skip = su->obj_type == NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005975
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005976 if (!skip) {
5977 /* We want __class__ to return the class of the super object
5978 (i.e. super, or a subclass), not the class of su->obj. */
Martin v. Löwis5b222132007-06-10 09:51:05 +00005979 skip = (PyUnicode_Check(name) &&
5980 PyUnicode_GET_SIZE(name) == 9 &&
5981 PyUnicode_CompareWithASCIIString(name, "__class__") == 0);
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005982 }
5983
5984 if (!skip) {
Tim Petersa91e9642001-11-14 23:32:33 +00005985 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005986 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005987 descrgetfunc f;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005988 Py_ssize_t i, n;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005989
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005990 starttype = su->obj_type;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005991 mro = starttype->tp_mro;
5992
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005993 if (mro == NULL)
5994 n = 0;
5995 else {
5996 assert(PyTuple_Check(mro));
5997 n = PyTuple_GET_SIZE(mro);
5998 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005999 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00006000 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00006001 break;
6002 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006003 i++;
6004 res = NULL;
6005 for (; i < n; i++) {
6006 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00006007 if (PyType_Check(tmp))
6008 dict = ((PyTypeObject *)tmp)->tp_dict;
Tim Petersa91e9642001-11-14 23:32:33 +00006009 else
6010 continue;
6011 res = PyDict_GetItem(dict, name);
Guido van Rossum6cc5bb62003-04-16 20:01:36 +00006012 if (res != NULL) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00006013 Py_INCREF(res);
Christian Heimes90aa7642007-12-19 02:45:37 +00006014 f = Py_TYPE(res)->tp_descr_get;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006015 if (f != NULL) {
Phillip J. Eby91a968a2004-03-25 02:19:34 +00006016 tmp = f(res,
6017 /* Only pass 'obj' param if
6018 this is instance-mode super
6019 (See SF ID #743627)
6020 */
Hye-Shik Changff365c92004-03-25 16:37:03 +00006021 (su->obj == (PyObject *)
6022 su->obj_type
Phillip J. Eby91a968a2004-03-25 02:19:34 +00006023 ? (PyObject *)NULL
6024 : su->obj),
Guido van Rossumd4641072002-04-03 02:13:37 +00006025 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00006026 Py_DECREF(res);
6027 res = tmp;
6028 }
6029 return res;
6030 }
6031 }
6032 }
6033 return PyObject_GenericGetAttr(self, name);
6034}
6035
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006036static PyTypeObject *
Guido van Rossum5b443c62001-12-03 15:38:28 +00006037supercheck(PyTypeObject *type, PyObject *obj)
6038{
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006039 /* Check that a super() call makes sense. Return a type object.
6040
6041 obj can be a new-style class, or an instance of one:
6042
Guido van Rossumd8faa362007-04-27 19:54:29 +00006043 - If it is a class, it must be a subclass of 'type'. This case is
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006044 used for class methods; the return value is obj.
6045
6046 - If it is an instance, it must be an instance of 'type'. This is
6047 the normal case; the return value is obj.__class__.
6048
6049 But... when obj is an instance, we want to allow for the case where
Christian Heimes90aa7642007-12-19 02:45:37 +00006050 Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006051 This will allow using super() with a proxy for obj.
6052 */
6053
Guido van Rossum8e80a722003-02-18 19:22:22 +00006054 /* Check for first bullet above (special case) */
6055 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
6056 Py_INCREF(obj);
6057 return (PyTypeObject *)obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006058 }
Guido van Rossum8e80a722003-02-18 19:22:22 +00006059
6060 /* Normal case */
Christian Heimes90aa7642007-12-19 02:45:37 +00006061 if (PyType_IsSubtype(Py_TYPE(obj), type)) {
6062 Py_INCREF(Py_TYPE(obj));
6063 return Py_TYPE(obj);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006064 }
6065 else {
6066 /* Try the slow way */
6067 static PyObject *class_str = NULL;
6068 PyObject *class_attr;
6069
6070 if (class_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00006071 class_str = PyUnicode_FromString("__class__");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006072 if (class_str == NULL)
6073 return NULL;
6074 }
6075
6076 class_attr = PyObject_GetAttr(obj, class_str);
6077
6078 if (class_attr != NULL &&
6079 PyType_Check(class_attr) &&
Christian Heimes90aa7642007-12-19 02:45:37 +00006080 (PyTypeObject *)class_attr != Py_TYPE(obj))
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006081 {
6082 int ok = PyType_IsSubtype(
6083 (PyTypeObject *)class_attr, type);
6084 if (ok)
6085 return (PyTypeObject *)class_attr;
6086 }
6087
6088 if (class_attr == NULL)
6089 PyErr_Clear();
6090 else
6091 Py_DECREF(class_attr);
6092 }
6093
Guido van Rossumd8faa362007-04-27 19:54:29 +00006094 PyErr_SetString(PyExc_TypeError,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006095 "super(type, obj): "
6096 "obj must be an instance or subtype of type");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006097 return NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00006098}
6099
Guido van Rossum705f0f52001-08-24 16:47:00 +00006100static PyObject *
6101super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
6102{
6103 superobject *su = (superobject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006104 superobject *newobj;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006105
6106 if (obj == NULL || obj == Py_None || su->obj != NULL) {
6107 /* Not binding to an object, or already bound */
6108 Py_INCREF(self);
6109 return self;
6110 }
Christian Heimes90aa7642007-12-19 02:45:37 +00006111 if (Py_TYPE(su) != &PySuper_Type)
Armin Rigo7726dc02005-05-15 15:32:08 +00006112 /* If su is an instance of a (strict) subclass of super,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006113 call its type */
Christian Heimes90aa7642007-12-19 02:45:37 +00006114 return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
Guido van Rossumd8faa362007-04-27 19:54:29 +00006115 su->type, obj, NULL);
Guido van Rossum5b443c62001-12-03 15:38:28 +00006116 else {
6117 /* Inline the common case */
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006118 PyTypeObject *obj_type = supercheck(su->type, obj);
6119 if (obj_type == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00006120 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006121 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006122 NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006123 if (newobj == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00006124 return NULL;
6125 Py_INCREF(su->type);
6126 Py_INCREF(obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006127 newobj->type = su->type;
6128 newobj->obj = obj;
6129 newobj->obj_type = obj_type;
6130 return (PyObject *)newobj;
Guido van Rossum5b443c62001-12-03 15:38:28 +00006131 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006132}
6133
6134static int
6135super_init(PyObject *self, PyObject *args, PyObject *kwds)
6136{
6137 superobject *su = (superobject *)self;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006138 PyTypeObject *type = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00006139 PyObject *obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006140 PyTypeObject *obj_type = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006141
Thomas Wouters89f507f2006-12-13 04:49:30 +00006142 if (!_PyArg_NoKeywords("super", kwds))
6143 return -1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006144 if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj))
Guido van Rossum705f0f52001-08-24 16:47:00 +00006145 return -1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006146
6147 if (type == NULL) {
6148 /* Call super(), without args -- fill in from __class__
6149 and first local variable on the stack. */
6150 PyFrameObject *f = PyThreadState_GET()->frame;
6151 PyCodeObject *co = f->f_code;
6152 int i, n;
6153 if (co == NULL) {
6154 PyErr_SetString(PyExc_SystemError,
6155 "super(): no code object");
6156 return -1;
6157 }
6158 if (co->co_argcount == 0) {
6159 PyErr_SetString(PyExc_SystemError,
6160 "super(): no arguments");
6161 return -1;
6162 }
6163 obj = f->f_localsplus[0];
6164 if (obj == NULL) {
6165 PyErr_SetString(PyExc_SystemError,
6166 "super(): arg[0] deleted");
6167 return -1;
6168 }
6169 if (co->co_freevars == NULL)
6170 n = 0;
6171 else {
6172 assert(PyTuple_Check(co->co_freevars));
6173 n = PyTuple_GET_SIZE(co->co_freevars);
6174 }
6175 for (i = 0; i < n; i++) {
6176 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
6177 assert(PyUnicode_Check(name));
6178 if (!PyUnicode_CompareWithASCIIString(name,
6179 "__class__")) {
6180 PyObject *cell =
6181 f->f_localsplus[co->co_nlocals + i];
6182 if (cell == NULL || !PyCell_Check(cell)) {
6183 PyErr_SetString(PyExc_SystemError,
6184 "super(): bad __class__ cell");
6185 return -1;
6186 }
6187 type = (PyTypeObject *) PyCell_GET(cell);
6188 if (type == NULL) {
6189 PyErr_SetString(PyExc_SystemError,
6190 "super(): empty __class__ cell");
6191 return -1;
6192 }
6193 if (!PyType_Check(type)) {
6194 PyErr_Format(PyExc_SystemError,
6195 "super(): __class__ is not a type (%s)",
Christian Heimes90aa7642007-12-19 02:45:37 +00006196 Py_TYPE(type)->tp_name);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006197 return -1;
6198 }
6199 break;
6200 }
6201 }
6202 if (type == NULL) {
6203 PyErr_SetString(PyExc_SystemError,
6204 "super(): __class__ cell not found");
6205 return -1;
6206 }
6207 }
6208
Guido van Rossum705f0f52001-08-24 16:47:00 +00006209 if (obj == Py_None)
6210 obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006211 if (obj != NULL) {
6212 obj_type = supercheck(type, obj);
6213 if (obj_type == NULL)
6214 return -1;
6215 Py_INCREF(obj);
6216 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006217 Py_INCREF(type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00006218 su->type = type;
6219 su->obj = obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006220 su->obj_type = obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006221 return 0;
6222}
6223
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006224PyDoc_STRVAR(super_doc,
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006225"super() -> same as super(__class__, <first argument>)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00006226"super(type) -> unbound super object\n"
6227"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00006228"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00006229"Typical use to call a cooperative superclass method:\n"
6230"class C(B):\n"
6231" def meth(self, arg):\n"
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006232" super().meth(arg)\n"
6233"This works for class methods too:\n"
6234"class C(B):\n"
6235" @classmethod\n"
6236" def cmeth(cls, arg):\n"
6237" super().cmeth(arg)\n");
Guido van Rossum705f0f52001-08-24 16:47:00 +00006238
Guido van Rossum048eb752001-10-02 21:24:57 +00006239static int
6240super_traverse(PyObject *self, visitproc visit, void *arg)
6241{
6242 superobject *su = (superobject *)self;
Guido van Rossum048eb752001-10-02 21:24:57 +00006243
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006244 Py_VISIT(su->obj);
6245 Py_VISIT(su->type);
6246 Py_VISIT(su->obj_type);
Guido van Rossum048eb752001-10-02 21:24:57 +00006247
6248 return 0;
6249}
6250
Guido van Rossum705f0f52001-08-24 16:47:00 +00006251PyTypeObject PySuper_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00006252 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00006253 "super", /* tp_name */
6254 sizeof(superobject), /* tp_basicsize */
6255 0, /* tp_itemsize */
6256 /* methods */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006257 super_dealloc, /* tp_dealloc */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006258 0, /* tp_print */
6259 0, /* tp_getattr */
6260 0, /* tp_setattr */
6261 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00006262 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006263 0, /* tp_as_number */
6264 0, /* tp_as_sequence */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006265 0, /* tp_as_mapping */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006266 0, /* tp_hash */
6267 0, /* tp_call */
6268 0, /* tp_str */
6269 super_getattro, /* tp_getattro */
6270 0, /* tp_setattro */
6271 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00006272 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
6273 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006274 super_doc, /* tp_doc */
6275 super_traverse, /* tp_traverse */
6276 0, /* tp_clear */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006277 0, /* tp_richcompare */
6278 0, /* tp_weaklistoffset */
6279 0, /* tp_iter */
6280 0, /* tp_iternext */
6281 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00006282 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006283 0, /* tp_getset */
6284 0, /* tp_base */
6285 0, /* tp_dict */
6286 super_descr_get, /* tp_descr_get */
6287 0, /* tp_descr_set */
6288 0, /* tp_dictoffset */
6289 super_init, /* tp_init */
6290 PyType_GenericAlloc, /* tp_alloc */
6291 PyType_GenericNew, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006292 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006293};