blob: 0ce7f8286148477a35029ed5bfb36f65342d71fa [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"
Tim Peters6d6c1a32001-08-02 04:15:00 +00004#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum9923ffe2002-06-04 19:52:53 +00006#include <ctype.h>
7
Guido van Rossum6f799372001-09-20 20:46:19 +00008static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +00009 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
10 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
11 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
Guido van Rossum9676b222001-08-17 20:32:36 +000012 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000013 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
15 {"__dictoffset__", T_LONG,
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
Tim Peters6d6c1a32001-08-02 04:15:00 +000017 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
18 {0}
19};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossumc0b618a1997-05-02 03:12:38 +000021static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +000022type_name(PyTypeObject *type, void *context)
23{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000024 const char *s;
Guido van Rossumc3542212001-08-16 09:18:56 +000025
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000026 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Guido van Rossume5c691a2003-03-07 15:13:17 +000027 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
Tim Petersea7f75d2002-12-07 21:39:16 +000028
Georg Brandlc255c7b2006-02-20 22:27:28 +000029 Py_INCREF(et->ht_name);
30 return et->ht_name;
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000031 }
32 else {
33 s = strrchr(type->tp_name, '.');
34 if (s == NULL)
35 s = type->tp_name;
36 else
37 s++;
38 return PyString_FromString(s);
39 }
Guido van Rossumc3542212001-08-16 09:18:56 +000040}
41
Michael W. Hudson98bbc492002-11-26 14:47:27 +000042static int
43type_set_name(PyTypeObject *type, PyObject *value, void *context)
44{
Guido van Rossume5c691a2003-03-07 15:13:17 +000045 PyHeapTypeObject* et;
Michael W. Hudson98bbc492002-11-26 14:47:27 +000046
47 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
48 PyErr_Format(PyExc_TypeError,
49 "can't set %s.__name__", type->tp_name);
50 return -1;
51 }
52 if (!value) {
53 PyErr_Format(PyExc_TypeError,
54 "can't delete %s.__name__", type->tp_name);
55 return -1;
56 }
57 if (!PyString_Check(value)) {
58 PyErr_Format(PyExc_TypeError,
59 "can only assign string to %s.__name__, not '%s'",
60 type->tp_name, value->ob_type->tp_name);
61 return -1;
62 }
Tim Petersea7f75d2002-12-07 21:39:16 +000063 if (strlen(PyString_AS_STRING(value))
Michael W. Hudson98bbc492002-11-26 14:47:27 +000064 != (size_t)PyString_GET_SIZE(value)) {
65 PyErr_Format(PyExc_ValueError,
66 "__name__ must not contain null bytes");
67 return -1;
68 }
69
Guido van Rossume5c691a2003-03-07 15:13:17 +000070 et = (PyHeapTypeObject*)type;
Michael W. Hudson98bbc492002-11-26 14:47:27 +000071
72 Py_INCREF(value);
73
Georg Brandlc255c7b2006-02-20 22:27:28 +000074 Py_DECREF(et->ht_name);
75 et->ht_name = value;
Michael W. Hudson98bbc492002-11-26 14:47:27 +000076
77 type->tp_name = PyString_AS_STRING(value);
78
79 return 0;
80}
81
Guido van Rossumc3542212001-08-16 09:18:56 +000082static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000083type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000084{
Guido van Rossumc3542212001-08-16 09:18:56 +000085 PyObject *mod;
86 char *s;
87
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000088 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
89 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Anthony Baxter3ecdb252004-06-11 14:41:18 +000090 if (!mod) {
91 PyErr_Format(PyExc_AttributeError, "__module__");
92 return 0;
93 }
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000094 Py_XINCREF(mod);
Guido van Rossumc3542212001-08-16 09:18:56 +000095 return mod;
96 }
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000097 else {
98 s = strrchr(type->tp_name, '.');
99 if (s != NULL)
100 return PyString_FromStringAndSize(
Armin Rigo7ccbca92006-10-04 12:17:45 +0000101 type->tp_name, (Py_ssize_t)(s - type->tp_name));
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +0000102 return PyString_FromString("__builtin__");
103 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000104}
105
Guido van Rossum3926a632001-09-25 16:25:58 +0000106static int
107type_set_module(PyTypeObject *type, PyObject *value, void *context)
108{
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000109 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
Guido van Rossum3926a632001-09-25 16:25:58 +0000110 PyErr_Format(PyExc_TypeError,
111 "can't set %s.__module__", type->tp_name);
112 return -1;
113 }
114 if (!value) {
115 PyErr_Format(PyExc_TypeError,
116 "can't delete %s.__module__", type->tp_name);
117 return -1;
118 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000119
Guido van Rossum3926a632001-09-25 16:25:58 +0000120 return PyDict_SetItemString(type->tp_dict, "__module__", value);
121}
122
Tim Peters6d6c1a32001-08-02 04:15:00 +0000123static PyObject *
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000124type_get_bases(PyTypeObject *type, void *context)
125{
126 Py_INCREF(type->tp_bases);
127 return type->tp_bases;
128}
129
Jeremy Hyltonfa955692007-02-27 18:29:45 +0000130static PyTypeObject *most_derived_metaclass(PyTypeObject *, PyObject *);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000131static PyTypeObject *best_base(PyObject *);
132static int mro_internal(PyTypeObject *);
133static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
134static int add_subclass(PyTypeObject*, PyTypeObject*);
135static void remove_subclass(PyTypeObject *, PyTypeObject *);
136static void update_all_slots(PyTypeObject *);
137
Guido van Rossum8d24ee92003-03-24 23:49:49 +0000138typedef int (*update_callback)(PyTypeObject *, void *);
139static int update_subclasses(PyTypeObject *type, PyObject *name,
140 update_callback callback, void *data);
141static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
142 update_callback callback, void *data);
143
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000144static int
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000145mro_subclasses(PyTypeObject *type, PyObject* temp)
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000146{
147 PyTypeObject *subclass;
148 PyObject *ref, *subclasses, *old_mro;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000149 Py_ssize_t i, n;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000150
151 subclasses = type->tp_subclasses;
152 if (subclasses == NULL)
153 return 0;
154 assert(PyList_Check(subclasses));
155 n = PyList_GET_SIZE(subclasses);
156 for (i = 0; i < n; i++) {
157 ref = PyList_GET_ITEM(subclasses, i);
158 assert(PyWeakref_CheckRef(ref));
159 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
160 assert(subclass != NULL);
161 if ((PyObject *)subclass == Py_None)
162 continue;
163 assert(PyType_Check(subclass));
164 old_mro = subclass->tp_mro;
165 if (mro_internal(subclass) < 0) {
166 subclass->tp_mro = old_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000167 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000168 }
169 else {
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000170 PyObject* tuple;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000171 tuple = PyTuple_Pack(2, subclass, old_mro);
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000172 Py_DECREF(old_mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000173 if (!tuple)
174 return -1;
175 if (PyList_Append(temp, tuple) < 0)
176 return -1;
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000177 Py_DECREF(tuple);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000178 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000179 if (mro_subclasses(subclass, temp) < 0)
180 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000181 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000182 return 0;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000183}
184
185static int
186type_set_bases(PyTypeObject *type, PyObject *value, void *context)
187{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000188 Py_ssize_t i;
189 int r = 0;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000190 PyObject *ob, *temp;
Jeremy Hyltonfa955692007-02-27 18:29:45 +0000191 PyTypeObject *new_base, *old_base, *metatype;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000192 PyObject *old_bases, *old_mro;
193
194 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
195 PyErr_Format(PyExc_TypeError,
196 "can't set %s.__bases__", type->tp_name);
197 return -1;
198 }
199 if (!value) {
200 PyErr_Format(PyExc_TypeError,
201 "can't delete %s.__bases__", type->tp_name);
202 return -1;
203 }
204 if (!PyTuple_Check(value)) {
205 PyErr_Format(PyExc_TypeError,
206 "can only assign tuple to %s.__bases__, not %s",
207 type->tp_name, value->ob_type->tp_name);
208 return -1;
209 }
Guido van Rossum3bbc0ee2002-12-13 17:49:38 +0000210 if (PyTuple_GET_SIZE(value) == 0) {
211 PyErr_Format(PyExc_TypeError,
212 "can only assign non-empty tuple to %s.__bases__, not ()",
213 type->tp_name);
214 return -1;
215 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000216 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
217 ob = PyTuple_GET_ITEM(value, i);
218 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
219 PyErr_Format(
220 PyExc_TypeError,
221 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
222 type->tp_name, ob->ob_type->tp_name);
223 return -1;
224 }
Michael W. Hudsoncaf17be2002-11-27 10:24:44 +0000225 if (PyType_Check(ob)) {
226 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
227 PyErr_SetString(PyExc_TypeError,
228 "a __bases__ item causes an inheritance cycle");
229 return -1;
230 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000231 }
232 }
233
Jeremy Hyltonfa955692007-02-27 18:29:45 +0000234
235 metatype = most_derived_metaclass(type->ob_type, value);
236 if (metatype == NULL)
237 return -1;
238 if (metatype != type->ob_type) {
239 PyErr_SetString(PyExc_TypeError,
240 "assignment to __bases__ may not change "
241 "metatype");
242 return -1;
243 }
244
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000245 new_base = best_base(value);
246
247 if (!new_base) {
248 return -1;
249 }
250
251 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
252 return -1;
253
254 Py_INCREF(new_base);
255 Py_INCREF(value);
256
257 old_bases = type->tp_bases;
258 old_base = type->tp_base;
259 old_mro = type->tp_mro;
260
261 type->tp_bases = value;
262 type->tp_base = new_base;
263
264 if (mro_internal(type) < 0) {
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000265 goto bail;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000266 }
267
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000268 temp = PyList_New(0);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000269 if (!temp)
270 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000271
272 r = mro_subclasses(type, temp);
273
274 if (r < 0) {
275 for (i = 0; i < PyList_Size(temp); i++) {
276 PyTypeObject* cls;
277 PyObject* mro;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000278 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
279 "", 2, 2, &cls, &mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000280 Py_DECREF(cls->tp_mro);
281 cls->tp_mro = mro;
282 Py_INCREF(cls->tp_mro);
283 }
284 Py_DECREF(temp);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000285 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000286 }
287
288 Py_DECREF(temp);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000289
290 /* any base that was in __bases__ but now isn't, we
Raymond Hettingera8285862002-12-14 17:17:56 +0000291 need to remove |type| from its tp_subclasses.
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000292 conversely, any class now in __bases__ that wasn't
Raymond Hettingera8285862002-12-14 17:17:56 +0000293 needs to have |type| added to its subclasses. */
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000294
295 /* for now, sod that: just remove from all old_bases,
296 add to all new_bases */
297
298 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
299 ob = PyTuple_GET_ITEM(old_bases, i);
300 if (PyType_Check(ob)) {
301 remove_subclass(
302 (PyTypeObject*)ob, type);
303 }
304 }
305
306 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
307 ob = PyTuple_GET_ITEM(value, i);
308 if (PyType_Check(ob)) {
309 if (add_subclass((PyTypeObject*)ob, type) < 0)
310 r = -1;
311 }
312 }
313
314 update_all_slots(type);
315
316 Py_DECREF(old_bases);
317 Py_DECREF(old_base);
318 Py_DECREF(old_mro);
319
320 return r;
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000321
322 bail:
Michael W. Hudsone723e452003-08-07 14:58:10 +0000323 Py_DECREF(type->tp_bases);
324 Py_DECREF(type->tp_base);
325 if (type->tp_mro != old_mro) {
326 Py_DECREF(type->tp_mro);
327 }
328
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000329 type->tp_bases = old_bases;
330 type->tp_base = old_base;
331 type->tp_mro = old_mro;
Tim Petersea7f75d2002-12-07 21:39:16 +0000332
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000333 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000334}
335
336static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000337type_dict(PyTypeObject *type, void *context)
338{
339 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340 Py_INCREF(Py_None);
341 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000342 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000343 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000344}
345
Tim Peters24008312002-03-17 18:56:20 +0000346static PyObject *
347type_get_doc(PyTypeObject *type, void *context)
348{
349 PyObject *result;
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000350 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Tim Peters24008312002-03-17 18:56:20 +0000351 return PyString_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +0000352 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000353 if (result == NULL) {
354 result = Py_None;
355 Py_INCREF(result);
356 }
357 else if (result->ob_type->tp_descr_get) {
Tim Peters2b858972002-04-18 04:12:28 +0000358 result = result->ob_type->tp_descr_get(result, NULL,
359 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000360 }
361 else {
362 Py_INCREF(result);
363 }
Tim Peters24008312002-03-17 18:56:20 +0000364 return result;
365}
366
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000367static PyGetSetDef type_getsets[] = {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000368 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
369 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000370 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000371 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000372 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000373 {0}
374};
375
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000376static int
377type_compare(PyObject *v, PyObject *w)
378{
379 /* This is called with type objects only. So we
380 can just compare the addresses. */
381 Py_uintptr_t vv = (Py_uintptr_t)v;
382 Py_uintptr_t ww = (Py_uintptr_t)w;
383 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
384}
385
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000387type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000389 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000390 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000391
392 mod = type_module(type, NULL);
393 if (mod == NULL)
394 PyErr_Clear();
395 else if (!PyString_Check(mod)) {
396 Py_DECREF(mod);
397 mod = NULL;
398 }
399 name = type_name(type, NULL);
400 if (name == NULL)
401 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000402
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000403 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
404 kind = "class";
405 else
406 kind = "type";
407
Barry Warsaw7ce36942001-08-24 18:34:26 +0000408 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000409 rtn = PyString_FromFormat("<%s '%s.%s'>",
410 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000411 PyString_AS_STRING(mod),
412 PyString_AS_STRING(name));
413 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000414 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000415 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000416
Guido van Rossumc3542212001-08-16 09:18:56 +0000417 Py_XDECREF(mod);
418 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000419 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420}
421
Tim Peters6d6c1a32001-08-02 04:15:00 +0000422static PyObject *
423type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
424{
425 PyObject *obj;
426
427 if (type->tp_new == NULL) {
428 PyErr_Format(PyExc_TypeError,
429 "cannot create '%.100s' instances",
430 type->tp_name);
431 return NULL;
432 }
433
Tim Peters3f996e72001-09-13 19:18:27 +0000434 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000435 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000436 /* Ugly exception: when the call was type(something),
437 don't call tp_init on the result. */
438 if (type == &PyType_Type &&
439 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
440 (kwds == NULL ||
441 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
442 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000443 /* If the returned object is not an instance of type,
444 it won't be initialized. */
445 if (!PyType_IsSubtype(obj->ob_type, type))
446 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000447 type = obj->ob_type;
Jeremy Hylton719841e2002-07-16 19:39:38 +0000448 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
449 type->tp_init != NULL &&
Tim Peters6d6c1a32001-08-02 04:15:00 +0000450 type->tp_init(obj, args, kwds) < 0) {
451 Py_DECREF(obj);
452 obj = NULL;
453 }
454 }
455 return obj;
456}
457
458PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000459PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000460{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000461 PyObject *obj;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000462 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
463 /* note that we need to add one, for the sentinel */
Tim Peters406fe3b2001-10-06 19:04:01 +0000464
465 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000466 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000467 else
Anthony Baxtera6286212006-04-11 07:42:36 +0000468 obj = (PyObject *)PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000469
Neil Schemenauerc806c882001-08-29 23:54:54 +0000470 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000471 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000472
Neil Schemenauerc806c882001-08-29 23:54:54 +0000473 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000474
Tim Peters6d6c1a32001-08-02 04:15:00 +0000475 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
476 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000477
Tim Peters6d6c1a32001-08-02 04:15:00 +0000478 if (type->tp_itemsize == 0)
479 PyObject_INIT(obj, type);
480 else
481 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000482
Tim Peters6d6c1a32001-08-02 04:15:00 +0000483 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000484 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000485 return obj;
486}
487
488PyObject *
489PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
490{
491 return type->tp_alloc(type, 0);
492}
493
Guido van Rossum9475a232001-10-05 20:51:39 +0000494/* Helpers for subtyping */
495
496static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000497traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
498{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000499 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000500 PyMemberDef *mp;
501
502 n = type->ob_size;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000503 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000504 for (i = 0; i < n; i++, mp++) {
505 if (mp->type == T_OBJECT_EX) {
506 char *addr = (char *)self + mp->offset;
507 PyObject *obj = *(PyObject **)addr;
508 if (obj != NULL) {
509 int err = visit(obj, arg);
510 if (err)
511 return err;
512 }
513 }
514 }
515 return 0;
516}
517
518static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000519subtype_traverse(PyObject *self, visitproc visit, void *arg)
520{
521 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000522 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000523
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000524 /* Find the nearest base with a different tp_traverse,
525 and traverse slots while we're at it */
Guido van Rossum9475a232001-10-05 20:51:39 +0000526 type = self->ob_type;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000527 base = type;
528 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
529 if (base->ob_size) {
530 int err = traverse_slots(base, self, visit, arg);
531 if (err)
532 return err;
533 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000534 base = base->tp_base;
535 assert(base);
536 }
537
538 if (type->tp_dictoffset != base->tp_dictoffset) {
539 PyObject **dictptr = _PyObject_GetDictPtr(self);
Thomas Woutersc6e55062006-04-15 21:47:09 +0000540 if (dictptr && *dictptr)
541 Py_VISIT(*dictptr);
Guido van Rossum9475a232001-10-05 20:51:39 +0000542 }
543
Thomas Woutersc6e55062006-04-15 21:47:09 +0000544 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossuma3862092002-06-10 15:24:42 +0000545 /* For a heaptype, the instances count as references
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000546 to the type. Traverse the type so the collector
Guido van Rossuma3862092002-06-10 15:24:42 +0000547 can find cycles involving this link. */
Thomas Woutersc6e55062006-04-15 21:47:09 +0000548 Py_VISIT(type);
Guido van Rossuma3862092002-06-10 15:24:42 +0000549
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000550 if (basetraverse)
551 return basetraverse(self, visit, arg);
552 return 0;
553}
554
555static void
556clear_slots(PyTypeObject *type, PyObject *self)
557{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000558 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000559 PyMemberDef *mp;
560
561 n = type->ob_size;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000562 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000563 for (i = 0; i < n; i++, mp++) {
564 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
565 char *addr = (char *)self + mp->offset;
566 PyObject *obj = *(PyObject **)addr;
567 if (obj != NULL) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000568 *(PyObject **)addr = NULL;
Thomas Woutersedf17d82006-04-15 17:28:34 +0000569 Py_DECREF(obj);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000570 }
571 }
572 }
573}
574
575static int
576subtype_clear(PyObject *self)
577{
578 PyTypeObject *type, *base;
579 inquiry baseclear;
580
581 /* Find the nearest base with a different tp_clear
582 and clear slots while we're at it */
583 type = self->ob_type;
584 base = type;
585 while ((baseclear = base->tp_clear) == subtype_clear) {
586 if (base->ob_size)
587 clear_slots(base, self);
588 base = base->tp_base;
589 assert(base);
590 }
591
Guido van Rossuma3862092002-06-10 15:24:42 +0000592 /* There's no need to clear the instance dict (if any);
593 the collector will call its tp_clear handler. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000594
595 if (baseclear)
596 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000597 return 0;
598}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000599
600static void
601subtype_dealloc(PyObject *self)
602{
Guido van Rossum14227b42001-12-06 02:35:58 +0000603 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000604 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000605
Guido van Rossum22b13872002-08-06 21:41:44 +0000606 /* Extract the type; we expect it to be a heap type */
607 type = self->ob_type;
608 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000609
Guido van Rossum22b13872002-08-06 21:41:44 +0000610 /* Test whether the type has GC exactly once */
611
612 if (!PyType_IS_GC(type)) {
613 /* It's really rare to find a dynamic type that doesn't have
614 GC; it can only happen when deriving from 'object' and not
615 adding any slots or instance variables. This allows
616 certain simplifications: there's no need to call
617 clear_slots(), or DECREF the dict, or clear weakrefs. */
618
619 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000620 if (type->tp_del) {
621 type->tp_del(self);
622 if (self->ob_refcnt > 0)
623 return;
624 }
Guido van Rossum22b13872002-08-06 21:41:44 +0000625
626 /* Find the nearest base with a different tp_dealloc */
627 base = type;
628 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
629 assert(base->ob_size == 0);
630 base = base->tp_base;
631 assert(base);
632 }
633
634 /* Call the base tp_dealloc() */
635 assert(basedealloc);
636 basedealloc(self);
637
638 /* Can't reference self beyond this point */
639 Py_DECREF(type);
640
641 /* Done */
642 return;
643 }
644
645 /* We get here only if the type has GC */
646
647 /* UnTrack and re-Track around the trashcan macro, alas */
Andrew M. Kuchlingc9172d32003-02-06 15:22:49 +0000648 /* See explanation at end of function for full disclosure */
Guido van Rossum0906e072002-08-07 20:42:09 +0000649 PyObject_GC_UnTrack(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000650 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000651 Py_TRASHCAN_SAFE_BEGIN(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000652 --_PyTrash_delete_nesting;
Tim Petersf7f9e992003-11-13 21:59:32 +0000653 /* DO NOT restore GC tracking at this point. weakref callbacks
654 * (if any, and whether directly here or indirectly in something we
655 * call) may trigger GC, and if self is tracked at that point, it
656 * will look like trash to GC and GC will try to delete self again.
Tim Petersadd09b42003-11-12 20:43:28 +0000657 */
Guido van Rossum22b13872002-08-06 21:41:44 +0000658
Guido van Rossum59195fd2003-06-13 20:54:40 +0000659 /* Find the nearest base with a different tp_dealloc */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000660 base = type;
661 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662 base = base->tp_base;
663 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000664 }
665
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000666 /* If we added a weaklist, we clear it. Do this *before* calling
Guido van Rossum59195fd2003-06-13 20:54:40 +0000667 the finalizer (__del__), clearing slots, or clearing the instance
668 dict. */
669
Guido van Rossum1987c662003-05-29 14:29:23 +0000670 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
671 PyObject_ClearWeakRefs(self);
672
673 /* Maybe call finalizer; exit early if resurrected */
674 if (type->tp_del) {
Tim Petersf7f9e992003-11-13 21:59:32 +0000675 _PyObject_GC_TRACK(self);
Guido van Rossum1987c662003-05-29 14:29:23 +0000676 type->tp_del(self);
677 if (self->ob_refcnt > 0)
Tim Petersf7f9e992003-11-13 21:59:32 +0000678 goto endlabel; /* resurrected */
679 else
680 _PyObject_GC_UNTRACK(self);
Brett Cannonf5bee302007-01-23 23:21:22 +0000681 /* New weakrefs could be created during the finalizer call.
682 If this occurs, clear them out without calling their
683 finalizers since they might rely on part of the object
684 being finalized that has already been destroyed. */
685 if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
686 /* Modeled after GET_WEAKREFS_LISTPTR() */
687 PyWeakReference **list = (PyWeakReference **) \
688 PyObject_GET_WEAKREFS_LISTPTR(self);
689 while (*list)
690 _PyWeakref_ClearRef(*list);
691 }
Guido van Rossum1987c662003-05-29 14:29:23 +0000692 }
693
Guido van Rossum59195fd2003-06-13 20:54:40 +0000694 /* Clear slots up to the nearest base with a different tp_dealloc */
695 base = type;
696 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
697 if (base->ob_size)
698 clear_slots(base, self);
699 base = base->tp_base;
700 assert(base);
701 }
702
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000704 if (type->tp_dictoffset && !base->tp_dictoffset) {
705 PyObject **dictptr = _PyObject_GetDictPtr(self);
706 if (dictptr != NULL) {
707 PyObject *dict = *dictptr;
708 if (dict != NULL) {
709 Py_DECREF(dict);
710 *dictptr = NULL;
711 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712 }
713 }
714
Tim Peters0bd743c2003-11-13 22:50:00 +0000715 /* Call the base tp_dealloc(); first retrack self if
716 * basedealloc knows about gc.
717 */
718 if (PyType_IS_GC(base))
719 _PyObject_GC_TRACK(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000720 assert(basedealloc);
721 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722
723 /* Can't reference self beyond this point */
Guido van Rossum22b13872002-08-06 21:41:44 +0000724 Py_DECREF(type);
725
Guido van Rossum0906e072002-08-07 20:42:09 +0000726 endlabel:
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000727 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000728 Py_TRASHCAN_SAFE_END(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000729 --_PyTrash_delete_nesting;
730
731 /* Explanation of the weirdness around the trashcan macros:
732
733 Q. What do the trashcan macros do?
734
735 A. Read the comment titled "Trashcan mechanism" in object.h.
736 For one, this explains why there must be a call to GC-untrack
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000737 before the trashcan begin macro. Without understanding the
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000738 trashcan code, the answers to the following questions don't make
739 sense.
740
741 Q. Why do we GC-untrack before the trashcan and then immediately
742 GC-track again afterward?
743
744 A. In the case that the base class is GC-aware, the base class
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000745 probably GC-untracks the object. If it does that using the
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000746 UNTRACK macro, this will crash when the object is already
747 untracked. Because we don't know what the base class does, the
748 only safe thing is to make sure the object is tracked when we
749 call the base class dealloc. But... The trashcan begin macro
750 requires that the object is *untracked* before it is called. So
751 the dance becomes:
752
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000753 GC untrack
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000754 trashcan begin
755 GC track
756
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000757 Q. Why did the last question say "immediately GC-track again"?
758 It's nowhere near immediately.
Tim Petersf7f9e992003-11-13 21:59:32 +0000759
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000760 A. Because the code *used* to re-track immediately. Bad Idea.
761 self has a refcount of 0, and if gc ever gets its hands on it
762 (which can happen if any weakref callback gets invoked), it
763 looks like trash to gc too, and gc also tries to delete self
764 then. But we're already deleting self. Double dealloction is
765 a subtle disaster.
Tim Petersf7f9e992003-11-13 21:59:32 +0000766
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000767 Q. Why the bizarre (net-zero) manipulation of
768 _PyTrash_delete_nesting around the trashcan macros?
769
770 A. Some base classes (e.g. list) also use the trashcan mechanism.
771 The following scenario used to be possible:
772
773 - suppose the trashcan level is one below the trashcan limit
774
775 - subtype_dealloc() is called
776
777 - the trashcan limit is not yet reached, so the trashcan level
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000778 is incremented and the code between trashcan begin and end is
779 executed
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000780
781 - this destroys much of the object's contents, including its
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000782 slots and __dict__
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000783
784 - basedealloc() is called; this is really list_dealloc(), or
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000785 some other type which also uses the trashcan macros
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000786
787 - the trashcan limit is now reached, so the object is put on the
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000788 trashcan's to-be-deleted-later list
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000789
790 - basedealloc() returns
791
792 - subtype_dealloc() decrefs the object's type
793
794 - subtype_dealloc() returns
795
796 - later, the trashcan code starts deleting the objects from its
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000797 to-be-deleted-later list
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000798
799 - subtype_dealloc() is called *AGAIN* for the same object
800
801 - at the very least (if the destroyed slots and __dict__ don't
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000802 cause problems) the object's type gets decref'ed a second
803 time, which is *BAD*!!!
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000804
805 The remedy is to make sure that if the code between trashcan
806 begin and end in subtype_dealloc() is called, the code between
807 trashcan begin and end in basedealloc() will also be called.
808 This is done by decrementing the level after passing into the
809 trashcan block, and incrementing it just before leaving the
810 block.
811
812 But now it's possible that a chain of objects consisting solely
813 of objects whose deallocator is subtype_dealloc() will defeat
814 the trashcan mechanism completely: the decremented level means
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000815 that the effective level never reaches the limit. Therefore, we
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000816 *increment* the level *before* entering the trashcan block, and
817 matchingly decrement it after leaving. This means the trashcan
818 code will trigger a little early, but that's no big deal.
819
820 Q. Are there any live examples of code in need of all this
821 complexity?
822
823 A. Yes. See SF bug 668433 for code that crashed (when Python was
824 compiled in debug mode) before the trashcan level manipulations
825 were added. For more discussion, see SF patches 581742, 575073
826 and bug 574207.
827 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000828}
829
Jeremy Hylton938ace62002-07-17 16:30:39 +0000830static PyTypeObject *solid_base(PyTypeObject *type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000831
Tim Peters6d6c1a32001-08-02 04:15:00 +0000832/* type test with subclassing support */
833
834int
835PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
836{
837 PyObject *mro;
838
Guido van Rossum9478d072001-09-07 18:52:13 +0000839 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
840 return b == a || b == &PyBaseObject_Type;
841
Tim Peters6d6c1a32001-08-02 04:15:00 +0000842 mro = a->tp_mro;
843 if (mro != NULL) {
844 /* Deal with multiple inheritance without recursion
845 by walking the MRO tuple */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000846 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000847 assert(PyTuple_Check(mro));
848 n = PyTuple_GET_SIZE(mro);
849 for (i = 0; i < n; i++) {
850 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
851 return 1;
852 }
853 return 0;
854 }
855 else {
856 /* a is not completely initilized yet; follow tp_base */
857 do {
858 if (a == b)
859 return 1;
860 a = a->tp_base;
861 } while (a != NULL);
862 return b == &PyBaseObject_Type;
863 }
864}
865
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000866/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000867 without looking in the instance dictionary
868 (so we can't use PyObject_GetAttr) but still binding
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000869 it to the instance. The arguments are the object,
Guido van Rossum60718732001-08-28 17:47:51 +0000870 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000871 static variable used to cache the interned Python string.
872
873 Two variants:
874
875 - lookup_maybe() returns NULL without raising an exception
876 when the _PyType_Lookup() call fails;
877
878 - lookup_method() always raises an exception upon errors.
879*/
Guido van Rossum60718732001-08-28 17:47:51 +0000880
881static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000882lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000883{
884 PyObject *res;
885
886 if (*attrobj == NULL) {
887 *attrobj = PyString_InternFromString(attrstr);
888 if (*attrobj == NULL)
889 return NULL;
890 }
891 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000892 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000893 descrgetfunc f;
894 if ((f = res->ob_type->tp_descr_get) == NULL)
895 Py_INCREF(res);
896 else
897 res = f(res, self, (PyObject *)(self->ob_type));
898 }
899 return res;
900}
901
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000902static PyObject *
903lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
904{
905 PyObject *res = lookup_maybe(self, attrstr, attrobj);
906 if (res == NULL && !PyErr_Occurred())
907 PyErr_SetObject(PyExc_AttributeError, *attrobj);
908 return res;
909}
910
Guido van Rossum2730b132001-08-28 18:22:14 +0000911/* A variation of PyObject_CallMethod that uses lookup_method()
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +0000912 instead of PyObject_GetAttrString(). This uses the same convention
Guido van Rossum2730b132001-08-28 18:22:14 +0000913 as lookup_method to cache the interned name string object. */
914
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000915static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000916call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
917{
918 va_list va;
919 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000920 va_start(va, format);
921
Guido van Rossumda21c012001-10-03 00:50:18 +0000922 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000923 if (func == NULL) {
924 va_end(va);
925 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000926 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000927 return NULL;
928 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000929
930 if (format && *format)
931 args = Py_VaBuildValue(format, va);
932 else
933 args = PyTuple_New(0);
934
935 va_end(va);
936
937 if (args == NULL)
938 return NULL;
939
940 assert(PyTuple_Check(args));
941 retval = PyObject_Call(func, args, NULL);
942
943 Py_DECREF(args);
944 Py_DECREF(func);
945
946 return retval;
947}
948
949/* Clone of call_method() that returns NotImplemented when the lookup fails. */
950
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000951static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000952call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
953{
954 va_list va;
955 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000956 va_start(va, format);
957
Guido van Rossumda21c012001-10-03 00:50:18 +0000958 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000959 if (func == NULL) {
960 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000961 if (!PyErr_Occurred()) {
962 Py_INCREF(Py_NotImplemented);
963 return Py_NotImplemented;
964 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000965 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000966 }
967
968 if (format && *format)
969 args = Py_VaBuildValue(format, va);
970 else
971 args = PyTuple_New(0);
972
973 va_end(va);
974
Guido van Rossum717ce002001-09-14 16:58:08 +0000975 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000976 return NULL;
977
Guido van Rossum717ce002001-09-14 16:58:08 +0000978 assert(PyTuple_Check(args));
979 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000980
981 Py_DECREF(args);
982 Py_DECREF(func);
983
984 return retval;
985}
986
Tim Petersa91e9642001-11-14 23:32:33 +0000987static int
988fill_classic_mro(PyObject *mro, PyObject *cls)
989{
990 PyObject *bases, *base;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000991 Py_ssize_t i, n;
Tim Petersa91e9642001-11-14 23:32:33 +0000992
993 assert(PyList_Check(mro));
994 assert(PyClass_Check(cls));
995 i = PySequence_Contains(mro, cls);
996 if (i < 0)
997 return -1;
998 if (!i) {
999 if (PyList_Append(mro, cls) < 0)
1000 return -1;
1001 }
1002 bases = ((PyClassObject *)cls)->cl_bases;
1003 assert(bases && PyTuple_Check(bases));
1004 n = PyTuple_GET_SIZE(bases);
1005 for (i = 0; i < n; i++) {
1006 base = PyTuple_GET_ITEM(bases, i);
1007 if (fill_classic_mro(mro, base) < 0)
1008 return -1;
1009 }
1010 return 0;
1011}
1012
1013static PyObject *
1014classic_mro(PyObject *cls)
1015{
1016 PyObject *mro;
1017
1018 assert(PyClass_Check(cls));
1019 mro = PyList_New(0);
1020 if (mro != NULL) {
1021 if (fill_classic_mro(mro, cls) == 0)
1022 return mro;
1023 Py_DECREF(mro);
1024 }
1025 return NULL;
1026}
1027
Tim Petersea7f75d2002-12-07 21:39:16 +00001028/*
Guido van Rossum1f121312002-11-14 19:49:16 +00001029 Method resolution order algorithm C3 described in
1030 "A Monotonic Superclass Linearization for Dylan",
1031 by Kim Barrett, Bob Cassel, Paul Haahr,
Tim Petersea7f75d2002-12-07 21:39:16 +00001032 David A. Moon, Keith Playford, and P. Tucker Withington.
Guido van Rossum1f121312002-11-14 19:49:16 +00001033 (OOPSLA 1996)
1034
Guido van Rossum98f33732002-11-25 21:36:54 +00001035 Some notes about the rules implied by C3:
1036
Tim Petersea7f75d2002-12-07 21:39:16 +00001037 No duplicate bases.
Guido van Rossum98f33732002-11-25 21:36:54 +00001038 It isn't legal to repeat a class in a list of base classes.
1039
1040 The next three properties are the 3 constraints in "C3".
1041
Tim Petersea7f75d2002-12-07 21:39:16 +00001042 Local precendece order.
Guido van Rossum98f33732002-11-25 21:36:54 +00001043 If A precedes B in C's MRO, then A will precede B in the MRO of all
1044 subclasses of C.
1045
1046 Monotonicity.
1047 The MRO of a class must be an extension without reordering of the
1048 MRO of each of its superclasses.
1049
1050 Extended Precedence Graph (EPG).
1051 Linearization is consistent if there is a path in the EPG from
1052 each class to all its successors in the linearization. See
1053 the paper for definition of EPG.
Guido van Rossum1f121312002-11-14 19:49:16 +00001054 */
1055
Tim Petersea7f75d2002-12-07 21:39:16 +00001056static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001057tail_contains(PyObject *list, int whence, PyObject *o) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001058 Py_ssize_t j, size;
Guido van Rossum1f121312002-11-14 19:49:16 +00001059 size = PyList_GET_SIZE(list);
1060
1061 for (j = whence+1; j < size; j++) {
1062 if (PyList_GET_ITEM(list, j) == o)
1063 return 1;
1064 }
1065 return 0;
1066}
1067
Guido van Rossum98f33732002-11-25 21:36:54 +00001068static PyObject *
1069class_name(PyObject *cls)
1070{
1071 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1072 if (name == NULL) {
1073 PyErr_Clear();
1074 Py_XDECREF(name);
1075 name = PyObject_Repr(cls);
1076 }
1077 if (name == NULL)
1078 return NULL;
1079 if (!PyString_Check(name)) {
1080 Py_DECREF(name);
1081 return NULL;
1082 }
1083 return name;
1084}
1085
1086static int
1087check_duplicates(PyObject *list)
1088{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001089 Py_ssize_t i, j, n;
Guido van Rossum98f33732002-11-25 21:36:54 +00001090 /* Let's use a quadratic time algorithm,
1091 assuming that the bases lists is short.
1092 */
1093 n = PyList_GET_SIZE(list);
1094 for (i = 0; i < n; i++) {
1095 PyObject *o = PyList_GET_ITEM(list, i);
1096 for (j = i + 1; j < n; j++) {
1097 if (PyList_GET_ITEM(list, j) == o) {
1098 o = class_name(o);
1099 PyErr_Format(PyExc_TypeError,
1100 "duplicate base class %s",
1101 o ? PyString_AS_STRING(o) : "?");
1102 Py_XDECREF(o);
1103 return -1;
1104 }
1105 }
1106 }
1107 return 0;
1108}
1109
1110/* Raise a TypeError for an MRO order disagreement.
1111
1112 It's hard to produce a good error message. In the absence of better
1113 insight into error reporting, report the classes that were candidates
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00001114 to be put next into the MRO. There is some conflict between the
Guido van Rossum98f33732002-11-25 21:36:54 +00001115 order in which they should be put in the MRO, but it's hard to
1116 diagnose what constraint can't be satisfied.
1117*/
1118
1119static void
1120set_mro_error(PyObject *to_merge, int *remain)
1121{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001122 Py_ssize_t i, n, off, to_merge_size;
Guido van Rossum98f33732002-11-25 21:36:54 +00001123 char buf[1000];
1124 PyObject *k, *v;
1125 PyObject *set = PyDict_New();
Georg Brandl5c170fd2006-03-17 19:03:25 +00001126 if (!set) return;
Guido van Rossum98f33732002-11-25 21:36:54 +00001127
1128 to_merge_size = PyList_GET_SIZE(to_merge);
1129 for (i = 0; i < to_merge_size; i++) {
1130 PyObject *L = PyList_GET_ITEM(to_merge, i);
1131 if (remain[i] < PyList_GET_SIZE(L)) {
1132 PyObject *c = PyList_GET_ITEM(L, remain[i]);
Georg Brandl5c170fd2006-03-17 19:03:25 +00001133 if (PyDict_SetItem(set, c, Py_None) < 0) {
1134 Py_DECREF(set);
Guido van Rossum98f33732002-11-25 21:36:54 +00001135 return;
Georg Brandl5c170fd2006-03-17 19:03:25 +00001136 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001137 }
1138 }
1139 n = PyDict_Size(set);
1140
Raymond Hettingerf394df42003-04-06 19:13:41 +00001141 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1142consistent method resolution\norder (MRO) for bases");
Guido van Rossum98f33732002-11-25 21:36:54 +00001143 i = 0;
Skip Montanaro429433b2006-04-18 00:35:43 +00001144 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001145 PyObject *name = class_name(k);
1146 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1147 name ? PyString_AS_STRING(name) : "?");
1148 Py_XDECREF(name);
Skip Montanaro429433b2006-04-18 00:35:43 +00001149 if (--n && (size_t)(off+1) < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001150 buf[off++] = ',';
1151 buf[off] = '\0';
1152 }
1153 }
1154 PyErr_SetString(PyExc_TypeError, buf);
1155 Py_DECREF(set);
1156}
1157
Tim Petersea7f75d2002-12-07 21:39:16 +00001158static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001159pmerge(PyObject *acc, PyObject* to_merge) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001160 Py_ssize_t i, j, to_merge_size, empty_cnt;
Guido van Rossum1f121312002-11-14 19:49:16 +00001161 int *remain;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001162 int ok;
Tim Petersea7f75d2002-12-07 21:39:16 +00001163
Guido van Rossum1f121312002-11-14 19:49:16 +00001164 to_merge_size = PyList_GET_SIZE(to_merge);
1165
Guido van Rossum98f33732002-11-25 21:36:54 +00001166 /* remain stores an index into each sublist of to_merge.
1167 remain[i] is the index of the next base in to_merge[i]
1168 that is not included in acc.
1169 */
Anthony Baxtera6286212006-04-11 07:42:36 +00001170 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
Guido van Rossum1f121312002-11-14 19:49:16 +00001171 if (remain == NULL)
1172 return -1;
1173 for (i = 0; i < to_merge_size; i++)
1174 remain[i] = 0;
1175
1176 again:
1177 empty_cnt = 0;
1178 for (i = 0; i < to_merge_size; i++) {
1179 PyObject *candidate;
Tim Petersea7f75d2002-12-07 21:39:16 +00001180
Guido van Rossum1f121312002-11-14 19:49:16 +00001181 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1182
1183 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1184 empty_cnt++;
1185 continue;
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00001186 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001187
Guido van Rossum98f33732002-11-25 21:36:54 +00001188 /* Choose next candidate for MRO.
1189
1190 The input sequences alone can determine the choice.
1191 If not, choose the class which appears in the MRO
1192 of the earliest direct superclass of the new class.
1193 */
1194
Guido van Rossum1f121312002-11-14 19:49:16 +00001195 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1196 for (j = 0; j < to_merge_size; j++) {
1197 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum98f33732002-11-25 21:36:54 +00001198 if (tail_contains(j_lst, remain[j], candidate)) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001199 goto skip; /* continue outer loop */
Guido van Rossum98f33732002-11-25 21:36:54 +00001200 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001201 }
1202 ok = PyList_Append(acc, candidate);
1203 if (ok < 0) {
1204 PyMem_Free(remain);
1205 return -1;
1206 }
1207 for (j = 0; j < to_merge_size; j++) {
1208 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum768158c2002-12-31 16:33:01 +00001209 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1210 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001211 remain[j]++;
1212 }
1213 }
1214 goto again;
Tim Peters9a6b8d82002-11-14 23:22:33 +00001215 skip: ;
Guido van Rossum1f121312002-11-14 19:49:16 +00001216 }
1217
Guido van Rossum98f33732002-11-25 21:36:54 +00001218 if (empty_cnt == to_merge_size) {
1219 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001220 return 0;
Guido van Rossum98f33732002-11-25 21:36:54 +00001221 }
1222 set_mro_error(to_merge, remain);
1223 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001224 return -1;
1225}
1226
Tim Peters6d6c1a32001-08-02 04:15:00 +00001227static PyObject *
1228mro_implementation(PyTypeObject *type)
1229{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001230 Py_ssize_t i, n;
1231 int ok;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001232 PyObject *bases, *result;
Guido van Rossum1f121312002-11-14 19:49:16 +00001233 PyObject *to_merge, *bases_aslist;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001234
Guido van Rossum63517572002-06-18 16:44:57 +00001235 if(type->tp_dict == NULL) {
1236 if(PyType_Ready(type) < 0)
1237 return NULL;
1238 }
1239
Guido van Rossum98f33732002-11-25 21:36:54 +00001240 /* Find a superclass linearization that honors the constraints
1241 of the explicit lists of bases and the constraints implied by
Tim Petersea7f75d2002-12-07 21:39:16 +00001242 each base class.
Guido van Rossum98f33732002-11-25 21:36:54 +00001243
1244 to_merge is a list of lists, where each list is a superclass
1245 linearization implied by a base class. The last element of
1246 to_merge is the declared list of bases.
1247 */
1248
Tim Peters6d6c1a32001-08-02 04:15:00 +00001249 bases = type->tp_bases;
1250 n = PyTuple_GET_SIZE(bases);
Guido van Rossum1f121312002-11-14 19:49:16 +00001251
1252 to_merge = PyList_New(n+1);
1253 if (to_merge == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001254 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001255
Tim Peters6d6c1a32001-08-02 04:15:00 +00001256 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001257 PyObject *base = PyTuple_GET_ITEM(bases, i);
1258 PyObject *parentMRO;
1259 if (PyType_Check(base))
1260 parentMRO = PySequence_List(
1261 ((PyTypeObject*)base)->tp_mro);
1262 else
1263 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001264 if (parentMRO == NULL) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001265 Py_DECREF(to_merge);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001266 return NULL;
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00001267 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001268
1269 PyList_SET_ITEM(to_merge, i, parentMRO);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001270 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001271
1272 bases_aslist = PySequence_List(bases);
1273 if (bases_aslist == NULL) {
1274 Py_DECREF(to_merge);
1275 return NULL;
1276 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001277 /* This is just a basic sanity check. */
1278 if (check_duplicates(bases_aslist) < 0) {
1279 Py_DECREF(to_merge);
1280 Py_DECREF(bases_aslist);
1281 return NULL;
1282 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001283 PyList_SET_ITEM(to_merge, n, bases_aslist);
1284
1285 result = Py_BuildValue("[O]", (PyObject *)type);
1286 if (result == NULL) {
1287 Py_DECREF(to_merge);
1288 return NULL;
1289 }
1290
1291 ok = pmerge(result, to_merge);
1292 Py_DECREF(to_merge);
1293 if (ok < 0) {
1294 Py_DECREF(result);
1295 return NULL;
1296 }
1297
Tim Peters6d6c1a32001-08-02 04:15:00 +00001298 return result;
1299}
1300
1301static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001302mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001303{
1304 PyTypeObject *type = (PyTypeObject *)self;
1305
Tim Peters6d6c1a32001-08-02 04:15:00 +00001306 return mro_implementation(type);
1307}
1308
1309static int
1310mro_internal(PyTypeObject *type)
1311{
1312 PyObject *mro, *result, *tuple;
Armin Rigo037d1e02005-12-29 17:07:39 +00001313 int checkit = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001314
1315 if (type->ob_type == &PyType_Type) {
1316 result = mro_implementation(type);
1317 }
1318 else {
Guido van Rossum60718732001-08-28 17:47:51 +00001319 static PyObject *mro_str;
Armin Rigo037d1e02005-12-29 17:07:39 +00001320 checkit = 1;
Guido van Rossum60718732001-08-28 17:47:51 +00001321 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001322 if (mro == NULL)
1323 return -1;
1324 result = PyObject_CallObject(mro, NULL);
1325 Py_DECREF(mro);
1326 }
1327 if (result == NULL)
1328 return -1;
1329 tuple = PySequence_Tuple(result);
1330 Py_DECREF(result);
Armin Rigo037d1e02005-12-29 17:07:39 +00001331 if (tuple == NULL)
1332 return -1;
1333 if (checkit) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001334 Py_ssize_t i, len;
Armin Rigo037d1e02005-12-29 17:07:39 +00001335 PyObject *cls;
1336 PyTypeObject *solid;
1337
1338 solid = solid_base(type);
1339
1340 len = PyTuple_GET_SIZE(tuple);
1341
1342 for (i = 0; i < len; i++) {
1343 PyTypeObject *t;
1344 cls = PyTuple_GET_ITEM(tuple, i);
1345 if (PyClass_Check(cls))
1346 continue;
1347 else if (!PyType_Check(cls)) {
1348 PyErr_Format(PyExc_TypeError,
1349 "mro() returned a non-class ('%.500s')",
1350 cls->ob_type->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001351 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001352 return -1;
1353 }
1354 t = (PyTypeObject*)cls;
1355 if (!PyType_IsSubtype(solid, solid_base(t))) {
1356 PyErr_Format(PyExc_TypeError,
1357 "mro() returned base with unsuitable layout ('%.500s')",
1358 t->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001359 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001360 return -1;
1361 }
1362 }
1363 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001364 type->tp_mro = tuple;
1365 return 0;
1366}
1367
1368
1369/* Calculate the best base amongst multiple base classes.
Jeremy Hyltonfa955692007-02-27 18:29:45 +00001370 This is the first one that's on the path to the "solid base".
1371
1372 Requires that all base classes be types or classic classes.
1373
1374 Will return NULL with TypeError set if
1375 1) the base classes have conflicting layout instances, or
1376 2) all the bases are classic classes.
1377*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00001378
1379static PyTypeObject *
1380best_base(PyObject *bases)
1381{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001382 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001383 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +00001384 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001385
1386 assert(PyTuple_Check(bases));
1387 n = PyTuple_GET_SIZE(bases);
1388 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +00001389 base = NULL;
1390 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001391 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001392 base_proto = PyTuple_GET_ITEM(bases, i);
1393 if (PyClass_Check(base_proto))
1394 continue;
Jeremy Hyltonfa955692007-02-27 18:29:45 +00001395 assert(PyType_Check(base_proto));
Tim Petersa91e9642001-11-14 23:32:33 +00001396 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001397 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001398 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001399 return NULL;
1400 }
1401 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +00001402 if (winner == NULL) {
1403 winner = candidate;
1404 base = base_i;
1405 }
1406 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001407 ;
1408 else if (PyType_IsSubtype(candidate, winner)) {
1409 winner = candidate;
1410 base = base_i;
1411 }
1412 else {
1413 PyErr_SetString(
1414 PyExc_TypeError,
1415 "multiple bases have "
1416 "instance lay-out conflict");
1417 return NULL;
1418 }
1419 }
Guido van Rossume54616c2001-12-14 04:19:56 +00001420 if (base == NULL)
1421 PyErr_SetString(PyExc_TypeError,
1422 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001423 return base;
1424}
1425
1426static int
1427extra_ivars(PyTypeObject *type, PyTypeObject *base)
1428{
Neil Schemenauerc806c882001-08-29 23:54:54 +00001429 size_t t_size = type->tp_basicsize;
1430 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001431
Guido van Rossum9676b222001-08-17 20:32:36 +00001432 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001433 if (type->tp_itemsize || base->tp_itemsize) {
1434 /* If itemsize is involved, stricter rules */
1435 return t_size != b_size ||
1436 type->tp_itemsize != base->tp_itemsize;
1437 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001438 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1439 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1440 t_size -= sizeof(PyObject *);
1441 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1442 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1443 t_size -= sizeof(PyObject *);
1444
1445 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001446}
1447
Jeremy Hyltonfa955692007-02-27 18:29:45 +00001448/* Return the type object that will determine the layout of the instance. */
1449
Tim Peters6d6c1a32001-08-02 04:15:00 +00001450static PyTypeObject *
1451solid_base(PyTypeObject *type)
1452{
1453 PyTypeObject *base;
1454
1455 if (type->tp_base)
1456 base = solid_base(type->tp_base);
1457 else
1458 base = &PyBaseObject_Type;
1459 if (extra_ivars(type, base))
1460 return type;
1461 else
1462 return base;
1463}
1464
Jeremy Hyltonfa955692007-02-27 18:29:45 +00001465/* Determine the proper metatype to deal with this, and check some
1466 error cases while we're at it. Note that if some other metatype
1467 wins to contract, it's possible that its instances are not types.
1468
1469 Error cases of interest: 1. The metaclass is not a subclass of a
1470 base class. 2. A non-type, non-classic base class appears before
1471 type.
1472*/
1473
1474static PyTypeObject *
1475most_derived_metaclass(PyTypeObject *metatype, PyObject *bases)
1476{
1477 Py_ssize_t nbases, i;
1478 PyTypeObject *winner;
1479 /* types_ordered: One of three states possible:
1480 0 type is in bases
1481 1 non-types also in bases
1482 2 type follows non-type in bases (error)
1483 */
1484 int types_ordered = 0;
1485
1486 nbases = PyTuple_GET_SIZE(bases);
1487 winner = metatype;
1488 for (i = 0; i < nbases; i++) {
1489 PyObject *tmp = PyTuple_GET_ITEM(bases, i);
1490 PyTypeObject *tmptype = tmp->ob_type;
1491 if (tmptype == &PyClass_Type)
1492 continue; /* Special case classic classes */
1493 if (!PyType_Check(tmp)) {
1494 PyErr_SetString(PyExc_TypeError,
1495 "bases must be types");
1496 return NULL;
1497 }
1498 if (PyObject_IsSubclass(tmp, (PyObject*)&PyType_Type)) {
1499 if (types_ordered == 1) {
1500 types_ordered = 2;
1501 }
1502 }
1503 else if (!types_ordered)
1504 types_ordered = 1;
1505 if (winner == tmptype)
1506 continue;
1507 if (PyType_IsSubtype(winner, tmptype))
1508 continue;
1509 if (PyType_IsSubtype(tmptype, winner)) {
1510 winner = tmptype;
1511 continue;
1512 }
1513 PyErr_SetString(PyExc_TypeError,
1514 "metaclass conflict: "
1515 "the metaclass of a derived class "
1516 "must be a (non-strict) subclass "
1517 "of the metaclasses of all its bases");
1518 return NULL;
1519 }
1520 if (types_ordered == 2) {
1521 PyErr_SetString(PyExc_TypeError,
1522 "metaclass conflict: "
1523 "type must occur in bases before other "
1524 "non-classic base classes");
1525 return NULL;
1526 }
1527 return winner;
1528}
1529
Jeremy Hylton938ace62002-07-17 16:30:39 +00001530static void object_dealloc(PyObject *);
1531static int object_init(PyObject *, PyObject *, PyObject *);
1532static int update_slot(PyTypeObject *, PyObject *);
1533static void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001534
1535static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001536subtype_dict(PyObject *obj, void *context)
1537{
1538 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1539 PyObject *dict;
1540
1541 if (dictptr == NULL) {
1542 PyErr_SetString(PyExc_AttributeError,
1543 "This object has no __dict__");
1544 return NULL;
1545 }
1546 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +00001547 if (dict == NULL)
1548 *dictptr = dict = PyDict_New();
1549 Py_XINCREF(dict);
1550 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001551}
1552
Guido van Rossum6661be32001-10-26 04:26:12 +00001553static int
1554subtype_setdict(PyObject *obj, PyObject *value, void *context)
1555{
1556 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1557 PyObject *dict;
1558
1559 if (dictptr == NULL) {
1560 PyErr_SetString(PyExc_AttributeError,
1561 "This object has no __dict__");
1562 return -1;
1563 }
Guido van Rossumd331cb52001-12-05 19:46:42 +00001564 if (value != NULL && !PyDict_Check(value)) {
Georg Brandlccff7852006-06-18 22:17:29 +00001565 PyErr_Format(PyExc_TypeError,
1566 "__dict__ must be set to a dictionary, "
1567 "not a '%.200s'", value->ob_type->tp_name);
Guido van Rossum6661be32001-10-26 04:26:12 +00001568 return -1;
1569 }
1570 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +00001571 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +00001572 *dictptr = value;
1573 Py_XDECREF(dict);
1574 return 0;
1575}
1576
Guido van Rossumad47da02002-08-12 19:05:44 +00001577static PyObject *
1578subtype_getweakref(PyObject *obj, void *context)
1579{
1580 PyObject **weaklistptr;
1581 PyObject *result;
1582
1583 if (obj->ob_type->tp_weaklistoffset == 0) {
1584 PyErr_SetString(PyExc_AttributeError,
Fred Drake7a36f5f2006-08-04 05:17:21 +00001585 "This object has no __weakref__");
Guido van Rossumad47da02002-08-12 19:05:44 +00001586 return NULL;
1587 }
1588 assert(obj->ob_type->tp_weaklistoffset > 0);
1589 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
Guido van Rossum3747a0f2002-08-12 19:25:08 +00001590 (size_t)(obj->ob_type->tp_basicsize));
Guido van Rossumad47da02002-08-12 19:05:44 +00001591 weaklistptr = (PyObject **)
Guido van Rossum3747a0f2002-08-12 19:25:08 +00001592 ((char *)obj + obj->ob_type->tp_weaklistoffset);
Guido van Rossumad47da02002-08-12 19:05:44 +00001593 if (*weaklistptr == NULL)
1594 result = Py_None;
1595 else
1596 result = *weaklistptr;
1597 Py_INCREF(result);
1598 return result;
1599}
1600
Guido van Rossum373c7412003-01-07 13:41:37 +00001601/* Three variants on the subtype_getsets list. */
1602
1603static PyGetSetDef subtype_getsets_full[] = {
Guido van Rossumad47da02002-08-12 19:05:44 +00001604 {"__dict__", subtype_dict, subtype_setdict,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001605 PyDoc_STR("dictionary for instance variables (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001606 {"__weakref__", subtype_getweakref, NULL,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001607 PyDoc_STR("list of weak references to the object (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001608 {0}
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001609};
1610
Guido van Rossum373c7412003-01-07 13:41:37 +00001611static PyGetSetDef subtype_getsets_dict_only[] = {
1612 {"__dict__", subtype_dict, subtype_setdict,
1613 PyDoc_STR("dictionary for instance variables (if defined)")},
1614 {0}
1615};
1616
1617static PyGetSetDef subtype_getsets_weakref_only[] = {
1618 {"__weakref__", subtype_getweakref, NULL,
1619 PyDoc_STR("list of weak references to the object (if defined)")},
1620 {0}
1621};
1622
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001623static int
1624valid_identifier(PyObject *s)
1625{
Guido van Rossum03013a02002-07-16 14:30:28 +00001626 unsigned char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001627 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001628
1629 if (!PyString_Check(s)) {
Georg Brandlccff7852006-06-18 22:17:29 +00001630 PyErr_Format(PyExc_TypeError,
1631 "__slots__ items must be strings, not '%.200s'",
1632 s->ob_type->tp_name);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001633 return 0;
1634 }
Guido van Rossum03013a02002-07-16 14:30:28 +00001635 p = (unsigned char *) PyString_AS_STRING(s);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001636 n = PyString_GET_SIZE(s);
1637 /* We must reject an empty name. As a hack, we bump the
1638 length to 1 so that the loop will balk on the trailing \0. */
1639 if (n == 0)
1640 n = 1;
1641 for (i = 0; i < n; i++, p++) {
1642 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1643 PyErr_SetString(PyExc_TypeError,
1644 "__slots__ must be identifiers");
1645 return 0;
1646 }
1647 }
1648 return 1;
1649}
1650
Martin v. Löwisd919a592002-10-14 21:07:28 +00001651#ifdef Py_USING_UNICODE
1652/* Replace Unicode objects in slots. */
1653
1654static PyObject *
Martin v. Löwiseb079f12006-02-16 14:32:27 +00001655_unicode_to_string(PyObject *slots, Py_ssize_t nslots)
Martin v. Löwisd919a592002-10-14 21:07:28 +00001656{
Žiga Seilnacht71436f02007-03-14 12:24:09 +00001657 PyObject *tmp = NULL;
1658 PyObject *slot_name, *new_name;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00001659 Py_ssize_t i;
Žiga Seilnacht71436f02007-03-14 12:24:09 +00001660
Martin v. Löwisd919a592002-10-14 21:07:28 +00001661 for (i = 0; i < nslots; i++) {
Žiga Seilnacht71436f02007-03-14 12:24:09 +00001662 if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
1663 if (tmp == NULL) {
1664 tmp = PySequence_List(slots);
Martin v. Löwisd919a592002-10-14 21:07:28 +00001665 if (tmp == NULL)
1666 return NULL;
1667 }
Žiga Seilnacht71436f02007-03-14 12:24:09 +00001668 new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
1669 NULL);
1670 if (new_name == NULL) {
Martin v. Löwisd919a592002-10-14 21:07:28 +00001671 Py_DECREF(tmp);
Žiga Seilnacht71436f02007-03-14 12:24:09 +00001672 return NULL;
Martin v. Löwisd919a592002-10-14 21:07:28 +00001673 }
Žiga Seilnacht71436f02007-03-14 12:24:09 +00001674 Py_INCREF(new_name);
1675 PyList_SET_ITEM(tmp, i, new_name);
1676 Py_DECREF(slot_name);
Martin v. Löwisd919a592002-10-14 21:07:28 +00001677 }
1678 }
Žiga Seilnacht71436f02007-03-14 12:24:09 +00001679 if (tmp != NULL) {
1680 slots = PyList_AsTuple(tmp);
1681 Py_DECREF(tmp);
1682 }
1683 return slots;
Martin v. Löwisd919a592002-10-14 21:07:28 +00001684}
1685#endif
1686
Guido van Rossumf102e242007-03-23 18:53:03 +00001687/* Forward */
1688static int
1689object_init(PyObject *self, PyObject *args, PyObject *kwds);
1690
1691static int
1692type_init(PyObject *cls, PyObject *args, PyObject *kwds)
1693{
1694 int res;
1695
1696 assert(args != NULL && PyTuple_Check(args));
1697 assert(kwds == NULL || PyDict_Check(kwds));
1698
1699 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
1700 PyErr_SetString(PyExc_TypeError,
1701 "type.__init__() takes no keyword arguments");
1702 return -1;
1703 }
1704
1705 if (args != NULL && PyTuple_Check(args) &&
1706 (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
1707 PyErr_SetString(PyExc_TypeError,
1708 "type.__init__() takes 1 or 3 arguments");
1709 return -1;
1710 }
1711
1712 /* Call object.__init__(self) now. */
1713 /* XXX Could call super(type, cls).__init__() but what's the point? */
1714 args = PyTuple_GetSlice(args, 0, 0);
1715 res = object_init(cls, args, NULL);
1716 Py_DECREF(args);
1717 return res;
1718}
1719
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001720static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001721type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1722{
1723 PyObject *name, *bases, *dict;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001724 static char *kwlist[] = {"name", "bases", "dict", 0};
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001725 PyObject *slots, *tmp, *newslots;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001726 PyTypeObject *type, *base, *tmptype, *winner;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001727 PyHeapTypeObject *et;
Guido van Rossum6f799372001-09-20 20:46:19 +00001728 PyMemberDef *mp;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001729 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
Guido van Rossumad47da02002-08-12 19:05:44 +00001730 int j, may_add_dict, may_add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001731
Tim Peters3abca122001-10-27 19:37:48 +00001732 assert(args != NULL && PyTuple_Check(args));
1733 assert(kwds == NULL || PyDict_Check(kwds));
1734
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001735 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +00001736 {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001737 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1738 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
Tim Peters3abca122001-10-27 19:37:48 +00001739
1740 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1741 PyObject *x = PyTuple_GET_ITEM(args, 0);
1742 Py_INCREF(x->ob_type);
1743 return (PyObject *) x->ob_type;
1744 }
1745
1746 /* SF bug 475327 -- if that didn't trigger, we need 3
1747 arguments. but PyArg_ParseTupleAndKeywords below may give
1748 a msg saying type() needs exactly 3. */
1749 if (nargs + nkwds != 3) {
1750 PyErr_SetString(PyExc_TypeError,
1751 "type() takes 1 or 3 arguments");
1752 return NULL;
1753 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001754 }
1755
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001756 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001757 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1758 &name,
1759 &PyTuple_Type, &bases,
1760 &PyDict_Type, &dict))
1761 return NULL;
1762
Jeremy Hyltonfa955692007-02-27 18:29:45 +00001763 winner = most_derived_metaclass(metatype, bases);
1764 if (winner == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001765 return NULL;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001766 if (winner != metatype) {
Jeremy Hyltonfa955692007-02-27 18:29:45 +00001767 if (winner->tp_new != type_new) /* Pass it to the winner */ {
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001768 return winner->tp_new(winner, args, kwds);
Jeremy Hyltonfa955692007-02-27 18:29:45 +00001769 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001770 metatype = winner;
1771 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001772
1773 /* Adjust for empty tuple bases */
Jeremy Hyltonfa955692007-02-27 18:29:45 +00001774 nbases = PyTuple_GET_SIZE(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001775 if (nbases == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001776 bases = PyTuple_Pack(1, &PyBaseObject_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001777 if (bases == NULL)
1778 return NULL;
1779 nbases = 1;
1780 }
1781 else
1782 Py_INCREF(bases);
1783
1784 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1785
1786 /* Calculate best base, and check that all bases are type objects */
1787 base = best_base(bases);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001788 if (base == NULL) {
1789 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001790 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001791 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001792 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1793 PyErr_Format(PyExc_TypeError,
1794 "type '%.100s' is not an acceptable base type",
1795 base->tp_name);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001796 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001797 return NULL;
1798 }
1799
Tim Peters6d6c1a32001-08-02 04:15:00 +00001800 /* Check for a __slots__ sequence variable in dict, and count it */
1801 slots = PyDict_GetItemString(dict, "__slots__");
1802 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001803 add_dict = 0;
1804 add_weak = 0;
Guido van Rossumad47da02002-08-12 19:05:44 +00001805 may_add_dict = base->tp_dictoffset == 0;
1806 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1807 if (slots == NULL) {
1808 if (may_add_dict) {
1809 add_dict++;
1810 }
1811 if (may_add_weak) {
1812 add_weak++;
1813 }
1814 }
1815 else {
1816 /* Have slots */
1817
Tim Peters6d6c1a32001-08-02 04:15:00 +00001818 /* Make it into a tuple */
1819 if (PyString_Check(slots))
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001820 slots = PyTuple_Pack(1, slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001821 else
1822 slots = PySequence_Tuple(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001823 if (slots == NULL) {
1824 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001825 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001826 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001827 assert(PyTuple_Check(slots));
1828
1829 /* Are slots allowed? */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001830 nslots = PyTuple_GET_SIZE(slots);
Jeremy Hylton1c7a0ea2003-07-16 16:08:23 +00001831 if (nslots > 0 && base->tp_itemsize != 0) {
Guido van Rossumc4141872001-08-30 04:43:35 +00001832 PyErr_Format(PyExc_TypeError,
1833 "nonempty __slots__ "
1834 "not supported for subtype of '%s'",
1835 base->tp_name);
Guido van Rossumad47da02002-08-12 19:05:44 +00001836 bad_slots:
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001837 Py_DECREF(bases);
Guido van Rossumad47da02002-08-12 19:05:44 +00001838 Py_DECREF(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00001839 return NULL;
1840 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001841
Martin v. Löwisd919a592002-10-14 21:07:28 +00001842#ifdef Py_USING_UNICODE
1843 tmp = _unicode_to_string(slots, nslots);
Žiga Seilnacht71436f02007-03-14 12:24:09 +00001844 if (tmp == NULL)
1845 goto bad_slots;
Martin v. Löwis13b1a5c2002-10-14 21:11:34 +00001846 if (tmp != slots) {
1847 Py_DECREF(slots);
1848 slots = tmp;
1849 }
Martin v. Löwisd919a592002-10-14 21:07:28 +00001850#endif
Guido van Rossumad47da02002-08-12 19:05:44 +00001851 /* Check for valid slot names and two special cases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001852 for (i = 0; i < nslots; i++) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001853 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1854 char *s;
1855 if (!valid_identifier(tmp))
1856 goto bad_slots;
1857 assert(PyString_Check(tmp));
1858 s = PyString_AS_STRING(tmp);
1859 if (strcmp(s, "__dict__") == 0) {
1860 if (!may_add_dict || add_dict) {
1861 PyErr_SetString(PyExc_TypeError,
1862 "__dict__ slot disallowed: "
1863 "we already got one");
1864 goto bad_slots;
1865 }
1866 add_dict++;
1867 }
1868 if (strcmp(s, "__weakref__") == 0) {
1869 if (!may_add_weak || add_weak) {
1870 PyErr_SetString(PyExc_TypeError,
1871 "__weakref__ slot disallowed: "
1872 "either we already got one, "
1873 "or __itemsize__ != 0");
1874 goto bad_slots;
1875 }
1876 add_weak++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001877 }
1878 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001879
Žiga Seilnacht6f2d09c2007-03-16 11:59:38 +00001880 /* Copy slots into a list, mangle names and sort them.
1881 Sorted names are needed for __class__ assignment.
1882 Convert them back to tuple at the end.
1883 */
1884 newslots = PyList_New(nslots - add_dict - add_weak);
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001885 if (newslots == NULL)
Guido van Rossumad47da02002-08-12 19:05:44 +00001886 goto bad_slots;
1887 for (i = j = 0; i < nslots; i++) {
1888 char *s;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001889 tmp = PyTuple_GET_ITEM(slots, i);
Guido van Rossumad47da02002-08-12 19:05:44 +00001890 s = PyString_AS_STRING(tmp);
1891 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1892 (add_weak && strcmp(s, "__weakref__") == 0))
1893 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 tmp =_Py_Mangle(name, tmp);
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00001895 if (!tmp)
1896 goto bad_slots;
Žiga Seilnacht6f2d09c2007-03-16 11:59:38 +00001897 PyList_SET_ITEM(newslots, j, tmp);
Guido van Rossumad47da02002-08-12 19:05:44 +00001898 j++;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001899 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001900 assert(j == nslots - add_dict - add_weak);
1901 nslots = j;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001902 Py_DECREF(slots);
Žiga Seilnacht6f2d09c2007-03-16 11:59:38 +00001903 if (PyList_Sort(newslots) == -1) {
1904 Py_DECREF(bases);
1905 Py_DECREF(newslots);
1906 return NULL;
1907 }
1908 slots = PyList_AsTuple(newslots);
1909 Py_DECREF(newslots);
1910 if (slots == NULL) {
1911 Py_DECREF(bases);
1912 return NULL;
1913 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001914
Guido van Rossumad47da02002-08-12 19:05:44 +00001915 /* Secondary bases may provide weakrefs or dict */
1916 if (nbases > 1 &&
1917 ((may_add_dict && !add_dict) ||
1918 (may_add_weak && !add_weak))) {
1919 for (i = 0; i < nbases; i++) {
1920 tmp = PyTuple_GET_ITEM(bases, i);
1921 if (tmp == (PyObject *)base)
1922 continue; /* Skip primary base */
1923 if (PyClass_Check(tmp)) {
1924 /* Classic base class provides both */
1925 if (may_add_dict && !add_dict)
1926 add_dict++;
1927 if (may_add_weak && !add_weak)
1928 add_weak++;
1929 break;
1930 }
1931 assert(PyType_Check(tmp));
1932 tmptype = (PyTypeObject *)tmp;
1933 if (may_add_dict && !add_dict &&
1934 tmptype->tp_dictoffset != 0)
1935 add_dict++;
1936 if (may_add_weak && !add_weak &&
1937 tmptype->tp_weaklistoffset != 0)
1938 add_weak++;
1939 if (may_add_dict && !add_dict)
1940 continue;
1941 if (may_add_weak && !add_weak)
1942 continue;
1943 /* Nothing more to check */
1944 break;
1945 }
1946 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001947 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001948
1949 /* XXX From here until type is safely allocated,
1950 "return NULL" may leak slots! */
1951
1952 /* Allocate the type object */
1953 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
Guido van Rossumad47da02002-08-12 19:05:44 +00001954 if (type == NULL) {
1955 Py_XDECREF(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001956 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001957 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00001958 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001959
1960 /* Keep name and slots alive in the extended type object */
Guido van Rossume5c691a2003-03-07 15:13:17 +00001961 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001962 Py_INCREF(name);
Georg Brandlc255c7b2006-02-20 22:27:28 +00001963 et->ht_name = name;
1964 et->ht_slots = slots;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001965
Guido van Rossumdc91b992001-08-08 22:26:22 +00001966 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001967 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1968 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001969 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1970 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001971
1972 /* It's a new-style number unless it specifically inherits any
1973 old-style numeric behavior */
1974 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1975 (base->tp_as_number == NULL))
1976 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1977
1978 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001979 type->tp_as_number = &et->as_number;
1980 type->tp_as_sequence = &et->as_sequence;
1981 type->tp_as_mapping = &et->as_mapping;
1982 type->tp_as_buffer = &et->as_buffer;
1983 type->tp_name = PyString_AS_STRING(name);
1984
1985 /* Set tp_base and tp_bases */
1986 type->tp_bases = bases;
1987 Py_INCREF(base);
1988 type->tp_base = base;
1989
Guido van Rossum687ae002001-10-15 22:03:32 +00001990 /* Initialize tp_dict from passed-in dict */
1991 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001992 if (dict == NULL) {
1993 Py_DECREF(type);
1994 return NULL;
1995 }
1996
Guido van Rossumc3542212001-08-16 09:18:56 +00001997 /* Set __module__ in the dict */
1998 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1999 tmp = PyEval_GetGlobals();
2000 if (tmp != NULL) {
2001 tmp = PyDict_GetItemString(tmp, "__name__");
2002 if (tmp != NULL) {
2003 if (PyDict_SetItemString(dict, "__module__",
2004 tmp) < 0)
2005 return NULL;
2006 }
2007 }
2008 }
2009
Tim Peters2f93e282001-10-04 05:27:00 +00002010 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00002011 and is a string. The __doc__ accessor will first look for tp_doc;
2012 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00002013 */
2014 {
2015 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
2016 if (doc != NULL && PyString_Check(doc)) {
2017 const size_t n = (size_t)PyString_GET_SIZE(doc);
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00002018 char *tp_doc = (char *)PyObject_MALLOC(n+1);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002019 if (tp_doc == NULL) {
Tim Peters2f93e282001-10-04 05:27:00 +00002020 Py_DECREF(type);
2021 return NULL;
2022 }
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002023 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00002024 type->tp_doc = tp_doc;
Tim Peters2f93e282001-10-04 05:27:00 +00002025 }
2026 }
2027
Tim Peters6d6c1a32001-08-02 04:15:00 +00002028 /* Special-case __new__: if it's a plain function,
2029 make it a static function */
2030 tmp = PyDict_GetItemString(dict, "__new__");
2031 if (tmp != NULL && PyFunction_Check(tmp)) {
2032 tmp = PyStaticMethod_New(tmp);
2033 if (tmp == NULL) {
2034 Py_DECREF(type);
2035 return NULL;
2036 }
2037 PyDict_SetItemString(dict, "__new__", tmp);
2038 Py_DECREF(tmp);
2039 }
2040
2041 /* Add descriptors for custom slots from __slots__, or for __dict__ */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002042 mp = PyHeapType_GET_MEMBERS(et);
Neil Schemenauerc806c882001-08-29 23:54:54 +00002043 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002044 if (slots != NULL) {
2045 for (i = 0; i < nslots; i++, mp++) {
2046 mp->name = PyString_AS_STRING(
2047 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00002048 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002049 mp->offset = slotoffset;
Žiga Seilnacht89032082007-03-11 15:54:54 +00002050
2051 /* __dict__ and __weakref__ are already filtered out */
2052 assert(strcmp(mp->name, "__dict__") != 0);
2053 assert(strcmp(mp->name, "__weakref__") != 0);
2054
Tim Peters6d6c1a32001-08-02 04:15:00 +00002055 slotoffset += sizeof(PyObject *);
2056 }
2057 }
Guido van Rossumad47da02002-08-12 19:05:44 +00002058 if (add_dict) {
2059 if (base->tp_itemsize)
2060 type->tp_dictoffset = -(long)sizeof(PyObject *);
2061 else
2062 type->tp_dictoffset = slotoffset;
2063 slotoffset += sizeof(PyObject *);
2064 }
2065 if (add_weak) {
2066 assert(!base->tp_itemsize);
2067 type->tp_weaklistoffset = slotoffset;
2068 slotoffset += sizeof(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002069 }
2070 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00002071 type->tp_itemsize = base->tp_itemsize;
Guido van Rossume5c691a2003-03-07 15:13:17 +00002072 type->tp_members = PyHeapType_GET_MEMBERS(et);
Guido van Rossum373c7412003-01-07 13:41:37 +00002073
2074 if (type->tp_weaklistoffset && type->tp_dictoffset)
2075 type->tp_getset = subtype_getsets_full;
2076 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
2077 type->tp_getset = subtype_getsets_weakref_only;
2078 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
2079 type->tp_getset = subtype_getsets_dict_only;
2080 else
2081 type->tp_getset = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002082
2083 /* Special case some slots */
2084 if (type->tp_dictoffset != 0 || nslots > 0) {
2085 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
2086 type->tp_getattro = PyObject_GenericGetAttr;
2087 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
2088 type->tp_setattro = PyObject_GenericSetAttr;
2089 }
2090 type->tp_dealloc = subtype_dealloc;
2091
Guido van Rossum9475a232001-10-05 20:51:39 +00002092 /* Enable GC unless there are really no instance variables possible */
2093 if (!(type->tp_basicsize == sizeof(PyObject) &&
2094 type->tp_itemsize == 0))
2095 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2096
Tim Peters6d6c1a32001-08-02 04:15:00 +00002097 /* Always override allocation strategy to use regular heap */
2098 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00002099 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002100 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00002101 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00002102 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00002103 }
2104 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002105 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002106
2107 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002108 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002109 Py_DECREF(type);
2110 return NULL;
2111 }
2112
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002113 /* Put the proper slots in place */
2114 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00002115
Tim Peters6d6c1a32001-08-02 04:15:00 +00002116 return (PyObject *)type;
2117}
2118
2119/* Internal API to look for a name through the MRO.
2120 This returns a borrowed reference, and doesn't set an exception! */
2121PyObject *
2122_PyType_Lookup(PyTypeObject *type, PyObject *name)
2123{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002124 Py_ssize_t i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00002125 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002126
Guido van Rossum687ae002001-10-15 22:03:32 +00002127 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002128 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00002129
2130 /* If mro is NULL, the type is either not yet initialized
2131 by PyType_Ready(), or already cleared by type_clear().
2132 Either way the safest thing to do is to return NULL. */
2133 if (mro == NULL)
2134 return NULL;
2135
Tim Peters6d6c1a32001-08-02 04:15:00 +00002136 assert(PyTuple_Check(mro));
2137 n = PyTuple_GET_SIZE(mro);
2138 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002139 base = PyTuple_GET_ITEM(mro, i);
2140 if (PyClass_Check(base))
2141 dict = ((PyClassObject *)base)->cl_dict;
2142 else {
2143 assert(PyType_Check(base));
2144 dict = ((PyTypeObject *)base)->tp_dict;
2145 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002146 assert(dict && PyDict_Check(dict));
2147 res = PyDict_GetItem(dict, name);
2148 if (res != NULL)
2149 return res;
2150 }
2151 return NULL;
2152}
2153
2154/* This is similar to PyObject_GenericGetAttr(),
2155 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2156static PyObject *
2157type_getattro(PyTypeObject *type, PyObject *name)
2158{
2159 PyTypeObject *metatype = type->ob_type;
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002160 PyObject *meta_attribute, *attribute;
2161 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002162
2163 /* Initialize this type (we'll assume the metatype is initialized) */
2164 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002165 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002166 return NULL;
2167 }
2168
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002169 /* No readable descriptor found yet */
2170 meta_get = NULL;
Tim Peters34592512002-07-11 06:23:50 +00002171
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002172 /* Look for the attribute in the metatype */
2173 meta_attribute = _PyType_Lookup(metatype, name);
2174
2175 if (meta_attribute != NULL) {
2176 meta_get = meta_attribute->ob_type->tp_descr_get;
Tim Peters34592512002-07-11 06:23:50 +00002177
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002178 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2179 /* Data descriptors implement tp_descr_set to intercept
2180 * writes. Assume the attribute is not overridden in
2181 * type's tp_dict (and bases): call the descriptor now.
2182 */
2183 return meta_get(meta_attribute, (PyObject *)type,
2184 (PyObject *)metatype);
2185 }
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002186 Py_INCREF(meta_attribute);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002187 }
2188
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002189 /* No data descriptor found on metatype. Look in tp_dict of this
2190 * type and its bases */
2191 attribute = _PyType_Lookup(type, name);
2192 if (attribute != NULL) {
2193 /* Implement descriptor functionality, if any */
2194 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002195
2196 Py_XDECREF(meta_attribute);
2197
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002198 if (local_get != NULL) {
2199 /* NULL 2nd argument indicates the descriptor was
2200 * found on the target object itself (or a base) */
2201 return local_get(attribute, (PyObject *)NULL,
2202 (PyObject *)type);
2203 }
Tim Peters34592512002-07-11 06:23:50 +00002204
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002205 Py_INCREF(attribute);
2206 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002207 }
2208
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002209 /* No attribute found in local __dict__ (or bases): use the
2210 * descriptor from the metatype, if any */
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002211 if (meta_get != NULL) {
2212 PyObject *res;
2213 res = meta_get(meta_attribute, (PyObject *)type,
2214 (PyObject *)metatype);
2215 Py_DECREF(meta_attribute);
2216 return res;
2217 }
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002218
2219 /* If an ordinary attribute was found on the metatype, return it now */
2220 if (meta_attribute != NULL) {
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002221 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002222 }
2223
2224 /* Give up */
2225 PyErr_Format(PyExc_AttributeError,
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002226 "type object '%.50s' has no attribute '%.400s'",
2227 type->tp_name, PyString_AS_STRING(name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002228 return NULL;
2229}
2230
2231static int
2232type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2233{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002234 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2235 PyErr_Format(
2236 PyExc_TypeError,
2237 "can't set attributes of built-in/extension type '%s'",
2238 type->tp_name);
2239 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002240 }
Guido van Rossum8d24ee92003-03-24 23:49:49 +00002241 /* XXX Example of how I expect this to be used...
2242 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2243 return -1;
2244 */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002245 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2246 return -1;
2247 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002248}
2249
2250static void
2251type_dealloc(PyTypeObject *type)
2252{
Guido van Rossume5c691a2003-03-07 15:13:17 +00002253 PyHeapTypeObject *et;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002254
2255 /* Assert this is a heap-allocated type object */
2256 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002257 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00002258 PyObject_ClearWeakRefs((PyObject *)type);
Guido van Rossume5c691a2003-03-07 15:13:17 +00002259 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002260 Py_XDECREF(type->tp_base);
2261 Py_XDECREF(type->tp_dict);
2262 Py_XDECREF(type->tp_bases);
2263 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00002264 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00002265 Py_XDECREF(type->tp_subclasses);
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00002266 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2267 * of most other objects. It's okay to cast it to char *.
2268 */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002269 PyObject_Free((char *)type->tp_doc);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002270 Py_XDECREF(et->ht_name);
2271 Py_XDECREF(et->ht_slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002272 type->ob_type->tp_free((PyObject *)type);
2273}
2274
Guido van Rossum1c450732001-10-08 15:18:27 +00002275static PyObject *
2276type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2277{
2278 PyObject *list, *raw, *ref;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002279 Py_ssize_t i, n;
Guido van Rossum1c450732001-10-08 15:18:27 +00002280
2281 list = PyList_New(0);
2282 if (list == NULL)
2283 return NULL;
2284 raw = type->tp_subclasses;
2285 if (raw == NULL)
2286 return list;
2287 assert(PyList_Check(raw));
2288 n = PyList_GET_SIZE(raw);
2289 for (i = 0; i < n; i++) {
2290 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00002291 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00002292 ref = PyWeakref_GET_OBJECT(ref);
2293 if (ref != Py_None) {
2294 if (PyList_Append(list, ref) < 0) {
2295 Py_DECREF(list);
2296 return NULL;
2297 }
2298 }
2299 }
2300 return list;
2301}
2302
Tim Peters6d6c1a32001-08-02 04:15:00 +00002303static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002304 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002305 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
Guido van Rossum1c450732001-10-08 15:18:27 +00002306 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002307 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002308 {0}
2309};
2310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002311PyDoc_STRVAR(type_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002312"type(object) -> the object's type\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002313"type(name, bases, dict) -> a new type");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002314
Guido van Rossum048eb752001-10-02 21:24:57 +00002315static int
2316type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2317{
Guido van Rossuma3862092002-06-10 15:24:42 +00002318 /* Because of type_is_gc(), the collector only calls this
2319 for heaptypes. */
2320 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002321
Thomas Woutersc6e55062006-04-15 21:47:09 +00002322 Py_VISIT(type->tp_dict);
2323 Py_VISIT(type->tp_cache);
2324 Py_VISIT(type->tp_mro);
2325 Py_VISIT(type->tp_bases);
2326 Py_VISIT(type->tp_base);
Guido van Rossuma3862092002-06-10 15:24:42 +00002327
2328 /* There's no need to visit type->tp_subclasses or
Georg Brandlc255c7b2006-02-20 22:27:28 +00002329 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
Guido van Rossuma3862092002-06-10 15:24:42 +00002330 in cycles; tp_subclasses is a list of weak references,
2331 and slots is a tuple of strings. */
Guido van Rossum048eb752001-10-02 21:24:57 +00002332
Guido van Rossum048eb752001-10-02 21:24:57 +00002333 return 0;
2334}
2335
2336static int
2337type_clear(PyTypeObject *type)
2338{
Guido van Rossuma3862092002-06-10 15:24:42 +00002339 /* Because of type_is_gc(), the collector only calls this
2340 for heaptypes. */
2341 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002342
Guido van Rossuma3862092002-06-10 15:24:42 +00002343 /* The only field we need to clear is tp_mro, which is part of a
2344 hard cycle (its first element is the class itself) that won't
2345 be broken otherwise (it's a tuple and tuples don't have a
2346 tp_clear handler). None of the other fields need to be
2347 cleared, and here's why:
Guido van Rossum048eb752001-10-02 21:24:57 +00002348
Guido van Rossuma3862092002-06-10 15:24:42 +00002349 tp_dict:
2350 It is a dict, so the collector will call its tp_clear.
2351
2352 tp_cache:
2353 Not used; if it were, it would be a dict.
2354
2355 tp_bases, tp_base:
2356 If these are involved in a cycle, there must be at least
2357 one other, mutable object in the cycle, e.g. a base
2358 class's dict; the cycle will be broken that way.
2359
2360 tp_subclasses:
2361 A list of weak references can't be part of a cycle; and
2362 lists have their own tp_clear.
2363
Guido van Rossume5c691a2003-03-07 15:13:17 +00002364 slots (in PyHeapTypeObject):
Guido van Rossuma3862092002-06-10 15:24:42 +00002365 A tuple of strings can't be part of a cycle.
2366 */
2367
Thomas Woutersedf17d82006-04-15 17:28:34 +00002368 Py_CLEAR(type->tp_mro);
Guido van Rossum048eb752001-10-02 21:24:57 +00002369
2370 return 0;
2371}
2372
2373static int
2374type_is_gc(PyTypeObject *type)
2375{
2376 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2377}
2378
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002379PyTypeObject PyType_Type = {
2380 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002381 0, /* ob_size */
2382 "type", /* tp_name */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002383 sizeof(PyHeapTypeObject), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00002384 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002385 (destructor)type_dealloc, /* tp_dealloc */
2386 0, /* tp_print */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00002387 0, /* tp_getattr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002388 0, /* tp_setattr */
2389 type_compare, /* tp_compare */
2390 (reprfunc)type_repr, /* tp_repr */
2391 0, /* tp_as_number */
2392 0, /* tp_as_sequence */
2393 0, /* tp_as_mapping */
2394 (hashfunc)_Py_HashPointer, /* tp_hash */
2395 (ternaryfunc)type_call, /* tp_call */
2396 0, /* tp_str */
2397 (getattrofunc)type_getattro, /* tp_getattro */
2398 (setattrofunc)type_setattro, /* tp_setattro */
2399 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00002400 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002401 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002402 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00002403 (traverseproc)type_traverse, /* tp_traverse */
2404 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002405 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00002406 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002407 0, /* tp_iter */
2408 0, /* tp_iternext */
2409 type_methods, /* tp_methods */
2410 type_members, /* tp_members */
2411 type_getsets, /* tp_getset */
2412 0, /* tp_base */
2413 0, /* tp_dict */
2414 0, /* tp_descr_get */
2415 0, /* tp_descr_set */
2416 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
Guido van Rossumf102e242007-03-23 18:53:03 +00002417 type_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418 0, /* tp_alloc */
2419 type_new, /* tp_new */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00002420 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00002421 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002422};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002423
2424
2425/* The base type of all types (eventually)... except itself. */
2426
Guido van Rossum143b5642007-03-23 04:58:42 +00002427/* You may wonder why object.__new__() only complains about arguments
2428 when object.__init__() is not overridden, and vice versa.
2429
2430 Consider the use cases:
2431
2432 1. When neither is overridden, we want to hear complaints about
2433 excess (i.e., any) arguments, since their presence could
2434 indicate there's a bug.
2435
2436 2. When defining an Immutable type, we are likely to override only
2437 __new__(), since __init__() is called too late to initialize an
2438 Immutable object. Since __new__() defines the signature for the
2439 type, it would be a pain to have to override __init__() just to
2440 stop it from complaining about excess arguments.
2441
2442 3. When defining a Mutable type, we are likely to override only
2443 __init__(). So here the converse reasoning applies: we don't
2444 want to have to override __new__() just to stop it from
2445 complaining.
2446
2447 4. When __init__() is overridden, and the subclass __init__() calls
2448 object.__init__(), the latter should complain about excess
2449 arguments; ditto for __new__().
2450
2451 Use cases 2 and 3 make it unattractive to unconditionally check for
2452 excess arguments. The best solution that addresses all four use
2453 cases is as follows: __init__() complains about excess arguments
2454 unless __new__() is overridden and __init__() is not overridden
2455 (IOW, if __init__() is overridden or __new__() is not overridden);
2456 symmetrically, __new__() complains about excess arguments unless
2457 __init__() is overridden and __new__() is not overridden
2458 (IOW, if __new__() is overridden or __init__() is not overridden).
2459
2460 However, for backwards compatibility, this breaks too much code.
2461 Therefore, in 2.6, we'll *warn* about excess arguments when both
2462 methods are overridden; for all other cases we'll use the above
2463 rules.
2464
2465*/
2466
2467/* Forward */
2468static PyObject *
2469object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2470
2471static int
2472excess_args(PyObject *args, PyObject *kwds)
2473{
2474 return PyTuple_GET_SIZE(args) ||
2475 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
2476}
2477
Tim Peters6d6c1a32001-08-02 04:15:00 +00002478static int
2479object_init(PyObject *self, PyObject *args, PyObject *kwds)
2480{
Guido van Rossum143b5642007-03-23 04:58:42 +00002481 int err = 0;
2482 if (excess_args(args, kwds)) {
2483 PyTypeObject *type = self->ob_type;
2484 if (type->tp_init != object_init &&
2485 type->tp_new != object_new)
2486 {
2487 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2488 "object.__init__() takes no parameters",
2489 1);
2490 }
2491 else if (type->tp_init != object_init ||
2492 type->tp_new == object_new)
2493 {
2494 PyErr_SetString(PyExc_TypeError,
2495 "object.__init__() takes no parameters");
2496 err = -1;
2497 }
2498 }
2499 return err;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002500}
2501
Guido van Rossum298e4212003-02-13 16:30:16 +00002502static PyObject *
2503object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2504{
Guido van Rossum143b5642007-03-23 04:58:42 +00002505 int err = 0;
2506 if (excess_args(args, kwds)) {
2507 if (type->tp_new != object_new &&
2508 type->tp_init != object_init)
2509 {
2510 err = PyErr_WarnEx(PyExc_DeprecationWarning,
2511 "object.__new__() takes no parameters",
2512 1);
2513 }
2514 else if (type->tp_new != object_new ||
2515 type->tp_init == object_init)
2516 {
2517 PyErr_SetString(PyExc_TypeError,
2518 "object.__new__() takes no parameters");
2519 err = -1;
2520 }
Guido van Rossum298e4212003-02-13 16:30:16 +00002521 }
Guido van Rossum143b5642007-03-23 04:58:42 +00002522 if (err < 0)
2523 return NULL;
Guido van Rossum298e4212003-02-13 16:30:16 +00002524 return type->tp_alloc(type, 0);
2525}
2526
Tim Peters6d6c1a32001-08-02 04:15:00 +00002527static void
2528object_dealloc(PyObject *self)
2529{
2530 self->ob_type->tp_free(self);
2531}
2532
Guido van Rossum8e248182001-08-12 05:17:56 +00002533static PyObject *
2534object_repr(PyObject *self)
2535{
Guido van Rossum76e69632001-08-16 18:52:43 +00002536 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00002537 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002538
Guido van Rossum76e69632001-08-16 18:52:43 +00002539 type = self->ob_type;
2540 mod = type_module(type, NULL);
2541 if (mod == NULL)
2542 PyErr_Clear();
2543 else if (!PyString_Check(mod)) {
2544 Py_DECREF(mod);
2545 mod = NULL;
2546 }
2547 name = type_name(type, NULL);
2548 if (name == NULL)
2549 return NULL;
2550 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00002551 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002552 PyString_AS_STRING(mod),
2553 PyString_AS_STRING(name),
2554 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002555 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00002556 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002557 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002558 Py_XDECREF(mod);
2559 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00002560 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002561}
2562
Guido van Rossumb8f63662001-08-15 23:57:02 +00002563static PyObject *
2564object_str(PyObject *self)
2565{
2566 unaryfunc f;
2567
2568 f = self->ob_type->tp_repr;
2569 if (f == NULL)
2570 f = object_repr;
2571 return f(self);
2572}
2573
Guido van Rossum8e248182001-08-12 05:17:56 +00002574static long
2575object_hash(PyObject *self)
2576{
2577 return _Py_HashPointer(self);
2578}
Guido van Rossum8e248182001-08-12 05:17:56 +00002579
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002580static PyObject *
2581object_get_class(PyObject *self, void *closure)
2582{
2583 Py_INCREF(self->ob_type);
2584 return (PyObject *)(self->ob_type);
2585}
2586
2587static int
2588equiv_structs(PyTypeObject *a, PyTypeObject *b)
2589{
2590 return a == b ||
2591 (a != NULL &&
2592 b != NULL &&
2593 a->tp_basicsize == b->tp_basicsize &&
2594 a->tp_itemsize == b->tp_itemsize &&
2595 a->tp_dictoffset == b->tp_dictoffset &&
2596 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2597 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2598 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2599}
2600
2601static int
2602same_slots_added(PyTypeObject *a, PyTypeObject *b)
2603{
2604 PyTypeObject *base = a->tp_base;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002605 Py_ssize_t size;
Žiga Seilnacht6f2d09c2007-03-16 11:59:38 +00002606 PyObject *slots_a, *slots_b;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002607
2608 if (base != b->tp_base)
2609 return 0;
2610 if (equiv_structs(a, base) && equiv_structs(b, base))
2611 return 1;
2612 size = base->tp_basicsize;
2613 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2614 size += sizeof(PyObject *);
2615 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2616 size += sizeof(PyObject *);
Žiga Seilnacht6f2d09c2007-03-16 11:59:38 +00002617
2618 /* Check slots compliance */
2619 slots_a = ((PyHeapTypeObject *)a)->ht_slots;
2620 slots_b = ((PyHeapTypeObject *)b)->ht_slots;
2621 if (slots_a && slots_b) {
2622 if (PyObject_Compare(slots_a, slots_b) != 0)
2623 return 0;
2624 size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
2625 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002626 return size == a->tp_basicsize && size == b->tp_basicsize;
2627}
2628
2629static int
Anthony Baxtera6286212006-04-11 07:42:36 +00002630compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002631{
2632 PyTypeObject *newbase, *oldbase;
2633
Anthony Baxtera6286212006-04-11 07:42:36 +00002634 if (newto->tp_dealloc != oldto->tp_dealloc ||
2635 newto->tp_free != oldto->tp_free)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002636 {
2637 PyErr_Format(PyExc_TypeError,
2638 "%s assignment: "
2639 "'%s' deallocator differs from '%s'",
2640 attr,
Anthony Baxtera6286212006-04-11 07:42:36 +00002641 newto->tp_name,
2642 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002643 return 0;
2644 }
Anthony Baxtera6286212006-04-11 07:42:36 +00002645 newbase = newto;
2646 oldbase = oldto;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002647 while (equiv_structs(newbase, newbase->tp_base))
2648 newbase = newbase->tp_base;
2649 while (equiv_structs(oldbase, oldbase->tp_base))
2650 oldbase = oldbase->tp_base;
2651 if (newbase != oldbase &&
2652 (newbase->tp_base != oldbase->tp_base ||
2653 !same_slots_added(newbase, oldbase))) {
2654 PyErr_Format(PyExc_TypeError,
2655 "%s assignment: "
2656 "'%s' object layout differs from '%s'",
2657 attr,
Anthony Baxtera6286212006-04-11 07:42:36 +00002658 newto->tp_name,
2659 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002660 return 0;
2661 }
Tim Petersea7f75d2002-12-07 21:39:16 +00002662
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002663 return 1;
2664}
2665
2666static int
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002667object_set_class(PyObject *self, PyObject *value, void *closure)
2668{
Anthony Baxtera6286212006-04-11 07:42:36 +00002669 PyTypeObject *oldto = self->ob_type;
2670 PyTypeObject *newto;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002671
Guido van Rossumb6b89422002-04-15 01:03:30 +00002672 if (value == NULL) {
2673 PyErr_SetString(PyExc_TypeError,
2674 "can't delete __class__ attribute");
2675 return -1;
2676 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002677 if (!PyType_Check(value)) {
2678 PyErr_Format(PyExc_TypeError,
2679 "__class__ must be set to new-style class, not '%s' object",
2680 value->ob_type->tp_name);
2681 return -1;
2682 }
Anthony Baxtera6286212006-04-11 07:42:36 +00002683 newto = (PyTypeObject *)value;
2684 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2685 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
Guido van Rossum40af8892002-08-10 05:42:07 +00002686 {
2687 PyErr_Format(PyExc_TypeError,
2688 "__class__ assignment: only for heap types");
2689 return -1;
2690 }
Anthony Baxtera6286212006-04-11 07:42:36 +00002691 if (compatible_for_assignment(newto, oldto, "__class__")) {
2692 Py_INCREF(newto);
2693 self->ob_type = newto;
2694 Py_DECREF(oldto);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002695 return 0;
2696 }
2697 else {
Guido van Rossum9ee4b942002-05-24 18:47:47 +00002698 return -1;
2699 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002700}
2701
2702static PyGetSetDef object_getsets[] = {
2703 {"__class__", object_get_class, object_set_class,
Neal Norwitz858e34f2002-08-13 17:18:45 +00002704 PyDoc_STR("the object's class")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002705 {0}
2706};
2707
Guido van Rossumc53f0092003-02-18 22:05:12 +00002708
Guido van Rossum036f9992003-02-21 22:02:54 +00002709/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2710 We fall back to helpers in copy_reg for:
2711 - pickle protocols < 2
2712 - calculating the list of slot names (done only once per class)
2713 - the __newobj__ function (which is used as a token but never called)
2714*/
2715
2716static PyObject *
2717import_copy_reg(void)
2718{
2719 static PyObject *copy_reg_str;
Guido van Rossum3926a632001-09-25 16:25:58 +00002720
2721 if (!copy_reg_str) {
2722 copy_reg_str = PyString_InternFromString("copy_reg");
2723 if (copy_reg_str == NULL)
2724 return NULL;
2725 }
Guido van Rossum036f9992003-02-21 22:02:54 +00002726
2727 return PyImport_Import(copy_reg_str);
2728}
2729
2730static PyObject *
2731slotnames(PyObject *cls)
2732{
2733 PyObject *clsdict;
2734 PyObject *copy_reg;
2735 PyObject *slotnames;
2736
2737 if (!PyType_Check(cls)) {
2738 Py_INCREF(Py_None);
2739 return Py_None;
2740 }
2741
2742 clsdict = ((PyTypeObject *)cls)->tp_dict;
2743 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
Armin Rigoec862b92005-09-24 22:58:41 +00002744 if (slotnames != NULL && PyList_Check(slotnames)) {
Guido van Rossum036f9992003-02-21 22:02:54 +00002745 Py_INCREF(slotnames);
2746 return slotnames;
2747 }
2748
2749 copy_reg = import_copy_reg();
2750 if (copy_reg == NULL)
2751 return NULL;
2752
2753 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2754 Py_DECREF(copy_reg);
2755 if (slotnames != NULL &&
2756 slotnames != Py_None &&
2757 !PyList_Check(slotnames))
2758 {
2759 PyErr_SetString(PyExc_TypeError,
2760 "copy_reg._slotnames didn't return a list or None");
2761 Py_DECREF(slotnames);
2762 slotnames = NULL;
2763 }
2764
2765 return slotnames;
2766}
2767
2768static PyObject *
2769reduce_2(PyObject *obj)
2770{
2771 PyObject *cls, *getnewargs;
2772 PyObject *args = NULL, *args2 = NULL;
2773 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2774 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2775 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002776 Py_ssize_t i, n;
Guido van Rossum036f9992003-02-21 22:02:54 +00002777
2778 cls = PyObject_GetAttrString(obj, "__class__");
2779 if (cls == NULL)
2780 return NULL;
2781
2782 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2783 if (getnewargs != NULL) {
2784 args = PyObject_CallObject(getnewargs, NULL);
2785 Py_DECREF(getnewargs);
2786 if (args != NULL && !PyTuple_Check(args)) {
Georg Brandlccff7852006-06-18 22:17:29 +00002787 PyErr_Format(PyExc_TypeError,
2788 "__getnewargs__ should return a tuple, "
2789 "not '%.200s'", args->ob_type->tp_name);
Guido van Rossum036f9992003-02-21 22:02:54 +00002790 goto end;
2791 }
2792 }
2793 else {
2794 PyErr_Clear();
2795 args = PyTuple_New(0);
2796 }
2797 if (args == NULL)
2798 goto end;
2799
2800 getstate = PyObject_GetAttrString(obj, "__getstate__");
2801 if (getstate != NULL) {
2802 state = PyObject_CallObject(getstate, NULL);
2803 Py_DECREF(getstate);
Neal Norwitze2fdc612003-06-08 13:19:58 +00002804 if (state == NULL)
2805 goto end;
Guido van Rossum036f9992003-02-21 22:02:54 +00002806 }
2807 else {
Jim Fulton8a1a5942004-02-08 04:21:26 +00002808 PyErr_Clear();
Guido van Rossum036f9992003-02-21 22:02:54 +00002809 state = PyObject_GetAttrString(obj, "__dict__");
2810 if (state == NULL) {
2811 PyErr_Clear();
2812 state = Py_None;
2813 Py_INCREF(state);
2814 }
2815 names = slotnames(cls);
2816 if (names == NULL)
2817 goto end;
2818 if (names != Py_None) {
2819 assert(PyList_Check(names));
2820 slots = PyDict_New();
2821 if (slots == NULL)
2822 goto end;
2823 n = 0;
2824 /* Can't pre-compute the list size; the list
2825 is stored on the class so accessible to other
2826 threads, which may be run by DECREF */
2827 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2828 PyObject *name, *value;
2829 name = PyList_GET_ITEM(names, i);
2830 value = PyObject_GetAttr(obj, name);
2831 if (value == NULL)
2832 PyErr_Clear();
2833 else {
2834 int err = PyDict_SetItem(slots, name,
2835 value);
2836 Py_DECREF(value);
2837 if (err)
2838 goto end;
2839 n++;
2840 }
2841 }
2842 if (n) {
2843 state = Py_BuildValue("(NO)", state, slots);
2844 if (state == NULL)
2845 goto end;
2846 }
2847 }
2848 }
2849
2850 if (!PyList_Check(obj)) {
2851 listitems = Py_None;
2852 Py_INCREF(listitems);
2853 }
2854 else {
2855 listitems = PyObject_GetIter(obj);
2856 if (listitems == NULL)
2857 goto end;
2858 }
2859
2860 if (!PyDict_Check(obj)) {
2861 dictitems = Py_None;
2862 Py_INCREF(dictitems);
2863 }
2864 else {
2865 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2866 if (dictitems == NULL)
2867 goto end;
2868 }
2869
2870 copy_reg = import_copy_reg();
2871 if (copy_reg == NULL)
2872 goto end;
2873 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2874 if (newobj == NULL)
2875 goto end;
2876
2877 n = PyTuple_GET_SIZE(args);
2878 args2 = PyTuple_New(n+1);
2879 if (args2 == NULL)
2880 goto end;
2881 PyTuple_SET_ITEM(args2, 0, cls);
2882 cls = NULL;
2883 for (i = 0; i < n; i++) {
2884 PyObject *v = PyTuple_GET_ITEM(args, i);
2885 Py_INCREF(v);
2886 PyTuple_SET_ITEM(args2, i+1, v);
2887 }
2888
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002889 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
Guido van Rossum036f9992003-02-21 22:02:54 +00002890
2891 end:
2892 Py_XDECREF(cls);
2893 Py_XDECREF(args);
2894 Py_XDECREF(args2);
Jeremy Hyltond06483c2003-04-09 21:01:42 +00002895 Py_XDECREF(slots);
Guido van Rossum036f9992003-02-21 22:02:54 +00002896 Py_XDECREF(state);
2897 Py_XDECREF(names);
2898 Py_XDECREF(listitems);
2899 Py_XDECREF(dictitems);
2900 Py_XDECREF(copy_reg);
2901 Py_XDECREF(newobj);
2902 return res;
2903}
2904
Žiga Seilnacht20f43d32007-03-15 11:44:55 +00002905/*
2906 * There were two problems when object.__reduce__ and object.__reduce_ex__
2907 * were implemented in the same function:
2908 * - trying to pickle an object with a custom __reduce__ method that
2909 * fell back to object.__reduce__ in certain circumstances led to
2910 * infinite recursion at Python level and eventual RuntimeError.
2911 * - Pickling objects that lied about their type by overwriting the
2912 * __class__ descriptor could lead to infinite recursion at C level
2913 * and eventual segfault.
2914 *
2915 * Because of backwards compatibility, the two methods still have to
2916 * behave in the same way, even if this is not required by the pickle
2917 * protocol. This common functionality was moved to the _common_reduce
2918 * function.
2919 */
2920static PyObject *
2921_common_reduce(PyObject *self, int proto)
2922{
2923 PyObject *copy_reg, *res;
2924
2925 if (proto >= 2)
2926 return reduce_2(self);
2927
2928 copy_reg = import_copy_reg();
2929 if (!copy_reg)
2930 return NULL;
2931
2932 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
2933 Py_DECREF(copy_reg);
2934
2935 return res;
2936}
2937
2938static PyObject *
2939object_reduce(PyObject *self, PyObject *args)
2940{
2941 int proto = 0;
2942
2943 if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
2944 return NULL;
2945
2946 return _common_reduce(self, proto);
2947}
2948
Guido van Rossum036f9992003-02-21 22:02:54 +00002949static PyObject *
2950object_reduce_ex(PyObject *self, PyObject *args)
2951{
Žiga Seilnacht20f43d32007-03-15 11:44:55 +00002952 PyObject *reduce, *res;
Guido van Rossum036f9992003-02-21 22:02:54 +00002953 int proto = 0;
2954
2955 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2956 return NULL;
2957
2958 reduce = PyObject_GetAttrString(self, "__reduce__");
2959 if (reduce == NULL)
2960 PyErr_Clear();
2961 else {
2962 PyObject *cls, *clsreduce, *objreduce;
2963 int override;
2964 cls = PyObject_GetAttrString(self, "__class__");
2965 if (cls == NULL) {
2966 Py_DECREF(reduce);
2967 return NULL;
2968 }
2969 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2970 Py_DECREF(cls);
2971 if (clsreduce == NULL) {
2972 Py_DECREF(reduce);
2973 return NULL;
2974 }
2975 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2976 "__reduce__");
2977 override = (clsreduce != objreduce);
2978 Py_DECREF(clsreduce);
2979 if (override) {
2980 res = PyObject_CallObject(reduce, NULL);
2981 Py_DECREF(reduce);
2982 return res;
2983 }
2984 else
2985 Py_DECREF(reduce);
2986 }
2987
Žiga Seilnacht20f43d32007-03-15 11:44:55 +00002988 return _common_reduce(self, proto);
Guido van Rossum3926a632001-09-25 16:25:58 +00002989}
2990
2991static PyMethodDef object_methods[] = {
Guido van Rossumc53f0092003-02-18 22:05:12 +00002992 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2993 PyDoc_STR("helper for pickle")},
Žiga Seilnacht20f43d32007-03-15 11:44:55 +00002994 {"__reduce__", object_reduce, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002995 PyDoc_STR("helper for pickle")},
Guido van Rossum3926a632001-09-25 16:25:58 +00002996 {0}
2997};
2998
Guido van Rossum036f9992003-02-21 22:02:54 +00002999
Tim Peters6d6c1a32001-08-02 04:15:00 +00003000PyTypeObject PyBaseObject_Type = {
3001 PyObject_HEAD_INIT(&PyType_Type)
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00003002 0, /* ob_size */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003003 "object", /* tp_name */
3004 sizeof(PyObject), /* tp_basicsize */
3005 0, /* tp_itemsize */
Georg Brandl347b3002006-03-30 11:57:00 +00003006 object_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003007 0, /* tp_print */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00003008 0, /* tp_getattr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003009 0, /* tp_setattr */
3010 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003011 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003012 0, /* tp_as_number */
3013 0, /* tp_as_sequence */
3014 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003015 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003016 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003017 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003018 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00003019 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003020 0, /* tp_as_buffer */
3021 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003022 PyDoc_STR("The most base type"), /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003023 0, /* tp_traverse */
3024 0, /* tp_clear */
3025 0, /* tp_richcompare */
3026 0, /* tp_weaklistoffset */
3027 0, /* tp_iter */
3028 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00003029 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00003030 0, /* tp_members */
3031 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003032 0, /* tp_base */
3033 0, /* tp_dict */
3034 0, /* tp_descr_get */
3035 0, /* tp_descr_set */
3036 0, /* tp_dictoffset */
3037 object_init, /* tp_init */
3038 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossum298e4212003-02-13 16:30:16 +00003039 object_new, /* tp_new */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00003040 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003041};
3042
3043
3044/* Initialize the __dict__ in a type object */
3045
3046static int
3047add_methods(PyTypeObject *type, PyMethodDef *meth)
3048{
Guido van Rossum687ae002001-10-15 22:03:32 +00003049 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003050
3051 for (; meth->ml_name != NULL; meth++) {
3052 PyObject *descr;
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003053 if (PyDict_GetItemString(dict, meth->ml_name) &&
3054 !(meth->ml_flags & METH_COEXIST))
3055 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00003056 if (meth->ml_flags & METH_CLASS) {
3057 if (meth->ml_flags & METH_STATIC) {
3058 PyErr_SetString(PyExc_ValueError,
3059 "method cannot be both class and static");
3060 return -1;
3061 }
Tim Petersbca1cbc2002-12-09 22:56:13 +00003062 descr = PyDescr_NewClassMethod(type, meth);
Fred Drake7bf97152002-03-28 05:33:33 +00003063 }
3064 else if (meth->ml_flags & METH_STATIC) {
Guido van Rossum9af48ff2003-02-11 17:12:46 +00003065 PyObject *cfunc = PyCFunction_New(meth, NULL);
3066 if (cfunc == NULL)
3067 return -1;
3068 descr = PyStaticMethod_New(cfunc);
3069 Py_DECREF(cfunc);
Fred Drake7bf97152002-03-28 05:33:33 +00003070 }
3071 else {
3072 descr = PyDescr_NewMethod(type, meth);
3073 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003074 if (descr == NULL)
3075 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00003076 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003077 return -1;
3078 Py_DECREF(descr);
3079 }
3080 return 0;
3081}
3082
3083static int
Guido van Rossum6f799372001-09-20 20:46:19 +00003084add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003085{
Guido van Rossum687ae002001-10-15 22:03:32 +00003086 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003087
3088 for (; memb->name != NULL; memb++) {
3089 PyObject *descr;
3090 if (PyDict_GetItemString(dict, memb->name))
3091 continue;
3092 descr = PyDescr_NewMember(type, memb);
3093 if (descr == NULL)
3094 return -1;
3095 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
3096 return -1;
3097 Py_DECREF(descr);
3098 }
3099 return 0;
3100}
3101
3102static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00003103add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003104{
Guido van Rossum687ae002001-10-15 22:03:32 +00003105 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003106
3107 for (; gsp->name != NULL; gsp++) {
3108 PyObject *descr;
3109 if (PyDict_GetItemString(dict, gsp->name))
3110 continue;
3111 descr = PyDescr_NewGetSet(type, gsp);
3112
3113 if (descr == NULL)
3114 return -1;
3115 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
3116 return -1;
3117 Py_DECREF(descr);
3118 }
3119 return 0;
3120}
3121
Guido van Rossum13d52f02001-08-10 21:24:08 +00003122static void
3123inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003124{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003125 Py_ssize_t oldsize, newsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003126
Guido van Rossum13d52f02001-08-10 21:24:08 +00003127 /* Special flag magic */
3128 if (!type->tp_as_buffer && base->tp_as_buffer) {
3129 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
3130 type->tp_flags |=
3131 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
3132 }
3133 if (!type->tp_as_sequence && base->tp_as_sequence) {
3134 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
3135 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
3136 }
3137 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
3138 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
3139 if ((!type->tp_as_number && base->tp_as_number) ||
3140 (!type->tp_as_sequence && base->tp_as_sequence)) {
3141 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
3142 if (!type->tp_as_number && !type->tp_as_sequence) {
3143 type->tp_flags |= base->tp_flags &
3144 Py_TPFLAGS_HAVE_INPLACEOPS;
3145 }
3146 }
3147 /* Wow */
3148 }
3149 if (!type->tp_as_number && base->tp_as_number) {
3150 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
3151 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
3152 }
3153
3154 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00003155 oldsize = base->tp_basicsize;
3156 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
3157 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3158 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00003159 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
3160 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00003161 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003162 if (type->tp_traverse == NULL)
3163 type->tp_traverse = base->tp_traverse;
3164 if (type->tp_clear == NULL)
3165 type->tp_clear = base->tp_clear;
3166 }
3167 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00003168 /* The condition below could use some explanation.
3169 It appears that tp_new is not inherited for static types
3170 whose base class is 'object'; this seems to be a precaution
3171 so that old extension types don't suddenly become
3172 callable (object.__new__ wouldn't insure the invariants
3173 that the extension type's own factory function ensures).
3174 Heap types, of course, are under our control, so they do
3175 inherit tp_new; static extension types that specify some
3176 other built-in type as the default are considered
3177 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00003178 if (base != &PyBaseObject_Type ||
3179 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3180 if (type->tp_new == NULL)
3181 type->tp_new = base->tp_new;
3182 }
3183 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00003184 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00003185
3186 /* Copy other non-function slots */
3187
3188#undef COPYVAL
3189#define COPYVAL(SLOT) \
3190 if (type->SLOT == 0) type->SLOT = base->SLOT
3191
3192 COPYVAL(tp_itemsize);
3193 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
3194 COPYVAL(tp_weaklistoffset);
3195 }
3196 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3197 COPYVAL(tp_dictoffset);
3198 }
Neal Norwitzee3a1b52007-02-25 19:44:48 +00003199
3200 /* Setup fast subclass flags */
3201 if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
3202 type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
3203 else if (PyType_IsSubtype(base, &PyType_Type))
3204 type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
3205 else if (PyType_IsSubtype(base, &PyInt_Type))
3206 type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS;
3207 else if (PyType_IsSubtype(base, &PyLong_Type))
3208 type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
3209 else if (PyType_IsSubtype(base, &PyString_Type))
3210 type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS;
3211 else if (PyType_IsSubtype(base, &PyUnicode_Type))
3212 type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
3213 else if (PyType_IsSubtype(base, &PyTuple_Type))
3214 type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
3215 else if (PyType_IsSubtype(base, &PyList_Type))
3216 type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
3217 else if (PyType_IsSubtype(base, &PyDict_Type))
3218 type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003219}
3220
3221static void
3222inherit_slots(PyTypeObject *type, PyTypeObject *base)
3223{
3224 PyTypeObject *basebase;
3225
3226#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00003227#undef COPYSLOT
3228#undef COPYNUM
3229#undef COPYSEQ
3230#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00003231#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00003232
3233#define SLOTDEFINED(SLOT) \
3234 (base->SLOT != 0 && \
3235 (basebase == NULL || base->SLOT != basebase->SLOT))
3236
Tim Peters6d6c1a32001-08-02 04:15:00 +00003237#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00003238 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00003239
3240#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3241#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3242#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00003243#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003244
Guido van Rossum13d52f02001-08-10 21:24:08 +00003245 /* This won't inherit indirect slots (from tp_as_number etc.)
3246 if type doesn't provide the space. */
3247
3248 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3249 basebase = base->tp_base;
3250 if (basebase->tp_as_number == NULL)
3251 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003252 COPYNUM(nb_add);
3253 COPYNUM(nb_subtract);
3254 COPYNUM(nb_multiply);
3255 COPYNUM(nb_divide);
3256 COPYNUM(nb_remainder);
3257 COPYNUM(nb_divmod);
3258 COPYNUM(nb_power);
3259 COPYNUM(nb_negative);
3260 COPYNUM(nb_positive);
3261 COPYNUM(nb_absolute);
3262 COPYNUM(nb_nonzero);
3263 COPYNUM(nb_invert);
3264 COPYNUM(nb_lshift);
3265 COPYNUM(nb_rshift);
3266 COPYNUM(nb_and);
3267 COPYNUM(nb_xor);
3268 COPYNUM(nb_or);
3269 COPYNUM(nb_coerce);
3270 COPYNUM(nb_int);
3271 COPYNUM(nb_long);
3272 COPYNUM(nb_float);
3273 COPYNUM(nb_oct);
3274 COPYNUM(nb_hex);
3275 COPYNUM(nb_inplace_add);
3276 COPYNUM(nb_inplace_subtract);
3277 COPYNUM(nb_inplace_multiply);
3278 COPYNUM(nb_inplace_divide);
3279 COPYNUM(nb_inplace_remainder);
3280 COPYNUM(nb_inplace_power);
3281 COPYNUM(nb_inplace_lshift);
3282 COPYNUM(nb_inplace_rshift);
3283 COPYNUM(nb_inplace_and);
3284 COPYNUM(nb_inplace_xor);
3285 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003286 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3287 COPYNUM(nb_true_divide);
3288 COPYNUM(nb_floor_divide);
3289 COPYNUM(nb_inplace_true_divide);
3290 COPYNUM(nb_inplace_floor_divide);
3291 }
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003292 if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
3293 COPYNUM(nb_index);
3294 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003295 }
3296
Guido van Rossum13d52f02001-08-10 21:24:08 +00003297 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3298 basebase = base->tp_base;
3299 if (basebase->tp_as_sequence == NULL)
3300 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003301 COPYSEQ(sq_length);
3302 COPYSEQ(sq_concat);
3303 COPYSEQ(sq_repeat);
3304 COPYSEQ(sq_item);
3305 COPYSEQ(sq_slice);
3306 COPYSEQ(sq_ass_item);
3307 COPYSEQ(sq_ass_slice);
3308 COPYSEQ(sq_contains);
3309 COPYSEQ(sq_inplace_concat);
3310 COPYSEQ(sq_inplace_repeat);
3311 }
3312
Guido van Rossum13d52f02001-08-10 21:24:08 +00003313 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3314 basebase = base->tp_base;
3315 if (basebase->tp_as_mapping == NULL)
3316 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003317 COPYMAP(mp_length);
3318 COPYMAP(mp_subscript);
3319 COPYMAP(mp_ass_subscript);
3320 }
3321
Tim Petersfc57ccb2001-10-12 02:38:24 +00003322 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3323 basebase = base->tp_base;
3324 if (basebase->tp_as_buffer == NULL)
3325 basebase = NULL;
3326 COPYBUF(bf_getreadbuffer);
3327 COPYBUF(bf_getwritebuffer);
3328 COPYBUF(bf_getsegcount);
3329 COPYBUF(bf_getcharbuffer);
3330 }
3331
Guido van Rossum13d52f02001-08-10 21:24:08 +00003332 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003333
Tim Peters6d6c1a32001-08-02 04:15:00 +00003334 COPYSLOT(tp_dealloc);
3335 COPYSLOT(tp_print);
3336 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3337 type->tp_getattr = base->tp_getattr;
3338 type->tp_getattro = base->tp_getattro;
3339 }
3340 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3341 type->tp_setattr = base->tp_setattr;
3342 type->tp_setattro = base->tp_setattro;
3343 }
3344 /* tp_compare see tp_richcompare */
3345 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003346 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003347 COPYSLOT(tp_call);
3348 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003349 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003350 if (type->tp_compare == NULL &&
3351 type->tp_richcompare == NULL &&
3352 type->tp_hash == NULL)
3353 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003354 type->tp_compare = base->tp_compare;
3355 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003356 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003357 }
3358 }
3359 else {
3360 COPYSLOT(tp_compare);
3361 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003362 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3363 COPYSLOT(tp_iter);
3364 COPYSLOT(tp_iternext);
3365 }
3366 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3367 COPYSLOT(tp_descr_get);
3368 COPYSLOT(tp_descr_set);
3369 COPYSLOT(tp_dictoffset);
3370 COPYSLOT(tp_init);
3371 COPYSLOT(tp_alloc);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00003372 COPYSLOT(tp_is_gc);
Tim Peters3cfe7542003-05-21 21:29:48 +00003373 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3374 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3375 /* They agree about gc. */
3376 COPYSLOT(tp_free);
3377 }
3378 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3379 type->tp_free == NULL &&
3380 base->tp_free == _PyObject_Del) {
3381 /* A bit of magic to plug in the correct default
3382 * tp_free function when a derived class adds gc,
3383 * didn't define tp_free, and the base uses the
3384 * default non-gc tp_free.
3385 */
3386 type->tp_free = PyObject_GC_Del;
3387 }
3388 /* else they didn't agree about gc, and there isn't something
3389 * obvious to be done -- the type is on its own.
3390 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003391 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003392}
3393
Jeremy Hylton938ace62002-07-17 16:30:39 +00003394static int add_operators(PyTypeObject *);
Guido van Rossum13d52f02001-08-10 21:24:08 +00003395
Tim Peters6d6c1a32001-08-02 04:15:00 +00003396int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003397PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003398{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003399 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003400 PyTypeObject *base;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003401 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003402
Guido van Rossumcab05802002-06-10 15:29:03 +00003403 if (type->tp_flags & Py_TPFLAGS_READY) {
3404 assert(type->tp_dict != NULL);
Guido van Rossumd614f972001-08-10 17:39:49 +00003405 return 0;
Guido van Rossumcab05802002-06-10 15:29:03 +00003406 }
Guido van Rossumd614f972001-08-10 17:39:49 +00003407 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00003408
3409 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003410
Tim Peters36eb4df2003-03-23 03:33:13 +00003411#ifdef Py_TRACE_REFS
3412 /* PyType_Ready is the closest thing we have to a choke point
3413 * for type objects, so is the best place I can think of to try
3414 * to get type objects into the doubly-linked list of all objects.
3415 * Still, not all type objects go thru PyType_Ready.
3416 */
Tim Peters7571a0f2003-03-23 17:52:28 +00003417 _Py_AddToAllObjects((PyObject *)type, 0);
Tim Peters36eb4df2003-03-23 03:33:13 +00003418#endif
3419
Tim Peters6d6c1a32001-08-02 04:15:00 +00003420 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3421 base = type->tp_base;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003422 if (base == NULL && type != &PyBaseObject_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003423 base = type->tp_base = &PyBaseObject_Type;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003424 Py_INCREF(base);
3425 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003426
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00003427 /* Now the only way base can still be NULL is if type is
3428 * &PyBaseObject_Type.
3429 */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003430
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003431 /* Initialize the base class */
3432 if (base && base->tp_dict == NULL) {
3433 if (PyType_Ready(base) < 0)
3434 goto error;
3435 }
3436
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00003437 /* Initialize ob_type if NULL. This means extensions that want to be
Guido van Rossum0986d822002-04-08 01:38:42 +00003438 compilable separately on Windows can call PyType_Ready() instead of
3439 initializing the ob_type field of their type objects. */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00003440 /* The test for base != NULL is really unnecessary, since base is only
3441 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3442 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3443 know that. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003444 if (type->ob_type == NULL && base != NULL)
Guido van Rossum0986d822002-04-08 01:38:42 +00003445 type->ob_type = base->ob_type;
3446
Tim Peters6d6c1a32001-08-02 04:15:00 +00003447 /* Initialize tp_bases */
3448 bases = type->tp_bases;
3449 if (bases == NULL) {
3450 if (base == NULL)
3451 bases = PyTuple_New(0);
3452 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003453 bases = PyTuple_Pack(1, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003454 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003455 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003456 type->tp_bases = bases;
3457 }
3458
Guido van Rossum687ae002001-10-15 22:03:32 +00003459 /* Initialize tp_dict */
3460 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003461 if (dict == NULL) {
3462 dict = PyDict_New();
3463 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003464 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00003465 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003466 }
3467
Guido van Rossum687ae002001-10-15 22:03:32 +00003468 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003469 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003470 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003471 if (type->tp_methods != NULL) {
3472 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003473 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003474 }
3475 if (type->tp_members != NULL) {
3476 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003477 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003478 }
3479 if (type->tp_getset != NULL) {
3480 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003481 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003482 }
3483
Tim Peters6d6c1a32001-08-02 04:15:00 +00003484 /* Calculate method resolution order */
3485 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00003486 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003487 }
3488
Guido van Rossum13d52f02001-08-10 21:24:08 +00003489 /* Inherit special flags from dominant base */
3490 if (type->tp_base != NULL)
3491 inherit_special(type, type->tp_base);
3492
Tim Peters6d6c1a32001-08-02 04:15:00 +00003493 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003494 bases = type->tp_mro;
3495 assert(bases != NULL);
3496 assert(PyTuple_Check(bases));
3497 n = PyTuple_GET_SIZE(bases);
3498 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003499 PyObject *b = PyTuple_GET_ITEM(bases, i);
3500 if (PyType_Check(b))
3501 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003502 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003503
Tim Peters3cfe7542003-05-21 21:29:48 +00003504 /* Sanity check for tp_free. */
3505 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3506 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00003507 /* This base class needs to call tp_free, but doesn't have
3508 * one, or its tp_free is for non-gc'ed objects.
3509 */
Tim Peters3cfe7542003-05-21 21:29:48 +00003510 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3511 "gc and is a base type but has inappropriate "
3512 "tp_free slot",
3513 type->tp_name);
3514 goto error;
3515 }
3516
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003517 /* if the type dictionary doesn't contain a __doc__, set it from
3518 the tp_doc slot.
3519 */
3520 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3521 if (type->tp_doc != NULL) {
3522 PyObject *doc = PyString_FromString(type->tp_doc);
Neal Norwitze1fdb322006-07-21 05:32:28 +00003523 if (doc == NULL)
3524 goto error;
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003525 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3526 Py_DECREF(doc);
3527 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00003528 PyDict_SetItemString(type->tp_dict,
3529 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003530 }
3531 }
3532
Guido van Rossum13d52f02001-08-10 21:24:08 +00003533 /* Some more special stuff */
3534 base = type->tp_base;
3535 if (base != NULL) {
3536 if (type->tp_as_number == NULL)
3537 type->tp_as_number = base->tp_as_number;
3538 if (type->tp_as_sequence == NULL)
3539 type->tp_as_sequence = base->tp_as_sequence;
3540 if (type->tp_as_mapping == NULL)
3541 type->tp_as_mapping = base->tp_as_mapping;
Guido van Rossumeea47182003-02-11 20:39:59 +00003542 if (type->tp_as_buffer == NULL)
3543 type->tp_as_buffer = base->tp_as_buffer;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003544 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003545
Guido van Rossum1c450732001-10-08 15:18:27 +00003546 /* Link into each base class's list of subclasses */
3547 bases = type->tp_bases;
3548 n = PyTuple_GET_SIZE(bases);
3549 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003550 PyObject *b = PyTuple_GET_ITEM(bases, i);
3551 if (PyType_Check(b) &&
3552 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00003553 goto error;
3554 }
3555
Guido van Rossum13d52f02001-08-10 21:24:08 +00003556 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00003557 assert(type->tp_dict != NULL);
3558 type->tp_flags =
3559 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003560 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00003561
3562 error:
3563 type->tp_flags &= ~Py_TPFLAGS_READYING;
3564 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003565}
3566
Guido van Rossum1c450732001-10-08 15:18:27 +00003567static int
3568add_subclass(PyTypeObject *base, PyTypeObject *type)
3569{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003570 Py_ssize_t i;
3571 int result;
Anthony Baxtera6286212006-04-11 07:42:36 +00003572 PyObject *list, *ref, *newobj;
Guido van Rossum1c450732001-10-08 15:18:27 +00003573
3574 list = base->tp_subclasses;
3575 if (list == NULL) {
3576 base->tp_subclasses = list = PyList_New(0);
3577 if (list == NULL)
3578 return -1;
3579 }
3580 assert(PyList_Check(list));
Anthony Baxtera6286212006-04-11 07:42:36 +00003581 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
Guido van Rossum1c450732001-10-08 15:18:27 +00003582 i = PyList_GET_SIZE(list);
3583 while (--i >= 0) {
3584 ref = PyList_GET_ITEM(list, i);
3585 assert(PyWeakref_CheckRef(ref));
Guido van Rossum3930bc32002-10-18 13:51:49 +00003586 if (PyWeakref_GET_OBJECT(ref) == Py_None)
Anthony Baxtera6286212006-04-11 07:42:36 +00003587 return PyList_SetItem(list, i, newobj);
Guido van Rossum1c450732001-10-08 15:18:27 +00003588 }
Anthony Baxtera6286212006-04-11 07:42:36 +00003589 result = PyList_Append(list, newobj);
3590 Py_DECREF(newobj);
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003591 return result;
Guido van Rossum1c450732001-10-08 15:18:27 +00003592}
3593
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003594static void
3595remove_subclass(PyTypeObject *base, PyTypeObject *type)
3596{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003597 Py_ssize_t i;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003598 PyObject *list, *ref;
3599
3600 list = base->tp_subclasses;
3601 if (list == NULL) {
3602 return;
3603 }
3604 assert(PyList_Check(list));
3605 i = PyList_GET_SIZE(list);
3606 while (--i >= 0) {
3607 ref = PyList_GET_ITEM(list, i);
3608 assert(PyWeakref_CheckRef(ref));
3609 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3610 /* this can't fail, right? */
3611 PySequence_DelItem(list, i);
3612 return;
3613 }
3614 }
3615}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003616
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003617static int
3618check_num_args(PyObject *ob, int n)
3619{
3620 if (!PyTuple_CheckExact(ob)) {
3621 PyErr_SetString(PyExc_SystemError,
3622 "PyArg_UnpackTuple() argument list is not a tuple");
3623 return 0;
3624 }
3625 if (n == PyTuple_GET_SIZE(ob))
3626 return 1;
3627 PyErr_Format(
3628 PyExc_TypeError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00003629 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003630 return 0;
3631}
3632
Tim Peters6d6c1a32001-08-02 04:15:00 +00003633/* Generic wrappers for overloadable 'operators' such as __getitem__ */
3634
3635/* There's a wrapper *function* for each distinct function typedef used
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00003636 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
Tim Peters6d6c1a32001-08-02 04:15:00 +00003637 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3638 Most tables have only one entry; the tables for binary operators have two
3639 entries, one regular and one with reversed arguments. */
3640
3641static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003642wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003643{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003644 lenfunc func = (lenfunc)wrapped;
3645 Py_ssize_t res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003646
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003647 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003648 return NULL;
3649 res = (*func)(self);
3650 if (res == -1 && PyErr_Occurred())
3651 return NULL;
3652 return PyInt_FromLong((long)res);
3653}
3654
Tim Peters6d6c1a32001-08-02 04:15:00 +00003655static PyObject *
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003656wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3657{
3658 inquiry func = (inquiry)wrapped;
3659 int res;
3660
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003661 if (!check_num_args(args, 0))
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003662 return NULL;
3663 res = (*func)(self);
3664 if (res == -1 && PyErr_Occurred())
3665 return NULL;
3666 return PyBool_FromLong((long)res);
3667}
3668
3669static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003670wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3671{
3672 binaryfunc func = (binaryfunc)wrapped;
3673 PyObject *other;
3674
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003675 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003676 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003677 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003678 return (*func)(self, other);
3679}
3680
3681static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003682wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3683{
3684 binaryfunc func = (binaryfunc)wrapped;
3685 PyObject *other;
3686
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003687 if (!check_num_args(args, 1))
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003688 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003689 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003690 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003691 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003692 Py_INCREF(Py_NotImplemented);
3693 return Py_NotImplemented;
3694 }
3695 return (*func)(self, other);
3696}
3697
3698static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003699wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3700{
3701 binaryfunc func = (binaryfunc)wrapped;
3702 PyObject *other;
3703
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003704 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003705 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003706 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003707 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003708 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003709 Py_INCREF(Py_NotImplemented);
3710 return Py_NotImplemented;
3711 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003712 return (*func)(other, self);
3713}
3714
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003715static PyObject *
3716wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3717{
3718 coercion func = (coercion)wrapped;
3719 PyObject *other, *res;
3720 int ok;
3721
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003722 if (!check_num_args(args, 1))
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003723 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003724 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003725 ok = func(&self, &other);
3726 if (ok < 0)
3727 return NULL;
3728 if (ok > 0) {
3729 Py_INCREF(Py_NotImplemented);
3730 return Py_NotImplemented;
3731 }
3732 res = PyTuple_New(2);
3733 if (res == NULL) {
3734 Py_DECREF(self);
3735 Py_DECREF(other);
3736 return NULL;
3737 }
3738 PyTuple_SET_ITEM(res, 0, self);
3739 PyTuple_SET_ITEM(res, 1, other);
3740 return res;
3741}
3742
Tim Peters6d6c1a32001-08-02 04:15:00 +00003743static PyObject *
3744wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3745{
3746 ternaryfunc func = (ternaryfunc)wrapped;
3747 PyObject *other;
3748 PyObject *third = Py_None;
3749
3750 /* Note: This wrapper only works for __pow__() */
3751
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003752 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003753 return NULL;
3754 return (*func)(self, other, third);
3755}
3756
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00003757static PyObject *
3758wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3759{
3760 ternaryfunc func = (ternaryfunc)wrapped;
3761 PyObject *other;
3762 PyObject *third = Py_None;
3763
3764 /* Note: This wrapper only works for __pow__() */
3765
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003766 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00003767 return NULL;
3768 return (*func)(other, self, third);
3769}
3770
Tim Peters6d6c1a32001-08-02 04:15:00 +00003771static PyObject *
3772wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3773{
3774 unaryfunc func = (unaryfunc)wrapped;
3775
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003776 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777 return NULL;
3778 return (*func)(self);
3779}
3780
Tim Peters6d6c1a32001-08-02 04:15:00 +00003781static PyObject *
Armin Rigo314861c2006-03-30 14:04:02 +00003782wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003783{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003784 ssizeargfunc func = (ssizeargfunc)wrapped;
Armin Rigo314861c2006-03-30 14:04:02 +00003785 PyObject* o;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003786 Py_ssize_t i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003787
Armin Rigo314861c2006-03-30 14:04:02 +00003788 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
3789 return NULL;
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003790 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
Armin Rigo314861c2006-03-30 14:04:02 +00003791 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00003792 return NULL;
3793 return (*func)(self, i);
3794}
3795
Martin v. Löwis18e16552006-02-15 17:27:45 +00003796static Py_ssize_t
Guido van Rossum5d815f32001-08-17 21:57:47 +00003797getindex(PyObject *self, PyObject *arg)
3798{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003799 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003800
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00003801 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003802 if (i == -1 && PyErr_Occurred())
3803 return -1;
3804 if (i < 0) {
3805 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3806 if (sq && sq->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00003807 Py_ssize_t n = (*sq->sq_length)(self);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003808 if (n < 0)
3809 return -1;
3810 i += n;
3811 }
3812 }
3813 return i;
3814}
3815
3816static PyObject *
3817wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3818{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003819 ssizeargfunc func = (ssizeargfunc)wrapped;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003820 PyObject *arg;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003821 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003822
Guido van Rossumf4593e02001-10-03 12:09:30 +00003823 if (PyTuple_GET_SIZE(args) == 1) {
3824 arg = PyTuple_GET_ITEM(args, 0);
3825 i = getindex(self, arg);
3826 if (i == -1 && PyErr_Occurred())
3827 return NULL;
3828 return (*func)(self, i);
3829 }
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003830 check_num_args(args, 1);
Guido van Rossumf4593e02001-10-03 12:09:30 +00003831 assert(PyErr_Occurred());
3832 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003833}
3834
Tim Peters6d6c1a32001-08-02 04:15:00 +00003835static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003836wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003837{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003838 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
3839 Py_ssize_t i, j;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003840
Martin v. Löwis18e16552006-02-15 17:27:45 +00003841 if (!PyArg_ParseTuple(args, "nn", &i, &j))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003842 return NULL;
3843 return (*func)(self, i, j);
3844}
3845
Tim Peters6d6c1a32001-08-02 04:15:00 +00003846static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00003847wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003848{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003849 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3850 Py_ssize_t i;
3851 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003852 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003853
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003854 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
Guido van Rossum5d815f32001-08-17 21:57:47 +00003855 return NULL;
3856 i = getindex(self, arg);
3857 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00003858 return NULL;
3859 res = (*func)(self, i, value);
3860 if (res == -1 && PyErr_Occurred())
3861 return NULL;
3862 Py_INCREF(Py_None);
3863 return Py_None;
3864}
3865
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003866static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00003867wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003868{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003869 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3870 Py_ssize_t i;
3871 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003872 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003873
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003874 if (!check_num_args(args, 1))
Guido van Rossum5d815f32001-08-17 21:57:47 +00003875 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003876 arg = PyTuple_GET_ITEM(args, 0);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003877 i = getindex(self, arg);
3878 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003879 return NULL;
3880 res = (*func)(self, i, NULL);
3881 if (res == -1 && PyErr_Occurred())
3882 return NULL;
3883 Py_INCREF(Py_None);
3884 return Py_None;
3885}
3886
Tim Peters6d6c1a32001-08-02 04:15:00 +00003887static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003888wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003889{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003890 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3891 Py_ssize_t i, j;
3892 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003893 PyObject *value;
3894
Martin v. Löwis18e16552006-02-15 17:27:45 +00003895 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003896 return NULL;
3897 res = (*func)(self, i, j, value);
3898 if (res == -1 && PyErr_Occurred())
3899 return NULL;
3900 Py_INCREF(Py_None);
3901 return Py_None;
3902}
3903
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003904static PyObject *
3905wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3906{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003907 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3908 Py_ssize_t i, j;
3909 int res;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003910
Martin v. Löwis18e16552006-02-15 17:27:45 +00003911 if (!PyArg_ParseTuple(args, "nn", &i, &j))
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003912 return NULL;
3913 res = (*func)(self, i, j, NULL);
3914 if (res == -1 && PyErr_Occurred())
3915 return NULL;
3916 Py_INCREF(Py_None);
3917 return Py_None;
3918}
3919
Tim Peters6d6c1a32001-08-02 04:15:00 +00003920/* XXX objobjproc is a misnomer; should be objargpred */
3921static PyObject *
3922wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3923{
3924 objobjproc func = (objobjproc)wrapped;
3925 int res;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00003926 PyObject *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003927
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003928 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003929 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003930 value = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003931 res = (*func)(self, value);
3932 if (res == -1 && PyErr_Occurred())
3933 return NULL;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00003934 else
3935 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003936}
3937
Tim Peters6d6c1a32001-08-02 04:15:00 +00003938static PyObject *
3939wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3940{
3941 objobjargproc func = (objobjargproc)wrapped;
3942 int res;
3943 PyObject *key, *value;
3944
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003945 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003946 return NULL;
3947 res = (*func)(self, key, value);
3948 if (res == -1 && PyErr_Occurred())
3949 return NULL;
3950 Py_INCREF(Py_None);
3951 return Py_None;
3952}
3953
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003954static PyObject *
3955wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3956{
3957 objobjargproc func = (objobjargproc)wrapped;
3958 int res;
3959 PyObject *key;
3960
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003961 if (!check_num_args(args, 1))
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003962 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003963 key = PyTuple_GET_ITEM(args, 0);
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003964 res = (*func)(self, key, NULL);
3965 if (res == -1 && PyErr_Occurred())
3966 return NULL;
3967 Py_INCREF(Py_None);
3968 return Py_None;
3969}
3970
Tim Peters6d6c1a32001-08-02 04:15:00 +00003971static PyObject *
3972wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3973{
3974 cmpfunc func = (cmpfunc)wrapped;
3975 int res;
3976 PyObject *other;
3977
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003978 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003979 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003980 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00003981 if (other->ob_type->tp_compare != func &&
3982 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00003983 PyErr_Format(
3984 PyExc_TypeError,
3985 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3986 self->ob_type->tp_name,
3987 self->ob_type->tp_name,
3988 other->ob_type->tp_name);
3989 return NULL;
3990 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003991 res = (*func)(self, other);
3992 if (PyErr_Occurred())
3993 return NULL;
3994 return PyInt_FromLong((long)res);
3995}
3996
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003997/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
Guido van Rossum52b27052003-04-15 20:05:10 +00003998 This is called the Carlo Verre hack after its discoverer. */
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003999static int
4000hackcheck(PyObject *self, setattrofunc func, char *what)
4001{
4002 PyTypeObject *type = self->ob_type;
4003 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
4004 type = type->tp_base;
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00004005 /* If type is NULL now, this is a really weird type.
4006 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00004007 if (type && type->tp_setattro != func) {
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004008 PyErr_Format(PyExc_TypeError,
4009 "can't apply this %s to %s object",
4010 what,
4011 type->tp_name);
4012 return 0;
4013 }
4014 return 1;
4015}
4016
Tim Peters6d6c1a32001-08-02 04:15:00 +00004017static PyObject *
4018wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
4019{
4020 setattrofunc func = (setattrofunc)wrapped;
4021 int res;
4022 PyObject *name, *value;
4023
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004024 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004025 return NULL;
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004026 if (!hackcheck(self, func, "__setattr__"))
4027 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004028 res = (*func)(self, name, value);
4029 if (res < 0)
4030 return NULL;
4031 Py_INCREF(Py_None);
4032 return Py_None;
4033}
4034
4035static PyObject *
4036wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
4037{
4038 setattrofunc func = (setattrofunc)wrapped;
4039 int res;
4040 PyObject *name;
4041
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004042 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004043 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004044 name = PyTuple_GET_ITEM(args, 0);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00004045 if (!hackcheck(self, func, "__delattr__"))
4046 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004047 res = (*func)(self, name, NULL);
4048 if (res < 0)
4049 return NULL;
4050 Py_INCREF(Py_None);
4051 return Py_None;
4052}
4053
Tim Peters6d6c1a32001-08-02 04:15:00 +00004054static PyObject *
4055wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
4056{
4057 hashfunc func = (hashfunc)wrapped;
4058 long res;
4059
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004060 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004061 return NULL;
4062 res = (*func)(self);
4063 if (res == -1 && PyErr_Occurred())
4064 return NULL;
4065 return PyInt_FromLong(res);
4066}
4067
Tim Peters6d6c1a32001-08-02 04:15:00 +00004068static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00004069wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004070{
4071 ternaryfunc func = (ternaryfunc)wrapped;
4072
Guido van Rossumc8e56452001-10-22 00:43:43 +00004073 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004074}
4075
Tim Peters6d6c1a32001-08-02 04:15:00 +00004076static PyObject *
4077wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
4078{
4079 richcmpfunc func = (richcmpfunc)wrapped;
4080 PyObject *other;
4081
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004082 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004083 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004084 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004085 return (*func)(self, other, op);
4086}
4087
4088#undef RICHCMP_WRAPPER
4089#define RICHCMP_WRAPPER(NAME, OP) \
4090static PyObject * \
4091richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4092{ \
4093 return wrap_richcmpfunc(self, args, wrapped, OP); \
4094}
4095
Jack Jansen8e938b42001-08-08 15:29:49 +00004096RICHCMP_WRAPPER(lt, Py_LT)
4097RICHCMP_WRAPPER(le, Py_LE)
4098RICHCMP_WRAPPER(eq, Py_EQ)
4099RICHCMP_WRAPPER(ne, Py_NE)
4100RICHCMP_WRAPPER(gt, Py_GT)
4101RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004102
Tim Peters6d6c1a32001-08-02 04:15:00 +00004103static PyObject *
4104wrap_next(PyObject *self, PyObject *args, void *wrapped)
4105{
4106 unaryfunc func = (unaryfunc)wrapped;
4107 PyObject *res;
4108
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004109 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004110 return NULL;
4111 res = (*func)(self);
4112 if (res == NULL && !PyErr_Occurred())
4113 PyErr_SetNone(PyExc_StopIteration);
4114 return res;
4115}
4116
Tim Peters6d6c1a32001-08-02 04:15:00 +00004117static PyObject *
4118wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
4119{
4120 descrgetfunc func = (descrgetfunc)wrapped;
4121 PyObject *obj;
4122 PyObject *type = NULL;
4123
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004124 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004125 return NULL;
Guido van Rossum82ed25c2003-02-11 16:25:43 +00004126 if (obj == Py_None)
4127 obj = NULL;
4128 if (type == Py_None)
4129 type = NULL;
4130 if (type == NULL &&obj == NULL) {
4131 PyErr_SetString(PyExc_TypeError,
4132 "__get__(None, None) is invalid");
4133 return NULL;
4134 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004135 return (*func)(self, obj, type);
4136}
4137
Tim Peters6d6c1a32001-08-02 04:15:00 +00004138static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004139wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004140{
4141 descrsetfunc func = (descrsetfunc)wrapped;
4142 PyObject *obj, *value;
4143 int ret;
4144
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00004145 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00004146 return NULL;
4147 ret = (*func)(self, obj, value);
4148 if (ret < 0)
4149 return NULL;
4150 Py_INCREF(Py_None);
4151 return Py_None;
4152}
Guido van Rossum22b13872002-08-06 21:41:44 +00004153
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004154static PyObject *
4155wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
4156{
4157 descrsetfunc func = (descrsetfunc)wrapped;
4158 PyObject *obj;
4159 int ret;
4160
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004161 if (!check_num_args(args, 1))
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004162 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00004163 obj = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004164 ret = (*func)(self, obj, NULL);
4165 if (ret < 0)
4166 return NULL;
4167 Py_INCREF(Py_None);
4168 return Py_None;
4169}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004170
Tim Peters6d6c1a32001-08-02 04:15:00 +00004171static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00004172wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004173{
4174 initproc func = (initproc)wrapped;
4175
Guido van Rossumc8e56452001-10-22 00:43:43 +00004176 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004177 return NULL;
4178 Py_INCREF(Py_None);
4179 return Py_None;
4180}
4181
Tim Peters6d6c1a32001-08-02 04:15:00 +00004182static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004183tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004184{
Barry Warsaw60f01882001-08-22 19:24:42 +00004185 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004186 PyObject *arg0, *res;
4187
4188 if (self == NULL || !PyType_Check(self))
4189 Py_FatalError("__new__() called with non-type 'self'");
4190 type = (PyTypeObject *)self;
4191 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004192 PyErr_Format(PyExc_TypeError,
4193 "%s.__new__(): not enough arguments",
4194 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004195 return NULL;
4196 }
4197 arg0 = PyTuple_GET_ITEM(args, 0);
4198 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004199 PyErr_Format(PyExc_TypeError,
4200 "%s.__new__(X): X is not a type object (%s)",
4201 type->tp_name,
4202 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004203 return NULL;
4204 }
4205 subtype = (PyTypeObject *)arg0;
4206 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004207 PyErr_Format(PyExc_TypeError,
4208 "%s.__new__(%s): %s is not a subtype of %s",
4209 type->tp_name,
4210 subtype->tp_name,
4211 subtype->tp_name,
4212 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004213 return NULL;
4214 }
Barry Warsaw60f01882001-08-22 19:24:42 +00004215
4216 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00004217 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00004218 most derived base that's not a heap type is this type. */
4219 staticbase = subtype;
4220 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4221 staticbase = staticbase->tp_base;
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00004222 /* If staticbase is NULL now, it is a really weird type.
4223 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00004224 if (staticbase && staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00004225 PyErr_Format(PyExc_TypeError,
4226 "%s.__new__(%s) is not safe, use %s.__new__()",
4227 type->tp_name,
4228 subtype->tp_name,
4229 staticbase == NULL ? "?" : staticbase->tp_name);
4230 return NULL;
4231 }
4232
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004233 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4234 if (args == NULL)
4235 return NULL;
4236 res = type->tp_new(subtype, args, kwds);
4237 Py_DECREF(args);
4238 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004239}
4240
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004241static struct PyMethodDef tp_new_methoddef[] = {
4242 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00004243 PyDoc_STR("T.__new__(S, ...) -> "
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00004244 "a new object with type S, a subtype of T")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00004245 {0}
4246};
4247
4248static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004249add_tp_new_wrapper(PyTypeObject *type)
4250{
Guido van Rossumf040ede2001-08-07 16:40:56 +00004251 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004252
Guido van Rossum687ae002001-10-15 22:03:32 +00004253 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00004254 return 0;
4255 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004256 if (func == NULL)
4257 return -1;
Raymond Hettinger8d726ee2004-06-25 22:24:35 +00004258 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
Raymond Hettingerd56cbe52004-06-25 22:17:39 +00004259 Py_DECREF(func);
4260 return -1;
4261 }
4262 Py_DECREF(func);
4263 return 0;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00004264}
4265
Guido van Rossumf040ede2001-08-07 16:40:56 +00004266/* Slot wrappers that call the corresponding __foo__ slot. See comments
4267 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004268
Guido van Rossumdc91b992001-08-08 22:26:22 +00004269#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004270static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004271FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004272{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00004273 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004274 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004275}
4276
Guido van Rossumdc91b992001-08-08 22:26:22 +00004277#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004278static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004279FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004280{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004281 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004282 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004283}
4284
Guido van Rossumcd118802003-01-06 22:57:47 +00004285/* Boolean helper for SLOT1BINFULL().
4286 right.__class__ is a nontrivial subclass of left.__class__. */
4287static int
4288method_is_overloaded(PyObject *left, PyObject *right, char *name)
4289{
4290 PyObject *a, *b;
4291 int ok;
4292
4293 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
4294 if (b == NULL) {
4295 PyErr_Clear();
4296 /* If right doesn't have it, it's not overloaded */
4297 return 0;
4298 }
4299
4300 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
4301 if (a == NULL) {
4302 PyErr_Clear();
4303 Py_DECREF(b);
4304 /* If right has it but left doesn't, it's overloaded */
4305 return 1;
4306 }
4307
4308 ok = PyObject_RichCompareBool(a, b, Py_NE);
4309 Py_DECREF(a);
4310 Py_DECREF(b);
4311 if (ok < 0) {
4312 PyErr_Clear();
4313 return 0;
4314 }
4315
4316 return ok;
4317}
4318
Guido van Rossumdc91b992001-08-08 22:26:22 +00004319
4320#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004321static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004322FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004323{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004324 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00004325 int do_other = self->ob_type != other->ob_type && \
4326 other->ob_type->tp_as_number != NULL && \
4327 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004328 if (self->ob_type->tp_as_number != NULL && \
4329 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4330 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00004331 if (do_other && \
Guido van Rossumcd118802003-01-06 22:57:47 +00004332 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4333 method_is_overloaded(self, other, ROPSTR)) { \
Guido van Rossum55f20992001-10-01 17:18:22 +00004334 r = call_maybe( \
4335 other, ROPSTR, &rcache_str, "(O)", self); \
4336 if (r != Py_NotImplemented) \
4337 return r; \
4338 Py_DECREF(r); \
4339 do_other = 0; \
4340 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004341 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004342 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004343 if (r != Py_NotImplemented || \
4344 other->ob_type == self->ob_type) \
4345 return r; \
4346 Py_DECREF(r); \
4347 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00004348 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004349 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004350 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004351 } \
4352 Py_INCREF(Py_NotImplemented); \
4353 return Py_NotImplemented; \
4354}
4355
4356#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4357 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4358
4359#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4360static PyObject * \
4361FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4362{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004363 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004364 return call_method(self, OPSTR, &cache_str, \
4365 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004366}
4367
Martin v. Löwis18e16552006-02-15 17:27:45 +00004368static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +00004369slot_sq_length(PyObject *self)
4370{
Guido van Rossum2730b132001-08-28 18:22:14 +00004371 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004372 PyObject *res = call_method(self, "__len__", &len_str, "()");
Martin v. Löwis18e16552006-02-15 17:27:45 +00004373 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004374
4375 if (res == NULL)
4376 return -1;
Neal Norwitz1872b1c2006-08-12 18:44:06 +00004377 len = PyInt_AsSsize_t(res);
Guido van Rossum26111622001-10-01 16:42:49 +00004378 Py_DECREF(res);
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004379 if (len < 0) {
Armin Rigo7ccbca92006-10-04 12:17:45 +00004380 if (!PyErr_Occurred())
4381 PyErr_SetString(PyExc_ValueError,
4382 "__len__() should return >= 0");
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004383 return -1;
4384 }
Guido van Rossum26111622001-10-01 16:42:49 +00004385 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004386}
4387
Guido van Rossumf4593e02001-10-03 12:09:30 +00004388/* Super-optimized version of slot_sq_item.
4389 Other slots could do the same... */
4390static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00004391slot_sq_item(PyObject *self, Py_ssize_t i)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004392{
4393 static PyObject *getitem_str;
4394 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4395 descrgetfunc f;
4396
4397 if (getitem_str == NULL) {
4398 getitem_str = PyString_InternFromString("__getitem__");
4399 if (getitem_str == NULL)
4400 return NULL;
4401 }
4402 func = _PyType_Lookup(self->ob_type, getitem_str);
4403 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00004404 if ((f = func->ob_type->tp_descr_get) == NULL)
4405 Py_INCREF(func);
Neal Norwitz673cd822002-10-18 16:33:13 +00004406 else {
Guido van Rossumf4593e02001-10-03 12:09:30 +00004407 func = f(func, self, (PyObject *)(self->ob_type));
Neal Norwitz673cd822002-10-18 16:33:13 +00004408 if (func == NULL) {
4409 return NULL;
4410 }
4411 }
Martin v. Löwiseb079f12006-02-16 14:32:27 +00004412 ival = PyInt_FromSsize_t(i);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004413 if (ival != NULL) {
4414 args = PyTuple_New(1);
4415 if (args != NULL) {
4416 PyTuple_SET_ITEM(args, 0, ival);
4417 retval = PyObject_Call(func, args, NULL);
4418 Py_XDECREF(args);
4419 Py_XDECREF(func);
4420 return retval;
4421 }
4422 }
4423 }
4424 else {
4425 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4426 }
4427 Py_XDECREF(args);
4428 Py_XDECREF(ival);
4429 Py_XDECREF(func);
4430 return NULL;
4431}
4432
Martin v. Löwis18e16552006-02-15 17:27:45 +00004433SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004434
4435static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004436slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004437{
4438 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004439 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004440
4441 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004442 res = call_method(self, "__delitem__", &delitem_str,
Thomas Wouters4e908102006-04-21 11:26:56 +00004443 "(n)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004444 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004445 res = call_method(self, "__setitem__", &setitem_str,
Thomas Wouters4e908102006-04-21 11:26:56 +00004446 "(nO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004447 if (res == NULL)
4448 return -1;
4449 Py_DECREF(res);
4450 return 0;
4451}
4452
4453static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004454slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004455{
4456 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004457 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004458
4459 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004460 res = call_method(self, "__delslice__", &delslice_str,
Thomas Wouters4e908102006-04-21 11:26:56 +00004461 "(nn)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004462 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004463 res = call_method(self, "__setslice__", &setslice_str,
Thomas Wouters4e908102006-04-21 11:26:56 +00004464 "(nnO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004465 if (res == NULL)
4466 return -1;
4467 Py_DECREF(res);
4468 return 0;
4469}
4470
4471static int
4472slot_sq_contains(PyObject *self, PyObject *value)
4473{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004474 PyObject *func, *res, *args;
Tim Petersbf9b2442003-03-23 05:35:36 +00004475 int result = -1;
4476
Guido van Rossum60718732001-08-28 17:47:51 +00004477 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004478
Guido van Rossum55f20992001-10-01 17:18:22 +00004479 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004480 if (func != NULL) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004481 args = PyTuple_Pack(1, value);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004482 if (args == NULL)
4483 res = NULL;
4484 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004485 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004486 Py_DECREF(args);
4487 }
4488 Py_DECREF(func);
Tim Petersbf9b2442003-03-23 05:35:36 +00004489 if (res != NULL) {
4490 result = PyObject_IsTrue(res);
4491 Py_DECREF(res);
4492 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004493 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004494 else if (! PyErr_Occurred()) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00004495 /* Possible results: -1 and 1 */
4496 result = (int)_PySequence_IterSearch(self, value,
Tim Petersbf9b2442003-03-23 05:35:36 +00004497 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004498 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004499 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004500}
4501
Tim Peters6d6c1a32001-08-02 04:15:00 +00004502#define slot_mp_length slot_sq_length
4503
Guido van Rossumdc91b992001-08-08 22:26:22 +00004504SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004505
4506static int
4507slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4508{
4509 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004510 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004511
4512 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004513 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004514 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004515 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004516 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004517 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004518 if (res == NULL)
4519 return -1;
4520 Py_DECREF(res);
4521 return 0;
4522}
4523
Guido van Rossumdc91b992001-08-08 22:26:22 +00004524SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4525SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4526SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4527SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
4528SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4529SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4530
Jeremy Hylton938ace62002-07-17 16:30:39 +00004531static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
Guido van Rossumdc91b992001-08-08 22:26:22 +00004532
4533SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4534 nb_power, "__pow__", "__rpow__")
4535
4536static PyObject *
4537slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4538{
Guido van Rossum2730b132001-08-28 18:22:14 +00004539 static PyObject *pow_str;
4540
Guido van Rossumdc91b992001-08-08 22:26:22 +00004541 if (modulus == Py_None)
4542 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00004543 /* Three-arg power doesn't use __rpow__. But ternary_op
4544 can call this when the second argument's type uses
4545 slot_nb_power, so check before calling self.__pow__. */
4546 if (self->ob_type->tp_as_number != NULL &&
4547 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4548 return call_method(self, "__pow__", &pow_str,
4549 "(OO)", other, modulus);
4550 }
4551 Py_INCREF(Py_NotImplemented);
4552 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00004553}
4554
4555SLOT0(slot_nb_negative, "__neg__")
4556SLOT0(slot_nb_positive, "__pos__")
4557SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004558
4559static int
4560slot_nb_nonzero(PyObject *self)
4561{
Tim Petersea7f75d2002-12-07 21:39:16 +00004562 PyObject *func, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00004563 static PyObject *nonzero_str, *len_str;
Tim Petersea7f75d2002-12-07 21:39:16 +00004564 int result = -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004565
Guido van Rossum55f20992001-10-01 17:18:22 +00004566 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004567 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00004568 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00004569 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00004570 func = lookup_maybe(self, "__len__", &len_str);
Tim Petersea7f75d2002-12-07 21:39:16 +00004571 if (func == NULL)
4572 return PyErr_Occurred() ? -1 : 1;
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00004573 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004574 args = PyTuple_New(0);
4575 if (args != NULL) {
4576 PyObject *temp = PyObject_Call(func, args, NULL);
4577 Py_DECREF(args);
4578 if (temp != NULL) {
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004579 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
Jeremy Hylton090a3492003-06-27 16:46:45 +00004580 result = PyObject_IsTrue(temp);
4581 else {
4582 PyErr_Format(PyExc_TypeError,
4583 "__nonzero__ should return "
4584 "bool or int, returned %s",
4585 temp->ob_type->tp_name);
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004586 result = -1;
Jeremy Hylton090a3492003-06-27 16:46:45 +00004587 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004588 Py_DECREF(temp);
Guido van Rossum55f20992001-10-01 17:18:22 +00004589 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004590 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004591 Py_DECREF(func);
Tim Petersea7f75d2002-12-07 21:39:16 +00004592 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004593}
4594
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004595
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004596static PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004597slot_nb_index(PyObject *self)
4598{
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004599 static PyObject *index_str;
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004600 return call_method(self, "__index__", &index_str, "()");
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004601}
4602
4603
Guido van Rossumdc91b992001-08-08 22:26:22 +00004604SLOT0(slot_nb_invert, "__invert__")
4605SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4606SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4607SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4608SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4609SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004610
4611static int
4612slot_nb_coerce(PyObject **a, PyObject **b)
4613{
4614 static PyObject *coerce_str;
4615 PyObject *self = *a, *other = *b;
4616
4617 if (self->ob_type->tp_as_number != NULL &&
4618 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4619 PyObject *r;
4620 r = call_maybe(
4621 self, "__coerce__", &coerce_str, "(O)", other);
4622 if (r == NULL)
4623 return -1;
4624 if (r == Py_NotImplemented) {
4625 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004626 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004627 else {
4628 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4629 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004630 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00004631 Py_DECREF(r);
4632 return -1;
4633 }
4634 *a = PyTuple_GET_ITEM(r, 0);
4635 Py_INCREF(*a);
4636 *b = PyTuple_GET_ITEM(r, 1);
4637 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004638 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00004639 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004640 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004641 }
4642 if (other->ob_type->tp_as_number != NULL &&
4643 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4644 PyObject *r;
4645 r = call_maybe(
4646 other, "__coerce__", &coerce_str, "(O)", self);
4647 if (r == NULL)
4648 return -1;
4649 if (r == Py_NotImplemented) {
4650 Py_DECREF(r);
4651 return 1;
4652 }
4653 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4654 PyErr_SetString(PyExc_TypeError,
4655 "__coerce__ didn't return a 2-tuple");
4656 Py_DECREF(r);
4657 return -1;
4658 }
4659 *a = PyTuple_GET_ITEM(r, 1);
4660 Py_INCREF(*a);
4661 *b = PyTuple_GET_ITEM(r, 0);
4662 Py_INCREF(*b);
4663 Py_DECREF(r);
4664 return 0;
4665 }
4666 return 1;
4667}
4668
Guido van Rossumdc91b992001-08-08 22:26:22 +00004669SLOT0(slot_nb_int, "__int__")
4670SLOT0(slot_nb_long, "__long__")
4671SLOT0(slot_nb_float, "__float__")
4672SLOT0(slot_nb_oct, "__oct__")
4673SLOT0(slot_nb_hex, "__hex__")
4674SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4675SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4676SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4677SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
4678SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
Martin v. Löwisfd963262007-02-09 12:19:32 +00004679/* Can't use SLOT1 here, because nb_inplace_power is ternary */
4680static PyObject *
4681slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
4682{
4683 static PyObject *cache_str;
4684 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
4685}
Guido van Rossumdc91b992001-08-08 22:26:22 +00004686SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4687SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4688SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4689SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4690SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4691SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4692 "__floordiv__", "__rfloordiv__")
4693SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4694SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4695SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004696
4697static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00004698half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004699{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004700 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004701 static PyObject *cmp_str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004702 Py_ssize_t c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004703
Guido van Rossum60718732001-08-28 17:47:51 +00004704 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004705 if (func == NULL) {
4706 PyErr_Clear();
4707 }
4708 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004709 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004710 if (args == NULL)
4711 res = NULL;
4712 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004713 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004714 Py_DECREF(args);
4715 }
Raymond Hettingerab5dae32002-06-24 13:08:16 +00004716 Py_DECREF(func);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004717 if (res != Py_NotImplemented) {
4718 if (res == NULL)
4719 return -2;
4720 c = PyInt_AsLong(res);
4721 Py_DECREF(res);
4722 if (c == -1 && PyErr_Occurred())
4723 return -2;
4724 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4725 }
4726 Py_DECREF(res);
4727 }
4728 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004729}
4730
Guido van Rossumab3b0342001-09-18 20:38:53 +00004731/* This slot is published for the benefit of try_3way_compare in object.c */
4732int
4733_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00004734{
4735 int c;
4736
Guido van Rossumab3b0342001-09-18 20:38:53 +00004737 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004738 c = half_compare(self, other);
4739 if (c <= 1)
4740 return c;
4741 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00004742 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004743 c = half_compare(other, self);
4744 if (c < -1)
4745 return -2;
4746 if (c <= 1)
4747 return -c;
4748 }
4749 return (void *)self < (void *)other ? -1 :
4750 (void *)self > (void *)other ? 1 : 0;
4751}
4752
4753static PyObject *
4754slot_tp_repr(PyObject *self)
4755{
4756 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004757 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004758
Guido van Rossum60718732001-08-28 17:47:51 +00004759 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004760 if (func != NULL) {
4761 res = PyEval_CallObject(func, NULL);
4762 Py_DECREF(func);
4763 return res;
4764 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00004765 PyErr_Clear();
4766 return PyString_FromFormat("<%s object at %p>",
4767 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004768}
4769
4770static PyObject *
4771slot_tp_str(PyObject *self)
4772{
4773 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004774 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004775
Guido van Rossum60718732001-08-28 17:47:51 +00004776 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004777 if (func != NULL) {
4778 res = PyEval_CallObject(func, NULL);
4779 Py_DECREF(func);
4780 return res;
4781 }
4782 else {
4783 PyErr_Clear();
4784 return slot_tp_repr(self);
4785 }
4786}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004787
4788static long
4789slot_tp_hash(PyObject *self)
4790{
Tim Peters61ce0a92002-12-06 23:38:02 +00004791 PyObject *func;
Guido van Rossum60718732001-08-28 17:47:51 +00004792 static PyObject *hash_str, *eq_str, *cmp_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004793 long h;
4794
Guido van Rossum60718732001-08-28 17:47:51 +00004795 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004796
4797 if (func != NULL) {
Tim Peters61ce0a92002-12-06 23:38:02 +00004798 PyObject *res = PyEval_CallObject(func, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004799 Py_DECREF(func);
4800 if (res == NULL)
4801 return -1;
Martin v. Löwisab2f8f72006-08-09 07:57:39 +00004802 if (PyLong_Check(res))
Armin Rigo51fc8c42006-08-09 14:55:26 +00004803 h = PyLong_Type.tp_hash(res);
Martin v. Löwisab2f8f72006-08-09 07:57:39 +00004804 else
4805 h = PyInt_AsLong(res);
Tim Peters61ce0a92002-12-06 23:38:02 +00004806 Py_DECREF(res);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004807 }
4808 else {
4809 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004810 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004811 if (func == NULL) {
4812 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004813 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004814 }
4815 if (func != NULL) {
Georg Brandlccff7852006-06-18 22:17:29 +00004816 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
4817 self->ob_type->tp_name);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004818 Py_DECREF(func);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004819 return -1;
4820 }
4821 PyErr_Clear();
4822 h = _Py_HashPointer((void *)self);
4823 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004824 if (h == -1 && !PyErr_Occurred())
4825 h = -2;
4826 return h;
4827}
4828
4829static PyObject *
4830slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4831{
Guido van Rossum60718732001-08-28 17:47:51 +00004832 static PyObject *call_str;
4833 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004834 PyObject *res;
4835
4836 if (meth == NULL)
4837 return NULL;
Armin Rigo53c1692f2006-06-21 21:58:50 +00004838
4839 /* PyObject_Call() will end up calling slot_tp_call() again if
4840 the object returned for __call__ has __call__ itself defined
4841 upon it. This can be an infinite recursion if you set
4842 __call__ in a class to an instance of it. */
Neal Norwitzb1149842006-06-23 03:32:44 +00004843 if (Py_EnterRecursiveCall(" in __call__")) {
4844 Py_DECREF(meth);
Armin Rigo53c1692f2006-06-21 21:58:50 +00004845 return NULL;
Neal Norwitzb1149842006-06-23 03:32:44 +00004846 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004847 res = PyObject_Call(meth, args, kwds);
Armin Rigo53c1692f2006-06-21 21:58:50 +00004848 Py_LeaveRecursiveCall();
4849
Tim Peters6d6c1a32001-08-02 04:15:00 +00004850 Py_DECREF(meth);
4851 return res;
4852}
4853
Guido van Rossum14a6f832001-10-17 13:59:09 +00004854/* There are two slot dispatch functions for tp_getattro.
4855
4856 - slot_tp_getattro() is used when __getattribute__ is overridden
4857 but no __getattr__ hook is present;
4858
4859 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4860
Guido van Rossumc334df52002-04-04 23:44:47 +00004861 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4862 detects the absence of __getattr__ and then installs the simpler slot if
4863 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00004864
Tim Peters6d6c1a32001-08-02 04:15:00 +00004865static PyObject *
4866slot_tp_getattro(PyObject *self, PyObject *name)
4867{
Guido van Rossum14a6f832001-10-17 13:59:09 +00004868 static PyObject *getattribute_str = NULL;
4869 return call_method(self, "__getattribute__", &getattribute_str,
4870 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004871}
4872
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004873static PyObject *
4874slot_tp_getattr_hook(PyObject *self, PyObject *name)
4875{
4876 PyTypeObject *tp = self->ob_type;
4877 PyObject *getattr, *getattribute, *res;
4878 static PyObject *getattribute_str = NULL;
4879 static PyObject *getattr_str = NULL;
4880
4881 if (getattr_str == NULL) {
4882 getattr_str = PyString_InternFromString("__getattr__");
4883 if (getattr_str == NULL)
4884 return NULL;
4885 }
4886 if (getattribute_str == NULL) {
4887 getattribute_str =
4888 PyString_InternFromString("__getattribute__");
4889 if (getattribute_str == NULL)
4890 return NULL;
4891 }
4892 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004893 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004894 /* No __getattr__ hook: use a simpler dispatcher */
4895 tp->tp_getattro = slot_tp_getattro;
4896 return slot_tp_getattro(self, name);
4897 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004898 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004899 if (getattribute == NULL ||
4900 (getattribute->ob_type == &PyWrapperDescr_Type &&
4901 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4902 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004903 res = PyObject_GenericGetAttr(self, name);
4904 else
Georg Brandl684fd0c2006-05-25 19:15:31 +00004905 res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004906 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004907 PyErr_Clear();
Georg Brandl684fd0c2006-05-25 19:15:31 +00004908 res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004909 }
4910 return res;
4911}
4912
Tim Peters6d6c1a32001-08-02 04:15:00 +00004913static int
4914slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4915{
4916 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004917 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004918
4919 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004920 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004921 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004922 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004923 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004924 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004925 if (res == NULL)
4926 return -1;
4927 Py_DECREF(res);
4928 return 0;
4929}
4930
4931/* Map rich comparison operators to their __xx__ namesakes */
4932static char *name_op[] = {
4933 "__lt__",
4934 "__le__",
4935 "__eq__",
4936 "__ne__",
4937 "__gt__",
4938 "__ge__",
4939};
4940
4941static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00004942half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004943{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004944 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004945 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00004946
Guido van Rossum60718732001-08-28 17:47:51 +00004947 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004948 if (func == NULL) {
4949 PyErr_Clear();
4950 Py_INCREF(Py_NotImplemented);
4951 return Py_NotImplemented;
4952 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004953 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004954 if (args == NULL)
4955 res = NULL;
4956 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004957 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004958 Py_DECREF(args);
4959 }
4960 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004961 return res;
4962}
4963
Guido van Rossumb8f63662001-08-15 23:57:02 +00004964static PyObject *
4965slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4966{
4967 PyObject *res;
4968
4969 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4970 res = half_richcompare(self, other, op);
4971 if (res != Py_NotImplemented)
4972 return res;
4973 Py_DECREF(res);
4974 }
4975 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
Tim Petersf4aca752004-09-23 02:39:37 +00004976 res = half_richcompare(other, self, _Py_SwappedOp[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004977 if (res != Py_NotImplemented) {
4978 return res;
4979 }
4980 Py_DECREF(res);
4981 }
4982 Py_INCREF(Py_NotImplemented);
4983 return Py_NotImplemented;
4984}
4985
4986static PyObject *
4987slot_tp_iter(PyObject *self)
4988{
4989 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004990 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004991
Guido van Rossum60718732001-08-28 17:47:51 +00004992 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004993 if (func != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +00004994 PyObject *args;
4995 args = res = PyTuple_New(0);
4996 if (args != NULL) {
4997 res = PyObject_Call(func, args, NULL);
4998 Py_DECREF(args);
4999 }
5000 Py_DECREF(func);
5001 return res;
Guido van Rossumb8f63662001-08-15 23:57:02 +00005002 }
5003 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00005004 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005005 if (func == NULL) {
Georg Brandlccff7852006-06-18 22:17:29 +00005006 PyErr_Format(PyExc_TypeError,
5007 "'%.200s' object is not iterable",
5008 self->ob_type->tp_name);
Guido van Rossumb8f63662001-08-15 23:57:02 +00005009 return NULL;
5010 }
5011 Py_DECREF(func);
5012 return PySeqIter_New(self);
5013}
Tim Peters6d6c1a32001-08-02 04:15:00 +00005014
5015static PyObject *
5016slot_tp_iternext(PyObject *self)
5017{
Guido van Rossum2730b132001-08-28 18:22:14 +00005018 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00005019 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00005020}
5021
Guido van Rossum1a493502001-08-17 16:47:50 +00005022static PyObject *
5023slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5024{
5025 PyTypeObject *tp = self->ob_type;
5026 PyObject *get;
5027 static PyObject *get_str = NULL;
5028
5029 if (get_str == NULL) {
5030 get_str = PyString_InternFromString("__get__");
5031 if (get_str == NULL)
5032 return NULL;
5033 }
5034 get = _PyType_Lookup(tp, get_str);
5035 if (get == NULL) {
5036 /* Avoid further slowdowns */
5037 if (tp->tp_descr_get == slot_tp_descr_get)
5038 tp->tp_descr_get = NULL;
5039 Py_INCREF(self);
5040 return self;
5041 }
Guido van Rossum2c252392001-08-24 10:13:31 +00005042 if (obj == NULL)
5043 obj = Py_None;
5044 if (type == NULL)
5045 type = Py_None;
Georg Brandl684fd0c2006-05-25 19:15:31 +00005046 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
Guido van Rossum1a493502001-08-17 16:47:50 +00005047}
Tim Peters6d6c1a32001-08-02 04:15:00 +00005048
5049static int
5050slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
5051{
Guido van Rossum2c252392001-08-24 10:13:31 +00005052 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00005053 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00005054
5055 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00005056 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005057 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00005058 else
Guido van Rossum2730b132001-08-28 18:22:14 +00005059 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00005060 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005061 if (res == NULL)
5062 return -1;
5063 Py_DECREF(res);
5064 return 0;
5065}
5066
5067static int
5068slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
5069{
Guido van Rossum60718732001-08-28 17:47:51 +00005070 static PyObject *init_str;
5071 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005072 PyObject *res;
5073
5074 if (meth == NULL)
5075 return -1;
5076 res = PyObject_Call(meth, args, kwds);
5077 Py_DECREF(meth);
5078 if (res == NULL)
5079 return -1;
Raymond Hettingerb67cc802005-03-03 16:45:19 +00005080 if (res != Py_None) {
Georg Brandlccff7852006-06-18 22:17:29 +00005081 PyErr_Format(PyExc_TypeError,
5082 "__init__() should return None, not '%.200s'",
5083 res->ob_type->tp_name);
Raymond Hettingerb67cc802005-03-03 16:45:19 +00005084 Py_DECREF(res);
5085 return -1;
5086 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00005087 Py_DECREF(res);
5088 return 0;
5089}
5090
5091static PyObject *
5092slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5093{
Guido van Rossum7bed2132002-08-08 21:57:53 +00005094 static PyObject *new_str;
5095 PyObject *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005096 PyObject *newargs, *x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005097 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005098
Guido van Rossum7bed2132002-08-08 21:57:53 +00005099 if (new_str == NULL) {
5100 new_str = PyString_InternFromString("__new__");
5101 if (new_str == NULL)
5102 return NULL;
5103 }
5104 func = PyObject_GetAttr((PyObject *)type, new_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005105 if (func == NULL)
5106 return NULL;
5107 assert(PyTuple_Check(args));
5108 n = PyTuple_GET_SIZE(args);
5109 newargs = PyTuple_New(n+1);
5110 if (newargs == NULL)
5111 return NULL;
5112 Py_INCREF(type);
5113 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
5114 for (i = 0; i < n; i++) {
5115 x = PyTuple_GET_ITEM(args, i);
5116 Py_INCREF(x);
5117 PyTuple_SET_ITEM(newargs, i+1, x);
5118 }
5119 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00005120 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005121 Py_DECREF(func);
5122 return x;
5123}
5124
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005125static void
5126slot_tp_del(PyObject *self)
5127{
5128 static PyObject *del_str = NULL;
5129 PyObject *del, *res;
5130 PyObject *error_type, *error_value, *error_traceback;
5131
5132 /* Temporarily resurrect the object. */
5133 assert(self->ob_refcnt == 0);
5134 self->ob_refcnt = 1;
5135
5136 /* Save the current exception, if any. */
5137 PyErr_Fetch(&error_type, &error_value, &error_traceback);
5138
5139 /* Execute __del__ method, if any. */
5140 del = lookup_maybe(self, "__del__", &del_str);
5141 if (del != NULL) {
5142 res = PyEval_CallObject(del, NULL);
5143 if (res == NULL)
5144 PyErr_WriteUnraisable(del);
5145 else
5146 Py_DECREF(res);
5147 Py_DECREF(del);
5148 }
5149
5150 /* Restore the saved exception. */
5151 PyErr_Restore(error_type, error_value, error_traceback);
5152
5153 /* Undo the temporary resurrection; can't use DECREF here, it would
5154 * cause a recursive call.
5155 */
5156 assert(self->ob_refcnt > 0);
5157 if (--self->ob_refcnt == 0)
5158 return; /* this is the normal path out */
5159
5160 /* __del__ resurrected it! Make it look like the original Py_DECREF
5161 * never happened.
5162 */
5163 {
Martin v. Löwis725507b2006-03-07 12:08:51 +00005164 Py_ssize_t refcnt = self->ob_refcnt;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005165 _Py_NewReference(self);
5166 self->ob_refcnt = refcnt;
5167 }
5168 assert(!PyType_IS_GC(self->ob_type) ||
5169 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
Michael W. Hudson3f3b6682004-08-03 10:21:03 +00005170 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5171 * we need to undo that. */
5172 _Py_DEC_REFTOTAL;
5173 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5174 * chain, so no more to do there.
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005175 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5176 * _Py_NewReference bumped tp_allocs: both of those need to be
5177 * undone.
5178 */
5179#ifdef COUNT_ALLOCS
5180 --self->ob_type->tp_frees;
5181 --self->ob_type->tp_allocs;
5182#endif
5183}
5184
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005185
5186/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
Tim Petersbf9b2442003-03-23 05:35:36 +00005187 functions. The offsets here are relative to the 'PyHeapTypeObject'
Guido van Rossume5c691a2003-03-07 15:13:17 +00005188 structure, which incorporates the additional structures used for numbers,
5189 sequences and mappings.
5190 Note that multiple names may map to the same slot (e.g. __eq__,
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005191 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00005192 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
5193 terminated with an all-zero entry. (This table is further initialized and
5194 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005195
Guido van Rossum6d204072001-10-21 00:44:31 +00005196typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005197
5198#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00005199#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005200#undef ETSLOT
5201#undef SQSLOT
5202#undef MPSLOT
5203#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00005204#undef UNSLOT
5205#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005206#undef BINSLOT
5207#undef RBINSLOT
5208
Guido van Rossum6d204072001-10-21 00:44:31 +00005209#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005210 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5211 PyDoc_STR(DOC)}
Guido van Rossumc8e56452001-10-22 00:43:43 +00005212#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5213 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005214 PyDoc_STR(DOC), FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00005215#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Guido van Rossume5c691a2003-03-07 15:13:17 +00005216 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00005217 PyDoc_STR(DOC)}
Guido van Rossum6d204072001-10-21 00:44:31 +00005218#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5219 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5220#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5221 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5222#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5223 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5224#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5225 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5226 "x." NAME "() <==> " DOC)
5227#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5228 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5229 "x." NAME "(y) <==> x" DOC "y")
5230#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5231 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5232 "x." NAME "(y) <==> x" DOC "y")
5233#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5234 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5235 "x." NAME "(y) <==> y" DOC "x")
Anthony Baxter56616992005-06-03 14:12:21 +00005236#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5237 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5238 "x." NAME "(y) <==> " DOC)
5239#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5240 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5241 "x." NAME "(y) <==> " DOC)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005242
5243static slotdef slotdefs[] = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00005244 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00005245 "x.__len__() <==> len(x)"),
Armin Rigofd163f92005-12-29 15:59:19 +00005246 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
5247 The logic in abstract.c always falls back to nb_add/nb_multiply in
5248 this case. Defining both the nb_* and the sq_* slots to call the
5249 user-defined methods has unexpected side-effects, as shown by
5250 test_descr.notimplemented() */
5251 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005252 "x.__add__(y) <==> x+y"),
Armin Rigo314861c2006-03-30 14:04:02 +00005253 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005254 "x.__mul__(n) <==> x*n"),
Armin Rigo314861c2006-03-30 14:04:02 +00005255 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005256 "x.__rmul__(n) <==> n*x"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005257 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
5258 "x.__getitem__(y) <==> x[y]"),
Martin v. Löwis18e16552006-02-15 17:27:45 +00005259 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
Brett Cannon154da9b2003-05-20 02:30:04 +00005260 "x.__getslice__(i, j) <==> x[i:j]\n\
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005261 \n\
5262 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00005263 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00005264 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005265 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00005266 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005267 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Martin v. Löwis18e16552006-02-15 17:27:45 +00005268 wrap_ssizessizeobjargproc,
Brett Cannonbe67d872003-05-20 02:40:12 +00005269 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005270 \n\
5271 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00005272 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
Brett Cannonbe67d872003-05-20 02:40:12 +00005273 "x.__delslice__(i, j) <==> del x[i:j]\n\
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005274 \n\
5275 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00005276 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
5277 "x.__contains__(y) <==> y in x"),
Armin Rigofd163f92005-12-29 15:59:19 +00005278 SQSLOT("__iadd__", sq_inplace_concat, NULL,
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005279 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Armin Rigofd163f92005-12-29 15:59:19 +00005280 SQSLOT("__imul__", sq_inplace_repeat, NULL,
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005281 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005282
Martin v. Löwis18e16552006-02-15 17:27:45 +00005283 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00005284 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00005285 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005286 wrap_binaryfunc,
5287 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005288 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005289 wrap_objobjargproc,
5290 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005291 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00005292 wrap_delitem,
5293 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005294
Guido van Rossum6d204072001-10-21 00:44:31 +00005295 BINSLOT("__add__", nb_add, slot_nb_add,
5296 "+"),
5297 RBINSLOT("__radd__", nb_add, slot_nb_add,
5298 "+"),
5299 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
5300 "-"),
5301 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
5302 "-"),
5303 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
5304 "*"),
5305 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
5306 "*"),
5307 BINSLOT("__div__", nb_divide, slot_nb_divide,
5308 "/"),
5309 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
5310 "/"),
5311 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
5312 "%"),
5313 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
5314 "%"),
Anthony Baxter56616992005-06-03 14:12:21 +00005315 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00005316 "divmod(x, y)"),
Anthony Baxter56616992005-06-03 14:12:21 +00005317 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00005318 "divmod(y, x)"),
5319 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
5320 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
5321 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
5322 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
5323 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
5324 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
5325 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5326 "abs(x)"),
Raymond Hettingerf34f2642003-10-11 17:29:04 +00005327 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
Guido van Rossum6d204072001-10-21 00:44:31 +00005328 "x != 0"),
5329 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5330 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5331 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5332 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5333 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5334 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5335 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5336 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5337 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5338 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5339 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
5340 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
5341 "x.__coerce__(y) <==> coerce(x, y)"),
5342 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5343 "int(x)"),
5344 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
5345 "long(x)"),
5346 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5347 "float(x)"),
5348 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
5349 "oct(x)"),
5350 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
5351 "hex(x)"),
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00005352 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005353 "x[y:z] <==> x[y.__index__():z.__index__()]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005354 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5355 wrap_binaryfunc, "+"),
5356 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5357 wrap_binaryfunc, "-"),
5358 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5359 wrap_binaryfunc, "*"),
5360 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
5361 wrap_binaryfunc, "/"),
5362 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5363 wrap_binaryfunc, "%"),
5364 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
Guido van Rossum6e5680f2002-10-15 01:01:53 +00005365 wrap_binaryfunc, "**"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005366 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5367 wrap_binaryfunc, "<<"),
5368 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5369 wrap_binaryfunc, ">>"),
5370 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5371 wrap_binaryfunc, "&"),
5372 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5373 wrap_binaryfunc, "^"),
5374 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5375 wrap_binaryfunc, "|"),
5376 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5377 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5378 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5379 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5380 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5381 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5382 IBSLOT("__itruediv__", nb_inplace_true_divide,
5383 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005384
Guido van Rossum6d204072001-10-21 00:44:31 +00005385 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5386 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00005387 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00005388 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5389 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00005390 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00005391 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5392 "x.__cmp__(y) <==> cmp(x,y)"),
5393 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5394 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005395 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5396 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005397 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00005398 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5399 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5400 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5401 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5402 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5403 "x.__setattr__('name', value) <==> x.name = value"),
5404 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5405 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5406 "x.__delattr__('name') <==> del x.name"),
5407 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5408 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5409 "x.__lt__(y) <==> x<y"),
5410 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5411 "x.__le__(y) <==> x<=y"),
5412 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5413 "x.__eq__(y) <==> x==y"),
5414 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5415 "x.__ne__(y) <==> x!=y"),
5416 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5417 "x.__gt__(y) <==> x>y"),
5418 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5419 "x.__ge__(y) <==> x>=y"),
5420 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5421 "x.__iter__() <==> iter(x)"),
5422 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
5423 "x.next() -> the next value, or raise StopIteration"),
5424 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5425 "descr.__get__(obj[, type]) -> value"),
5426 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5427 "descr.__set__(obj, value)"),
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00005428 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5429 wrap_descr_delete, "descr.__delete__(obj)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005430 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00005431 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00005432 "see x.__class__.__doc__ for signature",
5433 PyWrapperFlag_KEYWORDS),
5434 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005435 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005436 {NULL}
5437};
5438
Guido van Rossumc334df52002-04-04 23:44:47 +00005439/* Given a type pointer and an offset gotten from a slotdef entry, return a
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005440 pointer to the actual slot. This is not quite the same as simply adding
Guido van Rossumc334df52002-04-04 23:44:47 +00005441 the offset to the type pointer, since it takes care to indirect through the
5442 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5443 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005444static void **
Martin v. Löwis18e16552006-02-15 17:27:45 +00005445slotptr(PyTypeObject *type, int ioffset)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005446{
5447 char *ptr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005448 long offset = ioffset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005449
Guido van Rossume5c691a2003-03-07 15:13:17 +00005450 /* Note: this depends on the order of the members of PyHeapTypeObject! */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005451 assert(offset >= 0);
Skip Montanaro429433b2006-04-18 00:35:43 +00005452 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5453 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
Martin v. Löwisee36d652006-04-11 09:08:02 +00005454 ptr = (char *)type->tp_as_sequence;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005455 offset -= offsetof(PyHeapTypeObject, as_sequence);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005456 }
Skip Montanaro429433b2006-04-18 00:35:43 +00005457 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
Martin v. Löwisee36d652006-04-11 09:08:02 +00005458 ptr = (char *)type->tp_as_mapping;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005459 offset -= offsetof(PyHeapTypeObject, as_mapping);
Guido van Rossum09638c12002-06-13 19:17:46 +00005460 }
Skip Montanaro429433b2006-04-18 00:35:43 +00005461 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
Martin v. Löwisee36d652006-04-11 09:08:02 +00005462 ptr = (char *)type->tp_as_number;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005463 offset -= offsetof(PyHeapTypeObject, as_number);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005464 }
5465 else {
Martin v. Löwisee36d652006-04-11 09:08:02 +00005466 ptr = (char *)type;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005467 }
5468 if (ptr != NULL)
5469 ptr += offset;
5470 return (void **)ptr;
5471}
Guido van Rossumf040ede2001-08-07 16:40:56 +00005472
Guido van Rossumc334df52002-04-04 23:44:47 +00005473/* Length of array of slotdef pointers used to store slots with the
5474 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5475 the same __name__, for any __name__. Since that's a static property, it is
5476 appropriate to declare fixed-size arrays for this. */
5477#define MAX_EQUIV 10
5478
5479/* Return a slot pointer for a given name, but ONLY if the attribute has
5480 exactly one slot function. The name must be an interned string. */
5481static void **
5482resolve_slotdups(PyTypeObject *type, PyObject *name)
5483{
5484 /* XXX Maybe this could be optimized more -- but is it worth it? */
5485
5486 /* pname and ptrs act as a little cache */
5487 static PyObject *pname;
5488 static slotdef *ptrs[MAX_EQUIV];
5489 slotdef *p, **pp;
5490 void **res, **ptr;
5491
5492 if (pname != name) {
5493 /* Collect all slotdefs that match name into ptrs. */
5494 pname = name;
5495 pp = ptrs;
5496 for (p = slotdefs; p->name_strobj; p++) {
5497 if (p->name_strobj == name)
5498 *pp++ = p;
5499 }
5500 *pp = NULL;
5501 }
5502
5503 /* Look in all matching slots of the type; if exactly one of these has
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005504 a filled-in slot, return its value. Otherwise return NULL. */
Guido van Rossumc334df52002-04-04 23:44:47 +00005505 res = NULL;
5506 for (pp = ptrs; *pp; pp++) {
5507 ptr = slotptr(type, (*pp)->offset);
5508 if (ptr == NULL || *ptr == NULL)
5509 continue;
5510 if (res != NULL)
5511 return NULL;
5512 res = ptr;
5513 }
5514 return res;
5515}
5516
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005517/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
Guido van Rossumc334df52002-04-04 23:44:47 +00005518 does some incredibly complex thinking and then sticks something into the
5519 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5520 interests, and then stores a generic wrapper or a specific function into
5521 the slot.) Return a pointer to the next slotdef with a different offset,
5522 because that's convenient for fixup_slot_dispatchers(). */
5523static slotdef *
5524update_one_slot(PyTypeObject *type, slotdef *p)
5525{
5526 PyObject *descr;
5527 PyWrapperDescrObject *d;
5528 void *generic = NULL, *specific = NULL;
5529 int use_generic = 0;
5530 int offset = p->offset;
5531 void **ptr = slotptr(type, offset);
5532
5533 if (ptr == NULL) {
5534 do {
5535 ++p;
5536 } while (p->offset == offset);
5537 return p;
5538 }
5539 do {
5540 descr = _PyType_Lookup(type, p->name_strobj);
5541 if (descr == NULL)
5542 continue;
5543 if (descr->ob_type == &PyWrapperDescr_Type) {
5544 void **tptr = resolve_slotdups(type, p->name_strobj);
5545 if (tptr == NULL || tptr == ptr)
5546 generic = p->function;
5547 d = (PyWrapperDescrObject *)descr;
5548 if (d->d_base->wrapper == p->wrapper &&
5549 PyType_IsSubtype(type, d->d_type))
5550 {
5551 if (specific == NULL ||
5552 specific == d->d_wrapped)
5553 specific = d->d_wrapped;
5554 else
5555 use_generic = 1;
5556 }
5557 }
Guido van Rossum721f62e2002-08-09 02:14:34 +00005558 else if (descr->ob_type == &PyCFunction_Type &&
5559 PyCFunction_GET_FUNCTION(descr) ==
5560 (PyCFunction)tp_new_wrapper &&
5561 strcmp(p->name, "__new__") == 0)
5562 {
5563 /* The __new__ wrapper is not a wrapper descriptor,
5564 so must be special-cased differently.
5565 If we don't do this, creating an instance will
5566 always use slot_tp_new which will look up
5567 __new__ in the MRO which will call tp_new_wrapper
5568 which will look through the base classes looking
5569 for a static base and call its tp_new (usually
5570 PyType_GenericNew), after performing various
5571 sanity checks and constructing a new argument
5572 list. Cut all that nonsense short -- this speeds
5573 up instance creation tremendously. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005574 specific = (void *)type->tp_new;
Guido van Rossum721f62e2002-08-09 02:14:34 +00005575 /* XXX I'm not 100% sure that there isn't a hole
5576 in this reasoning that requires additional
5577 sanity checks. I'll buy the first person to
5578 point out a bug in this reasoning a beer. */
5579 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005580 else {
5581 use_generic = 1;
5582 generic = p->function;
5583 }
5584 } while ((++p)->offset == offset);
5585 if (specific && !use_generic)
5586 *ptr = specific;
5587 else
5588 *ptr = generic;
5589 return p;
5590}
5591
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005592/* In the type, update the slots whose slotdefs are gathered in the pp array.
5593 This is a callback for update_subclasses(). */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005594static int
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005595update_slots_callback(PyTypeObject *type, void *data)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005596{
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005597 slotdef **pp = (slotdef **)data;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005598
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005599 for (; *pp; pp++)
Guido van Rossumc334df52002-04-04 23:44:47 +00005600 update_one_slot(type, *pp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005601 return 0;
5602}
5603
Guido van Rossumc334df52002-04-04 23:44:47 +00005604/* Comparison function for qsort() to compare slotdefs by their offset, and
5605 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005606static int
5607slotdef_cmp(const void *aa, const void *bb)
5608{
5609 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5610 int c = a->offset - b->offset;
5611 if (c != 0)
5612 return c;
5613 else
Martin v. Löwis18e16552006-02-15 17:27:45 +00005614 /* Cannot use a-b, as this gives off_t,
5615 which may lose precision when converted to int. */
5616 return (a > b) ? 1 : (a < b) ? -1 : 0;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005617}
5618
Guido van Rossumc334df52002-04-04 23:44:47 +00005619/* Initialize the slotdefs table by adding interned string objects for the
5620 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005621static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005622init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005623{
5624 slotdef *p;
5625 static int initialized = 0;
5626
5627 if (initialized)
5628 return;
5629 for (p = slotdefs; p->name; p++) {
5630 p->name_strobj = PyString_InternFromString(p->name);
5631 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00005632 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005633 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005634 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5635 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005636 initialized = 1;
5637}
5638
Guido van Rossumc334df52002-04-04 23:44:47 +00005639/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005640static int
5641update_slot(PyTypeObject *type, PyObject *name)
5642{
Guido van Rossumc334df52002-04-04 23:44:47 +00005643 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005644 slotdef *p;
5645 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005646 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005647
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005648 init_slotdefs();
5649 pp = ptrs;
5650 for (p = slotdefs; p->name; p++) {
5651 /* XXX assume name is interned! */
5652 if (p->name_strobj == name)
5653 *pp++ = p;
5654 }
5655 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005656 for (pp = ptrs; *pp; pp++) {
5657 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005658 offset = p->offset;
5659 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005660 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005661 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005662 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005663 if (ptrs[0] == NULL)
5664 return 0; /* Not an attribute that affects any slots */
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005665 return update_subclasses(type, name,
5666 update_slots_callback, (void *)ptrs);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005667}
5668
Guido van Rossumc334df52002-04-04 23:44:47 +00005669/* Store the proper functions in the slot dispatches at class (type)
5670 definition time, based upon which operations the class overrides in its
5671 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00005672static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005673fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005674{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005675 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005676
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005677 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00005678 for (p = slotdefs; p->name; )
5679 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005680}
Guido van Rossum705f0f52001-08-24 16:47:00 +00005681
Michael W. Hudson98bbc492002-11-26 14:47:27 +00005682static void
5683update_all_slots(PyTypeObject* type)
5684{
5685 slotdef *p;
5686
5687 init_slotdefs();
5688 for (p = slotdefs; p->name; p++) {
5689 /* update_slot returns int but can't actually fail */
5690 update_slot(type, p->name_strobj);
5691 }
5692}
5693
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005694/* recurse_down_subclasses() and update_subclasses() are mutually
5695 recursive functions to call a callback for all subclasses,
5696 but refraining from recursing into subclasses that define 'name'. */
5697
5698static int
5699update_subclasses(PyTypeObject *type, PyObject *name,
5700 update_callback callback, void *data)
5701{
5702 if (callback(type, data) < 0)
5703 return -1;
5704 return recurse_down_subclasses(type, name, callback, data);
5705}
5706
5707static int
5708recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5709 update_callback callback, void *data)
5710{
5711 PyTypeObject *subclass;
5712 PyObject *ref, *subclasses, *dict;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005713 Py_ssize_t i, n;
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005714
5715 subclasses = type->tp_subclasses;
5716 if (subclasses == NULL)
5717 return 0;
5718 assert(PyList_Check(subclasses));
5719 n = PyList_GET_SIZE(subclasses);
5720 for (i = 0; i < n; i++) {
5721 ref = PyList_GET_ITEM(subclasses, i);
5722 assert(PyWeakref_CheckRef(ref));
5723 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5724 assert(subclass != NULL);
5725 if ((PyObject *)subclass == Py_None)
5726 continue;
5727 assert(PyType_Check(subclass));
5728 /* Avoid recursing down into unaffected classes */
5729 dict = subclass->tp_dict;
5730 if (dict != NULL && PyDict_Check(dict) &&
5731 PyDict_GetItem(dict, name) != NULL)
5732 continue;
5733 if (update_subclasses(subclass, name, callback, data) < 0)
5734 return -1;
5735 }
5736 return 0;
5737}
5738
Guido van Rossum6d204072001-10-21 00:44:31 +00005739/* This function is called by PyType_Ready() to populate the type's
5740 dictionary with method descriptors for function slots. For each
Guido van Rossum09638c12002-06-13 19:17:46 +00005741 function slot (like tp_repr) that's defined in the type, one or more
5742 corresponding descriptors are added in the type's tp_dict dictionary
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005743 under the appropriate name (like __repr__). Some function slots
Guido van Rossum09638c12002-06-13 19:17:46 +00005744 cause more than one descriptor to be added (for example, the nb_add
5745 slot adds both __add__ and __radd__ descriptors) and some function
5746 slots compete for the same descriptor (for example both sq_item and
5747 mp_subscript generate a __getitem__ descriptor).
5748
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005749 In the latter case, the first slotdef entry encoutered wins. Since
Tim Petersbf9b2442003-03-23 05:35:36 +00005750 slotdef entries are sorted by the offset of the slot in the
Guido van Rossume5c691a2003-03-07 15:13:17 +00005751 PyHeapTypeObject, this gives us some control over disambiguating
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005752 between competing slots: the members of PyHeapTypeObject are listed
5753 from most general to least general, so the most general slot is
5754 preferred. In particular, because as_mapping comes before as_sequence,
5755 for a type that defines both mp_subscript and sq_item, mp_subscript
5756 wins.
Guido van Rossum09638c12002-06-13 19:17:46 +00005757
5758 This only adds new descriptors and doesn't overwrite entries in
5759 tp_dict that were previously defined. The descriptors contain a
5760 reference to the C function they must call, so that it's safe if they
5761 are copied into a subtype's __dict__ and the subtype has a different
5762 C function in its slot -- calling the method defined by the
5763 descriptor will call the C function that was used to create it,
5764 rather than the C function present in the slot when it is called.
5765 (This is important because a subtype may have a C function in the
5766 slot that calls the method from the dictionary, and we want to avoid
5767 infinite recursion here.) */
Guido van Rossum6d204072001-10-21 00:44:31 +00005768
5769static int
5770add_operators(PyTypeObject *type)
5771{
5772 PyObject *dict = type->tp_dict;
5773 slotdef *p;
5774 PyObject *descr;
5775 void **ptr;
5776
5777 init_slotdefs();
5778 for (p = slotdefs; p->name; p++) {
5779 if (p->wrapper == NULL)
5780 continue;
5781 ptr = slotptr(type, p->offset);
5782 if (!ptr || !*ptr)
5783 continue;
5784 if (PyDict_GetItem(dict, p->name_strobj))
5785 continue;
5786 descr = PyDescr_NewWrapper(type, p, *ptr);
5787 if (descr == NULL)
5788 return -1;
5789 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5790 return -1;
5791 Py_DECREF(descr);
5792 }
5793 if (type->tp_new != NULL) {
5794 if (add_tp_new_wrapper(type) < 0)
5795 return -1;
5796 }
5797 return 0;
5798}
5799
Guido van Rossum705f0f52001-08-24 16:47:00 +00005800
5801/* Cooperative 'super' */
5802
5803typedef struct {
5804 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00005805 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005806 PyObject *obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005807 PyTypeObject *obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005808} superobject;
5809
Guido van Rossum6f799372001-09-20 20:46:19 +00005810static PyMemberDef super_members[] = {
5811 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5812 "the class invoking super()"},
5813 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5814 "the instance invoking super(); may be None"},
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005815 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005816 "the type of the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005817 {0}
5818};
5819
Guido van Rossum705f0f52001-08-24 16:47:00 +00005820static void
5821super_dealloc(PyObject *self)
5822{
5823 superobject *su = (superobject *)self;
5824
Guido van Rossum048eb752001-10-02 21:24:57 +00005825 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005826 Py_XDECREF(su->obj);
5827 Py_XDECREF(su->type);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005828 Py_XDECREF(su->obj_type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005829 self->ob_type->tp_free(self);
5830}
5831
5832static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005833super_repr(PyObject *self)
5834{
5835 superobject *su = (superobject *)self;
5836
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005837 if (su->obj_type)
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005838 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005839 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005840 su->type ? su->type->tp_name : "NULL",
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005841 su->obj_type->tp_name);
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005842 else
5843 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005844 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005845 su->type ? su->type->tp_name : "NULL");
5846}
5847
5848static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00005849super_getattro(PyObject *self, PyObject *name)
5850{
5851 superobject *su = (superobject *)self;
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005852 int skip = su->obj_type == NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005853
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005854 if (!skip) {
5855 /* We want __class__ to return the class of the super object
5856 (i.e. super, or a subclass), not the class of su->obj. */
5857 skip = (PyString_Check(name) &&
5858 PyString_GET_SIZE(name) == 9 &&
5859 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5860 }
5861
5862 if (!skip) {
Tim Petersa91e9642001-11-14 23:32:33 +00005863 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005864 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005865 descrgetfunc f;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005866 Py_ssize_t i, n;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005867
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005868 starttype = su->obj_type;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005869 mro = starttype->tp_mro;
5870
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005871 if (mro == NULL)
5872 n = 0;
5873 else {
5874 assert(PyTuple_Check(mro));
5875 n = PyTuple_GET_SIZE(mro);
5876 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005877 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00005878 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00005879 break;
5880 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005881 i++;
5882 res = NULL;
5883 for (; i < n; i++) {
5884 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00005885 if (PyType_Check(tmp))
5886 dict = ((PyTypeObject *)tmp)->tp_dict;
5887 else if (PyClass_Check(tmp))
5888 dict = ((PyClassObject *)tmp)->cl_dict;
5889 else
5890 continue;
5891 res = PyDict_GetItem(dict, name);
Guido van Rossum6cc5bb62003-04-16 20:01:36 +00005892 if (res != NULL) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00005893 Py_INCREF(res);
5894 f = res->ob_type->tp_descr_get;
5895 if (f != NULL) {
Phillip J. Eby91a968a2004-03-25 02:19:34 +00005896 tmp = f(res,
5897 /* Only pass 'obj' param if
5898 this is instance-mode super
5899 (See SF ID #743627)
5900 */
Hye-Shik Changff365c92004-03-25 16:37:03 +00005901 (su->obj == (PyObject *)
5902 su->obj_type
Phillip J. Eby91a968a2004-03-25 02:19:34 +00005903 ? (PyObject *)NULL
5904 : su->obj),
Guido van Rossumd4641072002-04-03 02:13:37 +00005905 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005906 Py_DECREF(res);
5907 res = tmp;
5908 }
5909 return res;
5910 }
5911 }
5912 }
5913 return PyObject_GenericGetAttr(self, name);
5914}
5915
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005916static PyTypeObject *
Guido van Rossum5b443c62001-12-03 15:38:28 +00005917supercheck(PyTypeObject *type, PyObject *obj)
5918{
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005919 /* Check that a super() call makes sense. Return a type object.
5920
5921 obj can be a new-style class, or an instance of one:
5922
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005923 - If it is a class, it must be a subclass of 'type'. This case is
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005924 used for class methods; the return value is obj.
5925
5926 - If it is an instance, it must be an instance of 'type'. This is
5927 the normal case; the return value is obj.__class__.
5928
5929 But... when obj is an instance, we want to allow for the case where
5930 obj->ob_type is not a subclass of type, but obj.__class__ is!
5931 This will allow using super() with a proxy for obj.
5932 */
5933
Guido van Rossum8e80a722003-02-18 19:22:22 +00005934 /* Check for first bullet above (special case) */
5935 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5936 Py_INCREF(obj);
5937 return (PyTypeObject *)obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005938 }
Guido van Rossum8e80a722003-02-18 19:22:22 +00005939
5940 /* Normal case */
5941 if (PyType_IsSubtype(obj->ob_type, type)) {
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005942 Py_INCREF(obj->ob_type);
5943 return obj->ob_type;
5944 }
5945 else {
5946 /* Try the slow way */
5947 static PyObject *class_str = NULL;
5948 PyObject *class_attr;
5949
5950 if (class_str == NULL) {
5951 class_str = PyString_FromString("__class__");
5952 if (class_str == NULL)
5953 return NULL;
5954 }
5955
5956 class_attr = PyObject_GetAttr(obj, class_str);
5957
5958 if (class_attr != NULL &&
5959 PyType_Check(class_attr) &&
5960 (PyTypeObject *)class_attr != obj->ob_type)
5961 {
5962 int ok = PyType_IsSubtype(
5963 (PyTypeObject *)class_attr, type);
5964 if (ok)
5965 return (PyTypeObject *)class_attr;
5966 }
5967
5968 if (class_attr == NULL)
5969 PyErr_Clear();
5970 else
5971 Py_DECREF(class_attr);
5972 }
5973
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005974 PyErr_SetString(PyExc_TypeError,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005975 "super(type, obj): "
5976 "obj must be an instance or subtype of type");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005977 return NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00005978}
5979
Guido van Rossum705f0f52001-08-24 16:47:00 +00005980static PyObject *
5981super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5982{
5983 superobject *su = (superobject *)self;
Anthony Baxtera6286212006-04-11 07:42:36 +00005984 superobject *newobj;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005985
5986 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5987 /* Not binding to an object, or already bound */
5988 Py_INCREF(self);
5989 return self;
5990 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00005991 if (su->ob_type != &PySuper_Type)
Armin Rigo7726dc02005-05-15 15:32:08 +00005992 /* If su is an instance of a (strict) subclass of super,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005993 call its type */
Georg Brandl684fd0c2006-05-25 19:15:31 +00005994 return PyObject_CallFunctionObjArgs((PyObject *)su->ob_type,
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00005995 su->type, obj, NULL);
Guido van Rossum5b443c62001-12-03 15:38:28 +00005996 else {
5997 /* Inline the common case */
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005998 PyTypeObject *obj_type = supercheck(su->type, obj);
5999 if (obj_type == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00006000 return NULL;
Anthony Baxtera6286212006-04-11 07:42:36 +00006001 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
Guido van Rossum5b443c62001-12-03 15:38:28 +00006002 NULL, NULL);
Anthony Baxtera6286212006-04-11 07:42:36 +00006003 if (newobj == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00006004 return NULL;
6005 Py_INCREF(su->type);
6006 Py_INCREF(obj);
Anthony Baxtera6286212006-04-11 07:42:36 +00006007 newobj->type = su->type;
6008 newobj->obj = obj;
6009 newobj->obj_type = obj_type;
6010 return (PyObject *)newobj;
Guido van Rossum5b443c62001-12-03 15:38:28 +00006011 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006012}
6013
6014static int
6015super_init(PyObject *self, PyObject *args, PyObject *kwds)
6016{
6017 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00006018 PyTypeObject *type;
6019 PyObject *obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006020 PyTypeObject *obj_type = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006021
Georg Brandl5d59c092006-09-30 08:43:30 +00006022 if (!_PyArg_NoKeywords("super", kwds))
6023 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006024 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
6025 return -1;
6026 if (obj == Py_None)
6027 obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006028 if (obj != NULL) {
6029 obj_type = supercheck(type, obj);
6030 if (obj_type == NULL)
6031 return -1;
6032 Py_INCREF(obj);
6033 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00006034 Py_INCREF(type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00006035 su->type = type;
6036 su->obj = obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00006037 su->obj_type = obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00006038 return 0;
6039}
6040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006041PyDoc_STRVAR(super_doc,
Guido van Rossum705f0f52001-08-24 16:47:00 +00006042"super(type) -> unbound super object\n"
6043"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00006044"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00006045"Typical use to call a cooperative superclass method:\n"
6046"class C(B):\n"
6047" def meth(self, arg):\n"
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00006048" super(C, self).meth(arg)");
Guido van Rossum705f0f52001-08-24 16:47:00 +00006049
Guido van Rossum048eb752001-10-02 21:24:57 +00006050static int
6051super_traverse(PyObject *self, visitproc visit, void *arg)
6052{
6053 superobject *su = (superobject *)self;
Guido van Rossum048eb752001-10-02 21:24:57 +00006054
Thomas Woutersc6e55062006-04-15 21:47:09 +00006055 Py_VISIT(su->obj);
6056 Py_VISIT(su->type);
6057 Py_VISIT(su->obj_type);
Guido van Rossum048eb752001-10-02 21:24:57 +00006058
6059 return 0;
6060}
6061
Guido van Rossum705f0f52001-08-24 16:47:00 +00006062PyTypeObject PySuper_Type = {
6063 PyObject_HEAD_INIT(&PyType_Type)
6064 0, /* ob_size */
6065 "super", /* tp_name */
6066 sizeof(superobject), /* tp_basicsize */
6067 0, /* tp_itemsize */
6068 /* methods */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00006069 super_dealloc, /* tp_dealloc */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006070 0, /* tp_print */
6071 0, /* tp_getattr */
6072 0, /* tp_setattr */
6073 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00006074 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006075 0, /* tp_as_number */
6076 0, /* tp_as_sequence */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00006077 0, /* tp_as_mapping */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006078 0, /* tp_hash */
6079 0, /* tp_call */
6080 0, /* tp_str */
6081 super_getattro, /* tp_getattro */
6082 0, /* tp_setattro */
6083 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00006084 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
6085 Py_TPFLAGS_BASETYPE, /* tp_flags */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00006086 super_doc, /* tp_doc */
6087 super_traverse, /* tp_traverse */
6088 0, /* tp_clear */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006089 0, /* tp_richcompare */
6090 0, /* tp_weaklistoffset */
6091 0, /* tp_iter */
6092 0, /* tp_iternext */
6093 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00006094 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006095 0, /* tp_getset */
6096 0, /* tp_base */
6097 0, /* tp_dict */
6098 super_descr_get, /* tp_descr_get */
6099 0, /* tp_descr_set */
6100 0, /* tp_dictoffset */
6101 super_init, /* tp_init */
6102 PyType_GenericAlloc, /* tp_alloc */
6103 PyType_GenericNew, /* tp_new */
Jeremy Hylton2d1f5c92007-02-27 17:24:48 +00006104 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00006105};