blob: be4b6f861ef7729675af771de9fed0ee5ec06771 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Type object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Guido van Rossumcd16bf62007-06-13 18:07:49 +00004#include "frameobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum9923ffe2002-06-04 19:52:53 +00007#include <ctype.h>
8
Christian Heimesa62da1d2008-01-12 19:39:10 +00009
10/* Support type attribute cache */
11
12/* The cache can keep references to the names alive for longer than
13 they normally would. This is why the maximum size is limited to
14 MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
15 strings are used as attribute names. */
16#define MCACHE_MAX_ATTR_SIZE 100
17#define MCACHE_SIZE_EXP 10
18#define MCACHE_HASH(version, name_hash) \
19 (((unsigned int)(version) * (unsigned int)(name_hash)) \
20 >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
21#define MCACHE_HASH_METHOD(type, name) \
22 MCACHE_HASH((type)->tp_version_tag, \
Georg Brandl1bcf35a2008-05-25 09:32:09 +000023 ((PyUnicodeObject *)(name))->hash)
Christian Heimesa62da1d2008-01-12 19:39:10 +000024#define MCACHE_CACHEABLE_NAME(name) \
Georg Brandl1bcf35a2008-05-25 09:32:09 +000025 PyUnicode_CheckExact(name) && \
26 PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
Christian Heimesa62da1d2008-01-12 19:39:10 +000027
28struct method_cache_entry {
29 unsigned int version;
30 PyObject *name; /* reference to exactly a str or None */
31 PyObject *value; /* borrowed */
32};
33
34static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
35static unsigned int next_version_tag = 0;
Christian Heimes26855632008-01-27 23:50:43 +000036
37unsigned int
38PyType_ClearCache(void)
39{
40 Py_ssize_t i;
41 unsigned int cur_version_tag = next_version_tag - 1;
42
43 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
44 method_cache[i].version = 0;
45 Py_CLEAR(method_cache[i].name);
46 method_cache[i].value = NULL;
47 }
48 next_version_tag = 0;
49 /* mark all version tags as invalid */
Georg Brandlf08a9dd2008-06-10 16:57:31 +000050 PyType_Modified(&PyBaseObject_Type);
Christian Heimes26855632008-01-27 23:50:43 +000051 return cur_version_tag;
52}
Christian Heimesa62da1d2008-01-12 19:39:10 +000053
Georg Brandlf08a9dd2008-06-10 16:57:31 +000054void
55PyType_Modified(PyTypeObject *type)
Christian Heimesa62da1d2008-01-12 19:39:10 +000056{
57 /* Invalidate any cached data for the specified type and all
58 subclasses. This function is called after the base
59 classes, mro, or attributes of the type are altered.
60
61 Invariants:
62
63 - Py_TPFLAGS_VALID_VERSION_TAG is never set if
64 Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
65 objects coming from non-recompiled extension modules)
66
67 - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
68 it must first be set on all super types.
69
70 This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
71 type (so it must first clear it on all subclasses). The
72 tp_version_tag value is meaningless unless this flag is set.
73 We don't assign new version tags eagerly, but only as
74 needed.
75 */
76 PyObject *raw, *ref;
77 Py_ssize_t i, n;
78
Christian Heimes412dc9c2008-01-27 18:55:54 +000079 if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
Christian Heimesa62da1d2008-01-12 19:39:10 +000080 return;
81
82 raw = type->tp_subclasses;
83 if (raw != NULL) {
84 n = PyList_GET_SIZE(raw);
85 for (i = 0; i < n; i++) {
86 ref = PyList_GET_ITEM(raw, i);
87 ref = PyWeakref_GET_OBJECT(ref);
88 if (ref != Py_None) {
Georg Brandlf08a9dd2008-06-10 16:57:31 +000089 PyType_Modified((PyTypeObject *)ref);
Christian Heimesa62da1d2008-01-12 19:39:10 +000090 }
91 }
92 }
93 type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
94}
95
96static void
97type_mro_modified(PyTypeObject *type, PyObject *bases) {
98 /*
99 Check that all base classes or elements of the mro of type are
100 able to be cached. This function is called after the base
101 classes or mro of the type are altered.
102
103 Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
104 inherits from an old-style class, either directly or if it
105 appears in the MRO of a new-style class. No support either for
106 custom MROs that include types that are not officially super
107 types.
108
109 Called from mro_internal, which will subsequently be called on
110 each subclass when their mro is recursively updated.
111 */
112 Py_ssize_t i, n;
113 int clear = 0;
114
Christian Heimes412dc9c2008-01-27 18:55:54 +0000115 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
Christian Heimesa62da1d2008-01-12 19:39:10 +0000116 return;
117
118 n = PyTuple_GET_SIZE(bases);
119 for (i = 0; i < n; i++) {
120 PyObject *b = PyTuple_GET_ITEM(bases, i);
121 PyTypeObject *cls;
122
123 if (!PyType_Check(b) ) {
124 clear = 1;
125 break;
126 }
127
128 cls = (PyTypeObject *)b;
129
130 if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
131 !PyType_IsSubtype(type, cls)) {
132 clear = 1;
133 break;
134 }
135 }
136
137 if (clear)
138 type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
139 Py_TPFLAGS_VALID_VERSION_TAG);
140}
141
142static int
143assign_version_tag(PyTypeObject *type)
144{
145 /* Ensure that the tp_version_tag is valid and set
146 Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
147 must first be done on all super classes. Return 0 if this
148 cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
149 */
150 Py_ssize_t i, n;
151 PyObject *bases;
152
153 if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
154 return 1;
155 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
156 return 0;
157 if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
158 return 0;
159
160 type->tp_version_tag = next_version_tag++;
161 /* for stress-testing: next_version_tag &= 0xFF; */
162
163 if (type->tp_version_tag == 0) {
164 /* wrap-around or just starting Python - clear the whole
165 cache by filling names with references to Py_None.
166 Values are also set to NULL for added protection, as they
167 are borrowed reference */
168 for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
169 method_cache[i].value = NULL;
170 Py_XDECREF(method_cache[i].name);
171 method_cache[i].name = Py_None;
172 Py_INCREF(Py_None);
173 }
174 /* mark all version tags as invalid */
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000175 PyType_Modified(&PyBaseObject_Type);
Christian Heimesa62da1d2008-01-12 19:39:10 +0000176 return 1;
177 }
178 bases = type->tp_bases;
179 n = PyTuple_GET_SIZE(bases);
180 for (i = 0; i < n; i++) {
181 PyObject *b = PyTuple_GET_ITEM(bases, i);
182 assert(PyType_Check(b));
183 if (!assign_version_tag((PyTypeObject *)b))
184 return 0;
185 }
186 type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
187 return 1;
188}
189
190
Guido van Rossum6f799372001-09-20 20:46:19 +0000191static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000192 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
193 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
194 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
Guido van Rossum9676b222001-08-17 20:32:36 +0000195 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000196 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
197 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
198 {"__dictoffset__", T_LONG,
199 offsetof(PyTypeObject, tp_dictoffset), READONLY},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
201 {0}
202};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +0000205type_name(PyTypeObject *type, void *context)
206{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000207 const char *s;
Guido van Rossumc3542212001-08-16 09:18:56 +0000208
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000209 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Guido van Rossume5c691a2003-03-07 15:13:17 +0000210 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
Tim Petersea7f75d2002-12-07 21:39:16 +0000211
Georg Brandlc255c7b2006-02-20 22:27:28 +0000212 Py_INCREF(et->ht_name);
213 return et->ht_name;
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000214 }
215 else {
216 s = strrchr(type->tp_name, '.');
217 if (s == NULL)
218 s = type->tp_name;
219 else
220 s++;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000221 return PyUnicode_FromString(s);
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000222 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000223}
224
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000225static int
226type_set_name(PyTypeObject *type, PyObject *value, void *context)
227{
Guido van Rossume5c691a2003-03-07 15:13:17 +0000228 PyHeapTypeObject* et;
Neal Norwitz80e7f272007-08-26 06:45:23 +0000229 char *tp_name;
Guido van Rossume845c0f2007-11-02 23:07:07 +0000230 PyObject *tmp;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000231
232 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
233 PyErr_Format(PyExc_TypeError,
234 "can't set %s.__name__", type->tp_name);
235 return -1;
236 }
237 if (!value) {
238 PyErr_Format(PyExc_TypeError,
239 "can't delete %s.__name__", type->tp_name);
240 return -1;
241 }
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000242 if (!PyUnicode_Check(value)) {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000243 PyErr_Format(PyExc_TypeError,
244 "can only assign string to %s.__name__, not '%s'",
Christian Heimes90aa7642007-12-19 02:45:37 +0000245 type->tp_name, Py_TYPE(value)->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000246 return -1;
247 }
Guido van Rossume845c0f2007-11-02 23:07:07 +0000248
249 /* Check absence of null characters */
250 tmp = PyUnicode_FromStringAndSize("\0", 1);
251 if (tmp == NULL)
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000252 return -1;
Guido van Rossume845c0f2007-11-02 23:07:07 +0000253 if (PyUnicode_Contains(value, tmp) != 0) {
254 Py_DECREF(tmp);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000255 PyErr_Format(PyExc_ValueError,
256 "__name__ must not contain null bytes");
257 return -1;
258 }
Guido van Rossume845c0f2007-11-02 23:07:07 +0000259 Py_DECREF(tmp);
260
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000261 tp_name = _PyUnicode_AsString(value);
Guido van Rossume845c0f2007-11-02 23:07:07 +0000262 if (tp_name == NULL)
263 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000264
Guido van Rossume5c691a2003-03-07 15:13:17 +0000265 et = (PyHeapTypeObject*)type;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000266
267 Py_INCREF(value);
268
Georg Brandlc255c7b2006-02-20 22:27:28 +0000269 Py_DECREF(et->ht_name);
270 et->ht_name = value;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000271
Neal Norwitz80e7f272007-08-26 06:45:23 +0000272 type->tp_name = tp_name;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000273
274 return 0;
275}
276
Guido van Rossumc3542212001-08-16 09:18:56 +0000277static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000278type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000279{
Guido van Rossumc3542212001-08-16 09:18:56 +0000280 PyObject *mod;
281 char *s;
282
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000283 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
284 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Anthony Baxter3ecdb252004-06-11 14:41:18 +0000285 if (!mod) {
286 PyErr_Format(PyExc_AttributeError, "__module__");
287 return 0;
288 }
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000289 Py_XINCREF(mod);
Guido van Rossumc3542212001-08-16 09:18:56 +0000290 return mod;
291 }
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000292 else {
293 s = strrchr(type->tp_name, '.');
294 if (s != NULL)
Martin v. Löwis5b222132007-06-10 09:51:05 +0000295 return PyUnicode_FromStringAndSize(
Thomas Wouters89f507f2006-12-13 04:49:30 +0000296 type->tp_name, (Py_ssize_t)(s - type->tp_name));
Georg Brandl1a3284e2007-12-02 09:40:06 +0000297 return PyUnicode_FromString("builtins");
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000298 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000299}
300
Guido van Rossum3926a632001-09-25 16:25:58 +0000301static int
302type_set_module(PyTypeObject *type, PyObject *value, void *context)
303{
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000304 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
Guido van Rossum3926a632001-09-25 16:25:58 +0000305 PyErr_Format(PyExc_TypeError,
306 "can't set %s.__module__", type->tp_name);
307 return -1;
308 }
309 if (!value) {
310 PyErr_Format(PyExc_TypeError,
311 "can't delete %s.__module__", type->tp_name);
312 return -1;
313 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000314
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000315 PyType_Modified(type);
Christian Heimesa62da1d2008-01-12 19:39:10 +0000316
Guido van Rossum3926a632001-09-25 16:25:58 +0000317 return PyDict_SetItemString(type->tp_dict, "__module__", value);
318}
319
Tim Peters6d6c1a32001-08-02 04:15:00 +0000320static PyObject *
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000321type_abstractmethods(PyTypeObject *type, void *context)
322{
323 PyObject *mod = PyDict_GetItemString(type->tp_dict,
324 "__abstractmethods__");
325 if (!mod) {
326 PyErr_Format(PyExc_AttributeError, "__abstractmethods__");
327 return NULL;
328 }
329 Py_XINCREF(mod);
330 return mod;
331}
332
333static int
334type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
335{
336 /* __abstractmethods__ should only be set once on a type, in
337 abc.ABCMeta.__new__, so this function doesn't do anything
338 special to update subclasses.
339 */
340 int res = PyDict_SetItemString(type->tp_dict,
341 "__abstractmethods__", value);
342 if (res == 0) {
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000343 PyType_Modified(type);
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000344 if (value && PyObject_IsTrue(value)) {
345 type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
346 }
347 else {
348 type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
349 }
350 }
351 return res;
352}
353
354static PyObject *
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000355type_get_bases(PyTypeObject *type, void *context)
356{
357 Py_INCREF(type->tp_bases);
358 return type->tp_bases;
359}
360
361static PyTypeObject *best_base(PyObject *);
362static int mro_internal(PyTypeObject *);
363static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
364static int add_subclass(PyTypeObject*, PyTypeObject*);
365static void remove_subclass(PyTypeObject *, PyTypeObject *);
366static void update_all_slots(PyTypeObject *);
367
Guido van Rossum8d24ee92003-03-24 23:49:49 +0000368typedef int (*update_callback)(PyTypeObject *, void *);
369static int update_subclasses(PyTypeObject *type, PyObject *name,
370 update_callback callback, void *data);
371static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
372 update_callback callback, void *data);
373
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000374static int
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000375mro_subclasses(PyTypeObject *type, PyObject* temp)
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000376{
377 PyTypeObject *subclass;
378 PyObject *ref, *subclasses, *old_mro;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379 Py_ssize_t i, n;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000380
381 subclasses = type->tp_subclasses;
382 if (subclasses == NULL)
383 return 0;
384 assert(PyList_Check(subclasses));
385 n = PyList_GET_SIZE(subclasses);
386 for (i = 0; i < n; i++) {
387 ref = PyList_GET_ITEM(subclasses, i);
388 assert(PyWeakref_CheckRef(ref));
389 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
390 assert(subclass != NULL);
391 if ((PyObject *)subclass == Py_None)
392 continue;
393 assert(PyType_Check(subclass));
394 old_mro = subclass->tp_mro;
395 if (mro_internal(subclass) < 0) {
396 subclass->tp_mro = old_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000397 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000398 }
399 else {
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000400 PyObject* tuple;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000401 tuple = PyTuple_Pack(2, subclass, old_mro);
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000402 Py_DECREF(old_mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000403 if (!tuple)
404 return -1;
405 if (PyList_Append(temp, tuple) < 0)
406 return -1;
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000407 Py_DECREF(tuple);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000408 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000409 if (mro_subclasses(subclass, temp) < 0)
410 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000411 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000412 return 0;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000413}
414
415static int
416type_set_bases(PyTypeObject *type, PyObject *value, void *context)
417{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000418 Py_ssize_t i;
419 int r = 0;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000420 PyObject *ob, *temp;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000421 PyTypeObject *new_base, *old_base;
422 PyObject *old_bases, *old_mro;
423
424 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
425 PyErr_Format(PyExc_TypeError,
426 "can't set %s.__bases__", type->tp_name);
427 return -1;
428 }
429 if (!value) {
430 PyErr_Format(PyExc_TypeError,
431 "can't delete %s.__bases__", type->tp_name);
432 return -1;
433 }
434 if (!PyTuple_Check(value)) {
435 PyErr_Format(PyExc_TypeError,
436 "can only assign tuple to %s.__bases__, not %s",
Christian Heimes90aa7642007-12-19 02:45:37 +0000437 type->tp_name, Py_TYPE(value)->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000438 return -1;
439 }
Guido van Rossum3bbc0ee2002-12-13 17:49:38 +0000440 if (PyTuple_GET_SIZE(value) == 0) {
441 PyErr_Format(PyExc_TypeError,
442 "can only assign non-empty tuple to %s.__bases__, not ()",
443 type->tp_name);
444 return -1;
445 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000446 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
447 ob = PyTuple_GET_ITEM(value, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +0000448 if (!PyType_Check(ob)) {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000449 PyErr_Format(
450 PyExc_TypeError,
451 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
Christian Heimes90aa7642007-12-19 02:45:37 +0000452 type->tp_name, Py_TYPE(ob)->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000453 return -1;
454 }
Michael W. Hudsoncaf17be2002-11-27 10:24:44 +0000455 if (PyType_Check(ob)) {
456 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
457 PyErr_SetString(PyExc_TypeError,
458 "a __bases__ item causes an inheritance cycle");
459 return -1;
460 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000461 }
462 }
463
464 new_base = best_base(value);
465
466 if (!new_base) {
467 return -1;
468 }
469
470 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
471 return -1;
472
473 Py_INCREF(new_base);
474 Py_INCREF(value);
475
476 old_bases = type->tp_bases;
477 old_base = type->tp_base;
478 old_mro = type->tp_mro;
479
480 type->tp_bases = value;
481 type->tp_base = new_base;
482
483 if (mro_internal(type) < 0) {
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000484 goto bail;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000485 }
486
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000487 temp = PyList_New(0);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000488 if (!temp)
489 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000490
491 r = mro_subclasses(type, temp);
492
493 if (r < 0) {
494 for (i = 0; i < PyList_Size(temp); i++) {
495 PyTypeObject* cls;
496 PyObject* mro;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000497 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
498 "", 2, 2, &cls, &mro);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000499 Py_INCREF(mro);
500 ob = cls->tp_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000501 cls->tp_mro = mro;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000502 Py_DECREF(ob);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000503 }
504 Py_DECREF(temp);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000505 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000506 }
507
508 Py_DECREF(temp);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000509
510 /* any base that was in __bases__ but now isn't, we
Raymond Hettingera8285862002-12-14 17:17:56 +0000511 need to remove |type| from its tp_subclasses.
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000512 conversely, any class now in __bases__ that wasn't
Raymond Hettingera8285862002-12-14 17:17:56 +0000513 needs to have |type| added to its subclasses. */
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000514
515 /* for now, sod that: just remove from all old_bases,
516 add to all new_bases */
517
518 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
519 ob = PyTuple_GET_ITEM(old_bases, i);
520 if (PyType_Check(ob)) {
521 remove_subclass(
522 (PyTypeObject*)ob, type);
523 }
524 }
525
526 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
527 ob = PyTuple_GET_ITEM(value, i);
528 if (PyType_Check(ob)) {
529 if (add_subclass((PyTypeObject*)ob, type) < 0)
530 r = -1;
531 }
532 }
533
534 update_all_slots(type);
535
536 Py_DECREF(old_bases);
537 Py_DECREF(old_base);
538 Py_DECREF(old_mro);
539
540 return r;
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000541
542 bail:
Michael W. Hudsone723e452003-08-07 14:58:10 +0000543 Py_DECREF(type->tp_bases);
544 Py_DECREF(type->tp_base);
545 if (type->tp_mro != old_mro) {
546 Py_DECREF(type->tp_mro);
547 }
548
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000549 type->tp_bases = old_bases;
550 type->tp_base = old_base;
551 type->tp_mro = old_mro;
Tim Petersea7f75d2002-12-07 21:39:16 +0000552
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000553 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000554}
555
556static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000557type_dict(PyTypeObject *type, void *context)
558{
559 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 Py_INCREF(Py_None);
561 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000562 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000563 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000564}
565
Tim Peters24008312002-03-17 18:56:20 +0000566static PyObject *
567type_get_doc(PyTypeObject *type, void *context)
568{
569 PyObject *result;
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000570 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Neal Norwitza369c5a2007-08-25 07:41:59 +0000571 return PyUnicode_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +0000572 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000573 if (result == NULL) {
574 result = Py_None;
575 Py_INCREF(result);
576 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000577 else if (Py_TYPE(result)->tp_descr_get) {
578 result = Py_TYPE(result)->tp_descr_get(result, NULL,
Tim Peters2b858972002-04-18 04:12:28 +0000579 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000580 }
581 else {
582 Py_INCREF(result);
583 }
Tim Peters24008312002-03-17 18:56:20 +0000584 return result;
585}
586
Antoine Pitrouec569b72008-08-26 22:40:48 +0000587static PyObject *
588type___instancecheck__(PyObject *type, PyObject *inst)
589{
590 switch (_PyObject_RealIsInstance(inst, type)) {
591 case -1:
592 return NULL;
593 case 0:
594 Py_RETURN_FALSE;
595 default:
596 Py_RETURN_TRUE;
597 }
598}
599
600
601static PyObject *
Antoine Pitrouec569b72008-08-26 22:40:48 +0000602type___subclasscheck__(PyObject *type, PyObject *inst)
603{
604 switch (_PyObject_RealIsSubclass(inst, type)) {
605 case -1:
606 return NULL;
607 case 0:
608 Py_RETURN_FALSE;
609 default:
610 Py_RETURN_TRUE;
611 }
612}
613
Antoine Pitrouec569b72008-08-26 22:40:48 +0000614
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000615static PyGetSetDef type_getsets[] = {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000616 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
617 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000618 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000619 {"__abstractmethods__", (getter)type_abstractmethods,
620 (setter)type_set_abstractmethods, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000621 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000622 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000623 {0}
624};
625
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000627type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000629 PyObject *mod, *name, *rtn;
Guido van Rossumc3542212001-08-16 09:18:56 +0000630
631 mod = type_module(type, NULL);
632 if (mod == NULL)
633 PyErr_Clear();
Martin v. Löwis5b222132007-06-10 09:51:05 +0000634 else if (!PyUnicode_Check(mod)) {
Guido van Rossumc3542212001-08-16 09:18:56 +0000635 Py_DECREF(mod);
636 mod = NULL;
637 }
638 name = type_name(type, NULL);
639 if (name == NULL)
640 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000641
Georg Brandl1a3284e2007-12-02 09:40:06 +0000642 if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
Martin v. Löwis250ad612008-04-07 05:43:42 +0000643 rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
Guido van Rossumc3542212001-08-16 09:18:56 +0000644 else
Martin v. Löwis250ad612008-04-07 05:43:42 +0000645 rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000646
Guido van Rossumc3542212001-08-16 09:18:56 +0000647 Py_XDECREF(mod);
648 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000649 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650}
651
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652static PyObject *
653type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
654{
655 PyObject *obj;
656
657 if (type->tp_new == NULL) {
658 PyErr_Format(PyExc_TypeError,
659 "cannot create '%.100s' instances",
660 type->tp_name);
661 return NULL;
662 }
663
Tim Peters3f996e72001-09-13 19:18:27 +0000664 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000665 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000666 /* Ugly exception: when the call was type(something),
667 don't call tp_init on the result. */
668 if (type == &PyType_Type &&
669 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
670 (kwds == NULL ||
671 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
672 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000673 /* If the returned object is not an instance of type,
674 it won't be initialized. */
Christian Heimes90aa7642007-12-19 02:45:37 +0000675 if (!PyType_IsSubtype(Py_TYPE(obj), type))
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000676 return obj;
Christian Heimes90aa7642007-12-19 02:45:37 +0000677 type = Py_TYPE(obj);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000678 if (type->tp_init != NULL &&
Tim Peters6d6c1a32001-08-02 04:15:00 +0000679 type->tp_init(obj, args, kwds) < 0) {
680 Py_DECREF(obj);
681 obj = NULL;
682 }
683 }
684 return obj;
685}
686
687PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000688PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000690 PyObject *obj;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000691 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
692 /* note that we need to add one, for the sentinel */
Tim Peters406fe3b2001-10-06 19:04:01 +0000693
694 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000695 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000696 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000697 obj = (PyObject *)PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000698
Neil Schemenauerc806c882001-08-29 23:54:54 +0000699 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000700 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000701
Neil Schemenauerc806c882001-08-29 23:54:54 +0000702 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000703
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
705 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000706
Tim Peters6d6c1a32001-08-02 04:15:00 +0000707 if (type->tp_itemsize == 0)
708 PyObject_INIT(obj, type);
709 else
710 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000711
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000713 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000714 return obj;
715}
716
717PyObject *
718PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
719{
720 return type->tp_alloc(type, 0);
721}
722
Guido van Rossum9475a232001-10-05 20:51:39 +0000723/* Helpers for subtyping */
724
725static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000726traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
727{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000728 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000729 PyMemberDef *mp;
730
Christian Heimes90aa7642007-12-19 02:45:37 +0000731 n = Py_SIZE(type);
Guido van Rossume5c691a2003-03-07 15:13:17 +0000732 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000733 for (i = 0; i < n; i++, mp++) {
734 if (mp->type == T_OBJECT_EX) {
735 char *addr = (char *)self + mp->offset;
736 PyObject *obj = *(PyObject **)addr;
737 if (obj != NULL) {
738 int err = visit(obj, arg);
739 if (err)
740 return err;
741 }
742 }
743 }
744 return 0;
745}
746
747static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000748subtype_traverse(PyObject *self, visitproc visit, void *arg)
749{
750 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000751 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000752
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000753 /* Find the nearest base with a different tp_traverse,
754 and traverse slots while we're at it */
Christian Heimes90aa7642007-12-19 02:45:37 +0000755 type = Py_TYPE(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000756 base = type;
757 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000758 if (Py_SIZE(base)) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000759 int err = traverse_slots(base, self, visit, arg);
760 if (err)
761 return err;
762 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000763 base = base->tp_base;
764 assert(base);
765 }
766
767 if (type->tp_dictoffset != base->tp_dictoffset) {
768 PyObject **dictptr = _PyObject_GetDictPtr(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769 if (dictptr && *dictptr)
770 Py_VISIT(*dictptr);
Guido van Rossum9475a232001-10-05 20:51:39 +0000771 }
772
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossuma3862092002-06-10 15:24:42 +0000774 /* For a heaptype, the instances count as references
Guido van Rossumd8faa362007-04-27 19:54:29 +0000775 to the type. Traverse the type so the collector
Guido van Rossuma3862092002-06-10 15:24:42 +0000776 can find cycles involving this link. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000777 Py_VISIT(type);
Guido van Rossuma3862092002-06-10 15:24:42 +0000778
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000779 if (basetraverse)
780 return basetraverse(self, visit, arg);
781 return 0;
782}
783
784static void
785clear_slots(PyTypeObject *type, PyObject *self)
786{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000787 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000788 PyMemberDef *mp;
789
Christian Heimes90aa7642007-12-19 02:45:37 +0000790 n = Py_SIZE(type);
Guido van Rossume5c691a2003-03-07 15:13:17 +0000791 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000792 for (i = 0; i < n; i++, mp++) {
793 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
794 char *addr = (char *)self + mp->offset;
795 PyObject *obj = *(PyObject **)addr;
796 if (obj != NULL) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000797 *(PyObject **)addr = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798 Py_DECREF(obj);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000799 }
800 }
801 }
802}
803
804static int
805subtype_clear(PyObject *self)
806{
807 PyTypeObject *type, *base;
808 inquiry baseclear;
809
810 /* Find the nearest base with a different tp_clear
811 and clear slots while we're at it */
Christian Heimes90aa7642007-12-19 02:45:37 +0000812 type = Py_TYPE(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000813 base = type;
814 while ((baseclear = base->tp_clear) == subtype_clear) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000815 if (Py_SIZE(base))
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000816 clear_slots(base, self);
817 base = base->tp_base;
818 assert(base);
819 }
820
Guido van Rossuma3862092002-06-10 15:24:42 +0000821 /* There's no need to clear the instance dict (if any);
822 the collector will call its tp_clear handler. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000823
824 if (baseclear)
825 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000826 return 0;
827}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000828
829static void
830subtype_dealloc(PyObject *self)
831{
Guido van Rossum14227b42001-12-06 02:35:58 +0000832 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000833 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000834
Guido van Rossum22b13872002-08-06 21:41:44 +0000835 /* Extract the type; we expect it to be a heap type */
Christian Heimes90aa7642007-12-19 02:45:37 +0000836 type = Py_TYPE(self);
Guido van Rossum22b13872002-08-06 21:41:44 +0000837 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000838
Guido van Rossum22b13872002-08-06 21:41:44 +0000839 /* Test whether the type has GC exactly once */
840
841 if (!PyType_IS_GC(type)) {
842 /* It's really rare to find a dynamic type that doesn't have
843 GC; it can only happen when deriving from 'object' and not
844 adding any slots or instance variables. This allows
845 certain simplifications: there's no need to call
846 clear_slots(), or DECREF the dict, or clear weakrefs. */
847
848 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000849 if (type->tp_del) {
850 type->tp_del(self);
851 if (self->ob_refcnt > 0)
852 return;
853 }
Guido van Rossum22b13872002-08-06 21:41:44 +0000854
855 /* Find the nearest base with a different tp_dealloc */
856 base = type;
857 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000858 assert(Py_SIZE(base) == 0);
Guido van Rossum22b13872002-08-06 21:41:44 +0000859 base = base->tp_base;
860 assert(base);
861 }
862
Benjamin Peterson193152c2009-04-25 01:08:45 +0000863 /* Extract the type again; tp_del may have changed it */
864 type = Py_TYPE(self);
865
Guido van Rossum22b13872002-08-06 21:41:44 +0000866 /* Call the base tp_dealloc() */
867 assert(basedealloc);
868 basedealloc(self);
869
870 /* Can't reference self beyond this point */
871 Py_DECREF(type);
872
873 /* Done */
874 return;
875 }
876
877 /* We get here only if the type has GC */
878
879 /* UnTrack and re-Track around the trashcan macro, alas */
Andrew M. Kuchlingc9172d32003-02-06 15:22:49 +0000880 /* See explanation at end of function for full disclosure */
Guido van Rossum0906e072002-08-07 20:42:09 +0000881 PyObject_GC_UnTrack(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000882 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000883 Py_TRASHCAN_SAFE_BEGIN(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000884 --_PyTrash_delete_nesting;
Tim Petersf7f9e992003-11-13 21:59:32 +0000885 /* DO NOT restore GC tracking at this point. weakref callbacks
886 * (if any, and whether directly here or indirectly in something we
887 * call) may trigger GC, and if self is tracked at that point, it
888 * will look like trash to GC and GC will try to delete self again.
Tim Petersadd09b42003-11-12 20:43:28 +0000889 */
Guido van Rossum22b13872002-08-06 21:41:44 +0000890
Guido van Rossum59195fd2003-06-13 20:54:40 +0000891 /* Find the nearest base with a different tp_dealloc */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000892 base = type;
893 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000894 base = base->tp_base;
895 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000896 }
897
Guido van Rossumd8faa362007-04-27 19:54:29 +0000898 /* If we added a weaklist, we clear it. Do this *before* calling
Guido van Rossum59195fd2003-06-13 20:54:40 +0000899 the finalizer (__del__), clearing slots, or clearing the instance
900 dict. */
901
Guido van Rossum1987c662003-05-29 14:29:23 +0000902 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
903 PyObject_ClearWeakRefs(self);
904
905 /* Maybe call finalizer; exit early if resurrected */
906 if (type->tp_del) {
Tim Petersf7f9e992003-11-13 21:59:32 +0000907 _PyObject_GC_TRACK(self);
Guido van Rossum1987c662003-05-29 14:29:23 +0000908 type->tp_del(self);
909 if (self->ob_refcnt > 0)
Tim Petersf7f9e992003-11-13 21:59:32 +0000910 goto endlabel; /* resurrected */
911 else
912 _PyObject_GC_UNTRACK(self);
Thomas Woutersb2137042007-02-01 18:02:27 +0000913 /* New weakrefs could be created during the finalizer call.
914 If this occurs, clear them out without calling their
915 finalizers since they might rely on part of the object
916 being finalized that has already been destroyed. */
917 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
918 /* Modeled after GET_WEAKREFS_LISTPTR() */
919 PyWeakReference **list = (PyWeakReference **) \
920 PyObject_GET_WEAKREFS_LISTPTR(self);
921 while (*list)
922 _PyWeakref_ClearRef(*list);
923 }
Guido van Rossum1987c662003-05-29 14:29:23 +0000924 }
925
Guido van Rossum59195fd2003-06-13 20:54:40 +0000926 /* Clear slots up to the nearest base with a different tp_dealloc */
927 base = type;
928 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000929 if (Py_SIZE(base))
Guido van Rossum59195fd2003-06-13 20:54:40 +0000930 clear_slots(base, self);
931 base = base->tp_base;
932 assert(base);
933 }
934
Tim Peters6d6c1a32001-08-02 04:15:00 +0000935 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000936 if (type->tp_dictoffset && !base->tp_dictoffset) {
937 PyObject **dictptr = _PyObject_GetDictPtr(self);
938 if (dictptr != NULL) {
939 PyObject *dict = *dictptr;
940 if (dict != NULL) {
941 Py_DECREF(dict);
942 *dictptr = NULL;
943 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000944 }
945 }
946
Benjamin Peterson193152c2009-04-25 01:08:45 +0000947 /* Extract the type again; tp_del may have changed it */
948 type = Py_TYPE(self);
949
Tim Peters0bd743c2003-11-13 22:50:00 +0000950 /* Call the base tp_dealloc(); first retrack self if
951 * basedealloc knows about gc.
952 */
953 if (PyType_IS_GC(base))
954 _PyObject_GC_TRACK(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000955 assert(basedealloc);
956 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957
958 /* Can't reference self beyond this point */
Guido van Rossum22b13872002-08-06 21:41:44 +0000959 Py_DECREF(type);
960
Guido van Rossum0906e072002-08-07 20:42:09 +0000961 endlabel:
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000962 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000963 Py_TRASHCAN_SAFE_END(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000964 --_PyTrash_delete_nesting;
965
966 /* Explanation of the weirdness around the trashcan macros:
967
968 Q. What do the trashcan macros do?
969
970 A. Read the comment titled "Trashcan mechanism" in object.h.
971 For one, this explains why there must be a call to GC-untrack
Guido van Rossumd8faa362007-04-27 19:54:29 +0000972 before the trashcan begin macro. Without understanding the
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000973 trashcan code, the answers to the following questions don't make
974 sense.
975
976 Q. Why do we GC-untrack before the trashcan and then immediately
977 GC-track again afterward?
978
979 A. In the case that the base class is GC-aware, the base class
Guido van Rossumd8faa362007-04-27 19:54:29 +0000980 probably GC-untracks the object. If it does that using the
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000981 UNTRACK macro, this will crash when the object is already
982 untracked. Because we don't know what the base class does, the
983 only safe thing is to make sure the object is tracked when we
984 call the base class dealloc. But... The trashcan begin macro
985 requires that the object is *untracked* before it is called. So
986 the dance becomes:
987
Guido van Rossumd8faa362007-04-27 19:54:29 +0000988 GC untrack
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000989 trashcan begin
990 GC track
991
Guido van Rossumd8faa362007-04-27 19:54:29 +0000992 Q. Why did the last question say "immediately GC-track again"?
993 It's nowhere near immediately.
Tim Petersf7f9e992003-11-13 21:59:32 +0000994
Guido van Rossumd8faa362007-04-27 19:54:29 +0000995 A. Because the code *used* to re-track immediately. Bad Idea.
996 self has a refcount of 0, and if gc ever gets its hands on it
997 (which can happen if any weakref callback gets invoked), it
998 looks like trash to gc too, and gc also tries to delete self
999 then. But we're already deleting self. Double dealloction is
1000 a subtle disaster.
Tim Petersf7f9e992003-11-13 21:59:32 +00001001
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001002 Q. Why the bizarre (net-zero) manipulation of
1003 _PyTrash_delete_nesting around the trashcan macros?
1004
1005 A. Some base classes (e.g. list) also use the trashcan mechanism.
1006 The following scenario used to be possible:
1007
1008 - suppose the trashcan level is one below the trashcan limit
1009
1010 - subtype_dealloc() is called
1011
1012 - the trashcan limit is not yet reached, so the trashcan level
Guido van Rossumd8faa362007-04-27 19:54:29 +00001013 is incremented and the code between trashcan begin and end is
1014 executed
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001015
1016 - this destroys much of the object's contents, including its
Guido van Rossumd8faa362007-04-27 19:54:29 +00001017 slots and __dict__
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001018
1019 - basedealloc() is called; this is really list_dealloc(), or
Guido van Rossumd8faa362007-04-27 19:54:29 +00001020 some other type which also uses the trashcan macros
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001021
1022 - the trashcan limit is now reached, so the object is put on the
Guido van Rossumd8faa362007-04-27 19:54:29 +00001023 trashcan's to-be-deleted-later list
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001024
1025 - basedealloc() returns
1026
1027 - subtype_dealloc() decrefs the object's type
1028
1029 - subtype_dealloc() returns
1030
1031 - later, the trashcan code starts deleting the objects from its
Guido van Rossumd8faa362007-04-27 19:54:29 +00001032 to-be-deleted-later list
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001033
1034 - subtype_dealloc() is called *AGAIN* for the same object
1035
1036 - at the very least (if the destroyed slots and __dict__ don't
Guido van Rossumd8faa362007-04-27 19:54:29 +00001037 cause problems) the object's type gets decref'ed a second
1038 time, which is *BAD*!!!
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001039
1040 The remedy is to make sure that if the code between trashcan
1041 begin and end in subtype_dealloc() is called, the code between
1042 trashcan begin and end in basedealloc() will also be called.
1043 This is done by decrementing the level after passing into the
1044 trashcan block, and incrementing it just before leaving the
1045 block.
1046
1047 But now it's possible that a chain of objects consisting solely
1048 of objects whose deallocator is subtype_dealloc() will defeat
1049 the trashcan mechanism completely: the decremented level means
Guido van Rossumd8faa362007-04-27 19:54:29 +00001050 that the effective level never reaches the limit. Therefore, we
Guido van Rossumce8bcd82003-02-05 22:39:45 +00001051 *increment* the level *before* entering the trashcan block, and
1052 matchingly decrement it after leaving. This means the trashcan
1053 code will trigger a little early, but that's no big deal.
1054
1055 Q. Are there any live examples of code in need of all this
1056 complexity?
1057
1058 A. Yes. See SF bug 668433 for code that crashed (when Python was
1059 compiled in debug mode) before the trashcan level manipulations
1060 were added. For more discussion, see SF patches 581742, 575073
1061 and bug 574207.
1062 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001063}
1064
Jeremy Hylton938ace62002-07-17 16:30:39 +00001065static PyTypeObject *solid_base(PyTypeObject *type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001066
Tim Peters6d6c1a32001-08-02 04:15:00 +00001067/* type test with subclassing support */
1068
1069int
1070PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
1071{
1072 PyObject *mro;
1073
1074 mro = a->tp_mro;
1075 if (mro != NULL) {
1076 /* Deal with multiple inheritance without recursion
1077 by walking the MRO tuple */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001078 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079 assert(PyTuple_Check(mro));
1080 n = PyTuple_GET_SIZE(mro);
1081 for (i = 0; i < n; i++) {
1082 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
1083 return 1;
1084 }
1085 return 0;
1086 }
1087 else {
1088 /* a is not completely initilized yet; follow tp_base */
1089 do {
1090 if (a == b)
1091 return 1;
1092 a = a->tp_base;
1093 } while (a != NULL);
1094 return b == &PyBaseObject_Type;
1095 }
1096}
1097
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001098/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +00001099 without looking in the instance dictionary
1100 (so we can't use PyObject_GetAttr) but still binding
Guido van Rossumd8faa362007-04-27 19:54:29 +00001101 it to the instance. The arguments are the object,
Guido van Rossum60718732001-08-28 17:47:51 +00001102 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001103 static variable used to cache the interned Python string.
1104
1105 Two variants:
1106
1107 - lookup_maybe() returns NULL without raising an exception
1108 when the _PyType_Lookup() call fails;
1109
1110 - lookup_method() always raises an exception upon errors.
Benjamin Peterson224205f2009-05-08 03:25:19 +00001111
1112 - _PyObject_LookupSpecial() exported for the benefit of other places.
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001113*/
Guido van Rossum60718732001-08-28 17:47:51 +00001114
1115static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001116lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +00001117{
1118 PyObject *res;
1119
1120 if (*attrobj == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001121 *attrobj = PyUnicode_InternFromString(attrstr);
Guido van Rossum60718732001-08-28 17:47:51 +00001122 if (*attrobj == NULL)
1123 return NULL;
1124 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001125 res = _PyType_Lookup(Py_TYPE(self), *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001126 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +00001127 descrgetfunc f;
Christian Heimes90aa7642007-12-19 02:45:37 +00001128 if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
Guido van Rossum60718732001-08-28 17:47:51 +00001129 Py_INCREF(res);
1130 else
Christian Heimes90aa7642007-12-19 02:45:37 +00001131 res = f(res, self, (PyObject *)(Py_TYPE(self)));
Guido van Rossum60718732001-08-28 17:47:51 +00001132 }
1133 return res;
1134}
1135
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001136static PyObject *
1137lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
1138{
1139 PyObject *res = lookup_maybe(self, attrstr, attrobj);
1140 if (res == NULL && !PyErr_Occurred())
1141 PyErr_SetObject(PyExc_AttributeError, *attrobj);
1142 return res;
1143}
1144
Benjamin Peterson224205f2009-05-08 03:25:19 +00001145PyObject *
1146_PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)
1147{
1148 return lookup_maybe(self, attrstr, attrobj);
1149}
1150
Guido van Rossum2730b132001-08-28 18:22:14 +00001151/* A variation of PyObject_CallMethod that uses lookup_method()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001152 instead of PyObject_GetAttrString(). This uses the same convention
Guido van Rossum2730b132001-08-28 18:22:14 +00001153 as lookup_method to cache the interned name string object. */
1154
Neil Schemenauerf23473f2001-10-21 22:28:58 +00001155static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +00001156call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1157{
1158 va_list va;
1159 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +00001160 va_start(va, format);
1161
Guido van Rossumda21c012001-10-03 00:50:18 +00001162 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001163 if (func == NULL) {
1164 va_end(va);
1165 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +00001166 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001167 return NULL;
1168 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001169
1170 if (format && *format)
1171 args = Py_VaBuildValue(format, va);
1172 else
1173 args = PyTuple_New(0);
1174
1175 va_end(va);
1176
1177 if (args == NULL)
1178 return NULL;
1179
1180 assert(PyTuple_Check(args));
1181 retval = PyObject_Call(func, args, NULL);
1182
1183 Py_DECREF(args);
1184 Py_DECREF(func);
1185
1186 return retval;
1187}
1188
1189/* Clone of call_method() that returns NotImplemented when the lookup fails. */
1190
Neil Schemenauerf23473f2001-10-21 22:28:58 +00001191static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001192call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1193{
1194 va_list va;
1195 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001196 va_start(va, format);
1197
Guido van Rossumda21c012001-10-03 00:50:18 +00001198 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +00001199 if (func == NULL) {
1200 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +00001201 if (!PyErr_Occurred()) {
1202 Py_INCREF(Py_NotImplemented);
1203 return Py_NotImplemented;
1204 }
Guido van Rossum717ce002001-09-14 16:58:08 +00001205 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +00001206 }
1207
1208 if (format && *format)
1209 args = Py_VaBuildValue(format, va);
1210 else
1211 args = PyTuple_New(0);
1212
1213 va_end(va);
1214
Guido van Rossum717ce002001-09-14 16:58:08 +00001215 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00001216 return NULL;
1217
Guido van Rossum717ce002001-09-14 16:58:08 +00001218 assert(PyTuple_Check(args));
1219 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +00001220
1221 Py_DECREF(args);
1222 Py_DECREF(func);
1223
1224 return retval;
1225}
1226
Tim Petersea7f75d2002-12-07 21:39:16 +00001227/*
Guido van Rossum1f121312002-11-14 19:49:16 +00001228 Method resolution order algorithm C3 described in
1229 "A Monotonic Superclass Linearization for Dylan",
1230 by Kim Barrett, Bob Cassel, Paul Haahr,
Tim Petersea7f75d2002-12-07 21:39:16 +00001231 David A. Moon, Keith Playford, and P. Tucker Withington.
Guido van Rossum1f121312002-11-14 19:49:16 +00001232 (OOPSLA 1996)
1233
Guido van Rossum98f33732002-11-25 21:36:54 +00001234 Some notes about the rules implied by C3:
1235
Tim Petersea7f75d2002-12-07 21:39:16 +00001236 No duplicate bases.
Guido van Rossum98f33732002-11-25 21:36:54 +00001237 It isn't legal to repeat a class in a list of base classes.
1238
1239 The next three properties are the 3 constraints in "C3".
1240
Tim Petersea7f75d2002-12-07 21:39:16 +00001241 Local precendece order.
Guido van Rossum98f33732002-11-25 21:36:54 +00001242 If A precedes B in C's MRO, then A will precede B in the MRO of all
1243 subclasses of C.
1244
1245 Monotonicity.
1246 The MRO of a class must be an extension without reordering of the
1247 MRO of each of its superclasses.
1248
1249 Extended Precedence Graph (EPG).
1250 Linearization is consistent if there is a path in the EPG from
1251 each class to all its successors in the linearization. See
1252 the paper for definition of EPG.
Guido van Rossum1f121312002-11-14 19:49:16 +00001253 */
1254
Tim Petersea7f75d2002-12-07 21:39:16 +00001255static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001256tail_contains(PyObject *list, int whence, PyObject *o) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001257 Py_ssize_t j, size;
Guido van Rossum1f121312002-11-14 19:49:16 +00001258 size = PyList_GET_SIZE(list);
1259
1260 for (j = whence+1; j < size; j++) {
1261 if (PyList_GET_ITEM(list, j) == o)
1262 return 1;
1263 }
1264 return 0;
1265}
1266
Guido van Rossum98f33732002-11-25 21:36:54 +00001267static PyObject *
1268class_name(PyObject *cls)
1269{
1270 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1271 if (name == NULL) {
1272 PyErr_Clear();
1273 Py_XDECREF(name);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001274 name = PyObject_Repr(cls);
Guido van Rossum98f33732002-11-25 21:36:54 +00001275 }
1276 if (name == NULL)
1277 return NULL;
Martin v. Löwis9b9905b2007-06-10 21:13:34 +00001278 if (!PyUnicode_Check(name)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001279 Py_DECREF(name);
1280 return NULL;
1281 }
1282 return name;
1283}
1284
1285static int
1286check_duplicates(PyObject *list)
1287{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001288 Py_ssize_t i, j, n;
Guido van Rossum98f33732002-11-25 21:36:54 +00001289 /* Let's use a quadratic time algorithm,
1290 assuming that the bases lists is short.
1291 */
1292 n = PyList_GET_SIZE(list);
1293 for (i = 0; i < n; i++) {
1294 PyObject *o = PyList_GET_ITEM(list, i);
1295 for (j = i + 1; j < n; j++) {
1296 if (PyList_GET_ITEM(list, j) == o) {
1297 o = class_name(o);
1298 PyErr_Format(PyExc_TypeError,
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00001299 "duplicate base class %.400s",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001300 o ? _PyUnicode_AsString(o) : "?");
Guido van Rossum98f33732002-11-25 21:36:54 +00001301 Py_XDECREF(o);
1302 return -1;
1303 }
1304 }
1305 }
1306 return 0;
1307}
1308
1309/* Raise a TypeError for an MRO order disagreement.
1310
1311 It's hard to produce a good error message. In the absence of better
1312 insight into error reporting, report the classes that were candidates
Guido van Rossumd8faa362007-04-27 19:54:29 +00001313 to be put next into the MRO. There is some conflict between the
Guido van Rossum98f33732002-11-25 21:36:54 +00001314 order in which they should be put in the MRO, but it's hard to
1315 diagnose what constraint can't be satisfied.
1316*/
1317
1318static void
1319set_mro_error(PyObject *to_merge, int *remain)
1320{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001321 Py_ssize_t i, n, off, to_merge_size;
Guido van Rossum98f33732002-11-25 21:36:54 +00001322 char buf[1000];
1323 PyObject *k, *v;
1324 PyObject *set = PyDict_New();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001325 if (!set) return;
Guido van Rossum98f33732002-11-25 21:36:54 +00001326
1327 to_merge_size = PyList_GET_SIZE(to_merge);
1328 for (i = 0; i < to_merge_size; i++) {
1329 PyObject *L = PyList_GET_ITEM(to_merge, i);
1330 if (remain[i] < PyList_GET_SIZE(L)) {
1331 PyObject *c = PyList_GET_ITEM(L, remain[i]);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001332 if (PyDict_SetItem(set, c, Py_None) < 0) {
1333 Py_DECREF(set);
Guido van Rossum98f33732002-11-25 21:36:54 +00001334 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001336 }
1337 }
1338 n = PyDict_Size(set);
1339
Raymond Hettingerf394df42003-04-06 19:13:41 +00001340 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1341consistent method resolution\norder (MRO) for bases");
Guido van Rossum98f33732002-11-25 21:36:54 +00001342 i = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001344 PyObject *name = class_name(k);
1345 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001346 name ? _PyUnicode_AsString(name) : "?");
Guido van Rossum98f33732002-11-25 21:36:54 +00001347 Py_XDECREF(name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001348 if (--n && (size_t)(off+1) < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001349 buf[off++] = ',';
1350 buf[off] = '\0';
1351 }
1352 }
1353 PyErr_SetString(PyExc_TypeError, buf);
1354 Py_DECREF(set);
1355}
1356
Tim Petersea7f75d2002-12-07 21:39:16 +00001357static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001358pmerge(PyObject *acc, PyObject* to_merge) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001359 Py_ssize_t i, j, to_merge_size, empty_cnt;
Guido van Rossum1f121312002-11-14 19:49:16 +00001360 int *remain;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001361 int ok;
Tim Petersea7f75d2002-12-07 21:39:16 +00001362
Guido van Rossum1f121312002-11-14 19:49:16 +00001363 to_merge_size = PyList_GET_SIZE(to_merge);
1364
Guido van Rossum98f33732002-11-25 21:36:54 +00001365 /* remain stores an index into each sublist of to_merge.
1366 remain[i] is the index of the next base in to_merge[i]
1367 that is not included in acc.
1368 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
Guido van Rossum1f121312002-11-14 19:49:16 +00001370 if (remain == NULL)
1371 return -1;
1372 for (i = 0; i < to_merge_size; i++)
1373 remain[i] = 0;
1374
1375 again:
1376 empty_cnt = 0;
1377 for (i = 0; i < to_merge_size; i++) {
1378 PyObject *candidate;
Tim Petersea7f75d2002-12-07 21:39:16 +00001379
Guido van Rossum1f121312002-11-14 19:49:16 +00001380 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1381
1382 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1383 empty_cnt++;
1384 continue;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001385 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001386
Guido van Rossum98f33732002-11-25 21:36:54 +00001387 /* Choose next candidate for MRO.
1388
1389 The input sequences alone can determine the choice.
1390 If not, choose the class which appears in the MRO
1391 of the earliest direct superclass of the new class.
1392 */
1393
Guido van Rossum1f121312002-11-14 19:49:16 +00001394 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1395 for (j = 0; j < to_merge_size; j++) {
1396 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum98f33732002-11-25 21:36:54 +00001397 if (tail_contains(j_lst, remain[j], candidate)) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001398 goto skip; /* continue outer loop */
Guido van Rossum98f33732002-11-25 21:36:54 +00001399 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001400 }
1401 ok = PyList_Append(acc, candidate);
1402 if (ok < 0) {
1403 PyMem_Free(remain);
1404 return -1;
1405 }
1406 for (j = 0; j < to_merge_size; j++) {
1407 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum768158c2002-12-31 16:33:01 +00001408 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1409 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001410 remain[j]++;
1411 }
1412 }
1413 goto again;
Tim Peters9a6b8d82002-11-14 23:22:33 +00001414 skip: ;
Guido van Rossum1f121312002-11-14 19:49:16 +00001415 }
1416
Guido van Rossum98f33732002-11-25 21:36:54 +00001417 if (empty_cnt == to_merge_size) {
1418 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001419 return 0;
Guido van Rossum98f33732002-11-25 21:36:54 +00001420 }
1421 set_mro_error(to_merge, remain);
1422 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001423 return -1;
1424}
1425
Tim Peters6d6c1a32001-08-02 04:15:00 +00001426static PyObject *
1427mro_implementation(PyTypeObject *type)
1428{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001429 Py_ssize_t i, n;
1430 int ok;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001431 PyObject *bases, *result;
Guido van Rossum1f121312002-11-14 19:49:16 +00001432 PyObject *to_merge, *bases_aslist;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001433
Christian Heimes412dc9c2008-01-27 18:55:54 +00001434 if (type->tp_dict == NULL) {
1435 if (PyType_Ready(type) < 0)
Guido van Rossum63517572002-06-18 16:44:57 +00001436 return NULL;
1437 }
1438
Guido van Rossum98f33732002-11-25 21:36:54 +00001439 /* Find a superclass linearization that honors the constraints
1440 of the explicit lists of bases and the constraints implied by
Tim Petersea7f75d2002-12-07 21:39:16 +00001441 each base class.
Guido van Rossum98f33732002-11-25 21:36:54 +00001442
1443 to_merge is a list of lists, where each list is a superclass
1444 linearization implied by a base class. The last element of
1445 to_merge is the declared list of bases.
1446 */
1447
Tim Peters6d6c1a32001-08-02 04:15:00 +00001448 bases = type->tp_bases;
1449 n = PyTuple_GET_SIZE(bases);
Guido van Rossum1f121312002-11-14 19:49:16 +00001450
1451 to_merge = PyList_New(n+1);
1452 if (to_merge == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001453 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001454
Tim Peters6d6c1a32001-08-02 04:15:00 +00001455 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001456 PyObject *base = PyTuple_GET_ITEM(bases, i);
1457 PyObject *parentMRO;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001458 parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001459 if (parentMRO == NULL) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001460 Py_DECREF(to_merge);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001461 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001462 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001463
1464 PyList_SET_ITEM(to_merge, i, parentMRO);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001465 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001466
1467 bases_aslist = PySequence_List(bases);
1468 if (bases_aslist == NULL) {
1469 Py_DECREF(to_merge);
1470 return NULL;
1471 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001472 /* This is just a basic sanity check. */
1473 if (check_duplicates(bases_aslist) < 0) {
1474 Py_DECREF(to_merge);
1475 Py_DECREF(bases_aslist);
1476 return NULL;
1477 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001478 PyList_SET_ITEM(to_merge, n, bases_aslist);
1479
1480 result = Py_BuildValue("[O]", (PyObject *)type);
1481 if (result == NULL) {
1482 Py_DECREF(to_merge);
1483 return NULL;
1484 }
1485
1486 ok = pmerge(result, to_merge);
1487 Py_DECREF(to_merge);
1488 if (ok < 0) {
1489 Py_DECREF(result);
1490 return NULL;
1491 }
1492
Tim Peters6d6c1a32001-08-02 04:15:00 +00001493 return result;
1494}
1495
1496static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001497mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001498{
1499 PyTypeObject *type = (PyTypeObject *)self;
1500
Tim Peters6d6c1a32001-08-02 04:15:00 +00001501 return mro_implementation(type);
1502}
1503
1504static int
1505mro_internal(PyTypeObject *type)
1506{
1507 PyObject *mro, *result, *tuple;
Armin Rigo037d1e02005-12-29 17:07:39 +00001508 int checkit = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001509
Christian Heimes90aa7642007-12-19 02:45:37 +00001510 if (Py_TYPE(type) == &PyType_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001511 result = mro_implementation(type);
1512 }
1513 else {
Guido van Rossum60718732001-08-28 17:47:51 +00001514 static PyObject *mro_str;
Armin Rigo037d1e02005-12-29 17:07:39 +00001515 checkit = 1;
Guido van Rossum60718732001-08-28 17:47:51 +00001516 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001517 if (mro == NULL)
1518 return -1;
1519 result = PyObject_CallObject(mro, NULL);
1520 Py_DECREF(mro);
1521 }
1522 if (result == NULL)
1523 return -1;
1524 tuple = PySequence_Tuple(result);
1525 Py_DECREF(result);
Armin Rigo037d1e02005-12-29 17:07:39 +00001526 if (tuple == NULL)
1527 return -1;
1528 if (checkit) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001529 Py_ssize_t i, len;
Armin Rigo037d1e02005-12-29 17:07:39 +00001530 PyObject *cls;
1531 PyTypeObject *solid;
1532
1533 solid = solid_base(type);
1534
1535 len = PyTuple_GET_SIZE(tuple);
1536
1537 for (i = 0; i < len; i++) {
1538 PyTypeObject *t;
1539 cls = PyTuple_GET_ITEM(tuple, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001540 if (!PyType_Check(cls)) {
Armin Rigo037d1e02005-12-29 17:07:39 +00001541 PyErr_Format(PyExc_TypeError,
1542 "mro() returned a non-class ('%.500s')",
Christian Heimes90aa7642007-12-19 02:45:37 +00001543 Py_TYPE(cls)->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001544 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001545 return -1;
1546 }
1547 t = (PyTypeObject*)cls;
1548 if (!PyType_IsSubtype(solid, solid_base(t))) {
1549 PyErr_Format(PyExc_TypeError,
1550 "mro() returned base with unsuitable layout ('%.500s')",
1551 t->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001552 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001553 return -1;
1554 }
1555 }
1556 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001557 type->tp_mro = tuple;
Christian Heimesa62da1d2008-01-12 19:39:10 +00001558
1559 type_mro_modified(type, type->tp_mro);
1560 /* corner case: the old-style super class might have been hidden
1561 from the custom MRO */
1562 type_mro_modified(type, type->tp_bases);
1563
Georg Brandlf08a9dd2008-06-10 16:57:31 +00001564 PyType_Modified(type);
Christian Heimesa62da1d2008-01-12 19:39:10 +00001565
Tim Peters6d6c1a32001-08-02 04:15:00 +00001566 return 0;
1567}
1568
1569
1570/* Calculate the best base amongst multiple base classes.
1571 This is the first one that's on the path to the "solid base". */
1572
1573static PyTypeObject *
1574best_base(PyObject *bases)
1575{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001576 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001577 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +00001578 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001579
1580 assert(PyTuple_Check(bases));
1581 n = PyTuple_GET_SIZE(bases);
1582 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +00001583 base = NULL;
1584 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001585 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001586 base_proto = PyTuple_GET_ITEM(bases, i);
Tim Petersa91e9642001-11-14 23:32:33 +00001587 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001588 PyErr_SetString(
1589 PyExc_TypeError,
1590 "bases must be types");
1591 return NULL;
1592 }
Tim Petersa91e9642001-11-14 23:32:33 +00001593 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001594 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001595 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001596 return NULL;
1597 }
1598 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +00001599 if (winner == NULL) {
1600 winner = candidate;
1601 base = base_i;
1602 }
1603 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001604 ;
1605 else if (PyType_IsSubtype(candidate, winner)) {
1606 winner = candidate;
1607 base = base_i;
1608 }
1609 else {
1610 PyErr_SetString(
1611 PyExc_TypeError,
1612 "multiple bases have "
1613 "instance lay-out conflict");
1614 return NULL;
1615 }
1616 }
Guido van Rossume54616c2001-12-14 04:19:56 +00001617 if (base == NULL)
1618 PyErr_SetString(PyExc_TypeError,
1619 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001620 return base;
1621}
1622
1623static int
1624extra_ivars(PyTypeObject *type, PyTypeObject *base)
1625{
Neil Schemenauerc806c882001-08-29 23:54:54 +00001626 size_t t_size = type->tp_basicsize;
1627 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001628
Guido van Rossum9676b222001-08-17 20:32:36 +00001629 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001630 if (type->tp_itemsize || base->tp_itemsize) {
1631 /* If itemsize is involved, stricter rules */
1632 return t_size != b_size ||
1633 type->tp_itemsize != base->tp_itemsize;
1634 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001635 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
Guido van Rossum360e4b82007-05-14 22:51:27 +00001636 type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
1637 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossum9676b222001-08-17 20:32:36 +00001638 t_size -= sizeof(PyObject *);
1639 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
Guido van Rossum360e4b82007-05-14 22:51:27 +00001640 type->tp_dictoffset + sizeof(PyObject *) == t_size &&
1641 type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossum9676b222001-08-17 20:32:36 +00001642 t_size -= sizeof(PyObject *);
1643
1644 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001645}
1646
1647static PyTypeObject *
1648solid_base(PyTypeObject *type)
1649{
1650 PyTypeObject *base;
1651
1652 if (type->tp_base)
1653 base = solid_base(type->tp_base);
1654 else
1655 base = &PyBaseObject_Type;
1656 if (extra_ivars(type, base))
1657 return type;
1658 else
1659 return base;
1660}
1661
Jeremy Hylton938ace62002-07-17 16:30:39 +00001662static void object_dealloc(PyObject *);
1663static int object_init(PyObject *, PyObject *, PyObject *);
1664static int update_slot(PyTypeObject *, PyObject *);
1665static void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001666
Guido van Rossum360e4b82007-05-14 22:51:27 +00001667/*
1668 * Helpers for __dict__ descriptor. We don't want to expose the dicts
1669 * inherited from various builtin types. The builtin base usually provides
1670 * its own __dict__ descriptor, so we use that when we can.
1671 */
1672static PyTypeObject *
1673get_builtin_base_with_dict(PyTypeObject *type)
1674{
1675 while (type->tp_base != NULL) {
1676 if (type->tp_dictoffset != 0 &&
1677 !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1678 return type;
1679 type = type->tp_base;
1680 }
1681 return NULL;
1682}
1683
1684static PyObject *
1685get_dict_descriptor(PyTypeObject *type)
1686{
1687 static PyObject *dict_str;
1688 PyObject *descr;
1689
1690 if (dict_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001691 dict_str = PyUnicode_InternFromString("__dict__");
Guido van Rossum360e4b82007-05-14 22:51:27 +00001692 if (dict_str == NULL)
1693 return NULL;
1694 }
1695 descr = _PyType_Lookup(type, dict_str);
1696 if (descr == NULL || !PyDescr_IsData(descr))
1697 return NULL;
1698
1699 return descr;
1700}
1701
1702static void
1703raise_dict_descr_error(PyObject *obj)
1704{
1705 PyErr_Format(PyExc_TypeError,
1706 "this __dict__ descriptor does not support "
Christian Heimes90aa7642007-12-19 02:45:37 +00001707 "'%.200s' objects", Py_TYPE(obj)->tp_name);
Guido van Rossum360e4b82007-05-14 22:51:27 +00001708}
1709
Tim Peters6d6c1a32001-08-02 04:15:00 +00001710static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001711subtype_dict(PyObject *obj, void *context)
1712{
Guido van Rossum360e4b82007-05-14 22:51:27 +00001713 PyObject **dictptr;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001714 PyObject *dict;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001715 PyTypeObject *base;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001716
Christian Heimes90aa7642007-12-19 02:45:37 +00001717 base = get_builtin_base_with_dict(Py_TYPE(obj));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001718 if (base != NULL) {
1719 descrgetfunc func;
1720 PyObject *descr = get_dict_descriptor(base);
1721 if (descr == NULL) {
1722 raise_dict_descr_error(obj);
1723 return NULL;
1724 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001725 func = Py_TYPE(descr)->tp_descr_get;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001726 if (func == NULL) {
1727 raise_dict_descr_error(obj);
1728 return NULL;
1729 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001730 return func(descr, obj, (PyObject *)(Py_TYPE(obj)));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001731 }
1732
1733 dictptr = _PyObject_GetDictPtr(obj);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001734 if (dictptr == NULL) {
1735 PyErr_SetString(PyExc_AttributeError,
1736 "This object has no __dict__");
1737 return NULL;
1738 }
1739 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +00001740 if (dict == NULL)
1741 *dictptr = dict = PyDict_New();
1742 Py_XINCREF(dict);
1743 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001744}
1745
Guido van Rossum6661be32001-10-26 04:26:12 +00001746static int
1747subtype_setdict(PyObject *obj, PyObject *value, void *context)
1748{
Guido van Rossum360e4b82007-05-14 22:51:27 +00001749 PyObject **dictptr;
Guido van Rossum6661be32001-10-26 04:26:12 +00001750 PyObject *dict;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001751 PyTypeObject *base;
Guido van Rossum6661be32001-10-26 04:26:12 +00001752
Christian Heimes90aa7642007-12-19 02:45:37 +00001753 base = get_builtin_base_with_dict(Py_TYPE(obj));
Guido van Rossum360e4b82007-05-14 22:51:27 +00001754 if (base != NULL) {
1755 descrsetfunc func;
1756 PyObject *descr = get_dict_descriptor(base);
1757 if (descr == NULL) {
1758 raise_dict_descr_error(obj);
1759 return -1;
1760 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001761 func = Py_TYPE(descr)->tp_descr_set;
Guido van Rossum360e4b82007-05-14 22:51:27 +00001762 if (func == NULL) {
1763 raise_dict_descr_error(obj);
1764 return -1;
1765 }
1766 return func(descr, obj, value);
1767 }
1768
1769 dictptr = _PyObject_GetDictPtr(obj);
Guido van Rossum6661be32001-10-26 04:26:12 +00001770 if (dictptr == NULL) {
1771 PyErr_SetString(PyExc_AttributeError,
1772 "This object has no __dict__");
1773 return -1;
1774 }
Guido van Rossumd331cb52001-12-05 19:46:42 +00001775 if (value != NULL && !PyDict_Check(value)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001776 PyErr_Format(PyExc_TypeError,
1777 "__dict__ must be set to a dictionary, "
Christian Heimes90aa7642007-12-19 02:45:37 +00001778 "not a '%.200s'", Py_TYPE(value)->tp_name);
Guido van Rossum6661be32001-10-26 04:26:12 +00001779 return -1;
1780 }
1781 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +00001782 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +00001783 *dictptr = value;
1784 Py_XDECREF(dict);
1785 return 0;
1786}
1787
Guido van Rossumad47da02002-08-12 19:05:44 +00001788static PyObject *
1789subtype_getweakref(PyObject *obj, void *context)
1790{
1791 PyObject **weaklistptr;
1792 PyObject *result;
1793
Christian Heimes90aa7642007-12-19 02:45:37 +00001794 if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001795 PyErr_SetString(PyExc_AttributeError,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001796 "This object has no __weakref__");
Guido van Rossumad47da02002-08-12 19:05:44 +00001797 return NULL;
1798 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001799 assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
1800 assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
1801 (size_t)(Py_TYPE(obj)->tp_basicsize));
Guido van Rossumad47da02002-08-12 19:05:44 +00001802 weaklistptr = (PyObject **)
Christian Heimes90aa7642007-12-19 02:45:37 +00001803 ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
Guido van Rossumad47da02002-08-12 19:05:44 +00001804 if (*weaklistptr == NULL)
1805 result = Py_None;
1806 else
1807 result = *weaklistptr;
1808 Py_INCREF(result);
1809 return result;
1810}
1811
Guido van Rossum373c7412003-01-07 13:41:37 +00001812/* Three variants on the subtype_getsets list. */
1813
1814static PyGetSetDef subtype_getsets_full[] = {
Guido van Rossumad47da02002-08-12 19:05:44 +00001815 {"__dict__", subtype_dict, subtype_setdict,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001816 PyDoc_STR("dictionary for instance variables (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001817 {"__weakref__", subtype_getweakref, NULL,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001818 PyDoc_STR("list of weak references to the object (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001819 {0}
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001820};
1821
Guido van Rossum373c7412003-01-07 13:41:37 +00001822static PyGetSetDef subtype_getsets_dict_only[] = {
1823 {"__dict__", subtype_dict, subtype_setdict,
1824 PyDoc_STR("dictionary for instance variables (if defined)")},
1825 {0}
1826};
1827
1828static PyGetSetDef subtype_getsets_weakref_only[] = {
1829 {"__weakref__", subtype_getweakref, NULL,
1830 PyDoc_STR("list of weak references to the object (if defined)")},
1831 {0}
1832};
1833
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001834static int
1835valid_identifier(PyObject *s)
1836{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001837 if (!PyUnicode_Check(s)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001838 PyErr_Format(PyExc_TypeError,
1839 "__slots__ items must be strings, not '%.200s'",
Christian Heimes90aa7642007-12-19 02:45:37 +00001840 Py_TYPE(s)->tp_name);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001841 return 0;
1842 }
Georg Brandlf4780d02007-08-30 18:29:48 +00001843 if (!PyUnicode_IsIdentifier(s)) {
1844 PyErr_SetString(PyExc_TypeError,
1845 "__slots__ must be identifiers");
1846 return 0;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001847 }
1848 return 1;
1849}
1850
Guido van Rossumd8faa362007-04-27 19:54:29 +00001851/* Forward */
1852static int
1853object_init(PyObject *self, PyObject *args, PyObject *kwds);
1854
1855static int
1856type_init(PyObject *cls, PyObject *args, PyObject *kwds)
1857{
1858 int res;
1859
1860 assert(args != NULL && PyTuple_Check(args));
1861 assert(kwds == NULL || PyDict_Check(kwds));
1862
1863 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
1864 PyErr_SetString(PyExc_TypeError,
1865 "type.__init__() takes no keyword arguments");
1866 return -1;
1867 }
1868
1869 if (args != NULL && PyTuple_Check(args) &&
1870 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
1871 PyErr_SetString(PyExc_TypeError,
1872 "type.__init__() takes 1 or 3 arguments");
1873 return -1;
1874 }
1875
1876 /* Call object.__init__(self) now. */
1877 /* XXX Could call super(type, cls).__init__() but what's the point? */
1878 args = PyTuple_GetSlice(args, 0, 0);
1879 res = object_init(cls, args, NULL);
1880 Py_DECREF(args);
1881 return res;
1882}
1883
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001884static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001885type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1886{
1887 PyObject *name, *bases, *dict;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001888 static char *kwlist[] = {"name", "bases", "dict", 0};
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001889 PyObject *slots, *tmp, *newslots;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001890 PyTypeObject *type, *base, *tmptype, *winner;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001891 PyHeapTypeObject *et;
Guido van Rossum6f799372001-09-20 20:46:19 +00001892 PyMemberDef *mp;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001893 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
Guido van Rossumad47da02002-08-12 19:05:44 +00001894 int j, may_add_dict, may_add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001895
Tim Peters3abca122001-10-27 19:37:48 +00001896 assert(args != NULL && PyTuple_Check(args));
1897 assert(kwds == NULL || PyDict_Check(kwds));
1898
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001899 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +00001900 {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001901 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1902 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
Tim Peters3abca122001-10-27 19:37:48 +00001903
1904 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1905 PyObject *x = PyTuple_GET_ITEM(args, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00001906 Py_INCREF(Py_TYPE(x));
1907 return (PyObject *) Py_TYPE(x);
Tim Peters3abca122001-10-27 19:37:48 +00001908 }
1909
1910 /* SF bug 475327 -- if that didn't trigger, we need 3
1911 arguments. but PyArg_ParseTupleAndKeywords below may give
1912 a msg saying type() needs exactly 3. */
1913 if (nargs + nkwds != 3) {
1914 PyErr_SetString(PyExc_TypeError,
1915 "type() takes 1 or 3 arguments");
1916 return NULL;
1917 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001918 }
1919
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001920 /* Check arguments: (name, bases, dict) */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001921 if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001922 &name,
1923 &PyTuple_Type, &bases,
1924 &PyDict_Type, &dict))
1925 return NULL;
1926
1927 /* Determine the proper metatype to deal with this,
1928 and check for metatype conflicts while we're at it.
1929 Note that if some other metatype wins to contract,
1930 it's possible that its instances are not types. */
1931 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001932 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001933 for (i = 0; i < nbases; i++) {
1934 tmp = PyTuple_GET_ITEM(bases, i);
Christian Heimes90aa7642007-12-19 02:45:37 +00001935 tmptype = Py_TYPE(tmp);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001936 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001937 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001938 if (PyType_IsSubtype(tmptype, winner)) {
1939 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001940 continue;
1941 }
1942 PyErr_SetString(PyExc_TypeError,
Guido van Rossum636688d2003-04-23 12:07:22 +00001943 "metaclass conflict: "
1944 "the metaclass of a derived class "
1945 "must be a (non-strict) subclass "
1946 "of the metaclasses of all its bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001947 return NULL;
1948 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001949 if (winner != metatype) {
1950 if (winner->tp_new != type_new) /* Pass it to the winner */
1951 return winner->tp_new(winner, args, kwds);
1952 metatype = winner;
1953 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001954
1955 /* Adjust for empty tuple bases */
1956 if (nbases == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001957 bases = PyTuple_Pack(1, &PyBaseObject_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001958 if (bases == NULL)
1959 return NULL;
1960 nbases = 1;
1961 }
1962 else
1963 Py_INCREF(bases);
1964
1965 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1966
1967 /* Calculate best base, and check that all bases are type objects */
1968 base = best_base(bases);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001969 if (base == NULL) {
1970 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001971 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001972 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001973 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1974 PyErr_Format(PyExc_TypeError,
1975 "type '%.100s' is not an acceptable base type",
1976 base->tp_name);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001977 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001978 return NULL;
1979 }
1980
Tim Peters6d6c1a32001-08-02 04:15:00 +00001981 /* Check for a __slots__ sequence variable in dict, and count it */
1982 slots = PyDict_GetItemString(dict, "__slots__");
1983 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001984 add_dict = 0;
1985 add_weak = 0;
Guido van Rossumad47da02002-08-12 19:05:44 +00001986 may_add_dict = base->tp_dictoffset == 0;
1987 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1988 if (slots == NULL) {
1989 if (may_add_dict) {
1990 add_dict++;
1991 }
1992 if (may_add_weak) {
1993 add_weak++;
1994 }
1995 }
1996 else {
1997 /* Have slots */
1998
Tim Peters6d6c1a32001-08-02 04:15:00 +00001999 /* Make it into a tuple */
Neal Norwitz80e7f272007-08-26 06:45:23 +00002000 if (PyUnicode_Check(slots))
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002001 slots = PyTuple_Pack(1, slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002002 else
2003 slots = PySequence_Tuple(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002004 if (slots == NULL) {
2005 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002006 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002007 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002008 assert(PyTuple_Check(slots));
2009
2010 /* Are slots allowed? */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002011 nslots = PyTuple_GET_SIZE(slots);
Jeremy Hylton1c7a0ea2003-07-16 16:08:23 +00002012 if (nslots > 0 && base->tp_itemsize != 0) {
Guido van Rossumc4141872001-08-30 04:43:35 +00002013 PyErr_Format(PyExc_TypeError,
2014 "nonempty __slots__ "
2015 "not supported for subtype of '%s'",
2016 base->tp_name);
Guido van Rossumad47da02002-08-12 19:05:44 +00002017 bad_slots:
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002018 Py_DECREF(bases);
Guido van Rossumad47da02002-08-12 19:05:44 +00002019 Py_DECREF(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00002020 return NULL;
2021 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002022
2023 /* Check for valid slot names and two special cases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002024 for (i = 0; i < nslots; i++) {
Guido van Rossumad47da02002-08-12 19:05:44 +00002025 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
Guido van Rossumad47da02002-08-12 19:05:44 +00002026 if (!valid_identifier(tmp))
2027 goto bad_slots;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002028 assert(PyUnicode_Check(tmp));
2029 if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00002030 if (!may_add_dict || add_dict) {
2031 PyErr_SetString(PyExc_TypeError,
2032 "__dict__ slot disallowed: "
2033 "we already got one");
2034 goto bad_slots;
2035 }
2036 add_dict++;
2037 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00002038 if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00002039 if (!may_add_weak || add_weak) {
2040 PyErr_SetString(PyExc_TypeError,
2041 "__weakref__ slot disallowed: "
2042 "either we already got one, "
2043 "or __itemsize__ != 0");
2044 goto bad_slots;
2045 }
2046 add_weak++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002047 }
2048 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002049
Guido van Rossumd8faa362007-04-27 19:54:29 +00002050 /* Copy slots into a list, mangle names and sort them.
2051 Sorted names are needed for __class__ assignment.
2052 Convert them back to tuple at the end.
2053 */
2054 newslots = PyList_New(nslots - add_dict - add_weak);
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002055 if (newslots == NULL)
Guido van Rossumad47da02002-08-12 19:05:44 +00002056 goto bad_slots;
2057 for (i = j = 0; i < nslots; i++) {
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002058 tmp = PyTuple_GET_ITEM(slots, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00002059 if ((add_dict &&
2060 PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) ||
2061 (add_weak &&
2062 PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0))
Guido van Rossumad47da02002-08-12 19:05:44 +00002063 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 tmp =_Py_Mangle(name, tmp);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002065 if (!tmp)
2066 goto bad_slots;
2067 PyList_SET_ITEM(newslots, j, tmp);
Guido van Rossumad47da02002-08-12 19:05:44 +00002068 j++;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002069 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002070 assert(j == nslots - add_dict - add_weak);
2071 nslots = j;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002072 Py_DECREF(slots);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002073 if (PyList_Sort(newslots) == -1) {
2074 Py_DECREF(bases);
2075 Py_DECREF(newslots);
2076 return NULL;
2077 }
2078 slots = PyList_AsTuple(newslots);
2079 Py_DECREF(newslots);
2080 if (slots == NULL) {
2081 Py_DECREF(bases);
2082 return NULL;
2083 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00002084
Guido van Rossumad47da02002-08-12 19:05:44 +00002085 /* Secondary bases may provide weakrefs or dict */
2086 if (nbases > 1 &&
2087 ((may_add_dict && !add_dict) ||
2088 (may_add_weak && !add_weak))) {
2089 for (i = 0; i < nbases; i++) {
2090 tmp = PyTuple_GET_ITEM(bases, i);
2091 if (tmp == (PyObject *)base)
2092 continue; /* Skip primary base */
Guido van Rossumad47da02002-08-12 19:05:44 +00002093 assert(PyType_Check(tmp));
2094 tmptype = (PyTypeObject *)tmp;
2095 if (may_add_dict && !add_dict &&
2096 tmptype->tp_dictoffset != 0)
2097 add_dict++;
2098 if (may_add_weak && !add_weak &&
2099 tmptype->tp_weaklistoffset != 0)
2100 add_weak++;
2101 if (may_add_dict && !add_dict)
2102 continue;
2103 if (may_add_weak && !add_weak)
2104 continue;
2105 /* Nothing more to check */
2106 break;
2107 }
2108 }
Guido van Rossum9676b222001-08-17 20:32:36 +00002109 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002110
2111 /* XXX From here until type is safely allocated,
2112 "return NULL" may leak slots! */
2113
2114 /* Allocate the type object */
2115 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
Guido van Rossumad47da02002-08-12 19:05:44 +00002116 if (type == NULL) {
2117 Py_XDECREF(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00002118 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002119 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00002120 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002121
2122 /* Keep name and slots alive in the extended type object */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002123 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002124 Py_INCREF(name);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002125 et->ht_name = name;
2126 et->ht_slots = slots;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002127
Guido van Rossumdc91b992001-08-08 22:26:22 +00002128 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002129 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2130 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00002131 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2132 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00002133
Guido van Rossumdc91b992001-08-08 22:26:22 +00002134 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002135 type->tp_as_number = &et->as_number;
2136 type->tp_as_sequence = &et->as_sequence;
2137 type->tp_as_mapping = &et->as_mapping;
2138 type->tp_as_buffer = &et->as_buffer;
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002139 type->tp_name = _PyUnicode_AsString(name);
Neal Norwitz80e7f272007-08-26 06:45:23 +00002140 if (!type->tp_name) {
2141 Py_DECREF(type);
2142 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002143 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002144
2145 /* Set tp_base and tp_bases */
2146 type->tp_bases = bases;
2147 Py_INCREF(base);
2148 type->tp_base = base;
2149
Guido van Rossum687ae002001-10-15 22:03:32 +00002150 /* Initialize tp_dict from passed-in dict */
2151 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002152 if (dict == NULL) {
2153 Py_DECREF(type);
2154 return NULL;
2155 }
2156
Guido van Rossumc3542212001-08-16 09:18:56 +00002157 /* Set __module__ in the dict */
2158 if (PyDict_GetItemString(dict, "__module__") == NULL) {
2159 tmp = PyEval_GetGlobals();
2160 if (tmp != NULL) {
2161 tmp = PyDict_GetItemString(tmp, "__name__");
2162 if (tmp != NULL) {
2163 if (PyDict_SetItemString(dict, "__module__",
2164 tmp) < 0)
2165 return NULL;
2166 }
2167 }
2168 }
2169
Tim Peters2f93e282001-10-04 05:27:00 +00002170 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00002171 and is a string. The __doc__ accessor will first look for tp_doc;
2172 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00002173 */
2174 {
2175 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002176 if (doc != NULL && PyUnicode_Check(doc)) {
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002177 Py_ssize_t len;
2178 char *doc_str;
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002179 char *tp_doc;
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002180
Alexandre Vassalotti394996b2009-06-04 00:43:04 +00002181 doc_str = _PyUnicode_AsString(doc);
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002182 if (doc_str == NULL) {
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002183 Py_DECREF(type);
2184 return NULL;
Tim Peters2f93e282001-10-04 05:27:00 +00002185 }
Alexandre Vassalotti394996b2009-06-04 00:43:04 +00002186 /* Silently truncate the docstring if it contains null bytes. */
2187 len = strlen(doc_str);
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002188 tp_doc = (char *)PyObject_MALLOC(len + 1);
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002189 if (tp_doc == NULL) {
2190 Py_DECREF(type);
2191 return NULL;
Neal Norwitza369c5a2007-08-25 07:41:59 +00002192 }
Alexandre Vassalottia85998a2008-05-03 18:24:43 +00002193 memcpy(tp_doc, doc_str, len + 1);
Neal Norwitz6ea45d32007-08-26 04:19:43 +00002194 type->tp_doc = tp_doc;
Tim Peters2f93e282001-10-04 05:27:00 +00002195 }
2196 }
2197
Tim Peters6d6c1a32001-08-02 04:15:00 +00002198 /* Special-case __new__: if it's a plain function,
2199 make it a static function */
2200 tmp = PyDict_GetItemString(dict, "__new__");
2201 if (tmp != NULL && PyFunction_Check(tmp)) {
2202 tmp = PyStaticMethod_New(tmp);
2203 if (tmp == NULL) {
2204 Py_DECREF(type);
2205 return NULL;
2206 }
2207 PyDict_SetItemString(dict, "__new__", tmp);
2208 Py_DECREF(tmp);
2209 }
2210
2211 /* Add descriptors for custom slots from __slots__, or for __dict__ */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002212 mp = PyHeapType_GET_MEMBERS(et);
Neil Schemenauerc806c882001-08-29 23:54:54 +00002213 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002214 if (slots != NULL) {
2215 for (i = 0; i < nslots; i++, mp++) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002216 mp->name = _PyUnicode_AsString(
Tim Peters6d6c1a32001-08-02 04:15:00 +00002217 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00002218 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002219 mp->offset = slotoffset;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002220
2221 /* __dict__ and __weakref__ are already filtered out */
2222 assert(strcmp(mp->name, "__dict__") != 0);
2223 assert(strcmp(mp->name, "__weakref__") != 0);
2224
Tim Peters6d6c1a32001-08-02 04:15:00 +00002225 slotoffset += sizeof(PyObject *);
2226 }
2227 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002228 if (add_dict) {
2229 if (base->tp_itemsize)
2230 type->tp_dictoffset = -(long)sizeof(PyObject *);
2231 else
2232 type->tp_dictoffset = slotoffset;
2233 slotoffset += sizeof(PyObject *);
2234 }
2235 if (add_weak) {
2236 assert(!base->tp_itemsize);
2237 type->tp_weaklistoffset = slotoffset;
2238 slotoffset += sizeof(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002239 }
2240 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00002241 type->tp_itemsize = base->tp_itemsize;
Guido van Rossume5c691a2003-03-07 15:13:17 +00002242 type->tp_members = PyHeapType_GET_MEMBERS(et);
Guido van Rossum373c7412003-01-07 13:41:37 +00002243
2244 if (type->tp_weaklistoffset && type->tp_dictoffset)
2245 type->tp_getset = subtype_getsets_full;
2246 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
2247 type->tp_getset = subtype_getsets_weakref_only;
2248 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
2249 type->tp_getset = subtype_getsets_dict_only;
2250 else
2251 type->tp_getset = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002252
2253 /* Special case some slots */
2254 if (type->tp_dictoffset != 0 || nslots > 0) {
2255 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
2256 type->tp_getattro = PyObject_GenericGetAttr;
2257 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
2258 type->tp_setattro = PyObject_GenericSetAttr;
2259 }
2260 type->tp_dealloc = subtype_dealloc;
2261
Guido van Rossum9475a232001-10-05 20:51:39 +00002262 /* Enable GC unless there are really no instance variables possible */
2263 if (!(type->tp_basicsize == sizeof(PyObject) &&
2264 type->tp_itemsize == 0))
2265 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2266
Tim Peters6d6c1a32001-08-02 04:15:00 +00002267 /* Always override allocation strategy to use regular heap */
2268 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00002269 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002270 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00002271 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00002272 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00002273 }
2274 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002275 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002276
2277 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002278 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002279 Py_DECREF(type);
2280 return NULL;
2281 }
2282
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002283 /* Put the proper slots in place */
2284 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00002285
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286 return (PyObject *)type;
2287}
2288
2289/* Internal API to look for a name through the MRO.
2290 This returns a borrowed reference, and doesn't set an exception! */
2291PyObject *
2292_PyType_Lookup(PyTypeObject *type, PyObject *name)
2293{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002294 Py_ssize_t i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00002295 PyObject *mro, *res, *base, *dict;
Christian Heimesa62da1d2008-01-12 19:39:10 +00002296 unsigned int h;
2297
2298 if (MCACHE_CACHEABLE_NAME(name) &&
Christian Heimes412dc9c2008-01-27 18:55:54 +00002299 PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
Christian Heimesa62da1d2008-01-12 19:39:10 +00002300 /* fast path */
2301 h = MCACHE_HASH_METHOD(type, name);
2302 if (method_cache[h].version == type->tp_version_tag &&
2303 method_cache[h].name == name)
2304 return method_cache[h].value;
2305 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002306
Guido van Rossum687ae002001-10-15 22:03:32 +00002307 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002308 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00002309
2310 /* If mro is NULL, the type is either not yet initialized
2311 by PyType_Ready(), or already cleared by type_clear().
2312 Either way the safest thing to do is to return NULL. */
2313 if (mro == NULL)
2314 return NULL;
2315
Christian Heimesa62da1d2008-01-12 19:39:10 +00002316 res = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002317 assert(PyTuple_Check(mro));
2318 n = PyTuple_GET_SIZE(mro);
2319 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002320 base = PyTuple_GET_ITEM(mro, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002321 assert(PyType_Check(base));
2322 dict = ((PyTypeObject *)base)->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002323 assert(dict && PyDict_Check(dict));
2324 res = PyDict_GetItem(dict, name);
2325 if (res != NULL)
Christian Heimesa62da1d2008-01-12 19:39:10 +00002326 break;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002327 }
Christian Heimesa62da1d2008-01-12 19:39:10 +00002328
2329 if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
2330 h = MCACHE_HASH_METHOD(type, name);
2331 method_cache[h].version = type->tp_version_tag;
2332 method_cache[h].value = res; /* borrowed */
2333 Py_INCREF(name);
2334 Py_DECREF(method_cache[h].name);
2335 method_cache[h].name = name;
2336 }
2337 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002338}
2339
2340/* This is similar to PyObject_GenericGetAttr(),
2341 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2342static PyObject *
2343type_getattro(PyTypeObject *type, PyObject *name)
2344{
Christian Heimes90aa7642007-12-19 02:45:37 +00002345 PyTypeObject *metatype = Py_TYPE(type);
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002346 PyObject *meta_attribute, *attribute;
2347 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002348
2349 /* Initialize this type (we'll assume the metatype is initialized) */
2350 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002351 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002352 return NULL;
2353 }
2354
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002355 /* No readable descriptor found yet */
2356 meta_get = NULL;
Tim Peters34592512002-07-11 06:23:50 +00002357
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002358 /* Look for the attribute in the metatype */
2359 meta_attribute = _PyType_Lookup(metatype, name);
2360
2361 if (meta_attribute != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +00002362 meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
Tim Peters34592512002-07-11 06:23:50 +00002363
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002364 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2365 /* Data descriptors implement tp_descr_set to intercept
2366 * writes. Assume the attribute is not overridden in
2367 * type's tp_dict (and bases): call the descriptor now.
2368 */
2369 return meta_get(meta_attribute, (PyObject *)type,
2370 (PyObject *)metatype);
2371 }
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002372 Py_INCREF(meta_attribute);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002373 }
2374
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002375 /* No data descriptor found on metatype. Look in tp_dict of this
2376 * type and its bases */
2377 attribute = _PyType_Lookup(type, name);
2378 if (attribute != NULL) {
2379 /* Implement descriptor functionality, if any */
Christian Heimes90aa7642007-12-19 02:45:37 +00002380 descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002381
2382 Py_XDECREF(meta_attribute);
2383
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002384 if (local_get != NULL) {
2385 /* NULL 2nd argument indicates the descriptor was
2386 * found on the target object itself (or a base) */
2387 return local_get(attribute, (PyObject *)NULL,
2388 (PyObject *)type);
2389 }
Tim Peters34592512002-07-11 06:23:50 +00002390
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002391 Py_INCREF(attribute);
2392 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002393 }
2394
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002395 /* No attribute found in local __dict__ (or bases): use the
2396 * descriptor from the metatype, if any */
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002397 if (meta_get != NULL) {
2398 PyObject *res;
2399 res = meta_get(meta_attribute, (PyObject *)type,
2400 (PyObject *)metatype);
2401 Py_DECREF(meta_attribute);
2402 return res;
2403 }
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002404
2405 /* If an ordinary attribute was found on the metatype, return it now */
2406 if (meta_attribute != NULL) {
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002407 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002408 }
2409
2410 /* Give up */
2411 PyErr_Format(PyExc_AttributeError,
Walter Dörwald75163602007-06-11 15:47:13 +00002412 "type object '%.50s' has no attribute '%U'",
2413 type->tp_name, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002414 return NULL;
2415}
2416
2417static int
2418type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2419{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002420 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2421 PyErr_Format(
2422 PyExc_TypeError,
2423 "can't set attributes of built-in/extension type '%s'",
2424 type->tp_name);
2425 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002426 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002427 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2428 return -1;
2429 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002430}
2431
2432static void
2433type_dealloc(PyTypeObject *type)
2434{
Guido van Rossume5c691a2003-03-07 15:13:17 +00002435 PyHeapTypeObject *et;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002436
2437 /* Assert this is a heap-allocated type object */
2438 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002439 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00002440 PyObject_ClearWeakRefs((PyObject *)type);
Guido van Rossume5c691a2003-03-07 15:13:17 +00002441 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002442 Py_XDECREF(type->tp_base);
2443 Py_XDECREF(type->tp_dict);
2444 Py_XDECREF(type->tp_bases);
2445 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00002446 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00002447 Py_XDECREF(type->tp_subclasses);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002448 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2449 * of most other objects. It's okay to cast it to char *.
2450 */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002451 PyObject_Free((char *)type->tp_doc);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002452 Py_XDECREF(et->ht_name);
2453 Py_XDECREF(et->ht_slots);
Christian Heimes90aa7642007-12-19 02:45:37 +00002454 Py_TYPE(type)->tp_free((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002455}
2456
Guido van Rossum1c450732001-10-08 15:18:27 +00002457static PyObject *
2458type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2459{
2460 PyObject *list, *raw, *ref;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002461 Py_ssize_t i, n;
Guido van Rossum1c450732001-10-08 15:18:27 +00002462
2463 list = PyList_New(0);
2464 if (list == NULL)
2465 return NULL;
2466 raw = type->tp_subclasses;
2467 if (raw == NULL)
2468 return list;
2469 assert(PyList_Check(raw));
2470 n = PyList_GET_SIZE(raw);
2471 for (i = 0; i < n; i++) {
2472 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00002473 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00002474 ref = PyWeakref_GET_OBJECT(ref);
2475 if (ref != Py_None) {
2476 if (PyList_Append(list, ref) < 0) {
2477 Py_DECREF(list);
2478 return NULL;
2479 }
2480 }
2481 }
2482 return list;
2483}
2484
Guido van Rossum47374822007-08-02 16:48:17 +00002485static PyObject *
2486type_prepare(PyObject *self, PyObject *args, PyObject *kwds)
2487{
2488 return PyDict_New();
2489}
2490
Tim Peters6d6c1a32001-08-02 04:15:00 +00002491static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002492 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002493 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
Guido van Rossum1c450732001-10-08 15:18:27 +00002494 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002495 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
Guido van Rossum47374822007-08-02 16:48:17 +00002496 {"__prepare__", (PyCFunction)type_prepare,
2497 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
2498 PyDoc_STR("__prepare__() -> dict\n"
2499 "used to create the namespace for the class statement")},
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002500 {"__instancecheck__", type___instancecheck__, METH_O,
2501 PyDoc_STR("__instancecheck__() -> check if an object is an instance")},
2502 {"__subclasscheck__", type___subclasscheck__, METH_O,
2503 PyDoc_STR("__subclasschck__ -> check if an class is a subclass")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002504 {0}
2505};
2506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002507PyDoc_STRVAR(type_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002508"type(object) -> the object's type\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002509"type(name, bases, dict) -> a new type");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002510
Guido van Rossum048eb752001-10-02 21:24:57 +00002511static int
2512type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2513{
Guido van Rossuma3862092002-06-10 15:24:42 +00002514 /* Because of type_is_gc(), the collector only calls this
2515 for heaptypes. */
2516 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002517
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002518 Py_VISIT(type->tp_dict);
2519 Py_VISIT(type->tp_cache);
2520 Py_VISIT(type->tp_mro);
2521 Py_VISIT(type->tp_bases);
2522 Py_VISIT(type->tp_base);
Guido van Rossuma3862092002-06-10 15:24:42 +00002523
2524 /* There's no need to visit type->tp_subclasses or
Georg Brandlc255c7b2006-02-20 22:27:28 +00002525 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
Guido van Rossuma3862092002-06-10 15:24:42 +00002526 in cycles; tp_subclasses is a list of weak references,
2527 and slots is a tuple of strings. */
Guido van Rossum048eb752001-10-02 21:24:57 +00002528
Guido van Rossum048eb752001-10-02 21:24:57 +00002529 return 0;
2530}
2531
2532static int
2533type_clear(PyTypeObject *type)
2534{
Guido van Rossuma3862092002-06-10 15:24:42 +00002535 /* Because of type_is_gc(), the collector only calls this
2536 for heaptypes. */
2537 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002538
Guido van Rossuma3862092002-06-10 15:24:42 +00002539 /* The only field we need to clear is tp_mro, which is part of a
2540 hard cycle (its first element is the class itself) that won't
2541 be broken otherwise (it's a tuple and tuples don't have a
2542 tp_clear handler). None of the other fields need to be
2543 cleared, and here's why:
Guido van Rossum048eb752001-10-02 21:24:57 +00002544
Guido van Rossuma3862092002-06-10 15:24:42 +00002545 tp_dict:
2546 It is a dict, so the collector will call its tp_clear.
2547
2548 tp_cache:
2549 Not used; if it were, it would be a dict.
2550
2551 tp_bases, tp_base:
2552 If these are involved in a cycle, there must be at least
2553 one other, mutable object in the cycle, e.g. a base
2554 class's dict; the cycle will be broken that way.
2555
2556 tp_subclasses:
2557 A list of weak references can't be part of a cycle; and
2558 lists have their own tp_clear.
2559
Guido van Rossume5c691a2003-03-07 15:13:17 +00002560 slots (in PyHeapTypeObject):
Guido van Rossuma3862092002-06-10 15:24:42 +00002561 A tuple of strings can't be part of a cycle.
2562 */
2563
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002564 Py_CLEAR(type->tp_mro);
Guido van Rossum048eb752001-10-02 21:24:57 +00002565
2566 return 0;
2567}
2568
2569static int
2570type_is_gc(PyTypeObject *type)
2571{
2572 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2573}
2574
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002575PyTypeObject PyType_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002576 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002577 "type", /* tp_name */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002578 sizeof(PyHeapTypeObject), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00002579 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002580 (destructor)type_dealloc, /* tp_dealloc */
2581 0, /* tp_print */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002582 0, /* tp_getattr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002583 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002584 0, /* tp_reserved */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002585 (reprfunc)type_repr, /* tp_repr */
2586 0, /* tp_as_number */
2587 0, /* tp_as_sequence */
2588 0, /* tp_as_mapping */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002589 0, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002590 (ternaryfunc)type_call, /* tp_call */
2591 0, /* tp_str */
2592 (getattrofunc)type_getattro, /* tp_getattro */
2593 (setattrofunc)type_setattro, /* tp_setattro */
2594 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00002595 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00002596 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002597 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00002598 (traverseproc)type_traverse, /* tp_traverse */
2599 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002600 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00002601 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002602 0, /* tp_iter */
2603 0, /* tp_iternext */
2604 type_methods, /* tp_methods */
2605 type_members, /* tp_members */
2606 type_getsets, /* tp_getset */
2607 0, /* tp_base */
2608 0, /* tp_dict */
2609 0, /* tp_descr_get */
2610 0, /* tp_descr_set */
2611 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002612 type_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002613 0, /* tp_alloc */
2614 type_new, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002615 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00002616 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002617};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002618
2619
2620/* The base type of all types (eventually)... except itself. */
2621
Guido van Rossumd8faa362007-04-27 19:54:29 +00002622/* You may wonder why object.__new__() only complains about arguments
2623 when object.__init__() is not overridden, and vice versa.
2624
2625 Consider the use cases:
2626
2627 1. When neither is overridden, we want to hear complaints about
2628 excess (i.e., any) arguments, since their presence could
2629 indicate there's a bug.
2630
2631 2. When defining an Immutable type, we are likely to override only
2632 __new__(), since __init__() is called too late to initialize an
2633 Immutable object. Since __new__() defines the signature for the
2634 type, it would be a pain to have to override __init__() just to
2635 stop it from complaining about excess arguments.
2636
2637 3. When defining a Mutable type, we are likely to override only
2638 __init__(). So here the converse reasoning applies: we don't
2639 want to have to override __new__() just to stop it from
2640 complaining.
2641
2642 4. When __init__() is overridden, and the subclass __init__() calls
2643 object.__init__(), the latter should complain about excess
2644 arguments; ditto for __new__().
2645
2646 Use cases 2 and 3 make it unattractive to unconditionally check for
2647 excess arguments. The best solution that addresses all four use
2648 cases is as follows: __init__() complains about excess arguments
2649 unless __new__() is overridden and __init__() is not overridden
2650 (IOW, if __init__() is overridden or __new__() is not overridden);
2651 symmetrically, __new__() complains about excess arguments unless
2652 __init__() is overridden and __new__() is not overridden
2653 (IOW, if __new__() is overridden or __init__() is not overridden).
2654
2655 However, for backwards compatibility, this breaks too much code.
2656 Therefore, in 2.6, we'll *warn* about excess arguments when both
2657 methods are overridden; for all other cases we'll use the above
2658 rules.
2659
2660*/
2661
2662/* Forward */
2663static PyObject *
2664object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2665
2666static int
2667excess_args(PyObject *args, PyObject *kwds)
2668{
2669 return PyTuple_GET_SIZE(args) ||
2670 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
2671}
2672
Tim Peters6d6c1a32001-08-02 04:15:00 +00002673static int
2674object_init(PyObject *self, PyObject *args, PyObject *kwds)
2675{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002676 int err = 0;
2677 if (excess_args(args, kwds)) {
Christian Heimes90aa7642007-12-19 02:45:37 +00002678 PyTypeObject *type = Py_TYPE(self);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002679 if (type->tp_init != object_init &&
2680 type->tp_new != object_new)
2681 {
2682 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2683 "object.__init__() takes no parameters",
2684 1);
2685 }
2686 else if (type->tp_init != object_init ||
2687 type->tp_new == object_new)
2688 {
2689 PyErr_SetString(PyExc_TypeError,
2690 "object.__init__() takes no parameters");
2691 err = -1;
2692 }
2693 }
2694 return err;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002695}
2696
Guido van Rossum298e4212003-02-13 16:30:16 +00002697static PyObject *
2698object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2699{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002700 int err = 0;
2701 if (excess_args(args, kwds)) {
2702 if (type->tp_new != object_new &&
2703 type->tp_init != object_init)
2704 {
2705 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2706 "object.__new__() takes no parameters",
2707 1);
2708 }
2709 else if (type->tp_new != object_new ||
2710 type->tp_init == object_init)
2711 {
2712 PyErr_SetString(PyExc_TypeError,
2713 "object.__new__() takes no parameters");
2714 err = -1;
2715 }
Guido van Rossum298e4212003-02-13 16:30:16 +00002716 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002717 if (err < 0)
2718 return NULL;
Christian Heimes9e7f1d22008-02-28 12:27:11 +00002719
2720 if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
2721 static PyObject *comma = NULL;
2722 PyObject *abstract_methods = NULL;
2723 PyObject *builtins;
2724 PyObject *sorted;
2725 PyObject *sorted_methods = NULL;
2726 PyObject *joined = NULL;
2727
2728 /* Compute ", ".join(sorted(type.__abstractmethods__))
2729 into joined. */
2730 abstract_methods = type_abstractmethods(type, NULL);
2731 if (abstract_methods == NULL)
2732 goto error;
2733 builtins = PyEval_GetBuiltins();
2734 if (builtins == NULL)
2735 goto error;
2736 sorted = PyDict_GetItemString(builtins, "sorted");
2737 if (sorted == NULL)
2738 goto error;
2739 sorted_methods = PyObject_CallFunctionObjArgs(sorted,
2740 abstract_methods,
2741 NULL);
2742 if (sorted_methods == NULL)
2743 goto error;
2744 if (comma == NULL) {
2745 comma = PyUnicode_InternFromString(", ");
2746 if (comma == NULL)
2747 goto error;
2748 }
2749 joined = PyObject_CallMethod(comma, "join",
2750 "O", sorted_methods);
2751 if (joined == NULL)
2752 goto error;
2753
2754 PyErr_Format(PyExc_TypeError,
2755 "Can't instantiate abstract class %s "
2756 "with abstract methods %U",
2757 type->tp_name,
2758 joined);
2759 error:
2760 Py_XDECREF(joined);
2761 Py_XDECREF(sorted_methods);
2762 Py_XDECREF(abstract_methods);
2763 return NULL;
2764 }
Guido van Rossum298e4212003-02-13 16:30:16 +00002765 return type->tp_alloc(type, 0);
2766}
2767
Tim Peters6d6c1a32001-08-02 04:15:00 +00002768static void
2769object_dealloc(PyObject *self)
2770{
Christian Heimes90aa7642007-12-19 02:45:37 +00002771 Py_TYPE(self)->tp_free(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002772}
2773
Guido van Rossum8e248182001-08-12 05:17:56 +00002774static PyObject *
2775object_repr(PyObject *self)
2776{
Guido van Rossum76e69632001-08-16 18:52:43 +00002777 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00002778 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002779
Christian Heimes90aa7642007-12-19 02:45:37 +00002780 type = Py_TYPE(self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002781 mod = type_module(type, NULL);
2782 if (mod == NULL)
2783 PyErr_Clear();
Martin v. Löwis5b222132007-06-10 09:51:05 +00002784 else if (!PyUnicode_Check(mod)) {
Guido van Rossum76e69632001-08-16 18:52:43 +00002785 Py_DECREF(mod);
2786 mod = NULL;
2787 }
2788 name = type_name(type, NULL);
2789 if (name == NULL)
2790 return NULL;
Georg Brandl1a3284e2007-12-02 09:40:06 +00002791 if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
Walter Dörwald4dbd01b2007-06-11 14:03:45 +00002792 rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002793 else
Walter Dörwald1ab83302007-05-18 17:15:44 +00002794 rtn = PyUnicode_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002795 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002796 Py_XDECREF(mod);
2797 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00002798 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002799}
2800
Guido van Rossumb8f63662001-08-15 23:57:02 +00002801static PyObject *
2802object_str(PyObject *self)
2803{
2804 unaryfunc f;
2805
Christian Heimes90aa7642007-12-19 02:45:37 +00002806 f = Py_TYPE(self)->tp_repr;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002807 if (f == NULL)
2808 f = object_repr;
2809 return f(self);
2810}
2811
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002812static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002813object_richcompare(PyObject *self, PyObject *other, int op)
2814{
2815 PyObject *res;
2816
2817 switch (op) {
2818
2819 case Py_EQ:
Guido van Rossumab078dd2008-01-06 00:09:11 +00002820 /* Return NotImplemented instead of False, so if two
2821 objects are compared, both get a chance at the
2822 comparison. See issue #1393. */
2823 res = (self == other) ? Py_True : Py_NotImplemented;
Guido van Rossum6b18a5b2007-03-29 20:49:57 +00002824 Py_INCREF(res);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002825 break;
2826
2827 case Py_NE:
Guido van Rossume27dc722007-03-27 22:37:34 +00002828 /* By default, != returns the opposite of ==,
2829 unless the latter returns NotImplemented. */
2830 res = PyObject_RichCompare(self, other, Py_EQ);
2831 if (res != NULL && res != Py_NotImplemented) {
2832 int ok = PyObject_IsTrue(res);
2833 Py_DECREF(res);
2834 if (ok < 0)
2835 res = NULL;
2836 else {
2837 if (ok)
2838 res = Py_False;
2839 else
2840 res = Py_True;
2841 Py_INCREF(res);
2842 }
2843 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002844 break;
2845
2846 default:
2847 res = Py_NotImplemented;
Guido van Rossum6b18a5b2007-03-29 20:49:57 +00002848 Py_INCREF(res);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002849 break;
2850 }
2851
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002852 return res;
2853}
2854
2855static PyObject *
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002856object_get_class(PyObject *self, void *closure)
2857{
Christian Heimes90aa7642007-12-19 02:45:37 +00002858 Py_INCREF(Py_TYPE(self));
2859 return (PyObject *)(Py_TYPE(self));
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002860}
2861
2862static int
2863equiv_structs(PyTypeObject *a, PyTypeObject *b)
2864{
2865 return a == b ||
2866 (a != NULL &&
2867 b != NULL &&
2868 a->tp_basicsize == b->tp_basicsize &&
2869 a->tp_itemsize == b->tp_itemsize &&
2870 a->tp_dictoffset == b->tp_dictoffset &&
2871 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2872 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2873 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2874}
2875
2876static int
2877same_slots_added(PyTypeObject *a, PyTypeObject *b)
2878{
2879 PyTypeObject *base = a->tp_base;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002880 Py_ssize_t size;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002881 PyObject *slots_a, *slots_b;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002882
2883 if (base != b->tp_base)
2884 return 0;
2885 if (equiv_structs(a, base) && equiv_structs(b, base))
2886 return 1;
2887 size = base->tp_basicsize;
2888 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2889 size += sizeof(PyObject *);
2890 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2891 size += sizeof(PyObject *);
Guido van Rossumd8faa362007-04-27 19:54:29 +00002892
2893 /* Check slots compliance */
2894 slots_a = ((PyHeapTypeObject *)a)->ht_slots;
2895 slots_b = ((PyHeapTypeObject *)b)->ht_slots;
2896 if (slots_a && slots_b) {
Mark Dickinsonf02e0aa2009-02-01 12:13:56 +00002897 if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002898 return 0;
2899 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
2900 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002901 return size == a->tp_basicsize && size == b->tp_basicsize;
2902}
2903
2904static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002905compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002906{
2907 PyTypeObject *newbase, *oldbase;
2908
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002909 if (newto->tp_dealloc != oldto->tp_dealloc ||
2910 newto->tp_free != oldto->tp_free)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002911 {
2912 PyErr_Format(PyExc_TypeError,
2913 "%s assignment: "
2914 "'%s' deallocator differs from '%s'",
2915 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002916 newto->tp_name,
2917 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002918 return 0;
2919 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002920 newbase = newto;
2921 oldbase = oldto;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002922 while (equiv_structs(newbase, newbase->tp_base))
2923 newbase = newbase->tp_base;
2924 while (equiv_structs(oldbase, oldbase->tp_base))
2925 oldbase = oldbase->tp_base;
2926 if (newbase != oldbase &&
2927 (newbase->tp_base != oldbase->tp_base ||
2928 !same_slots_added(newbase, oldbase))) {
2929 PyErr_Format(PyExc_TypeError,
2930 "%s assignment: "
2931 "'%s' object layout differs from '%s'",
2932 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002933 newto->tp_name,
2934 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002935 return 0;
2936 }
Tim Petersea7f75d2002-12-07 21:39:16 +00002937
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002938 return 1;
2939}
2940
2941static int
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002942object_set_class(PyObject *self, PyObject *value, void *closure)
2943{
Christian Heimes90aa7642007-12-19 02:45:37 +00002944 PyTypeObject *oldto = Py_TYPE(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002945 PyTypeObject *newto;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002946
Guido van Rossumb6b89422002-04-15 01:03:30 +00002947 if (value == NULL) {
2948 PyErr_SetString(PyExc_TypeError,
2949 "can't delete __class__ attribute");
2950 return -1;
2951 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002952 if (!PyType_Check(value)) {
2953 PyErr_Format(PyExc_TypeError,
2954 "__class__ must be set to new-style class, not '%s' object",
Christian Heimes90aa7642007-12-19 02:45:37 +00002955 Py_TYPE(value)->tp_name);
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002956 return -1;
2957 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002958 newto = (PyTypeObject *)value;
2959 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2960 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
Guido van Rossum40af8892002-08-10 05:42:07 +00002961 {
2962 PyErr_Format(PyExc_TypeError,
2963 "__class__ assignment: only for heap types");
2964 return -1;
2965 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002966 if (compatible_for_assignment(newto, oldto, "__class__")) {
2967 Py_INCREF(newto);
Christian Heimes90aa7642007-12-19 02:45:37 +00002968 Py_TYPE(self) = newto;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002969 Py_DECREF(oldto);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002970 return 0;
2971 }
2972 else {
Guido van Rossum9ee4b942002-05-24 18:47:47 +00002973 return -1;
2974 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002975}
2976
2977static PyGetSetDef object_getsets[] = {
2978 {"__class__", object_get_class, object_set_class,
Neal Norwitz858e34f2002-08-13 17:18:45 +00002979 PyDoc_STR("the object's class")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002980 {0}
2981};
2982
Guido van Rossumc53f0092003-02-18 22:05:12 +00002983
Guido van Rossum036f9992003-02-21 22:02:54 +00002984/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002985 We fall back to helpers in copyreg for:
Guido van Rossum036f9992003-02-21 22:02:54 +00002986 - pickle protocols < 2
2987 - calculating the list of slot names (done only once per class)
2988 - the __newobj__ function (which is used as a token but never called)
2989*/
2990
2991static PyObject *
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002992import_copyreg(void)
Guido van Rossum036f9992003-02-21 22:02:54 +00002993{
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002994 static PyObject *copyreg_str;
Guido van Rossum3926a632001-09-25 16:25:58 +00002995
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00002996 if (!copyreg_str) {
2997 copyreg_str = PyUnicode_InternFromString("copyreg");
2998 if (copyreg_str == NULL)
Guido van Rossum3926a632001-09-25 16:25:58 +00002999 return NULL;
3000 }
Guido van Rossum036f9992003-02-21 22:02:54 +00003001
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003002 return PyImport_Import(copyreg_str);
Guido van Rossum036f9992003-02-21 22:02:54 +00003003}
3004
3005static PyObject *
3006slotnames(PyObject *cls)
3007{
3008 PyObject *clsdict;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003009 PyObject *copyreg;
Guido van Rossum036f9992003-02-21 22:02:54 +00003010 PyObject *slotnames;
3011
3012 if (!PyType_Check(cls)) {
3013 Py_INCREF(Py_None);
3014 return Py_None;
3015 }
3016
3017 clsdict = ((PyTypeObject *)cls)->tp_dict;
3018 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
Armin Rigoec862b92005-09-24 22:58:41 +00003019 if (slotnames != NULL && PyList_Check(slotnames)) {
Guido van Rossum036f9992003-02-21 22:02:54 +00003020 Py_INCREF(slotnames);
3021 return slotnames;
3022 }
3023
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003024 copyreg = import_copyreg();
3025 if (copyreg == NULL)
Guido van Rossum036f9992003-02-21 22:02:54 +00003026 return NULL;
3027
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003028 slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
3029 Py_DECREF(copyreg);
Guido van Rossum036f9992003-02-21 22:02:54 +00003030 if (slotnames != NULL &&
3031 slotnames != Py_None &&
3032 !PyList_Check(slotnames))
3033 {
3034 PyErr_SetString(PyExc_TypeError,
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003035 "copyreg._slotnames didn't return a list or None");
Guido van Rossum036f9992003-02-21 22:02:54 +00003036 Py_DECREF(slotnames);
3037 slotnames = NULL;
3038 }
3039
3040 return slotnames;
3041}
3042
3043static PyObject *
3044reduce_2(PyObject *obj)
3045{
3046 PyObject *cls, *getnewargs;
3047 PyObject *args = NULL, *args2 = NULL;
3048 PyObject *getstate = NULL, *state = NULL, *names = NULL;
3049 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003050 PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003051 Py_ssize_t i, n;
Guido van Rossum036f9992003-02-21 22:02:54 +00003052
3053 cls = PyObject_GetAttrString(obj, "__class__");
3054 if (cls == NULL)
3055 return NULL;
3056
3057 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
3058 if (getnewargs != NULL) {
3059 args = PyObject_CallObject(getnewargs, NULL);
3060 Py_DECREF(getnewargs);
3061 if (args != NULL && !PyTuple_Check(args)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003062 PyErr_Format(PyExc_TypeError,
3063 "__getnewargs__ should return a tuple, "
Christian Heimes90aa7642007-12-19 02:45:37 +00003064 "not '%.200s'", Py_TYPE(args)->tp_name);
Guido van Rossum036f9992003-02-21 22:02:54 +00003065 goto end;
3066 }
3067 }
3068 else {
3069 PyErr_Clear();
3070 args = PyTuple_New(0);
3071 }
3072 if (args == NULL)
3073 goto end;
3074
3075 getstate = PyObject_GetAttrString(obj, "__getstate__");
3076 if (getstate != NULL) {
3077 state = PyObject_CallObject(getstate, NULL);
3078 Py_DECREF(getstate);
Neal Norwitze2fdc612003-06-08 13:19:58 +00003079 if (state == NULL)
3080 goto end;
Guido van Rossum036f9992003-02-21 22:02:54 +00003081 }
3082 else {
Jim Fulton8a1a5942004-02-08 04:21:26 +00003083 PyErr_Clear();
Guido van Rossum036f9992003-02-21 22:02:54 +00003084 state = PyObject_GetAttrString(obj, "__dict__");
3085 if (state == NULL) {
3086 PyErr_Clear();
3087 state = Py_None;
3088 Py_INCREF(state);
3089 }
3090 names = slotnames(cls);
3091 if (names == NULL)
3092 goto end;
3093 if (names != Py_None) {
3094 assert(PyList_Check(names));
3095 slots = PyDict_New();
3096 if (slots == NULL)
3097 goto end;
3098 n = 0;
3099 /* Can't pre-compute the list size; the list
3100 is stored on the class so accessible to other
3101 threads, which may be run by DECREF */
3102 for (i = 0; i < PyList_GET_SIZE(names); i++) {
3103 PyObject *name, *value;
3104 name = PyList_GET_ITEM(names, i);
3105 value = PyObject_GetAttr(obj, name);
3106 if (value == NULL)
3107 PyErr_Clear();
3108 else {
3109 int err = PyDict_SetItem(slots, name,
3110 value);
3111 Py_DECREF(value);
3112 if (err)
3113 goto end;
3114 n++;
3115 }
3116 }
3117 if (n) {
3118 state = Py_BuildValue("(NO)", state, slots);
3119 if (state == NULL)
3120 goto end;
3121 }
3122 }
3123 }
3124
3125 if (!PyList_Check(obj)) {
3126 listitems = Py_None;
3127 Py_INCREF(listitems);
3128 }
3129 else {
3130 listitems = PyObject_GetIter(obj);
3131 if (listitems == NULL)
3132 goto end;
3133 }
3134
3135 if (!PyDict_Check(obj)) {
3136 dictitems = Py_None;
3137 Py_INCREF(dictitems);
3138 }
3139 else {
Guido van Rossumcc2b0162007-02-11 06:12:03 +00003140 PyObject *items = PyObject_CallMethod(obj, "items", "");
3141 if (items == NULL)
3142 goto end;
3143 dictitems = PyObject_GetIter(items);
3144 Py_DECREF(items);
Guido van Rossum036f9992003-02-21 22:02:54 +00003145 if (dictitems == NULL)
3146 goto end;
3147 }
3148
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003149 copyreg = import_copyreg();
3150 if (copyreg == NULL)
Guido van Rossum036f9992003-02-21 22:02:54 +00003151 goto end;
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003152 newobj = PyObject_GetAttrString(copyreg, "__newobj__");
Guido van Rossum036f9992003-02-21 22:02:54 +00003153 if (newobj == NULL)
3154 goto end;
3155
3156 n = PyTuple_GET_SIZE(args);
3157 args2 = PyTuple_New(n+1);
3158 if (args2 == NULL)
3159 goto end;
3160 PyTuple_SET_ITEM(args2, 0, cls);
3161 cls = NULL;
3162 for (i = 0; i < n; i++) {
3163 PyObject *v = PyTuple_GET_ITEM(args, i);
3164 Py_INCREF(v);
3165 PyTuple_SET_ITEM(args2, i+1, v);
3166 }
3167
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003168 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
Guido van Rossum036f9992003-02-21 22:02:54 +00003169
3170 end:
3171 Py_XDECREF(cls);
3172 Py_XDECREF(args);
3173 Py_XDECREF(args2);
Jeremy Hyltond06483c2003-04-09 21:01:42 +00003174 Py_XDECREF(slots);
Guido van Rossum036f9992003-02-21 22:02:54 +00003175 Py_XDECREF(state);
3176 Py_XDECREF(names);
3177 Py_XDECREF(listitems);
3178 Py_XDECREF(dictitems);
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003179 Py_XDECREF(copyreg);
Guido van Rossum036f9992003-02-21 22:02:54 +00003180 Py_XDECREF(newobj);
3181 return res;
3182}
3183
Guido van Rossumd8faa362007-04-27 19:54:29 +00003184/*
3185 * There were two problems when object.__reduce__ and object.__reduce_ex__
3186 * were implemented in the same function:
3187 * - trying to pickle an object with a custom __reduce__ method that
3188 * fell back to object.__reduce__ in certain circumstances led to
3189 * infinite recursion at Python level and eventual RuntimeError.
3190 * - Pickling objects that lied about their type by overwriting the
3191 * __class__ descriptor could lead to infinite recursion at C level
3192 * and eventual segfault.
3193 *
3194 * Because of backwards compatibility, the two methods still have to
3195 * behave in the same way, even if this is not required by the pickle
3196 * protocol. This common functionality was moved to the _common_reduce
3197 * function.
3198 */
3199static PyObject *
3200_common_reduce(PyObject *self, int proto)
3201{
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003202 PyObject *copyreg, *res;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003203
3204 if (proto >= 2)
3205 return reduce_2(self);
3206
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003207 copyreg = import_copyreg();
3208 if (!copyreg)
Guido van Rossumd8faa362007-04-27 19:54:29 +00003209 return NULL;
3210
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003211 res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
3212 Py_DECREF(copyreg);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003213
3214 return res;
3215}
3216
3217static PyObject *
3218object_reduce(PyObject *self, PyObject *args)
3219{
3220 int proto = 0;
3221
3222 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
3223 return NULL;
3224
3225 return _common_reduce(self, proto);
3226}
3227
Guido van Rossum036f9992003-02-21 22:02:54 +00003228static PyObject *
3229object_reduce_ex(PyObject *self, PyObject *args)
3230{
Guido van Rossumd8faa362007-04-27 19:54:29 +00003231 PyObject *reduce, *res;
Guido van Rossum036f9992003-02-21 22:02:54 +00003232 int proto = 0;
3233
3234 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
3235 return NULL;
3236
3237 reduce = PyObject_GetAttrString(self, "__reduce__");
3238 if (reduce == NULL)
3239 PyErr_Clear();
3240 else {
3241 PyObject *cls, *clsreduce, *objreduce;
3242 int override;
3243 cls = PyObject_GetAttrString(self, "__class__");
3244 if (cls == NULL) {
3245 Py_DECREF(reduce);
3246 return NULL;
3247 }
3248 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
3249 Py_DECREF(cls);
3250 if (clsreduce == NULL) {
3251 Py_DECREF(reduce);
3252 return NULL;
3253 }
3254 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
3255 "__reduce__");
3256 override = (clsreduce != objreduce);
3257 Py_DECREF(clsreduce);
3258 if (override) {
3259 res = PyObject_CallObject(reduce, NULL);
3260 Py_DECREF(reduce);
3261 return res;
3262 }
3263 else
3264 Py_DECREF(reduce);
3265 }
3266
Guido van Rossumd8faa362007-04-27 19:54:29 +00003267 return _common_reduce(self, proto);
Guido van Rossum3926a632001-09-25 16:25:58 +00003268}
3269
Christian Heimes9e7f1d22008-02-28 12:27:11 +00003270static PyObject *
3271object_subclasshook(PyObject *cls, PyObject *args)
3272{
3273 Py_INCREF(Py_NotImplemented);
3274 return Py_NotImplemented;
3275}
3276
3277PyDoc_STRVAR(object_subclasshook_doc,
3278"Abstract classes can override this to customize issubclass().\n"
3279"\n"
3280"This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
3281"It should return True, False or NotImplemented. If it returns\n"
3282"NotImplemented, the normal algorithm is used. Otherwise, it\n"
3283"overrides the normal algorithm (and the outcome is cached).\n");
Eric Smith8c663262007-08-25 02:26:07 +00003284
3285/*
3286 from PEP 3101, this code implements:
3287
3288 class object:
3289 def __format__(self, format_spec):
3290 return format(str(self), format_spec)
3291*/
3292static PyObject *
3293object_format(PyObject *self, PyObject *args)
3294{
3295 PyObject *format_spec;
3296 PyObject *self_as_str = NULL;
3297 PyObject *result = NULL;
3298 PyObject *format_meth = NULL;
3299
Eric Smithfc6e8fe2008-01-11 00:17:22 +00003300 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
Eric Smith8c663262007-08-25 02:26:07 +00003301 return NULL;
Eric Smith8c663262007-08-25 02:26:07 +00003302
Thomas Heller519a0422007-11-15 20:48:54 +00003303 self_as_str = PyObject_Str(self);
Eric Smith8c663262007-08-25 02:26:07 +00003304 if (self_as_str != NULL) {
3305 /* find the format function */
3306 format_meth = PyObject_GetAttrString(self_as_str, "__format__");
3307 if (format_meth != NULL) {
3308 /* and call it */
3309 result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL);
3310 }
3311 }
3312
3313 Py_XDECREF(self_as_str);
3314 Py_XDECREF(format_meth);
3315
3316 return result;
3317}
3318
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003319static PyObject *
3320object_sizeof(PyObject *self, PyObject *args)
3321{
3322 Py_ssize_t res, isize;
3323
3324 res = 0;
3325 isize = self->ob_type->tp_itemsize;
3326 if (isize > 0)
3327 res = Py_SIZE(self->ob_type) * isize;
3328 res += self->ob_type->tp_basicsize;
3329
3330 return PyLong_FromSsize_t(res);
3331}
3332
Guido van Rossum3926a632001-09-25 16:25:58 +00003333static PyMethodDef object_methods[] = {
Guido van Rossumc53f0092003-02-18 22:05:12 +00003334 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
3335 PyDoc_STR("helper for pickle")},
Guido van Rossumd8faa362007-04-27 19:54:29 +00003336 {"__reduce__", object_reduce, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003337 PyDoc_STR("helper for pickle")},
Christian Heimes9e7f1d22008-02-28 12:27:11 +00003338 {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
3339 object_subclasshook_doc},
Eric Smith8c663262007-08-25 02:26:07 +00003340 {"__format__", object_format, METH_VARARGS,
3341 PyDoc_STR("default object formatter")},
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003342 {"__sizeof__", object_sizeof, METH_NOARGS,
3343 PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
Guido van Rossum3926a632001-09-25 16:25:58 +00003344 {0}
3345};
3346
Guido van Rossum036f9992003-02-21 22:02:54 +00003347
Tim Peters6d6c1a32001-08-02 04:15:00 +00003348PyTypeObject PyBaseObject_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003349 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003350 "object", /* tp_name */
3351 sizeof(PyObject), /* tp_basicsize */
3352 0, /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003353 object_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003354 0, /* tp_print */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003355 0, /* tp_getattr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003356 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00003357 0, /* tp_reserved */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003358 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003359 0, /* tp_as_number */
3360 0, /* tp_as_sequence */
3361 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003362 (hashfunc)_Py_HashPointer, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003363 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003364 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003365 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00003366 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003367 0, /* tp_as_buffer */
3368 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003369 PyDoc_STR("The most base type"), /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003370 0, /* tp_traverse */
3371 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003372 object_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373 0, /* tp_weaklistoffset */
3374 0, /* tp_iter */
3375 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00003376 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00003377 0, /* tp_members */
3378 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003379 0, /* tp_base */
3380 0, /* tp_dict */
3381 0, /* tp_descr_get */
3382 0, /* tp_descr_set */
3383 0, /* tp_dictoffset */
3384 object_init, /* tp_init */
3385 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossum298e4212003-02-13 16:30:16 +00003386 object_new, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003387 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003388};
3389
3390
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003391/* Add the methods from tp_methods to the __dict__ in a type object */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003392
3393static int
3394add_methods(PyTypeObject *type, PyMethodDef *meth)
3395{
Guido van Rossum687ae002001-10-15 22:03:32 +00003396 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003397
3398 for (; meth->ml_name != NULL; meth++) {
3399 PyObject *descr;
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003400 if (PyDict_GetItemString(dict, meth->ml_name) &&
3401 !(meth->ml_flags & METH_COEXIST))
3402 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00003403 if (meth->ml_flags & METH_CLASS) {
3404 if (meth->ml_flags & METH_STATIC) {
3405 PyErr_SetString(PyExc_ValueError,
3406 "method cannot be both class and static");
3407 return -1;
3408 }
Tim Petersbca1cbc2002-12-09 22:56:13 +00003409 descr = PyDescr_NewClassMethod(type, meth);
Fred Drake7bf97152002-03-28 05:33:33 +00003410 }
3411 else if (meth->ml_flags & METH_STATIC) {
Guido van Rossum9af48ff2003-02-11 17:12:46 +00003412 PyObject *cfunc = PyCFunction_New(meth, NULL);
3413 if (cfunc == NULL)
3414 return -1;
3415 descr = PyStaticMethod_New(cfunc);
3416 Py_DECREF(cfunc);
Fred Drake7bf97152002-03-28 05:33:33 +00003417 }
3418 else {
3419 descr = PyDescr_NewMethod(type, meth);
3420 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003421 if (descr == NULL)
3422 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00003423 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003424 return -1;
3425 Py_DECREF(descr);
3426 }
3427 return 0;
3428}
3429
3430static int
Guido van Rossum6f799372001-09-20 20:46:19 +00003431add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003432{
Guido van Rossum687ae002001-10-15 22:03:32 +00003433 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003434
3435 for (; memb->name != NULL; memb++) {
3436 PyObject *descr;
3437 if (PyDict_GetItemString(dict, memb->name))
3438 continue;
3439 descr = PyDescr_NewMember(type, memb);
3440 if (descr == NULL)
3441 return -1;
3442 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
3443 return -1;
3444 Py_DECREF(descr);
3445 }
3446 return 0;
3447}
3448
3449static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00003450add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003451{
Guido van Rossum687ae002001-10-15 22:03:32 +00003452 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003453
3454 for (; gsp->name != NULL; gsp++) {
3455 PyObject *descr;
3456 if (PyDict_GetItemString(dict, gsp->name))
3457 continue;
3458 descr = PyDescr_NewGetSet(type, gsp);
3459
3460 if (descr == NULL)
3461 return -1;
3462 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
3463 return -1;
3464 Py_DECREF(descr);
3465 }
3466 return 0;
3467}
3468
Guido van Rossum13d52f02001-08-10 21:24:08 +00003469static void
3470inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003471{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003472 Py_ssize_t oldsize, newsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003473
Guido van Rossum13d52f02001-08-10 21:24:08 +00003474 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00003475 oldsize = base->tp_basicsize;
3476 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
3477 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3478 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00003479 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00003480 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003481 if (type->tp_traverse == NULL)
3482 type->tp_traverse = base->tp_traverse;
3483 if (type->tp_clear == NULL)
3484 type->tp_clear = base->tp_clear;
3485 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003486 {
Guido van Rossumf884b742001-12-17 17:14:22 +00003487 /* The condition below could use some explanation.
3488 It appears that tp_new is not inherited for static types
3489 whose base class is 'object'; this seems to be a precaution
3490 so that old extension types don't suddenly become
3491 callable (object.__new__ wouldn't insure the invariants
3492 that the extension type's own factory function ensures).
3493 Heap types, of course, are under our control, so they do
3494 inherit tp_new; static extension types that specify some
3495 other built-in type as the default are considered
3496 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00003497 if (base != &PyBaseObject_Type ||
3498 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3499 if (type->tp_new == NULL)
3500 type->tp_new = base->tp_new;
3501 }
3502 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00003503 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00003504
3505 /* Copy other non-function slots */
3506
3507#undef COPYVAL
3508#define COPYVAL(SLOT) \
3509 if (type->SLOT == 0) type->SLOT = base->SLOT
3510
3511 COPYVAL(tp_itemsize);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003512 COPYVAL(tp_weaklistoffset);
3513 COPYVAL(tp_dictoffset);
Thomas Wouters27d517b2007-02-25 20:39:11 +00003514
3515 /* Setup fast subclass flags */
3516 if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
3517 type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
3518 else if (PyType_IsSubtype(base, &PyType_Type))
3519 type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
3520 else if (PyType_IsSubtype(base, &PyLong_Type))
3521 type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
Christian Heimes72b710a2008-05-26 13:28:38 +00003522 else if (PyType_IsSubtype(base, &PyBytes_Type))
3523 type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS;
Thomas Wouters27d517b2007-02-25 20:39:11 +00003524 else if (PyType_IsSubtype(base, &PyUnicode_Type))
3525 type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
3526 else if (PyType_IsSubtype(base, &PyTuple_Type))
3527 type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
3528 else if (PyType_IsSubtype(base, &PyList_Type))
3529 type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
3530 else if (PyType_IsSubtype(base, &PyDict_Type))
3531 type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003532}
3533
Guido van Rossumf5243f02008-01-01 04:06:48 +00003534static char *hash_name_op[] = {
Guido van Rossum38938152006-08-21 23:36:26 +00003535 "__eq__",
Guido van Rossum38938152006-08-21 23:36:26 +00003536 "__hash__",
Guido van Rossumf5243f02008-01-01 04:06:48 +00003537 NULL
Guido van Rossum38938152006-08-21 23:36:26 +00003538};
3539
3540static int
Guido van Rossumf5243f02008-01-01 04:06:48 +00003541overrides_hash(PyTypeObject *type)
Guido van Rossum38938152006-08-21 23:36:26 +00003542{
Guido van Rossumf5243f02008-01-01 04:06:48 +00003543 char **p;
Guido van Rossum38938152006-08-21 23:36:26 +00003544 PyObject *dict = type->tp_dict;
3545
3546 assert(dict != NULL);
Guido van Rossumf5243f02008-01-01 04:06:48 +00003547 for (p = hash_name_op; *p; p++) {
3548 if (PyDict_GetItemString(dict, *p) != NULL)
Guido van Rossum38938152006-08-21 23:36:26 +00003549 return 1;
3550 }
3551 return 0;
3552}
3553
Guido van Rossum13d52f02001-08-10 21:24:08 +00003554static void
3555inherit_slots(PyTypeObject *type, PyTypeObject *base)
3556{
3557 PyTypeObject *basebase;
3558
3559#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00003560#undef COPYSLOT
3561#undef COPYNUM
3562#undef COPYSEQ
3563#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00003564#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00003565
3566#define SLOTDEFINED(SLOT) \
3567 (base->SLOT != 0 && \
3568 (basebase == NULL || base->SLOT != basebase->SLOT))
3569
Tim Peters6d6c1a32001-08-02 04:15:00 +00003570#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00003571 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00003572
3573#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3574#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3575#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00003576#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003577
Guido van Rossum13d52f02001-08-10 21:24:08 +00003578 /* This won't inherit indirect slots (from tp_as_number etc.)
3579 if type doesn't provide the space. */
3580
3581 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3582 basebase = base->tp_base;
3583 if (basebase->tp_as_number == NULL)
3584 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003585 COPYNUM(nb_add);
3586 COPYNUM(nb_subtract);
3587 COPYNUM(nb_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003588 COPYNUM(nb_remainder);
3589 COPYNUM(nb_divmod);
3590 COPYNUM(nb_power);
3591 COPYNUM(nb_negative);
3592 COPYNUM(nb_positive);
3593 COPYNUM(nb_absolute);
Jack Diederich4dafcc42006-11-28 19:15:13 +00003594 COPYNUM(nb_bool);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003595 COPYNUM(nb_invert);
3596 COPYNUM(nb_lshift);
3597 COPYNUM(nb_rshift);
3598 COPYNUM(nb_and);
3599 COPYNUM(nb_xor);
3600 COPYNUM(nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003601 COPYNUM(nb_int);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003602 COPYNUM(nb_float);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003603 COPYNUM(nb_inplace_add);
3604 COPYNUM(nb_inplace_subtract);
3605 COPYNUM(nb_inplace_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003606 COPYNUM(nb_inplace_remainder);
3607 COPYNUM(nb_inplace_power);
3608 COPYNUM(nb_inplace_lshift);
3609 COPYNUM(nb_inplace_rshift);
3610 COPYNUM(nb_inplace_and);
3611 COPYNUM(nb_inplace_xor);
3612 COPYNUM(nb_inplace_or);
Neal Norwitzbcc0db82006-03-24 08:14:36 +00003613 COPYNUM(nb_true_divide);
3614 COPYNUM(nb_floor_divide);
3615 COPYNUM(nb_inplace_true_divide);
3616 COPYNUM(nb_inplace_floor_divide);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003617 COPYNUM(nb_index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003618 }
3619
Guido van Rossum13d52f02001-08-10 21:24:08 +00003620 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3621 basebase = base->tp_base;
3622 if (basebase->tp_as_sequence == NULL)
3623 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003624 COPYSEQ(sq_length);
3625 COPYSEQ(sq_concat);
3626 COPYSEQ(sq_repeat);
3627 COPYSEQ(sq_item);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003628 COPYSEQ(sq_ass_item);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003629 COPYSEQ(sq_contains);
3630 COPYSEQ(sq_inplace_concat);
3631 COPYSEQ(sq_inplace_repeat);
3632 }
3633
Guido van Rossum13d52f02001-08-10 21:24:08 +00003634 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3635 basebase = base->tp_base;
3636 if (basebase->tp_as_mapping == NULL)
3637 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003638 COPYMAP(mp_length);
3639 COPYMAP(mp_subscript);
3640 COPYMAP(mp_ass_subscript);
3641 }
3642
Tim Petersfc57ccb2001-10-12 02:38:24 +00003643 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3644 basebase = base->tp_base;
3645 if (basebase->tp_as_buffer == NULL)
3646 basebase = NULL;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00003647 COPYBUF(bf_getbuffer);
3648 COPYBUF(bf_releasebuffer);
Tim Petersfc57ccb2001-10-12 02:38:24 +00003649 }
3650
Guido van Rossum13d52f02001-08-10 21:24:08 +00003651 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003652
Tim Peters6d6c1a32001-08-02 04:15:00 +00003653 COPYSLOT(tp_dealloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003654 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3655 type->tp_getattr = base->tp_getattr;
3656 type->tp_getattro = base->tp_getattro;
3657 }
3658 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3659 type->tp_setattr = base->tp_setattr;
3660 type->tp_setattro = base->tp_setattro;
3661 }
Mark Dickinsone94c6792009-02-02 20:36:42 +00003662 /* tp_reserved is ignored */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003663 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003664 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003665 COPYSLOT(tp_call);
3666 COPYSLOT(tp_str);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003667 {
Guido van Rossum38938152006-08-21 23:36:26 +00003668 /* Copy comparison-related slots only when
3669 not overriding them anywhere */
Mark Dickinsonc008a172009-02-01 13:59:22 +00003670 if (type->tp_richcompare == NULL &&
Guido van Rossum38938152006-08-21 23:36:26 +00003671 type->tp_hash == NULL &&
Guido van Rossumf5243f02008-01-01 04:06:48 +00003672 !overrides_hash(type))
Guido van Rossumb8f63662001-08-15 23:57:02 +00003673 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003674 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003675 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003676 }
3677 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003678 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003679 COPYSLOT(tp_iter);
3680 COPYSLOT(tp_iternext);
3681 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003682 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003683 COPYSLOT(tp_descr_get);
3684 COPYSLOT(tp_descr_set);
3685 COPYSLOT(tp_dictoffset);
3686 COPYSLOT(tp_init);
3687 COPYSLOT(tp_alloc);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00003688 COPYSLOT(tp_is_gc);
Tim Peters3cfe7542003-05-21 21:29:48 +00003689 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3690 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3691 /* They agree about gc. */
3692 COPYSLOT(tp_free);
3693 }
3694 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3695 type->tp_free == NULL &&
Neal Norwitz30d1c512007-08-19 22:48:23 +00003696 base->tp_free == PyObject_Free) {
Tim Peters3cfe7542003-05-21 21:29:48 +00003697 /* A bit of magic to plug in the correct default
3698 * tp_free function when a derived class adds gc,
3699 * didn't define tp_free, and the base uses the
3700 * default non-gc tp_free.
3701 */
3702 type->tp_free = PyObject_GC_Del;
3703 }
3704 /* else they didn't agree about gc, and there isn't something
3705 * obvious to be done -- the type is on its own.
3706 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003707 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003708}
3709
Jeremy Hylton938ace62002-07-17 16:30:39 +00003710static int add_operators(PyTypeObject *);
Guido van Rossum13d52f02001-08-10 21:24:08 +00003711
Tim Peters6d6c1a32001-08-02 04:15:00 +00003712int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003713PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003714{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003715 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003716 PyTypeObject *base;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003717 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003718
Guido van Rossumcab05802002-06-10 15:29:03 +00003719 if (type->tp_flags & Py_TPFLAGS_READY) {
3720 assert(type->tp_dict != NULL);
Guido van Rossumd614f972001-08-10 17:39:49 +00003721 return 0;
Guido van Rossumcab05802002-06-10 15:29:03 +00003722 }
Guido van Rossumd614f972001-08-10 17:39:49 +00003723 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00003724
3725 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003726
Tim Peters36eb4df2003-03-23 03:33:13 +00003727#ifdef Py_TRACE_REFS
3728 /* PyType_Ready is the closest thing we have to a choke point
3729 * for type objects, so is the best place I can think of to try
3730 * to get type objects into the doubly-linked list of all objects.
3731 * Still, not all type objects go thru PyType_Ready.
3732 */
Tim Peters7571a0f2003-03-23 17:52:28 +00003733 _Py_AddToAllObjects((PyObject *)type, 0);
Tim Peters36eb4df2003-03-23 03:33:13 +00003734#endif
3735
Tim Peters6d6c1a32001-08-02 04:15:00 +00003736 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3737 base = type->tp_base;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003738 if (base == NULL && type != &PyBaseObject_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003739 base = type->tp_base = &PyBaseObject_Type;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003740 Py_INCREF(base);
3741 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003742
Guido van Rossumd8faa362007-04-27 19:54:29 +00003743 /* Now the only way base can still be NULL is if type is
3744 * &PyBaseObject_Type.
3745 */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003746
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003747 /* Initialize the base class */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003748 if (base != NULL && base->tp_dict == NULL) {
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003749 if (PyType_Ready(base) < 0)
3750 goto error;
3751 }
3752
Guido van Rossumd8faa362007-04-27 19:54:29 +00003753 /* Initialize ob_type if NULL. This means extensions that want to be
Guido van Rossum0986d822002-04-08 01:38:42 +00003754 compilable separately on Windows can call PyType_Ready() instead of
3755 initializing the ob_type field of their type objects. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00003756 /* The test for base != NULL is really unnecessary, since base is only
3757 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3758 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3759 know that. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003760 if (Py_TYPE(type) == NULL && base != NULL)
3761 Py_TYPE(type) = Py_TYPE(base);
Guido van Rossum0986d822002-04-08 01:38:42 +00003762
Tim Peters6d6c1a32001-08-02 04:15:00 +00003763 /* Initialize tp_bases */
3764 bases = type->tp_bases;
3765 if (bases == NULL) {
3766 if (base == NULL)
3767 bases = PyTuple_New(0);
3768 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003769 bases = PyTuple_Pack(1, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003770 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003771 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003772 type->tp_bases = bases;
3773 }
3774
Guido van Rossum687ae002001-10-15 22:03:32 +00003775 /* Initialize tp_dict */
3776 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777 if (dict == NULL) {
3778 dict = PyDict_New();
3779 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003780 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00003781 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003782 }
3783
Guido van Rossum687ae002001-10-15 22:03:32 +00003784 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003785 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003786 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003787 if (type->tp_methods != NULL) {
3788 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003789 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003790 }
3791 if (type->tp_members != NULL) {
3792 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003793 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003794 }
3795 if (type->tp_getset != NULL) {
3796 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003797 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003798 }
3799
Tim Peters6d6c1a32001-08-02 04:15:00 +00003800 /* Calculate method resolution order */
3801 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00003802 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003803 }
3804
Guido van Rossum13d52f02001-08-10 21:24:08 +00003805 /* Inherit special flags from dominant base */
3806 if (type->tp_base != NULL)
3807 inherit_special(type, type->tp_base);
3808
Tim Peters6d6c1a32001-08-02 04:15:00 +00003809 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003810 bases = type->tp_mro;
3811 assert(bases != NULL);
3812 assert(PyTuple_Check(bases));
3813 n = PyTuple_GET_SIZE(bases);
3814 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003815 PyObject *b = PyTuple_GET_ITEM(bases, i);
3816 if (PyType_Check(b))
3817 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003818 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003819
Tim Peters3cfe7542003-05-21 21:29:48 +00003820 /* Sanity check for tp_free. */
3821 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3822 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003823 /* This base class needs to call tp_free, but doesn't have
3824 * one, or its tp_free is for non-gc'ed objects.
3825 */
Tim Peters3cfe7542003-05-21 21:29:48 +00003826 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3827 "gc and is a base type but has inappropriate "
3828 "tp_free slot",
3829 type->tp_name);
3830 goto error;
3831 }
3832
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003833 /* if the type dictionary doesn't contain a __doc__, set it from
3834 the tp_doc slot.
3835 */
3836 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3837 if (type->tp_doc != NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +00003838 PyObject *doc = PyUnicode_FromString(type->tp_doc);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003839 if (doc == NULL)
3840 goto error;
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003841 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3842 Py_DECREF(doc);
3843 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00003844 PyDict_SetItemString(type->tp_dict,
3845 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003846 }
3847 }
3848
Guido van Rossum38938152006-08-21 23:36:26 +00003849 /* Hack for tp_hash and __hash__.
3850 If after all that, tp_hash is still NULL, and __hash__ is not in
Nick Coghland1abd252008-07-15 15:46:38 +00003851 tp_dict, set tp_hash to PyObject_HashNotImplemented and
3852 tp_dict['__hash__'] equal to None.
Guido van Rossum38938152006-08-21 23:36:26 +00003853 This signals that __hash__ is not inherited.
3854 */
3855 if (type->tp_hash == NULL) {
3856 if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
3857 if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
3858 goto error;
Nick Coghland1abd252008-07-15 15:46:38 +00003859 type->tp_hash = PyObject_HashNotImplemented;
Guido van Rossum38938152006-08-21 23:36:26 +00003860 }
3861 }
3862
Guido van Rossum13d52f02001-08-10 21:24:08 +00003863 /* Some more special stuff */
3864 base = type->tp_base;
3865 if (base != NULL) {
3866 if (type->tp_as_number == NULL)
3867 type->tp_as_number = base->tp_as_number;
3868 if (type->tp_as_sequence == NULL)
3869 type->tp_as_sequence = base->tp_as_sequence;
3870 if (type->tp_as_mapping == NULL)
3871 type->tp_as_mapping = base->tp_as_mapping;
Guido van Rossumeea47182003-02-11 20:39:59 +00003872 if (type->tp_as_buffer == NULL)
3873 type->tp_as_buffer = base->tp_as_buffer;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003874 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003875
Guido van Rossum1c450732001-10-08 15:18:27 +00003876 /* Link into each base class's list of subclasses */
3877 bases = type->tp_bases;
3878 n = PyTuple_GET_SIZE(bases);
3879 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003880 PyObject *b = PyTuple_GET_ITEM(bases, i);
3881 if (PyType_Check(b) &&
3882 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00003883 goto error;
3884 }
3885
Mark Dickinson2a7d45b2009-02-08 11:02:10 +00003886 /* Warn for a type that implements tp_compare (now known as
3887 tp_reserved) but not tp_richcompare. */
3888 if (type->tp_reserved && !type->tp_richcompare) {
3889 int error;
3890 char msg[240];
3891 PyOS_snprintf(msg, sizeof(msg),
3892 "Type %.100s defines tp_reserved (formerly "
3893 "tp_compare) but not tp_richcompare. "
3894 "Comparisons may not behave as intended.",
3895 type->tp_name);
3896 error = PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1);
3897 if (error == -1)
3898 goto error;
3899 }
3900
Guido van Rossum13d52f02001-08-10 21:24:08 +00003901 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00003902 assert(type->tp_dict != NULL);
3903 type->tp_flags =
3904 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003905 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00003906
3907 error:
3908 type->tp_flags &= ~Py_TPFLAGS_READYING;
3909 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003910}
3911
Guido van Rossum1c450732001-10-08 15:18:27 +00003912static int
3913add_subclass(PyTypeObject *base, PyTypeObject *type)
3914{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003915 Py_ssize_t i;
3916 int result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003917 PyObject *list, *ref, *newobj;
Guido van Rossum1c450732001-10-08 15:18:27 +00003918
3919 list = base->tp_subclasses;
3920 if (list == NULL) {
3921 base->tp_subclasses = list = PyList_New(0);
3922 if (list == NULL)
3923 return -1;
3924 }
3925 assert(PyList_Check(list));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003926 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
Guido van Rossum1c450732001-10-08 15:18:27 +00003927 i = PyList_GET_SIZE(list);
3928 while (--i >= 0) {
3929 ref = PyList_GET_ITEM(list, i);
3930 assert(PyWeakref_CheckRef(ref));
Guido van Rossum3930bc32002-10-18 13:51:49 +00003931 if (PyWeakref_GET_OBJECT(ref) == Py_None)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003932 return PyList_SetItem(list, i, newobj);
Guido van Rossum1c450732001-10-08 15:18:27 +00003933 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003934 result = PyList_Append(list, newobj);
3935 Py_DECREF(newobj);
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003936 return result;
Guido van Rossum1c450732001-10-08 15:18:27 +00003937}
3938
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003939static void
3940remove_subclass(PyTypeObject *base, PyTypeObject *type)
3941{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003942 Py_ssize_t i;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003943 PyObject *list, *ref;
3944
3945 list = base->tp_subclasses;
3946 if (list == NULL) {
3947 return;
3948 }
3949 assert(PyList_Check(list));
3950 i = PyList_GET_SIZE(list);
3951 while (--i >= 0) {
3952 ref = PyList_GET_ITEM(list, i);
3953 assert(PyWeakref_CheckRef(ref));
3954 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3955 /* this can't fail, right? */
3956 PySequence_DelItem(list, i);
3957 return;
3958 }
3959 }
3960}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003961
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003962static int
3963check_num_args(PyObject *ob, int n)
3964{
3965 if (!PyTuple_CheckExact(ob)) {
3966 PyErr_SetString(PyExc_SystemError,
3967 "PyArg_UnpackTuple() argument list is not a tuple");
3968 return 0;
3969 }
3970 if (n == PyTuple_GET_SIZE(ob))
3971 return 1;
3972 PyErr_Format(
3973 PyExc_TypeError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00003974 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003975 return 0;
3976}
3977
Tim Peters6d6c1a32001-08-02 04:15:00 +00003978/* Generic wrappers for overloadable 'operators' such as __getitem__ */
3979
3980/* There's a wrapper *function* for each distinct function typedef used
Guido van Rossumd8faa362007-04-27 19:54:29 +00003981 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
Tim Peters6d6c1a32001-08-02 04:15:00 +00003982 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3983 Most tables have only one entry; the tables for binary operators have two
3984 entries, one regular and one with reversed arguments. */
3985
3986static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003987wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003988{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003989 lenfunc func = (lenfunc)wrapped;
3990 Py_ssize_t res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003991
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003992 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003993 return NULL;
3994 res = (*func)(self);
3995 if (res == -1 && PyErr_Occurred())
3996 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00003997 return PyLong_FromLong((long)res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003998}
3999
Tim Peters6d6c1a32001-08-02 04:15:00 +00004000static PyObject *
Raymond Hettingerf34f2642003-10-11 17:29:04 +00004001wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
4002{
4003 inquiry func = (inquiry)wrapped;
4004 int res;
4005
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004006 if (!check_num_args(args, 0))
Raymond Hettingerf34f2642003-10-11 17:29:04 +00004007 return NULL;
4008 res = (*func)(self);
4009 if (res == -1 && PyErr_Occurred())
4010 return NULL;
4011 return PyBool_FromLong((long)res);
4012}
4013
4014static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004015wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
4016{
4017 binaryfunc func = (binaryfunc)wrapped;
4018 PyObject *other;
4019
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004020 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004021 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004022 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004023 return (*func)(self, other);
4024}
4025
4026static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00004027wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
4028{
4029 binaryfunc func = (binaryfunc)wrapped;
4030 PyObject *other;
4031
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004032 if (!check_num_args(args, 1))
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00004033 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004034 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00004035 return (*func)(self, other);
4036}
4037
4038static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004039wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4040{
4041 binaryfunc func = (binaryfunc)wrapped;
4042 PyObject *other;
4043
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004044 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004045 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004046 other = PyTuple_GET_ITEM(args, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00004047 if (!PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00004048 Py_INCREF(Py_NotImplemented);
4049 return Py_NotImplemented;
4050 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004051 return (*func)(other, self);
4052}
4053
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004054static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004055wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
4056{
4057 ternaryfunc func = (ternaryfunc)wrapped;
4058 PyObject *other;
4059 PyObject *third = Py_None;
4060
4061 /* Note: This wrapper only works for __pow__() */
4062
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004063 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004064 return NULL;
4065 return (*func)(self, other, third);
4066}
4067
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00004068static PyObject *
4069wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4070{
4071 ternaryfunc func = (ternaryfunc)wrapped;
4072 PyObject *other;
4073 PyObject *third = Py_None;
4074
4075 /* Note: This wrapper only works for __pow__() */
4076
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004077 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00004078 return NULL;
4079 return (*func)(other, self, third);
4080}
4081
Tim Peters6d6c1a32001-08-02 04:15:00 +00004082static PyObject *
4083wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
4084{
4085 unaryfunc func = (unaryfunc)wrapped;
4086
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004087 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004088 return NULL;
4089 return (*func)(self);
4090}
4091
Tim Peters6d6c1a32001-08-02 04:15:00 +00004092static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004093wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004094{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004095 ssizeargfunc func = (ssizeargfunc)wrapped;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004096 PyObject* o;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004097 Py_ssize_t i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004098
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004099 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
4100 return NULL;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004101 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004102 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00004103 return NULL;
4104 return (*func)(self, i);
4105}
4106
Martin v. Löwis18e16552006-02-15 17:27:45 +00004107static Py_ssize_t
Guido van Rossum5d815f32001-08-17 21:57:47 +00004108getindex(PyObject *self, PyObject *arg)
4109{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004110 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004111
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004112 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004113 if (i == -1 && PyErr_Occurred())
4114 return -1;
4115 if (i < 0) {
Christian Heimes90aa7642007-12-19 02:45:37 +00004116 PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004117 if (sq && sq->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00004118 Py_ssize_t n = (*sq->sq_length)(self);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004119 if (n < 0)
4120 return -1;
4121 i += n;
4122 }
4123 }
4124 return i;
4125}
4126
4127static PyObject *
4128wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
4129{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004130 ssizeargfunc func = (ssizeargfunc)wrapped;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004131 PyObject *arg;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004132 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004133
Guido van Rossumf4593e02001-10-03 12:09:30 +00004134 if (PyTuple_GET_SIZE(args) == 1) {
4135 arg = PyTuple_GET_ITEM(args, 0);
4136 i = getindex(self, arg);
4137 if (i == -1 && PyErr_Occurred())
4138 return NULL;
4139 return (*func)(self, i);
4140 }
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004141 check_num_args(args, 1);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004142 assert(PyErr_Occurred());
4143 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004144}
4145
Tim Peters6d6c1a32001-08-02 04:15:00 +00004146static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00004147wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004148{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004149 ssizeobjargproc func = (ssizeobjargproc)wrapped;
4150 Py_ssize_t i;
4151 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004152 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004153
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004154 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
Guido van Rossum5d815f32001-08-17 21:57:47 +00004155 return NULL;
4156 i = getindex(self, arg);
4157 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00004158 return NULL;
4159 res = (*func)(self, i, value);
4160 if (res == -1 && PyErr_Occurred())
4161 return NULL;
4162 Py_INCREF(Py_None);
4163 return Py_None;
4164}
4165
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004166static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00004167wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004168{
Martin v. Löwis18e16552006-02-15 17:27:45 +00004169 ssizeobjargproc func = (ssizeobjargproc)wrapped;
4170 Py_ssize_t i;
4171 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00004172 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004173
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004174 if (!check_num_args(args, 1))
Guido van Rossum5d815f32001-08-17 21:57:47 +00004175 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004176 arg = PyTuple_GET_ITEM(args, 0);
Guido van Rossum5d815f32001-08-17 21:57:47 +00004177 i = getindex(self, arg);
4178 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004179 return NULL;
4180 res = (*func)(self, i, NULL);
4181 if (res == -1 && PyErr_Occurred())
4182 return NULL;
4183 Py_INCREF(Py_None);
4184 return Py_None;
4185}
4186
Tim Peters6d6c1a32001-08-02 04:15:00 +00004187/* XXX objobjproc is a misnomer; should be objargpred */
4188static PyObject *
4189wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
4190{
4191 objobjproc func = (objobjproc)wrapped;
4192 int res;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00004193 PyObject *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004194
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004195 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004196 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004197 value = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004198 res = (*func)(self, value);
4199 if (res == -1 && PyErr_Occurred())
4200 return NULL;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00004201 else
4202 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004203}
4204
Tim Peters6d6c1a32001-08-02 04:15:00 +00004205static PyObject *
4206wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
4207{
4208 objobjargproc func = (objobjargproc)wrapped;
4209 int res;
4210 PyObject *key, *value;
4211
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004212 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004213 return NULL;
4214 res = (*func)(self, key, value);
4215 if (res == -1 && PyErr_Occurred())
4216 return NULL;
4217 Py_INCREF(Py_None);
4218 return Py_None;
4219}
4220
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004221static PyObject *
4222wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
4223{
4224 objobjargproc func = (objobjargproc)wrapped;
4225 int res;
4226 PyObject *key;
4227
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004228 if (!check_num_args(args, 1))
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004229 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004230 key = PyTuple_GET_ITEM(args, 0);
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00004231 res = (*func)(self, key, NULL);
4232 if (res == -1 && PyErr_Occurred())
4233 return NULL;
4234 Py_INCREF(Py_None);
4235 return Py_None;
4236}
4237
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004238/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
Guido van Rossum52b27052003-04-15 20:05:10 +00004239 This is called the Carlo Verre hack after its discoverer. */
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004240static int
4241hackcheck(PyObject *self, setattrofunc func, char *what)
4242{
Christian Heimes90aa7642007-12-19 02:45:37 +00004243 PyTypeObject *type = Py_TYPE(self);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004244 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
4245 type = type->tp_base;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004246 /* If type is NULL now, this is a really weird type.
4247 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00004248 if (type && type->tp_setattro != func) {
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004249 PyErr_Format(PyExc_TypeError,
4250 "can't apply this %s to %s object",
4251 what,
4252 type->tp_name);
4253 return 0;
4254 }
4255 return 1;
4256}
4257
Tim Peters6d6c1a32001-08-02 04:15:00 +00004258static PyObject *
4259wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
4260{
4261 setattrofunc func = (setattrofunc)wrapped;
4262 int res;
4263 PyObject *name, *value;
4264
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004265 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004266 return NULL;
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004267 if (!hackcheck(self, func, "__setattr__"))
4268 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004269 res = (*func)(self, name, value);
4270 if (res < 0)
4271 return NULL;
4272 Py_INCREF(Py_None);
4273 return Py_None;
4274}
4275
4276static PyObject *
4277wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
4278{
4279 setattrofunc func = (setattrofunc)wrapped;
4280 int res;
4281 PyObject *name;
4282
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004283 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004284 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004285 name = PyTuple_GET_ITEM(args, 0);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004286 if (!hackcheck(self, func, "__delattr__"))
4287 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004288 res = (*func)(self, name, NULL);
4289 if (res < 0)
4290 return NULL;
4291 Py_INCREF(Py_None);
4292 return Py_None;
4293}
4294
Tim Peters6d6c1a32001-08-02 04:15:00 +00004295static PyObject *
4296wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
4297{
4298 hashfunc func = (hashfunc)wrapped;
4299 long res;
4300
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004301 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004302 return NULL;
4303 res = (*func)(self);
4304 if (res == -1 && PyErr_Occurred())
4305 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00004306 return PyLong_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004307}
4308
Tim Peters6d6c1a32001-08-02 04:15:00 +00004309static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00004310wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004311{
4312 ternaryfunc func = (ternaryfunc)wrapped;
4313
Guido van Rossumc8e56452001-10-22 00:43:43 +00004314 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004315}
4316
Tim Peters6d6c1a32001-08-02 04:15:00 +00004317static PyObject *
4318wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
4319{
4320 richcmpfunc func = (richcmpfunc)wrapped;
4321 PyObject *other;
4322
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004323 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004324 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004325 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004326 return (*func)(self, other, op);
4327}
4328
4329#undef RICHCMP_WRAPPER
4330#define RICHCMP_WRAPPER(NAME, OP) \
4331static PyObject * \
4332richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4333{ \
4334 return wrap_richcmpfunc(self, args, wrapped, OP); \
4335}
4336
Jack Jansen8e938b42001-08-08 15:29:49 +00004337RICHCMP_WRAPPER(lt, Py_LT)
4338RICHCMP_WRAPPER(le, Py_LE)
4339RICHCMP_WRAPPER(eq, Py_EQ)
4340RICHCMP_WRAPPER(ne, Py_NE)
4341RICHCMP_WRAPPER(gt, Py_GT)
4342RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004343
Tim Peters6d6c1a32001-08-02 04:15:00 +00004344static PyObject *
4345wrap_next(PyObject *self, PyObject *args, void *wrapped)
4346{
4347 unaryfunc func = (unaryfunc)wrapped;
4348 PyObject *res;
4349
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004350 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004351 return NULL;
4352 res = (*func)(self);
4353 if (res == NULL && !PyErr_Occurred())
4354 PyErr_SetNone(PyExc_StopIteration);
4355 return res;
4356}
4357
Tim Peters6d6c1a32001-08-02 04:15:00 +00004358static PyObject *
4359wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
4360{
4361 descrgetfunc func = (descrgetfunc)wrapped;
4362 PyObject *obj;
4363 PyObject *type = NULL;
4364
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004365 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004366 return NULL;
Guido van Rossum82ed25c2003-02-11 16:25:43 +00004367 if (obj == Py_None)
4368 obj = NULL;
4369 if (type == Py_None)
4370 type = NULL;
4371 if (type == NULL &&obj == NULL) {
4372 PyErr_SetString(PyExc_TypeError,
4373 "__get__(None, None) is invalid");
4374 return NULL;
4375 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004376 return (*func)(self, obj, type);
4377}
4378
Tim Peters6d6c1a32001-08-02 04:15:00 +00004379static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004380wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004381{
4382 descrsetfunc func = (descrsetfunc)wrapped;
4383 PyObject *obj, *value;
4384 int ret;
4385
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004386 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004387 return NULL;
4388 ret = (*func)(self, obj, value);
4389 if (ret < 0)
4390 return NULL;
4391 Py_INCREF(Py_None);
4392 return Py_None;
4393}
Guido van Rossum22b13872002-08-06 21:41:44 +00004394
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004395static PyObject *
4396wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
4397{
4398 descrsetfunc func = (descrsetfunc)wrapped;
4399 PyObject *obj;
4400 int ret;
4401
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004402 if (!check_num_args(args, 1))
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004403 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004404 obj = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004405 ret = (*func)(self, obj, NULL);
4406 if (ret < 0)
4407 return NULL;
4408 Py_INCREF(Py_None);
4409 return Py_None;
4410}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004411
Tim Peters6d6c1a32001-08-02 04:15:00 +00004412static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00004413wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004414{
4415 initproc func = (initproc)wrapped;
4416
Guido van Rossumc8e56452001-10-22 00:43:43 +00004417 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004418 return NULL;
4419 Py_INCREF(Py_None);
4420 return Py_None;
4421}
4422
Tim Peters6d6c1a32001-08-02 04:15:00 +00004423static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004424tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004425{
Barry Warsaw60f01882001-08-22 19:24:42 +00004426 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004427 PyObject *arg0, *res;
4428
4429 if (self == NULL || !PyType_Check(self))
4430 Py_FatalError("__new__() called with non-type 'self'");
4431 type = (PyTypeObject *)self;
4432 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004433 PyErr_Format(PyExc_TypeError,
4434 "%s.__new__(): not enough arguments",
4435 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004436 return NULL;
4437 }
4438 arg0 = PyTuple_GET_ITEM(args, 0);
4439 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004440 PyErr_Format(PyExc_TypeError,
4441 "%s.__new__(X): X is not a type object (%s)",
4442 type->tp_name,
Christian Heimes90aa7642007-12-19 02:45:37 +00004443 Py_TYPE(arg0)->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004444 return NULL;
4445 }
4446 subtype = (PyTypeObject *)arg0;
4447 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004448 PyErr_Format(PyExc_TypeError,
4449 "%s.__new__(%s): %s is not a subtype of %s",
4450 type->tp_name,
4451 subtype->tp_name,
4452 subtype->tp_name,
4453 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004454 return NULL;
4455 }
Barry Warsaw60f01882001-08-22 19:24:42 +00004456
4457 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00004458 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00004459 most derived base that's not a heap type is this type. */
4460 staticbase = subtype;
4461 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4462 staticbase = staticbase->tp_base;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004463 /* If staticbase is NULL now, it is a really weird type.
4464 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00004465 if (staticbase && staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004466 PyErr_Format(PyExc_TypeError,
4467 "%s.__new__(%s) is not safe, use %s.__new__()",
4468 type->tp_name,
4469 subtype->tp_name,
4470 staticbase == NULL ? "?" : staticbase->tp_name);
4471 return NULL;
4472 }
4473
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004474 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4475 if (args == NULL)
4476 return NULL;
4477 res = type->tp_new(subtype, args, kwds);
4478 Py_DECREF(args);
4479 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004480}
4481
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004482static struct PyMethodDef tp_new_methoddef[] = {
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004483 {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00004484 PyDoc_STR("T.__new__(S, ...) -> "
Guido van Rossumd8faa362007-04-27 19:54:29 +00004485 "a new object with type S, a subtype of T")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00004486 {0}
4487};
4488
4489static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004490add_tp_new_wrapper(PyTypeObject *type)
4491{
Guido van Rossumf040ede2001-08-07 16:40:56 +00004492 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004493
Guido van Rossum687ae002001-10-15 22:03:32 +00004494 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00004495 return 0;
4496 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004497 if (func == NULL)
4498 return -1;
Raymond Hettinger8d726ee2004-06-25 22:24:35 +00004499 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
Raymond Hettingerd56cbe52004-06-25 22:17:39 +00004500 Py_DECREF(func);
4501 return -1;
4502 }
4503 Py_DECREF(func);
4504 return 0;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004505}
4506
Guido van Rossumf040ede2001-08-07 16:40:56 +00004507/* Slot wrappers that call the corresponding __foo__ slot. See comments
4508 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004509
Guido van Rossumdc91b992001-08-08 22:26:22 +00004510#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004511static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004512FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004513{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00004514 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004515 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004516}
4517
Guido van Rossumdc91b992001-08-08 22:26:22 +00004518#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004519static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004520FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004521{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004522 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004523 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004524}
4525
Guido van Rossumcd118802003-01-06 22:57:47 +00004526/* Boolean helper for SLOT1BINFULL().
4527 right.__class__ is a nontrivial subclass of left.__class__. */
4528static int
4529method_is_overloaded(PyObject *left, PyObject *right, char *name)
4530{
4531 PyObject *a, *b;
4532 int ok;
4533
Christian Heimes90aa7642007-12-19 02:45:37 +00004534 b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
Guido van Rossumcd118802003-01-06 22:57:47 +00004535 if (b == NULL) {
4536 PyErr_Clear();
4537 /* If right doesn't have it, it's not overloaded */
4538 return 0;
4539 }
4540
Christian Heimes90aa7642007-12-19 02:45:37 +00004541 a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
Guido van Rossumcd118802003-01-06 22:57:47 +00004542 if (a == NULL) {
4543 PyErr_Clear();
4544 Py_DECREF(b);
4545 /* If right has it but left doesn't, it's overloaded */
4546 return 1;
4547 }
4548
4549 ok = PyObject_RichCompareBool(a, b, Py_NE);
4550 Py_DECREF(a);
4551 Py_DECREF(b);
4552 if (ok < 0) {
4553 PyErr_Clear();
4554 return 0;
4555 }
4556
4557 return ok;
4558}
4559
Guido van Rossumdc91b992001-08-08 22:26:22 +00004560
4561#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004562static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004563FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004564{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004565 static PyObject *cache_str, *rcache_str; \
Christian Heimes90aa7642007-12-19 02:45:37 +00004566 int do_other = Py_TYPE(self) != Py_TYPE(other) && \
4567 Py_TYPE(other)->tp_as_number != NULL && \
4568 Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
4569 if (Py_TYPE(self)->tp_as_number != NULL && \
4570 Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004571 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00004572 if (do_other && \
Christian Heimes90aa7642007-12-19 02:45:37 +00004573 PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
Guido van Rossumcd118802003-01-06 22:57:47 +00004574 method_is_overloaded(self, other, ROPSTR)) { \
Guido van Rossum55f20992001-10-01 17:18:22 +00004575 r = call_maybe( \
4576 other, ROPSTR, &rcache_str, "(O)", self); \
4577 if (r != Py_NotImplemented) \
4578 return r; \
4579 Py_DECREF(r); \
4580 do_other = 0; \
4581 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004582 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004583 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004584 if (r != Py_NotImplemented || \
Christian Heimes90aa7642007-12-19 02:45:37 +00004585 Py_TYPE(other) == Py_TYPE(self)) \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004586 return r; \
4587 Py_DECREF(r); \
4588 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00004589 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004590 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004591 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004592 } \
4593 Py_INCREF(Py_NotImplemented); \
4594 return Py_NotImplemented; \
4595}
4596
4597#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4598 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4599
4600#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4601static PyObject * \
4602FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4603{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004604 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004605 return call_method(self, OPSTR, &cache_str, \
4606 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004607}
4608
Martin v. Löwis18e16552006-02-15 17:27:45 +00004609static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +00004610slot_sq_length(PyObject *self)
4611{
Guido van Rossum2730b132001-08-28 18:22:14 +00004612 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004613 PyObject *res = call_method(self, "__len__", &len_str, "()");
Martin v. Löwis18e16552006-02-15 17:27:45 +00004614 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004615
4616 if (res == NULL)
4617 return -1;
Benjamin Petersonee1ae7c2009-02-08 21:07:20 +00004618 len = PyNumber_AsSsize_t(res, PyExc_OverflowError);
Guido van Rossum26111622001-10-01 16:42:49 +00004619 Py_DECREF(res);
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004620 if (len < 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004621 if (!PyErr_Occurred())
4622 PyErr_SetString(PyExc_ValueError,
4623 "__len__() should return >= 0");
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004624 return -1;
4625 }
Guido van Rossum26111622001-10-01 16:42:49 +00004626 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004627}
4628
Guido van Rossumf4593e02001-10-03 12:09:30 +00004629/* Super-optimized version of slot_sq_item.
4630 Other slots could do the same... */
4631static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00004632slot_sq_item(PyObject *self, Py_ssize_t i)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004633{
4634 static PyObject *getitem_str;
4635 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4636 descrgetfunc f;
4637
4638 if (getitem_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00004639 getitem_str = PyUnicode_InternFromString("__getitem__");
Guido van Rossumf4593e02001-10-03 12:09:30 +00004640 if (getitem_str == NULL)
4641 return NULL;
4642 }
Christian Heimes90aa7642007-12-19 02:45:37 +00004643 func = _PyType_Lookup(Py_TYPE(self), getitem_str);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004644 if (func != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +00004645 if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004646 Py_INCREF(func);
Neal Norwitz673cd822002-10-18 16:33:13 +00004647 else {
Christian Heimes90aa7642007-12-19 02:45:37 +00004648 func = f(func, self, (PyObject *)(Py_TYPE(self)));
Neal Norwitz673cd822002-10-18 16:33:13 +00004649 if (func == NULL) {
4650 return NULL;
4651 }
4652 }
Christian Heimes217cfd12007-12-02 14:31:20 +00004653 ival = PyLong_FromSsize_t(i);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004654 if (ival != NULL) {
4655 args = PyTuple_New(1);
4656 if (args != NULL) {
4657 PyTuple_SET_ITEM(args, 0, ival);
4658 retval = PyObject_Call(func, args, NULL);
4659 Py_XDECREF(args);
4660 Py_XDECREF(func);
4661 return retval;
4662 }
4663 }
4664 }
4665 else {
4666 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4667 }
4668 Py_XDECREF(args);
4669 Py_XDECREF(ival);
4670 Py_XDECREF(func);
4671 return NULL;
4672}
4673
Tim Peters6d6c1a32001-08-02 04:15:00 +00004674static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004675slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004676{
4677 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004678 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004679
4680 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004681 res = call_method(self, "__delitem__", &delitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004682 "(n)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004683 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004684 res = call_method(self, "__setitem__", &setitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004685 "(nO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004686 if (res == NULL)
4687 return -1;
4688 Py_DECREF(res);
4689 return 0;
4690}
4691
4692static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00004693slot_sq_contains(PyObject *self, PyObject *value)
4694{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004695 PyObject *func, *res, *args;
Tim Petersbf9b2442003-03-23 05:35:36 +00004696 int result = -1;
4697
Guido van Rossum60718732001-08-28 17:47:51 +00004698 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004699
Guido van Rossum55f20992001-10-01 17:18:22 +00004700 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004701 if (func != NULL) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004702 args = PyTuple_Pack(1, value);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004703 if (args == NULL)
4704 res = NULL;
4705 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004706 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004707 Py_DECREF(args);
4708 }
4709 Py_DECREF(func);
Tim Petersbf9b2442003-03-23 05:35:36 +00004710 if (res != NULL) {
4711 result = PyObject_IsTrue(res);
4712 Py_DECREF(res);
4713 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004714 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004715 else if (! PyErr_Occurred()) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00004716 /* Possible results: -1 and 1 */
4717 result = (int)_PySequence_IterSearch(self, value,
Tim Petersbf9b2442003-03-23 05:35:36 +00004718 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004719 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004720 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004721}
4722
Tim Peters6d6c1a32001-08-02 04:15:00 +00004723#define slot_mp_length slot_sq_length
4724
Guido van Rossumdc91b992001-08-08 22:26:22 +00004725SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004726
4727static int
4728slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4729{
4730 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004731 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004732
4733 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004734 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004735 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004736 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004737 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004738 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004739 if (res == NULL)
4740 return -1;
4741 Py_DECREF(res);
4742 return 0;
4743}
4744
Guido van Rossumdc91b992001-08-08 22:26:22 +00004745SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4746SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4747SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004748SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4749SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4750
Jeremy Hylton938ace62002-07-17 16:30:39 +00004751static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
Guido van Rossumdc91b992001-08-08 22:26:22 +00004752
4753SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4754 nb_power, "__pow__", "__rpow__")
4755
4756static PyObject *
4757slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4758{
Guido van Rossum2730b132001-08-28 18:22:14 +00004759 static PyObject *pow_str;
4760
Guido van Rossumdc91b992001-08-08 22:26:22 +00004761 if (modulus == Py_None)
4762 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00004763 /* Three-arg power doesn't use __rpow__. But ternary_op
4764 can call this when the second argument's type uses
4765 slot_nb_power, so check before calling self.__pow__. */
Christian Heimes90aa7642007-12-19 02:45:37 +00004766 if (Py_TYPE(self)->tp_as_number != NULL &&
4767 Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
Guido van Rossum23094982002-06-10 14:30:43 +00004768 return call_method(self, "__pow__", &pow_str,
4769 "(OO)", other, modulus);
4770 }
4771 Py_INCREF(Py_NotImplemented);
4772 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00004773}
4774
4775SLOT0(slot_nb_negative, "__neg__")
4776SLOT0(slot_nb_positive, "__pos__")
4777SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004778
4779static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00004780slot_nb_bool(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004781{
Tim Petersea7f75d2002-12-07 21:39:16 +00004782 PyObject *func, *args;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004783 static PyObject *bool_str, *len_str;
Tim Petersea7f75d2002-12-07 21:39:16 +00004784 int result = -1;
Benjamin Petersonf07d0022009-03-21 17:31:58 +00004785 int using_len = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004786
Jack Diederich4dafcc42006-11-28 19:15:13 +00004787 func = lookup_maybe(self, "__bool__", &bool_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004788 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00004789 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00004790 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00004791 func = lookup_maybe(self, "__len__", &len_str);
Tim Petersea7f75d2002-12-07 21:39:16 +00004792 if (func == NULL)
4793 return PyErr_Occurred() ? -1 : 1;
Benjamin Petersonf07d0022009-03-21 17:31:58 +00004794 using_len = 1;
4795 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004796 args = PyTuple_New(0);
4797 if (args != NULL) {
4798 PyObject *temp = PyObject_Call(func, args, NULL);
4799 Py_DECREF(args);
4800 if (temp != NULL) {
Benjamin Petersonf07d0022009-03-21 17:31:58 +00004801 if (using_len) {
Jack Diederich4dafcc42006-11-28 19:15:13 +00004802 /* enforced by slot_nb_len */
Jeremy Hylton090a3492003-06-27 16:46:45 +00004803 result = PyObject_IsTrue(temp);
Jack Diederich4dafcc42006-11-28 19:15:13 +00004804 }
4805 else if (PyBool_Check(temp)) {
4806 result = PyObject_IsTrue(temp);
4807 }
Jeremy Hylton090a3492003-06-27 16:46:45 +00004808 else {
4809 PyErr_Format(PyExc_TypeError,
Amaury Forgeot d'Arc097cd072009-07-07 00:43:08 +00004810 "__bool__ should return "
4811 "bool, returned %s",
Benjamin Petersonf07d0022009-03-21 17:31:58 +00004812 Py_TYPE(temp)->tp_name);
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004813 result = -1;
Jeremy Hylton090a3492003-06-27 16:46:45 +00004814 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004815 Py_DECREF(temp);
Guido van Rossum55f20992001-10-01 17:18:22 +00004816 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004817 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004818 Py_DECREF(func);
Tim Petersea7f75d2002-12-07 21:39:16 +00004819 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004820}
4821
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004822
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004823static PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004824slot_nb_index(PyObject *self)
4825{
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004826 static PyObject *index_str;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004827 return call_method(self, "__index__", &index_str, "()");
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004828}
4829
4830
Guido van Rossumdc91b992001-08-08 22:26:22 +00004831SLOT0(slot_nb_invert, "__invert__")
4832SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4833SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4834SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4835SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4836SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004837
Guido van Rossumdc91b992001-08-08 22:26:22 +00004838SLOT0(slot_nb_int, "__int__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004839SLOT0(slot_nb_float, "__float__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004840SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4841SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4842SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004843SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
Thomas Wouterscf297e42007-02-23 15:07:44 +00004844/* Can't use SLOT1 here, because nb_inplace_power is ternary */
4845static PyObject *
4846slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
4847{
4848 static PyObject *cache_str;
4849 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
4850}
Guido van Rossumdc91b992001-08-08 22:26:22 +00004851SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4852SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4853SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4854SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4855SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4856SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4857 "__floordiv__", "__rfloordiv__")
4858SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4859SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4860SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004861
Guido van Rossumb8f63662001-08-15 23:57:02 +00004862static PyObject *
4863slot_tp_repr(PyObject *self)
4864{
4865 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004866 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004867
Guido van Rossum60718732001-08-28 17:47:51 +00004868 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004869 if (func != NULL) {
4870 res = PyEval_CallObject(func, NULL);
4871 Py_DECREF(func);
4872 return res;
4873 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00004874 PyErr_Clear();
Walter Dörwald1ab83302007-05-18 17:15:44 +00004875 return PyUnicode_FromFormat("<%s object at %p>",
Christian Heimes90aa7642007-12-19 02:45:37 +00004876 Py_TYPE(self)->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004877}
4878
4879static PyObject *
4880slot_tp_str(PyObject *self)
4881{
4882 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004883 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004884
Guido van Rossum60718732001-08-28 17:47:51 +00004885 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004886 if (func != NULL) {
4887 res = PyEval_CallObject(func, NULL);
4888 Py_DECREF(func);
4889 return res;
4890 }
4891 else {
Walter Dörwald1ab83302007-05-18 17:15:44 +00004892 PyObject *ress;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004893 PyErr_Clear();
Walter Dörwald1ab83302007-05-18 17:15:44 +00004894 res = slot_tp_repr(self);
4895 if (!res)
4896 return NULL;
4897 ress = _PyUnicode_AsDefaultEncodedString(res, NULL);
4898 Py_DECREF(res);
4899 return ress;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004900 }
4901}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004902
4903static long
4904slot_tp_hash(PyObject *self)
4905{
Guido van Rossum4011a242006-08-17 23:09:57 +00004906 PyObject *func, *res;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004907 static PyObject *hash_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004908 long h;
4909
Guido van Rossum60718732001-08-28 17:47:51 +00004910 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004911
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004912 if (func == Py_None) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004913 Py_DECREF(func);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004914 func = NULL;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004915 }
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004916
4917 if (func == NULL) {
Nick Coghland1abd252008-07-15 15:46:38 +00004918 return PyObject_HashNotImplemented(self);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004919 }
4920
Guido van Rossum4011a242006-08-17 23:09:57 +00004921 res = PyEval_CallObject(func, NULL);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004922 Py_DECREF(func);
4923 if (res == NULL)
4924 return -1;
4925 if (PyLong_Check(res))
4926 h = PyLong_Type.tp_hash(res);
4927 else
Christian Heimes217cfd12007-12-02 14:31:20 +00004928 h = PyLong_AsLong(res);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004929 Py_DECREF(res);
4930 if (h == -1 && !PyErr_Occurred())
4931 h = -2;
4932 return h;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004933}
4934
4935static PyObject *
4936slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4937{
Guido van Rossum60718732001-08-28 17:47:51 +00004938 static PyObject *call_str;
4939 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004940 PyObject *res;
4941
4942 if (meth == NULL)
4943 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004944
Tim Peters6d6c1a32001-08-02 04:15:00 +00004945 res = PyObject_Call(meth, args, kwds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004946
Tim Peters6d6c1a32001-08-02 04:15:00 +00004947 Py_DECREF(meth);
4948 return res;
4949}
4950
Guido van Rossum14a6f832001-10-17 13:59:09 +00004951/* There are two slot dispatch functions for tp_getattro.
4952
4953 - slot_tp_getattro() is used when __getattribute__ is overridden
4954 but no __getattr__ hook is present;
4955
4956 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4957
Guido van Rossumc334df52002-04-04 23:44:47 +00004958 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4959 detects the absence of __getattr__ and then installs the simpler slot if
4960 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00004961
Tim Peters6d6c1a32001-08-02 04:15:00 +00004962static PyObject *
4963slot_tp_getattro(PyObject *self, PyObject *name)
4964{
Guido van Rossum14a6f832001-10-17 13:59:09 +00004965 static PyObject *getattribute_str = NULL;
4966 return call_method(self, "__getattribute__", &getattribute_str,
4967 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004968}
4969
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004970static PyObject *
Benjamin Peterson9262b842008-11-17 22:45:50 +00004971call_attribute(PyObject *self, PyObject *attr, PyObject *name)
4972{
4973 PyObject *res, *descr = NULL;
4974 descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
4975
4976 if (f != NULL) {
4977 descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
4978 if (descr == NULL)
4979 return NULL;
4980 else
4981 attr = descr;
4982 }
4983 res = PyObject_CallFunctionObjArgs(attr, name, NULL);
4984 Py_XDECREF(descr);
4985 return res;
4986}
4987
4988static PyObject *
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004989slot_tp_getattr_hook(PyObject *self, PyObject *name)
4990{
Christian Heimes90aa7642007-12-19 02:45:37 +00004991 PyTypeObject *tp = Py_TYPE(self);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004992 PyObject *getattr, *getattribute, *res;
4993 static PyObject *getattribute_str = NULL;
4994 static PyObject *getattr_str = NULL;
4995
4996 if (getattr_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00004997 getattr_str = PyUnicode_InternFromString("__getattr__");
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004998 if (getattr_str == NULL)
4999 return NULL;
5000 }
5001 if (getattribute_str == NULL) {
5002 getattribute_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00005003 PyUnicode_InternFromString("__getattribute__");
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005004 if (getattribute_str == NULL)
5005 return NULL;
5006 }
Benjamin Peterson9262b842008-11-17 22:45:50 +00005007 /* speed hack: we could use lookup_maybe, but that would resolve the
5008 method fully for each attribute lookup for classes with
5009 __getattr__, even when the attribute is present. So we use
5010 _PyType_Lookup and create the method only when needed, with
5011 call_attribute. */
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005012 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00005013 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005014 /* No __getattr__ hook: use a simpler dispatcher */
5015 tp->tp_getattro = slot_tp_getattro;
5016 return slot_tp_getattro(self, name);
5017 }
Benjamin Peterson9262b842008-11-17 22:45:50 +00005018 Py_INCREF(getattr);
5019 /* speed hack: we could use lookup_maybe, but that would resolve the
5020 method fully for each attribute lookup for classes with
5021 __getattr__, even when self has the default __getattribute__
5022 method. So we use _PyType_Lookup and create the method only when
5023 needed, with call_attribute. */
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005024 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00005025 if (getattribute == NULL ||
Christian Heimes90aa7642007-12-19 02:45:37 +00005026 (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
Guido van Rossum14a6f832001-10-17 13:59:09 +00005027 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
5028 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005029 res = PyObject_GenericGetAttr(self, name);
Benjamin Peterson9262b842008-11-17 22:45:50 +00005030 else {
5031 Py_INCREF(getattribute);
5032 res = call_attribute(self, getattribute, name);
5033 Py_DECREF(getattribute);
5034 }
Guido van Rossum14a6f832001-10-17 13:59:09 +00005035 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005036 PyErr_Clear();
Benjamin Peterson9262b842008-11-17 22:45:50 +00005037 res = call_attribute(self, getattr, name);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005038 }
Benjamin Peterson9262b842008-11-17 22:45:50 +00005039 Py_DECREF(getattr);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00005040 return res;
5041}
5042
Tim Peters6d6c1a32001-08-02 04:15:00 +00005043static int
5044slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
5045{
5046 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00005047 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005048
5049 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00005050 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005051 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005052 else
Guido van Rossum2730b132001-08-28 18:22:14 +00005053 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005054 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005055 if (res == NULL)
5056 return -1;
5057 Py_DECREF(res);
5058 return 0;
5059}
5060
Guido van Rossumf5243f02008-01-01 04:06:48 +00005061static char *name_op[] = {
5062 "__lt__",
5063 "__le__",
5064 "__eq__",
5065 "__ne__",
5066 "__gt__",
5067 "__ge__",
5068};
5069
Tim Peters6d6c1a32001-08-02 04:15:00 +00005070static PyObject *
Mark Dickinson6f1d0492009-11-15 13:58:49 +00005071slot_tp_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005072{
Guido van Rossumb8f63662001-08-15 23:57:02 +00005073 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00005074 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00005075
Guido van Rossum60718732001-08-28 17:47:51 +00005076 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005077 if (func == NULL) {
5078 PyErr_Clear();
5079 Py_INCREF(Py_NotImplemented);
5080 return Py_NotImplemented;
5081 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00005082 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005083 if (args == NULL)
5084 res = NULL;
5085 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00005086 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005087 Py_DECREF(args);
5088 }
5089 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005090 return res;
5091}
5092
Guido van Rossumb8f63662001-08-15 23:57:02 +00005093static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00005094slot_tp_iter(PyObject *self)
5095{
5096 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00005097 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00005098
Guido van Rossum60718732001-08-28 17:47:51 +00005099 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005100 if (func != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +00005101 PyObject *args;
5102 args = res = PyTuple_New(0);
5103 if (args != NULL) {
5104 res = PyObject_Call(func, args, NULL);
5105 Py_DECREF(args);
5106 }
5107 Py_DECREF(func);
5108 return res;
Guido van Rossumb8f63662001-08-15 23:57:02 +00005109 }
5110 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00005111 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005112 if (func == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005113 PyErr_Format(PyExc_TypeError,
5114 "'%.200s' object is not iterable",
Christian Heimes90aa7642007-12-19 02:45:37 +00005115 Py_TYPE(self)->tp_name);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005116 return NULL;
5117 }
5118 Py_DECREF(func);
5119 return PySeqIter_New(self);
5120}
Tim Peters6d6c1a32001-08-02 04:15:00 +00005121
5122static PyObject *
5123slot_tp_iternext(PyObject *self)
5124{
Guido van Rossum2730b132001-08-28 18:22:14 +00005125 static PyObject *next_str;
Georg Brandla18af4e2007-04-21 15:47:16 +00005126 return call_method(self, "__next__", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00005127}
5128
Guido van Rossum1a493502001-08-17 16:47:50 +00005129static PyObject *
5130slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5131{
Christian Heimes90aa7642007-12-19 02:45:37 +00005132 PyTypeObject *tp = Py_TYPE(self);
Guido van Rossum1a493502001-08-17 16:47:50 +00005133 PyObject *get;
5134 static PyObject *get_str = NULL;
5135
5136 if (get_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005137 get_str = PyUnicode_InternFromString("__get__");
Guido van Rossum1a493502001-08-17 16:47:50 +00005138 if (get_str == NULL)
5139 return NULL;
5140 }
5141 get = _PyType_Lookup(tp, get_str);
5142 if (get == NULL) {
5143 /* Avoid further slowdowns */
5144 if (tp->tp_descr_get == slot_tp_descr_get)
5145 tp->tp_descr_get = NULL;
5146 Py_INCREF(self);
5147 return self;
5148 }
Guido van Rossum2c252392001-08-24 10:13:31 +00005149 if (obj == NULL)
5150 obj = Py_None;
5151 if (type == NULL)
5152 type = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00005153 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
Guido van Rossum1a493502001-08-17 16:47:50 +00005154}
Tim Peters6d6c1a32001-08-02 04:15:00 +00005155
5156static int
5157slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
5158{
Guido van Rossum2c252392001-08-24 10:13:31 +00005159 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00005160 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00005161
5162 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00005163 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005164 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00005165 else
Guido van Rossum2730b132001-08-28 18:22:14 +00005166 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005167 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005168 if (res == NULL)
5169 return -1;
5170 Py_DECREF(res);
5171 return 0;
5172}
5173
5174static int
5175slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
5176{
Guido van Rossum60718732001-08-28 17:47:51 +00005177 static PyObject *init_str;
5178 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005179 PyObject *res;
5180
5181 if (meth == NULL)
5182 return -1;
5183 res = PyObject_Call(meth, args, kwds);
5184 Py_DECREF(meth);
5185 if (res == NULL)
5186 return -1;
Raymond Hettingerb67cc802005-03-03 16:45:19 +00005187 if (res != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005188 PyErr_Format(PyExc_TypeError,
5189 "__init__() should return None, not '%.200s'",
Christian Heimes90aa7642007-12-19 02:45:37 +00005190 Py_TYPE(res)->tp_name);
Raymond Hettingerb67cc802005-03-03 16:45:19 +00005191 Py_DECREF(res);
5192 return -1;
5193 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00005194 Py_DECREF(res);
5195 return 0;
5196}
5197
5198static PyObject *
5199slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5200{
Guido van Rossum7bed2132002-08-08 21:57:53 +00005201 static PyObject *new_str;
5202 PyObject *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005203 PyObject *newargs, *x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005204 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005205
Guido van Rossum7bed2132002-08-08 21:57:53 +00005206 if (new_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005207 new_str = PyUnicode_InternFromString("__new__");
Guido van Rossum7bed2132002-08-08 21:57:53 +00005208 if (new_str == NULL)
5209 return NULL;
5210 }
5211 func = PyObject_GetAttr((PyObject *)type, new_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005212 if (func == NULL)
5213 return NULL;
5214 assert(PyTuple_Check(args));
5215 n = PyTuple_GET_SIZE(args);
5216 newargs = PyTuple_New(n+1);
5217 if (newargs == NULL)
5218 return NULL;
5219 Py_INCREF(type);
5220 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
5221 for (i = 0; i < n; i++) {
5222 x = PyTuple_GET_ITEM(args, i);
5223 Py_INCREF(x);
5224 PyTuple_SET_ITEM(newargs, i+1, x);
5225 }
5226 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00005227 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005228 Py_DECREF(func);
5229 return x;
5230}
5231
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005232static void
5233slot_tp_del(PyObject *self)
5234{
5235 static PyObject *del_str = NULL;
5236 PyObject *del, *res;
5237 PyObject *error_type, *error_value, *error_traceback;
5238
5239 /* Temporarily resurrect the object. */
5240 assert(self->ob_refcnt == 0);
5241 self->ob_refcnt = 1;
5242
5243 /* Save the current exception, if any. */
5244 PyErr_Fetch(&error_type, &error_value, &error_traceback);
5245
5246 /* Execute __del__ method, if any. */
5247 del = lookup_maybe(self, "__del__", &del_str);
5248 if (del != NULL) {
5249 res = PyEval_CallObject(del, NULL);
5250 if (res == NULL)
5251 PyErr_WriteUnraisable(del);
5252 else
5253 Py_DECREF(res);
5254 Py_DECREF(del);
5255 }
5256
5257 /* Restore the saved exception. */
5258 PyErr_Restore(error_type, error_value, error_traceback);
5259
5260 /* Undo the temporary resurrection; can't use DECREF here, it would
5261 * cause a recursive call.
5262 */
5263 assert(self->ob_refcnt > 0);
5264 if (--self->ob_refcnt == 0)
5265 return; /* this is the normal path out */
5266
5267 /* __del__ resurrected it! Make it look like the original Py_DECREF
5268 * never happened.
5269 */
5270 {
Martin v. Löwis725507b2006-03-07 12:08:51 +00005271 Py_ssize_t refcnt = self->ob_refcnt;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005272 _Py_NewReference(self);
5273 self->ob_refcnt = refcnt;
5274 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005275 assert(!PyType_IS_GC(Py_TYPE(self)) ||
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005276 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
Michael W. Hudson3f3b6682004-08-03 10:21:03 +00005277 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5278 * we need to undo that. */
5279 _Py_DEC_REFTOTAL;
5280 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5281 * chain, so no more to do there.
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005282 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5283 * _Py_NewReference bumped tp_allocs: both of those need to be
5284 * undone.
5285 */
5286#ifdef COUNT_ALLOCS
Christian Heimes90aa7642007-12-19 02:45:37 +00005287 --Py_TYPE(self)->tp_frees;
5288 --Py_TYPE(self)->tp_allocs;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005289#endif
5290}
5291
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005292
5293/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
Tim Petersbf9b2442003-03-23 05:35:36 +00005294 functions. The offsets here are relative to the 'PyHeapTypeObject'
Guido van Rossume5c691a2003-03-07 15:13:17 +00005295 structure, which incorporates the additional structures used for numbers,
5296 sequences and mappings.
5297 Note that multiple names may map to the same slot (e.g. __eq__,
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005298 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00005299 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
5300 terminated with an all-zero entry. (This table is further initialized and
5301 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005302
Guido van Rossum6d204072001-10-21 00:44:31 +00005303typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005304
5305#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00005306#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005307#undef ETSLOT
5308#undef SQSLOT
5309#undef MPSLOT
5310#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00005311#undef UNSLOT
5312#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005313#undef BINSLOT
5314#undef RBINSLOT
5315
Guido van Rossum6d204072001-10-21 00:44:31 +00005316#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005317 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5318 PyDoc_STR(DOC)}
Guido van Rossumc8e56452001-10-22 00:43:43 +00005319#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5320 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005321 PyDoc_STR(DOC), FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00005322#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Guido van Rossume5c691a2003-03-07 15:13:17 +00005323 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005324 PyDoc_STR(DOC)}
Guido van Rossum6d204072001-10-21 00:44:31 +00005325#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5326 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5327#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5328 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5329#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5330 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5331#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5332 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5333 "x." NAME "() <==> " DOC)
5334#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5335 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5336 "x." NAME "(y) <==> x" DOC "y")
5337#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5338 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5339 "x." NAME "(y) <==> x" DOC "y")
5340#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5341 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5342 "x." NAME "(y) <==> y" DOC "x")
Anthony Baxter56616992005-06-03 14:12:21 +00005343#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5344 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5345 "x." NAME "(y) <==> " DOC)
5346#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5347 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5348 "x." NAME "(y) <==> " DOC)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005349
5350static slotdef slotdefs[] = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00005351 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00005352 "x.__len__() <==> len(x)"),
Armin Rigofd163f92005-12-29 15:59:19 +00005353 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5354 The logic in abstract.c always falls back to nb_add/nb_multiply in
5355 this case. Defining both the nb_* and the sq_* slots to call the
5356 user-defined methods has unexpected side-effects, as shown by
5357 test_descr.notimplemented() */
5358 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005359 "x.__add__(y) <==> x+y"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005360 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005361 "x.__mul__(n) <==> x*n"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005362 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005363 "x.__rmul__(n) <==> n*x"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005364 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
5365 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005366 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00005367 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005368 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00005369 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005370 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
5371 "x.__contains__(y) <==> y in x"),
Armin Rigofd163f92005-12-29 15:59:19 +00005372 SQSLOT("__iadd__", sq_inplace_concat, NULL,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005373 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Armin Rigofd163f92005-12-29 15:59:19 +00005374 SQSLOT("__imul__", sq_inplace_repeat, NULL,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005375 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005376
Martin v. Löwis18e16552006-02-15 17:27:45 +00005377 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00005378 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00005379 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005380 wrap_binaryfunc,
5381 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005382 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005383 wrap_objobjargproc,
5384 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005385 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005386 wrap_delitem,
5387 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005388
Guido van Rossum6d204072001-10-21 00:44:31 +00005389 BINSLOT("__add__", nb_add, slot_nb_add,
5390 "+"),
5391 RBINSLOT("__radd__", nb_add, slot_nb_add,
5392 "+"),
5393 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5394 "-"),
5395 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5396 "-"),
5397 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5398 "*"),
5399 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5400 "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005401 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5402 "%"),
5403 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5404 "%"),
Anthony Baxter56616992005-06-03 14:12:21 +00005405 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00005406 "divmod(x, y)"),
Anthony Baxter56616992005-06-03 14:12:21 +00005407 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00005408 "divmod(y, x)"),
5409 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5410 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5411 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5412 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5413 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5414 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5415 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5416 "abs(x)"),
Jack Diederich4dafcc42006-11-28 19:15:13 +00005417 UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
Guido van Rossum6d204072001-10-21 00:44:31 +00005418 "x != 0"),
5419 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5420 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5421 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5422 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5423 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5424 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5425 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5426 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5427 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5428 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5429 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005430 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5431 "int(x)"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005432 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5433 "float(x)"),
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00005434 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005435 "x[y:z] <==> x[y.__index__():z.__index__()]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005436 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5437 wrap_binaryfunc, "+"),
5438 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5439 wrap_binaryfunc, "-"),
5440 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5441 wrap_binaryfunc, "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005442 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5443 wrap_binaryfunc, "%"),
5444 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
Guido van Rossum6e5680f2002-10-15 01:01:53 +00005445 wrap_binaryfunc, "**"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005446 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5447 wrap_binaryfunc, "<<"),
5448 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5449 wrap_binaryfunc, ">>"),
5450 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5451 wrap_binaryfunc, "&"),
5452 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5453 wrap_binaryfunc, "^"),
5454 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5455 wrap_binaryfunc, "|"),
5456 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5457 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5458 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5459 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5460 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5461 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5462 IBSLOT("__itruediv__", nb_inplace_true_divide,
5463 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005464
Guido van Rossum6d204072001-10-21 00:44:31 +00005465 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5466 "x.__str__() <==> str(x)"),
5467 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5468 "x.__repr__() <==> repr(x)"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005469 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5470 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005471 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5472 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005473 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00005474 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5475 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5476 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5477 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5478 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5479 "x.__setattr__('name', value) <==> x.name = value"),
5480 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5481 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5482 "x.__delattr__('name') <==> del x.name"),
5483 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5484 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5485 "x.__lt__(y) <==> x<y"),
5486 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5487 "x.__le__(y) <==> x<=y"),
5488 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5489 "x.__eq__(y) <==> x==y"),
5490 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5491 "x.__ne__(y) <==> x!=y"),
5492 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5493 "x.__gt__(y) <==> x>y"),
5494 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5495 "x.__ge__(y) <==> x>=y"),
5496 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5497 "x.__iter__() <==> iter(x)"),
Georg Brandla18af4e2007-04-21 15:47:16 +00005498 TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
5499 "x.__next__() <==> next(x)"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005500 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5501 "descr.__get__(obj[, type]) -> value"),
5502 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5503 "descr.__set__(obj, value)"),
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00005504 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5505 wrap_descr_delete, "descr.__delete__(obj)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005506 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00005507 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00005508 "see x.__class__.__doc__ for signature",
5509 PyWrapperFlag_KEYWORDS),
5510 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005511 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005512 {NULL}
5513};
5514
Guido van Rossumc334df52002-04-04 23:44:47 +00005515/* Given a type pointer and an offset gotten from a slotdef entry, return a
Guido van Rossumd8faa362007-04-27 19:54:29 +00005516 pointer to the actual slot. This is not quite the same as simply adding
Guido van Rossumc334df52002-04-04 23:44:47 +00005517 the offset to the type pointer, since it takes care to indirect through the
5518 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5519 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005520static void **
Martin v. Löwis18e16552006-02-15 17:27:45 +00005521slotptr(PyTypeObject *type, int ioffset)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005522{
5523 char *ptr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005524 long offset = ioffset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005525
Guido van Rossume5c691a2003-03-07 15:13:17 +00005526 /* Note: this depends on the order of the members of PyHeapTypeObject! */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005527 assert(offset >= 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005528 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5529 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5530 ptr = (char *)type->tp_as_sequence;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005531 offset -= offsetof(PyHeapTypeObject, as_sequence);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005532 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005533 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5534 ptr = (char *)type->tp_as_mapping;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005535 offset -= offsetof(PyHeapTypeObject, as_mapping);
Guido van Rossum09638c12002-06-13 19:17:46 +00005536 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005537 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
5538 ptr = (char *)type->tp_as_number;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005539 offset -= offsetof(PyHeapTypeObject, as_number);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005540 }
5541 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005542 ptr = (char *)type;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005543 }
5544 if (ptr != NULL)
5545 ptr += offset;
5546 return (void **)ptr;
5547}
Guido van Rossumf040ede2001-08-07 16:40:56 +00005548
Guido van Rossumc334df52002-04-04 23:44:47 +00005549/* Length of array of slotdef pointers used to store slots with the
5550 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5551 the same __name__, for any __name__. Since that's a static property, it is
5552 appropriate to declare fixed-size arrays for this. */
5553#define MAX_EQUIV 10
5554
5555/* Return a slot pointer for a given name, but ONLY if the attribute has
5556 exactly one slot function. The name must be an interned string. */
5557static void **
5558resolve_slotdups(PyTypeObject *type, PyObject *name)
5559{
5560 /* XXX Maybe this could be optimized more -- but is it worth it? */
5561
5562 /* pname and ptrs act as a little cache */
5563 static PyObject *pname;
5564 static slotdef *ptrs[MAX_EQUIV];
5565 slotdef *p, **pp;
5566 void **res, **ptr;
5567
5568 if (pname != name) {
5569 /* Collect all slotdefs that match name into ptrs. */
5570 pname = name;
5571 pp = ptrs;
5572 for (p = slotdefs; p->name_strobj; p++) {
5573 if (p->name_strobj == name)
5574 *pp++ = p;
5575 }
5576 *pp = NULL;
5577 }
5578
5579 /* Look in all matching slots of the type; if exactly one of these has
Guido van Rossumd8faa362007-04-27 19:54:29 +00005580 a filled-in slot, return its value. Otherwise return NULL. */
Guido van Rossumc334df52002-04-04 23:44:47 +00005581 res = NULL;
5582 for (pp = ptrs; *pp; pp++) {
5583 ptr = slotptr(type, (*pp)->offset);
5584 if (ptr == NULL || *ptr == NULL)
5585 continue;
5586 if (res != NULL)
5587 return NULL;
5588 res = ptr;
5589 }
5590 return res;
5591}
5592
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005593/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
Guido van Rossumc334df52002-04-04 23:44:47 +00005594 does some incredibly complex thinking and then sticks something into the
5595 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5596 interests, and then stores a generic wrapper or a specific function into
5597 the slot.) Return a pointer to the next slotdef with a different offset,
5598 because that's convenient for fixup_slot_dispatchers(). */
5599static slotdef *
5600update_one_slot(PyTypeObject *type, slotdef *p)
5601{
5602 PyObject *descr;
5603 PyWrapperDescrObject *d;
5604 void *generic = NULL, *specific = NULL;
5605 int use_generic = 0;
5606 int offset = p->offset;
5607 void **ptr = slotptr(type, offset);
5608
5609 if (ptr == NULL) {
5610 do {
5611 ++p;
5612 } while (p->offset == offset);
5613 return p;
5614 }
5615 do {
5616 descr = _PyType_Lookup(type, p->name_strobj);
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00005617 if (descr == NULL) {
5618 if (ptr == (void**)&type->tp_iternext) {
5619 specific = _PyObject_NextNotImplemented;
5620 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005621 continue;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00005622 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005623 if (Py_TYPE(descr) == &PyWrapperDescr_Type) {
Guido van Rossumc334df52002-04-04 23:44:47 +00005624 void **tptr = resolve_slotdups(type, p->name_strobj);
5625 if (tptr == NULL || tptr == ptr)
5626 generic = p->function;
5627 d = (PyWrapperDescrObject *)descr;
5628 if (d->d_base->wrapper == p->wrapper &&
Alexandre Vassalotti2db046d2009-07-22 03:56:36 +00005629 PyType_IsSubtype(type, PyDescr_TYPE(d)))
Guido van Rossumc334df52002-04-04 23:44:47 +00005630 {
5631 if (specific == NULL ||
5632 specific == d->d_wrapped)
5633 specific = d->d_wrapped;
5634 else
5635 use_generic = 1;
5636 }
5637 }
Christian Heimes90aa7642007-12-19 02:45:37 +00005638 else if (Py_TYPE(descr) == &PyCFunction_Type &&
Guido van Rossum721f62e2002-08-09 02:14:34 +00005639 PyCFunction_GET_FUNCTION(descr) ==
5640 (PyCFunction)tp_new_wrapper &&
Benjamin Petersonb5479792009-01-18 22:10:38 +00005641 ptr == (void**)&type->tp_new)
Guido van Rossum721f62e2002-08-09 02:14:34 +00005642 {
5643 /* The __new__ wrapper is not a wrapper descriptor,
5644 so must be special-cased differently.
5645 If we don't do this, creating an instance will
5646 always use slot_tp_new which will look up
5647 __new__ in the MRO which will call tp_new_wrapper
5648 which will look through the base classes looking
5649 for a static base and call its tp_new (usually
5650 PyType_GenericNew), after performing various
5651 sanity checks and constructing a new argument
5652 list. Cut all that nonsense short -- this speeds
5653 up instance creation tremendously. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005654 specific = (void *)type->tp_new;
Guido van Rossum721f62e2002-08-09 02:14:34 +00005655 /* XXX I'm not 100% sure that there isn't a hole
5656 in this reasoning that requires additional
5657 sanity checks. I'll buy the first person to
5658 point out a bug in this reasoning a beer. */
5659 }
Nick Coghland1abd252008-07-15 15:46:38 +00005660 else if (descr == Py_None &&
Benjamin Petersonb5479792009-01-18 22:10:38 +00005661 ptr == (void**)&type->tp_hash) {
Nick Coghland1abd252008-07-15 15:46:38 +00005662 /* We specifically allow __hash__ to be set to None
5663 to prevent inheritance of the default
5664 implementation from object.__hash__ */
5665 specific = PyObject_HashNotImplemented;
5666 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005667 else {
5668 use_generic = 1;
5669 generic = p->function;
5670 }
5671 } while ((++p)->offset == offset);
5672 if (specific && !use_generic)
5673 *ptr = specific;
5674 else
5675 *ptr = generic;
5676 return p;
5677}
5678
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005679/* In the type, update the slots whose slotdefs are gathered in the pp array.
5680 This is a callback for update_subclasses(). */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005681static int
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005682update_slots_callback(PyTypeObject *type, void *data)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005683{
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005684 slotdef **pp = (slotdef **)data;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005685
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005686 for (; *pp; pp++)
Guido van Rossumc334df52002-04-04 23:44:47 +00005687 update_one_slot(type, *pp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005688 return 0;
5689}
5690
Guido van Rossumc334df52002-04-04 23:44:47 +00005691/* Comparison function for qsort() to compare slotdefs by their offset, and
5692 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005693static int
5694slotdef_cmp(const void *aa, const void *bb)
5695{
5696 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5697 int c = a->offset - b->offset;
5698 if (c != 0)
5699 return c;
5700 else
Martin v. Löwis18e16552006-02-15 17:27:45 +00005701 /* Cannot use a-b, as this gives off_t,
5702 which may lose precision when converted to int. */
5703 return (a > b) ? 1 : (a < b) ? -1 : 0;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005704}
5705
Guido van Rossumc334df52002-04-04 23:44:47 +00005706/* Initialize the slotdefs table by adding interned string objects for the
5707 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005708static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005709init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005710{
5711 slotdef *p;
5712 static int initialized = 0;
5713
5714 if (initialized)
5715 return;
5716 for (p = slotdefs; p->name; p++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00005717 p->name_strobj = PyUnicode_InternFromString(p->name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005718 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00005719 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005720 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005721 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5722 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005723 initialized = 1;
5724}
5725
Guido van Rossumc334df52002-04-04 23:44:47 +00005726/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005727static int
5728update_slot(PyTypeObject *type, PyObject *name)
5729{
Guido van Rossumc334df52002-04-04 23:44:47 +00005730 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005731 slotdef *p;
5732 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005733 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005734
Christian Heimesa62da1d2008-01-12 19:39:10 +00005735 /* Clear the VALID_VERSION flag of 'type' and all its
5736 subclasses. This could possibly be unified with the
5737 update_subclasses() recursion below, but carefully:
5738 they each have their own conditions on which to stop
5739 recursing into subclasses. */
Georg Brandlf08a9dd2008-06-10 16:57:31 +00005740 PyType_Modified(type);
Christian Heimesa62da1d2008-01-12 19:39:10 +00005741
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005742 init_slotdefs();
5743 pp = ptrs;
5744 for (p = slotdefs; p->name; p++) {
5745 /* XXX assume name is interned! */
5746 if (p->name_strobj == name)
5747 *pp++ = p;
5748 }
5749 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005750 for (pp = ptrs; *pp; pp++) {
5751 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005752 offset = p->offset;
5753 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005754 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005755 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005756 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005757 if (ptrs[0] == NULL)
5758 return 0; /* Not an attribute that affects any slots */
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005759 return update_subclasses(type, name,
5760 update_slots_callback, (void *)ptrs);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005761}
5762
Guido van Rossumc334df52002-04-04 23:44:47 +00005763/* Store the proper functions in the slot dispatches at class (type)
5764 definition time, based upon which operations the class overrides in its
5765 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00005766static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005767fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005768{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005769 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005770
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005771 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00005772 for (p = slotdefs; p->name; )
5773 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005774}
Guido van Rossum705f0f52001-08-24 16:47:00 +00005775
Michael W. Hudson98bbc492002-11-26 14:47:27 +00005776static void
5777update_all_slots(PyTypeObject* type)
5778{
5779 slotdef *p;
5780
5781 init_slotdefs();
5782 for (p = slotdefs; p->name; p++) {
5783 /* update_slot returns int but can't actually fail */
5784 update_slot(type, p->name_strobj);
5785 }
5786}
5787
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005788/* recurse_down_subclasses() and update_subclasses() are mutually
5789 recursive functions to call a callback for all subclasses,
5790 but refraining from recursing into subclasses that define 'name'. */
5791
5792static int
5793update_subclasses(PyTypeObject *type, PyObject *name,
5794 update_callback callback, void *data)
5795{
5796 if (callback(type, data) < 0)
5797 return -1;
5798 return recurse_down_subclasses(type, name, callback, data);
5799}
5800
5801static int
5802recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5803 update_callback callback, void *data)
5804{
5805 PyTypeObject *subclass;
5806 PyObject *ref, *subclasses, *dict;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005807 Py_ssize_t i, n;
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005808
5809 subclasses = type->tp_subclasses;
5810 if (subclasses == NULL)
5811 return 0;
5812 assert(PyList_Check(subclasses));
5813 n = PyList_GET_SIZE(subclasses);
5814 for (i = 0; i < n; i++) {
5815 ref = PyList_GET_ITEM(subclasses, i);
5816 assert(PyWeakref_CheckRef(ref));
5817 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5818 assert(subclass != NULL);
5819 if ((PyObject *)subclass == Py_None)
5820 continue;
5821 assert(PyType_Check(subclass));
5822 /* Avoid recursing down into unaffected classes */
5823 dict = subclass->tp_dict;
5824 if (dict != NULL && PyDict_Check(dict) &&
5825 PyDict_GetItem(dict, name) != NULL)
5826 continue;
5827 if (update_subclasses(subclass, name, callback, data) < 0)
5828 return -1;
5829 }
5830 return 0;
5831}
5832
Guido van Rossum6d204072001-10-21 00:44:31 +00005833/* This function is called by PyType_Ready() to populate the type's
5834 dictionary with method descriptors for function slots. For each
Guido van Rossum09638c12002-06-13 19:17:46 +00005835 function slot (like tp_repr) that's defined in the type, one or more
5836 corresponding descriptors are added in the type's tp_dict dictionary
Guido van Rossumd8faa362007-04-27 19:54:29 +00005837 under the appropriate name (like __repr__). Some function slots
Guido van Rossum09638c12002-06-13 19:17:46 +00005838 cause more than one descriptor to be added (for example, the nb_add
5839 slot adds both __add__ and __radd__ descriptors) and some function
5840 slots compete for the same descriptor (for example both sq_item and
5841 mp_subscript generate a __getitem__ descriptor).
5842
Guido van Rossumd8faa362007-04-27 19:54:29 +00005843 In the latter case, the first slotdef entry encoutered wins. Since
Tim Petersbf9b2442003-03-23 05:35:36 +00005844 slotdef entries are sorted by the offset of the slot in the
Guido van Rossume5c691a2003-03-07 15:13:17 +00005845 PyHeapTypeObject, this gives us some control over disambiguating
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005846 between competing slots: the members of PyHeapTypeObject are listed
5847 from most general to least general, so the most general slot is
5848 preferred. In particular, because as_mapping comes before as_sequence,
5849 for a type that defines both mp_subscript and sq_item, mp_subscript
5850 wins.
Guido van Rossum09638c12002-06-13 19:17:46 +00005851
5852 This only adds new descriptors and doesn't overwrite entries in
5853 tp_dict that were previously defined. The descriptors contain a
5854 reference to the C function they must call, so that it's safe if they
5855 are copied into a subtype's __dict__ and the subtype has a different
5856 C function in its slot -- calling the method defined by the
5857 descriptor will call the C function that was used to create it,
5858 rather than the C function present in the slot when it is called.
5859 (This is important because a subtype may have a C function in the
5860 slot that calls the method from the dictionary, and we want to avoid
5861 infinite recursion here.) */
Guido van Rossum6d204072001-10-21 00:44:31 +00005862
5863static int
5864add_operators(PyTypeObject *type)
5865{
5866 PyObject *dict = type->tp_dict;
5867 slotdef *p;
5868 PyObject *descr;
5869 void **ptr;
5870
5871 init_slotdefs();
5872 for (p = slotdefs; p->name; p++) {
5873 if (p->wrapper == NULL)
5874 continue;
5875 ptr = slotptr(type, p->offset);
5876 if (!ptr || !*ptr)
5877 continue;
5878 if (PyDict_GetItem(dict, p->name_strobj))
5879 continue;
Nick Coghland1abd252008-07-15 15:46:38 +00005880 if (*ptr == PyObject_HashNotImplemented) {
5881 /* Classes may prevent the inheritance of the tp_hash
5882 slot by storing PyObject_HashNotImplemented in it. Make it
5883 visible as a None value for the __hash__ attribute. */
5884 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
5885 return -1;
5886 }
5887 else {
5888 descr = PyDescr_NewWrapper(type, p, *ptr);
5889 if (descr == NULL)
5890 return -1;
5891 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5892 return -1;
5893 Py_DECREF(descr);
5894 }
Guido van Rossum6d204072001-10-21 00:44:31 +00005895 }
5896 if (type->tp_new != NULL) {
5897 if (add_tp_new_wrapper(type) < 0)
5898 return -1;
5899 }
5900 return 0;
5901}
5902
Guido van Rossum705f0f52001-08-24 16:47:00 +00005903
5904/* Cooperative 'super' */
5905
5906typedef struct {
5907 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00005908 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005909 PyObject *obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005910 PyTypeObject *obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005911} superobject;
5912
Guido van Rossum6f799372001-09-20 20:46:19 +00005913static PyMemberDef super_members[] = {
5914 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5915 "the class invoking super()"},
5916 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5917 "the instance invoking super(); may be None"},
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005918 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005919 "the type of the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005920 {0}
5921};
5922
Guido van Rossum705f0f52001-08-24 16:47:00 +00005923static void
5924super_dealloc(PyObject *self)
5925{
5926 superobject *su = (superobject *)self;
5927
Guido van Rossum048eb752001-10-02 21:24:57 +00005928 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005929 Py_XDECREF(su->obj);
5930 Py_XDECREF(su->type);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005931 Py_XDECREF(su->obj_type);
Christian Heimes90aa7642007-12-19 02:45:37 +00005932 Py_TYPE(self)->tp_free(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005933}
5934
5935static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005936super_repr(PyObject *self)
5937{
5938 superobject *su = (superobject *)self;
5939
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005940 if (su->obj_type)
Walter Dörwald1ab83302007-05-18 17:15:44 +00005941 return PyUnicode_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005942 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005943 su->type ? su->type->tp_name : "NULL",
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005944 su->obj_type->tp_name);
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005945 else
Walter Dörwald1ab83302007-05-18 17:15:44 +00005946 return PyUnicode_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005947 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005948 su->type ? su->type->tp_name : "NULL");
5949}
5950
5951static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00005952super_getattro(PyObject *self, PyObject *name)
5953{
5954 superobject *su = (superobject *)self;
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005955 int skip = su->obj_type == NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005956
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005957 if (!skip) {
5958 /* We want __class__ to return the class of the super object
5959 (i.e. super, or a subclass), not the class of su->obj. */
Martin v. Löwis5b222132007-06-10 09:51:05 +00005960 skip = (PyUnicode_Check(name) &&
5961 PyUnicode_GET_SIZE(name) == 9 &&
5962 PyUnicode_CompareWithASCIIString(name, "__class__") == 0);
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005963 }
5964
5965 if (!skip) {
Tim Petersa91e9642001-11-14 23:32:33 +00005966 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005967 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005968 descrgetfunc f;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005969 Py_ssize_t i, n;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005970
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005971 starttype = su->obj_type;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005972 mro = starttype->tp_mro;
5973
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005974 if (mro == NULL)
5975 n = 0;
5976 else {
5977 assert(PyTuple_Check(mro));
5978 n = PyTuple_GET_SIZE(mro);
5979 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005980 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00005981 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00005982 break;
5983 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005984 i++;
5985 res = NULL;
5986 for (; i < n; i++) {
5987 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00005988 if (PyType_Check(tmp))
5989 dict = ((PyTypeObject *)tmp)->tp_dict;
Tim Petersa91e9642001-11-14 23:32:33 +00005990 else
5991 continue;
5992 res = PyDict_GetItem(dict, name);
Guido van Rossum6cc5bb62003-04-16 20:01:36 +00005993 if (res != NULL) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00005994 Py_INCREF(res);
Christian Heimes90aa7642007-12-19 02:45:37 +00005995 f = Py_TYPE(res)->tp_descr_get;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005996 if (f != NULL) {
Phillip J. Eby91a968a2004-03-25 02:19:34 +00005997 tmp = f(res,
5998 /* Only pass 'obj' param if
5999 this is instance-mode super
6000 (See SF ID #743627)
6001 */
Hye-Shik Changff365c92004-03-25 16:37:03 +00006002 (su->obj == (PyObject *)
6003 su->obj_type
Phillip J. Eby91a968a2004-03-25 02:19:34 +00006004 ? (PyObject *)NULL
6005 : su->obj),
Guido van Rossumd4641072002-04-03 02:13:37 +00006006 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00006007 Py_DECREF(res);
6008 res = tmp;
6009 }
6010 return res;
6011 }
6012 }
6013 }
6014 return PyObject_GenericGetAttr(self, name);
6015}
6016
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006017static PyTypeObject *
Guido van Rossum5b443c62001-12-03 15:38:28 +00006018supercheck(PyTypeObject *type, PyObject *obj)
6019{
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006020 /* Check that a super() call makes sense. Return a type object.
6021
6022 obj can be a new-style class, or an instance of one:
6023
Guido van Rossumd8faa362007-04-27 19:54:29 +00006024 - If it is a class, it must be a subclass of 'type'. This case is
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006025 used for class methods; the return value is obj.
6026
6027 - If it is an instance, it must be an instance of 'type'. This is
6028 the normal case; the return value is obj.__class__.
6029
6030 But... when obj is an instance, we want to allow for the case where
Christian Heimes90aa7642007-12-19 02:45:37 +00006031 Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006032 This will allow using super() with a proxy for obj.
6033 */
6034
Guido van Rossum8e80a722003-02-18 19:22:22 +00006035 /* Check for first bullet above (special case) */
6036 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
6037 Py_INCREF(obj);
6038 return (PyTypeObject *)obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006039 }
Guido van Rossum8e80a722003-02-18 19:22:22 +00006040
6041 /* Normal case */
Christian Heimes90aa7642007-12-19 02:45:37 +00006042 if (PyType_IsSubtype(Py_TYPE(obj), type)) {
6043 Py_INCREF(Py_TYPE(obj));
6044 return Py_TYPE(obj);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006045 }
6046 else {
6047 /* Try the slow way */
6048 static PyObject *class_str = NULL;
6049 PyObject *class_attr;
6050
6051 if (class_str == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00006052 class_str = PyUnicode_FromString("__class__");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006053 if (class_str == NULL)
6054 return NULL;
6055 }
6056
6057 class_attr = PyObject_GetAttr(obj, class_str);
6058
6059 if (class_attr != NULL &&
6060 PyType_Check(class_attr) &&
Christian Heimes90aa7642007-12-19 02:45:37 +00006061 (PyTypeObject *)class_attr != Py_TYPE(obj))
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006062 {
6063 int ok = PyType_IsSubtype(
6064 (PyTypeObject *)class_attr, type);
6065 if (ok)
6066 return (PyTypeObject *)class_attr;
6067 }
6068
6069 if (class_attr == NULL)
6070 PyErr_Clear();
6071 else
6072 Py_DECREF(class_attr);
6073 }
6074
Guido van Rossumd8faa362007-04-27 19:54:29 +00006075 PyErr_SetString(PyExc_TypeError,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006076 "super(type, obj): "
6077 "obj must be an instance or subtype of type");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006078 return NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00006079}
6080
Guido van Rossum705f0f52001-08-24 16:47:00 +00006081static PyObject *
6082super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
6083{
6084 superobject *su = (superobject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006085 superobject *newobj;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006086
6087 if (obj == NULL || obj == Py_None || su->obj != NULL) {
6088 /* Not binding to an object, or already bound */
6089 Py_INCREF(self);
6090 return self;
6091 }
Christian Heimes90aa7642007-12-19 02:45:37 +00006092 if (Py_TYPE(su) != &PySuper_Type)
Armin Rigo7726dc02005-05-15 15:32:08 +00006093 /* If su is an instance of a (strict) subclass of super,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006094 call its type */
Christian Heimes90aa7642007-12-19 02:45:37 +00006095 return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
Guido van Rossumd8faa362007-04-27 19:54:29 +00006096 su->type, obj, NULL);
Guido van Rossum5b443c62001-12-03 15:38:28 +00006097 else {
6098 /* Inline the common case */
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006099 PyTypeObject *obj_type = supercheck(su->type, obj);
6100 if (obj_type == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00006101 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006102 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006103 NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006104 if (newobj == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00006105 return NULL;
6106 Py_INCREF(su->type);
6107 Py_INCREF(obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006108 newobj->type = su->type;
6109 newobj->obj = obj;
6110 newobj->obj_type = obj_type;
6111 return (PyObject *)newobj;
Guido van Rossum5b443c62001-12-03 15:38:28 +00006112 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006113}
6114
6115static int
6116super_init(PyObject *self, PyObject *args, PyObject *kwds)
6117{
6118 superobject *su = (superobject *)self;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006119 PyTypeObject *type = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00006120 PyObject *obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006121 PyTypeObject *obj_type = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006122
Thomas Wouters89f507f2006-12-13 04:49:30 +00006123 if (!_PyArg_NoKeywords("super", kwds))
6124 return -1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006125 if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj))
Guido van Rossum705f0f52001-08-24 16:47:00 +00006126 return -1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006127
6128 if (type == NULL) {
6129 /* Call super(), without args -- fill in from __class__
6130 and first local variable on the stack. */
6131 PyFrameObject *f = PyThreadState_GET()->frame;
6132 PyCodeObject *co = f->f_code;
6133 int i, n;
6134 if (co == NULL) {
6135 PyErr_SetString(PyExc_SystemError,
6136 "super(): no code object");
6137 return -1;
6138 }
6139 if (co->co_argcount == 0) {
6140 PyErr_SetString(PyExc_SystemError,
6141 "super(): no arguments");
6142 return -1;
6143 }
6144 obj = f->f_localsplus[0];
6145 if (obj == NULL) {
6146 PyErr_SetString(PyExc_SystemError,
6147 "super(): arg[0] deleted");
6148 return -1;
6149 }
6150 if (co->co_freevars == NULL)
6151 n = 0;
6152 else {
6153 assert(PyTuple_Check(co->co_freevars));
6154 n = PyTuple_GET_SIZE(co->co_freevars);
6155 }
6156 for (i = 0; i < n; i++) {
6157 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
6158 assert(PyUnicode_Check(name));
6159 if (!PyUnicode_CompareWithASCIIString(name,
6160 "__class__")) {
Barry Warsaw91cc8fb2008-11-20 20:01:57 +00006161 Py_ssize_t index = co->co_nlocals +
6162 PyTuple_GET_SIZE(co->co_cellvars) + i;
6163 PyObject *cell = f->f_localsplus[index];
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006164 if (cell == NULL || !PyCell_Check(cell)) {
6165 PyErr_SetString(PyExc_SystemError,
6166 "super(): bad __class__ cell");
6167 return -1;
6168 }
6169 type = (PyTypeObject *) PyCell_GET(cell);
6170 if (type == NULL) {
6171 PyErr_SetString(PyExc_SystemError,
6172 "super(): empty __class__ cell");
6173 return -1;
6174 }
6175 if (!PyType_Check(type)) {
6176 PyErr_Format(PyExc_SystemError,
6177 "super(): __class__ is not a type (%s)",
Christian Heimes90aa7642007-12-19 02:45:37 +00006178 Py_TYPE(type)->tp_name);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006179 return -1;
6180 }
6181 break;
6182 }
6183 }
6184 if (type == NULL) {
6185 PyErr_SetString(PyExc_SystemError,
6186 "super(): __class__ cell not found");
6187 return -1;
6188 }
6189 }
6190
Guido van Rossum705f0f52001-08-24 16:47:00 +00006191 if (obj == Py_None)
6192 obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006193 if (obj != NULL) {
6194 obj_type = supercheck(type, obj);
6195 if (obj_type == NULL)
6196 return -1;
6197 Py_INCREF(obj);
6198 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006199 Py_INCREF(type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00006200 su->type = type;
6201 su->obj = obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006202 su->obj_type = obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006203 return 0;
6204}
6205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006206PyDoc_STRVAR(super_doc,
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006207"super() -> same as super(__class__, <first argument>)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00006208"super(type) -> unbound super object\n"
6209"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00006210"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00006211"Typical use to call a cooperative superclass method:\n"
6212"class C(B):\n"
6213" def meth(self, arg):\n"
Guido van Rossumcd16bf62007-06-13 18:07:49 +00006214" super().meth(arg)\n"
6215"This works for class methods too:\n"
6216"class C(B):\n"
6217" @classmethod\n"
6218" def cmeth(cls, arg):\n"
6219" super().cmeth(arg)\n");
Guido van Rossum705f0f52001-08-24 16:47:00 +00006220
Guido van Rossum048eb752001-10-02 21:24:57 +00006221static int
6222super_traverse(PyObject *self, visitproc visit, void *arg)
6223{
6224 superobject *su = (superobject *)self;
Guido van Rossum048eb752001-10-02 21:24:57 +00006225
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006226 Py_VISIT(su->obj);
6227 Py_VISIT(su->type);
6228 Py_VISIT(su->obj_type);
Guido van Rossum048eb752001-10-02 21:24:57 +00006229
6230 return 0;
6231}
6232
Guido van Rossum705f0f52001-08-24 16:47:00 +00006233PyTypeObject PySuper_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00006234 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00006235 "super", /* tp_name */
6236 sizeof(superobject), /* tp_basicsize */
6237 0, /* tp_itemsize */
6238 /* methods */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006239 super_dealloc, /* tp_dealloc */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006240 0, /* tp_print */
6241 0, /* tp_getattr */
6242 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00006243 0, /* tp_reserved */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00006244 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006245 0, /* tp_as_number */
6246 0, /* tp_as_sequence */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006247 0, /* tp_as_mapping */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006248 0, /* tp_hash */
6249 0, /* tp_call */
6250 0, /* tp_str */
6251 super_getattro, /* tp_getattro */
6252 0, /* tp_setattro */
6253 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00006254 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
6255 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006256 super_doc, /* tp_doc */
6257 super_traverse, /* tp_traverse */
6258 0, /* tp_clear */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006259 0, /* tp_richcompare */
6260 0, /* tp_weaklistoffset */
6261 0, /* tp_iter */
6262 0, /* tp_iternext */
6263 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00006264 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006265 0, /* tp_getset */
6266 0, /* tp_base */
6267 0, /* tp_dict */
6268 super_descr_get, /* tp_descr_get */
6269 0, /* tp_descr_set */
6270 0, /* tp_dictoffset */
6271 super_init, /* tp_init */
6272 PyType_GenericAlloc, /* tp_alloc */
6273 PyType_GenericNew, /* tp_new */
Guido van Rossumd8faa362007-04-27 19:54:29 +00006274 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006275};