blob: c8bc61dca4c5bb1a6fb154c98e2cf4544155d0ee [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. Hudsonade8c8b22002-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. Hudsonade8c8b22002-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. Hudsonade8c8b22002-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. Hudsonade8c8b22002-11-27 16:29:26 +000094 Py_XINCREF(mod);
Guido van Rossumc3542212001-08-16 09:18:56 +000095 return mod;
96 }
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +000097 else {
98 s = strrchr(type->tp_name, '.');
99 if (s != NULL)
100 return PyString_FromStringAndSize(
101 type->tp_name, (int)(s - type->tp_name));
102 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
130static PyTypeObject *best_base(PyObject *);
131static int mro_internal(PyTypeObject *);
132static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
133static int add_subclass(PyTypeObject*, PyTypeObject*);
134static void remove_subclass(PyTypeObject *, PyTypeObject *);
135static void update_all_slots(PyTypeObject *);
136
Guido van Rossum8d24ee92003-03-24 23:49:49 +0000137typedef int (*update_callback)(PyTypeObject *, void *);
138static int update_subclasses(PyTypeObject *type, PyObject *name,
139 update_callback callback, void *data);
140static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
141 update_callback callback, void *data);
142
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000143static int
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000144mro_subclasses(PyTypeObject *type, PyObject* temp)
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000145{
146 PyTypeObject *subclass;
147 PyObject *ref, *subclasses, *old_mro;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000148 Py_ssize_t i, n;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000149
150 subclasses = type->tp_subclasses;
151 if (subclasses == NULL)
152 return 0;
153 assert(PyList_Check(subclasses));
154 n = PyList_GET_SIZE(subclasses);
155 for (i = 0; i < n; i++) {
156 ref = PyList_GET_ITEM(subclasses, i);
157 assert(PyWeakref_CheckRef(ref));
158 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
159 assert(subclass != NULL);
160 if ((PyObject *)subclass == Py_None)
161 continue;
162 assert(PyType_Check(subclass));
163 old_mro = subclass->tp_mro;
164 if (mro_internal(subclass) < 0) {
165 subclass->tp_mro = old_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000166 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000167 }
168 else {
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000169 PyObject* tuple;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000170 tuple = PyTuple_Pack(2, subclass, old_mro);
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000171 Py_DECREF(old_mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000172 if (!tuple)
173 return -1;
174 if (PyList_Append(temp, tuple) < 0)
175 return -1;
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000176 Py_DECREF(tuple);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000177 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000178 if (mro_subclasses(subclass, temp) < 0)
179 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000180 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000181 return 0;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000182}
183
184static int
185type_set_bases(PyTypeObject *type, PyObject *value, void *context)
186{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000187 Py_ssize_t i;
188 int r = 0;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000189 PyObject *ob, *temp;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000190 PyTypeObject *new_base, *old_base;
191 PyObject *old_bases, *old_mro;
192
193 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
194 PyErr_Format(PyExc_TypeError,
195 "can't set %s.__bases__", type->tp_name);
196 return -1;
197 }
198 if (!value) {
199 PyErr_Format(PyExc_TypeError,
200 "can't delete %s.__bases__", type->tp_name);
201 return -1;
202 }
203 if (!PyTuple_Check(value)) {
204 PyErr_Format(PyExc_TypeError,
205 "can only assign tuple to %s.__bases__, not %s",
206 type->tp_name, value->ob_type->tp_name);
207 return -1;
208 }
Guido van Rossum3bbc0ee2002-12-13 17:49:38 +0000209 if (PyTuple_GET_SIZE(value) == 0) {
210 PyErr_Format(PyExc_TypeError,
211 "can only assign non-empty tuple to %s.__bases__, not ()",
212 type->tp_name);
213 return -1;
214 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000215 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
216 ob = PyTuple_GET_ITEM(value, i);
217 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
218 PyErr_Format(
219 PyExc_TypeError,
220 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
221 type->tp_name, ob->ob_type->tp_name);
222 return -1;
223 }
Michael W. Hudsoncaf17be2002-11-27 10:24:44 +0000224 if (PyType_Check(ob)) {
225 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
226 PyErr_SetString(PyExc_TypeError,
227 "a __bases__ item causes an inheritance cycle");
228 return -1;
229 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000230 }
231 }
232
233 new_base = best_base(value);
234
235 if (!new_base) {
236 return -1;
237 }
238
239 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
240 return -1;
241
242 Py_INCREF(new_base);
243 Py_INCREF(value);
244
245 old_bases = type->tp_bases;
246 old_base = type->tp_base;
247 old_mro = type->tp_mro;
248
249 type->tp_bases = value;
250 type->tp_base = new_base;
251
252 if (mro_internal(type) < 0) {
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000253 goto bail;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000254 }
255
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000256 temp = PyList_New(0);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000257 if (!temp)
258 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000259
260 r = mro_subclasses(type, temp);
261
262 if (r < 0) {
263 for (i = 0; i < PyList_Size(temp); i++) {
264 PyTypeObject* cls;
265 PyObject* mro;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000266 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
267 "", 2, 2, &cls, &mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000268 Py_DECREF(cls->tp_mro);
269 cls->tp_mro = mro;
270 Py_INCREF(cls->tp_mro);
271 }
272 Py_DECREF(temp);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000273 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000274 }
275
276 Py_DECREF(temp);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000277
278 /* any base that was in __bases__ but now isn't, we
Raymond Hettingera8285862002-12-14 17:17:56 +0000279 need to remove |type| from its tp_subclasses.
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000280 conversely, any class now in __bases__ that wasn't
Raymond Hettingera8285862002-12-14 17:17:56 +0000281 needs to have |type| added to its subclasses. */
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000282
283 /* for now, sod that: just remove from all old_bases,
284 add to all new_bases */
285
286 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
287 ob = PyTuple_GET_ITEM(old_bases, i);
288 if (PyType_Check(ob)) {
289 remove_subclass(
290 (PyTypeObject*)ob, type);
291 }
292 }
293
294 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
295 ob = PyTuple_GET_ITEM(value, i);
296 if (PyType_Check(ob)) {
297 if (add_subclass((PyTypeObject*)ob, type) < 0)
298 r = -1;
299 }
300 }
301
302 update_all_slots(type);
303
304 Py_DECREF(old_bases);
305 Py_DECREF(old_base);
306 Py_DECREF(old_mro);
307
308 return r;
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000309
310 bail:
Michael W. Hudsone723e452003-08-07 14:58:10 +0000311 Py_DECREF(type->tp_bases);
312 Py_DECREF(type->tp_base);
313 if (type->tp_mro != old_mro) {
314 Py_DECREF(type->tp_mro);
315 }
316
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000317 type->tp_bases = old_bases;
318 type->tp_base = old_base;
319 type->tp_mro = old_mro;
Tim Petersea7f75d2002-12-07 21:39:16 +0000320
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000321 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000322}
323
324static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000325type_dict(PyTypeObject *type, void *context)
326{
327 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328 Py_INCREF(Py_None);
329 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000330 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000331 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000332}
333
Tim Peters24008312002-03-17 18:56:20 +0000334static PyObject *
335type_get_doc(PyTypeObject *type, void *context)
336{
337 PyObject *result;
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000338 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Tim Peters24008312002-03-17 18:56:20 +0000339 return PyString_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +0000340 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000341 if (result == NULL) {
342 result = Py_None;
343 Py_INCREF(result);
344 }
345 else if (result->ob_type->tp_descr_get) {
Tim Peters2b858972002-04-18 04:12:28 +0000346 result = result->ob_type->tp_descr_get(result, NULL,
347 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000348 }
349 else {
350 Py_INCREF(result);
351 }
Tim Peters24008312002-03-17 18:56:20 +0000352 return result;
353}
354
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000355static PyGetSetDef type_getsets[] = {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000356 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
357 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000358 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000359 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000360 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000361 {0}
362};
363
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000364static int
365type_compare(PyObject *v, PyObject *w)
366{
367 /* This is called with type objects only. So we
368 can just compare the addresses. */
369 Py_uintptr_t vv = (Py_uintptr_t)v;
370 Py_uintptr_t ww = (Py_uintptr_t)w;
371 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
372}
373
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000375type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000377 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000378 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000379
380 mod = type_module(type, NULL);
381 if (mod == NULL)
382 PyErr_Clear();
383 else if (!PyString_Check(mod)) {
384 Py_DECREF(mod);
385 mod = NULL;
386 }
387 name = type_name(type, NULL);
388 if (name == NULL)
389 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000390
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000391 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
392 kind = "class";
393 else
394 kind = "type";
395
Barry Warsaw7ce36942001-08-24 18:34:26 +0000396 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000397 rtn = PyString_FromFormat("<%s '%s.%s'>",
398 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000399 PyString_AS_STRING(mod),
400 PyString_AS_STRING(name));
401 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000402 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000403 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000404
Guido van Rossumc3542212001-08-16 09:18:56 +0000405 Py_XDECREF(mod);
406 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000407 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408}
409
Tim Peters6d6c1a32001-08-02 04:15:00 +0000410static PyObject *
411type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
412{
413 PyObject *obj;
414
415 if (type->tp_new == NULL) {
416 PyErr_Format(PyExc_TypeError,
417 "cannot create '%.100s' instances",
418 type->tp_name);
419 return NULL;
420 }
421
Tim Peters3f996e72001-09-13 19:18:27 +0000422 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000423 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000424 /* Ugly exception: when the call was type(something),
425 don't call tp_init on the result. */
426 if (type == &PyType_Type &&
427 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
428 (kwds == NULL ||
429 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
430 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000431 /* If the returned object is not an instance of type,
432 it won't be initialized. */
433 if (!PyType_IsSubtype(obj->ob_type, type))
434 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000435 type = obj->ob_type;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000436 if (type->tp_init != NULL &&
Tim Peters6d6c1a32001-08-02 04:15:00 +0000437 type->tp_init(obj, args, kwds) < 0) {
438 Py_DECREF(obj);
439 obj = NULL;
440 }
441 }
442 return obj;
443}
444
445PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000446PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000447{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000448 PyObject *obj;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000449 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
450 /* note that we need to add one, for the sentinel */
Tim Peters406fe3b2001-10-06 19:04:01 +0000451
452 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000453 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000454 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455 obj = (PyObject *)PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000456
Neil Schemenauerc806c882001-08-29 23:54:54 +0000457 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000458 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000459
Neil Schemenauerc806c882001-08-29 23:54:54 +0000460 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000461
Tim Peters6d6c1a32001-08-02 04:15:00 +0000462 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
463 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000464
Tim Peters6d6c1a32001-08-02 04:15:00 +0000465 if (type->tp_itemsize == 0)
466 PyObject_INIT(obj, type);
467 else
468 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000469
Tim Peters6d6c1a32001-08-02 04:15:00 +0000470 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000471 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000472 return obj;
473}
474
475PyObject *
476PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
477{
478 return type->tp_alloc(type, 0);
479}
480
Guido van Rossum9475a232001-10-05 20:51:39 +0000481/* Helpers for subtyping */
482
483static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000484traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
485{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000486 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000487 PyMemberDef *mp;
488
489 n = type->ob_size;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000490 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000491 for (i = 0; i < n; i++, mp++) {
492 if (mp->type == T_OBJECT_EX) {
493 char *addr = (char *)self + mp->offset;
494 PyObject *obj = *(PyObject **)addr;
495 if (obj != NULL) {
496 int err = visit(obj, arg);
497 if (err)
498 return err;
499 }
500 }
501 }
502 return 0;
503}
504
505static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000506subtype_traverse(PyObject *self, visitproc visit, void *arg)
507{
508 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000509 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000510
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000511 /* Find the nearest base with a different tp_traverse,
512 and traverse slots while we're at it */
Guido van Rossum9475a232001-10-05 20:51:39 +0000513 type = self->ob_type;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000514 base = type;
515 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
516 if (base->ob_size) {
517 int err = traverse_slots(base, self, visit, arg);
518 if (err)
519 return err;
520 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000521 base = base->tp_base;
522 assert(base);
523 }
524
525 if (type->tp_dictoffset != base->tp_dictoffset) {
526 PyObject **dictptr = _PyObject_GetDictPtr(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527 if (dictptr && *dictptr)
528 Py_VISIT(*dictptr);
Guido van Rossum9475a232001-10-05 20:51:39 +0000529 }
530
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000531 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossuma3862092002-06-10 15:24:42 +0000532 /* For a heaptype, the instances count as references
533 to the type. Traverse the type so the collector
534 can find cycles involving this link. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535 Py_VISIT(type);
Guido van Rossuma3862092002-06-10 15:24:42 +0000536
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000537 if (basetraverse)
538 return basetraverse(self, visit, arg);
539 return 0;
540}
541
542static void
543clear_slots(PyTypeObject *type, PyObject *self)
544{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000545 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000546 PyMemberDef *mp;
547
548 n = type->ob_size;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000549 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000550 for (i = 0; i < n; i++, mp++) {
551 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
552 char *addr = (char *)self + mp->offset;
553 PyObject *obj = *(PyObject **)addr;
554 if (obj != NULL) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000555 *(PyObject **)addr = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556 Py_DECREF(obj);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000557 }
558 }
559 }
560}
561
562static int
563subtype_clear(PyObject *self)
564{
565 PyTypeObject *type, *base;
566 inquiry baseclear;
567
568 /* Find the nearest base with a different tp_clear
569 and clear slots while we're at it */
570 type = self->ob_type;
571 base = type;
572 while ((baseclear = base->tp_clear) == subtype_clear) {
573 if (base->ob_size)
574 clear_slots(base, self);
575 base = base->tp_base;
576 assert(base);
577 }
578
Guido van Rossuma3862092002-06-10 15:24:42 +0000579 /* There's no need to clear the instance dict (if any);
580 the collector will call its tp_clear handler. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000581
582 if (baseclear)
583 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000584 return 0;
585}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000586
587static void
588subtype_dealloc(PyObject *self)
589{
Guido van Rossum14227b42001-12-06 02:35:58 +0000590 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000591 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000592
Guido van Rossum22b13872002-08-06 21:41:44 +0000593 /* Extract the type; we expect it to be a heap type */
594 type = self->ob_type;
595 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000596
Guido van Rossum22b13872002-08-06 21:41:44 +0000597 /* Test whether the type has GC exactly once */
598
599 if (!PyType_IS_GC(type)) {
600 /* It's really rare to find a dynamic type that doesn't have
601 GC; it can only happen when deriving from 'object' and not
602 adding any slots or instance variables. This allows
603 certain simplifications: there's no need to call
604 clear_slots(), or DECREF the dict, or clear weakrefs. */
605
606 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000607 if (type->tp_del) {
608 type->tp_del(self);
609 if (self->ob_refcnt > 0)
610 return;
611 }
Guido van Rossum22b13872002-08-06 21:41:44 +0000612
613 /* Find the nearest base with a different tp_dealloc */
614 base = type;
615 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
616 assert(base->ob_size == 0);
617 base = base->tp_base;
618 assert(base);
619 }
620
621 /* Call the base tp_dealloc() */
622 assert(basedealloc);
623 basedealloc(self);
624
625 /* Can't reference self beyond this point */
626 Py_DECREF(type);
627
628 /* Done */
629 return;
630 }
631
632 /* We get here only if the type has GC */
633
634 /* UnTrack and re-Track around the trashcan macro, alas */
Andrew M. Kuchlingc9172d32003-02-06 15:22:49 +0000635 /* See explanation at end of function for full disclosure */
Guido van Rossum0906e072002-08-07 20:42:09 +0000636 PyObject_GC_UnTrack(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000637 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000638 Py_TRASHCAN_SAFE_BEGIN(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000639 --_PyTrash_delete_nesting;
Tim Petersf7f9e992003-11-13 21:59:32 +0000640 /* DO NOT restore GC tracking at this point. weakref callbacks
641 * (if any, and whether directly here or indirectly in something we
642 * call) may trigger GC, and if self is tracked at that point, it
643 * will look like trash to GC and GC will try to delete self again.
Tim Petersadd09b42003-11-12 20:43:28 +0000644 */
Guido van Rossum22b13872002-08-06 21:41:44 +0000645
Guido van Rossum59195fd2003-06-13 20:54:40 +0000646 /* Find the nearest base with a different tp_dealloc */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000647 base = type;
648 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000649 base = base->tp_base;
650 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000651 }
652
Guido van Rossum1987c662003-05-29 14:29:23 +0000653 /* If we added a weaklist, we clear it. Do this *before* calling
Guido van Rossum59195fd2003-06-13 20:54:40 +0000654 the finalizer (__del__), clearing slots, or clearing the instance
655 dict. */
656
Guido van Rossum1987c662003-05-29 14:29:23 +0000657 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
658 PyObject_ClearWeakRefs(self);
659
660 /* Maybe call finalizer; exit early if resurrected */
661 if (type->tp_del) {
Tim Petersf7f9e992003-11-13 21:59:32 +0000662 _PyObject_GC_TRACK(self);
Guido van Rossum1987c662003-05-29 14:29:23 +0000663 type->tp_del(self);
664 if (self->ob_refcnt > 0)
Tim Petersf7f9e992003-11-13 21:59:32 +0000665 goto endlabel; /* resurrected */
666 else
667 _PyObject_GC_UNTRACK(self);
Guido van Rossum1987c662003-05-29 14:29:23 +0000668 }
669
Guido van Rossum59195fd2003-06-13 20:54:40 +0000670 /* Clear slots up to the nearest base with a different tp_dealloc */
671 base = type;
672 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
673 if (base->ob_size)
674 clear_slots(base, self);
675 base = base->tp_base;
676 assert(base);
677 }
678
Tim Peters6d6c1a32001-08-02 04:15:00 +0000679 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000680 if (type->tp_dictoffset && !base->tp_dictoffset) {
681 PyObject **dictptr = _PyObject_GetDictPtr(self);
682 if (dictptr != NULL) {
683 PyObject *dict = *dictptr;
684 if (dict != NULL) {
685 Py_DECREF(dict);
686 *dictptr = NULL;
687 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000688 }
689 }
690
Tim Peters0bd743c2003-11-13 22:50:00 +0000691 /* Call the base tp_dealloc(); first retrack self if
692 * basedealloc knows about gc.
693 */
694 if (PyType_IS_GC(base))
695 _PyObject_GC_TRACK(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000696 assert(basedealloc);
697 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698
699 /* Can't reference self beyond this point */
Guido van Rossum22b13872002-08-06 21:41:44 +0000700 Py_DECREF(type);
701
Guido van Rossum0906e072002-08-07 20:42:09 +0000702 endlabel:
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000703 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000704 Py_TRASHCAN_SAFE_END(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000705 --_PyTrash_delete_nesting;
706
707 /* Explanation of the weirdness around the trashcan macros:
708
709 Q. What do the trashcan macros do?
710
711 A. Read the comment titled "Trashcan mechanism" in object.h.
712 For one, this explains why there must be a call to GC-untrack
713 before the trashcan begin macro. Without understanding the
714 trashcan code, the answers to the following questions don't make
715 sense.
716
717 Q. Why do we GC-untrack before the trashcan and then immediately
718 GC-track again afterward?
719
720 A. In the case that the base class is GC-aware, the base class
721 probably GC-untracks the object. If it does that using the
722 UNTRACK macro, this will crash when the object is already
723 untracked. Because we don't know what the base class does, the
724 only safe thing is to make sure the object is tracked when we
725 call the base class dealloc. But... The trashcan begin macro
726 requires that the object is *untracked* before it is called. So
727 the dance becomes:
728
729 GC untrack
730 trashcan begin
731 GC track
732
Tim Petersf7f9e992003-11-13 21:59:32 +0000733 Q. Why did the last question say "immediately GC-track again"?
734 It's nowhere near immediately.
735
736 A. Because the code *used* to re-track immediately. Bad Idea.
737 self has a refcount of 0, and if gc ever gets its hands on it
738 (which can happen if any weakref callback gets invoked), it
739 looks like trash to gc too, and gc also tries to delete self
740 then. But we're already deleting self. Double dealloction is
741 a subtle disaster.
742
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000743 Q. Why the bizarre (net-zero) manipulation of
744 _PyTrash_delete_nesting around the trashcan macros?
745
746 A. Some base classes (e.g. list) also use the trashcan mechanism.
747 The following scenario used to be possible:
748
749 - suppose the trashcan level is one below the trashcan limit
750
751 - subtype_dealloc() is called
752
753 - the trashcan limit is not yet reached, so the trashcan level
754 is incremented and the code between trashcan begin and end is
755 executed
756
757 - this destroys much of the object's contents, including its
758 slots and __dict__
759
760 - basedealloc() is called; this is really list_dealloc(), or
761 some other type which also uses the trashcan macros
762
763 - the trashcan limit is now reached, so the object is put on the
764 trashcan's to-be-deleted-later list
765
766 - basedealloc() returns
767
768 - subtype_dealloc() decrefs the object's type
769
770 - subtype_dealloc() returns
771
772 - later, the trashcan code starts deleting the objects from its
773 to-be-deleted-later list
774
775 - subtype_dealloc() is called *AGAIN* for the same object
776
777 - at the very least (if the destroyed slots and __dict__ don't
778 cause problems) the object's type gets decref'ed a second
779 time, which is *BAD*!!!
780
781 The remedy is to make sure that if the code between trashcan
782 begin and end in subtype_dealloc() is called, the code between
783 trashcan begin and end in basedealloc() will also be called.
784 This is done by decrementing the level after passing into the
785 trashcan block, and incrementing it just before leaving the
786 block.
787
788 But now it's possible that a chain of objects consisting solely
789 of objects whose deallocator is subtype_dealloc() will defeat
790 the trashcan mechanism completely: the decremented level means
791 that the effective level never reaches the limit. Therefore, we
792 *increment* the level *before* entering the trashcan block, and
793 matchingly decrement it after leaving. This means the trashcan
794 code will trigger a little early, but that's no big deal.
795
796 Q. Are there any live examples of code in need of all this
797 complexity?
798
799 A. Yes. See SF bug 668433 for code that crashed (when Python was
800 compiled in debug mode) before the trashcan level manipulations
801 were added. For more discussion, see SF patches 581742, 575073
802 and bug 574207.
803 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000804}
805
Jeremy Hylton938ace62002-07-17 16:30:39 +0000806static PyTypeObject *solid_base(PyTypeObject *type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000807
Tim Peters6d6c1a32001-08-02 04:15:00 +0000808/* type test with subclassing support */
809
810int
811PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
812{
813 PyObject *mro;
814
815 mro = a->tp_mro;
816 if (mro != NULL) {
817 /* Deal with multiple inheritance without recursion
818 by walking the MRO tuple */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000819 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000820 assert(PyTuple_Check(mro));
821 n = PyTuple_GET_SIZE(mro);
822 for (i = 0; i < n; i++) {
823 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
824 return 1;
825 }
826 return 0;
827 }
828 else {
829 /* a is not completely initilized yet; follow tp_base */
830 do {
831 if (a == b)
832 return 1;
833 a = a->tp_base;
834 } while (a != NULL);
835 return b == &PyBaseObject_Type;
836 }
837}
838
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000839/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000840 without looking in the instance dictionary
841 (so we can't use PyObject_GetAttr) but still binding
842 it to the instance. The arguments are the object,
843 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000844 static variable used to cache the interned Python string.
845
846 Two variants:
847
848 - lookup_maybe() returns NULL without raising an exception
849 when the _PyType_Lookup() call fails;
850
851 - lookup_method() always raises an exception upon errors.
852*/
Guido van Rossum60718732001-08-28 17:47:51 +0000853
854static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000855lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000856{
857 PyObject *res;
858
859 if (*attrobj == NULL) {
860 *attrobj = PyString_InternFromString(attrstr);
861 if (*attrobj == NULL)
862 return NULL;
863 }
864 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000865 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000866 descrgetfunc f;
867 if ((f = res->ob_type->tp_descr_get) == NULL)
868 Py_INCREF(res);
869 else
870 res = f(res, self, (PyObject *)(self->ob_type));
871 }
872 return res;
873}
874
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000875static PyObject *
876lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
877{
878 PyObject *res = lookup_maybe(self, attrstr, attrobj);
879 if (res == NULL && !PyErr_Occurred())
880 PyErr_SetObject(PyExc_AttributeError, *attrobj);
881 return res;
882}
883
Guido van Rossum2730b132001-08-28 18:22:14 +0000884/* A variation of PyObject_CallMethod that uses lookup_method()
885 instead of PyObject_GetAttrString(). This uses the same convention
886 as lookup_method to cache the interned name string object. */
887
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000888static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000889call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
890{
891 va_list va;
892 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000893 va_start(va, format);
894
Guido van Rossumda21c012001-10-03 00:50:18 +0000895 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000896 if (func == NULL) {
897 va_end(va);
898 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000899 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000900 return NULL;
901 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000902
903 if (format && *format)
904 args = Py_VaBuildValue(format, va);
905 else
906 args = PyTuple_New(0);
907
908 va_end(va);
909
910 if (args == NULL)
911 return NULL;
912
913 assert(PyTuple_Check(args));
914 retval = PyObject_Call(func, args, NULL);
915
916 Py_DECREF(args);
917 Py_DECREF(func);
918
919 return retval;
920}
921
922/* Clone of call_method() that returns NotImplemented when the lookup fails. */
923
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000924static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000925call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
926{
927 va_list va;
928 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000929 va_start(va, format);
930
Guido van Rossumda21c012001-10-03 00:50:18 +0000931 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000932 if (func == NULL) {
933 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000934 if (!PyErr_Occurred()) {
935 Py_INCREF(Py_NotImplemented);
936 return Py_NotImplemented;
937 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000938 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000939 }
940
941 if (format && *format)
942 args = Py_VaBuildValue(format, va);
943 else
944 args = PyTuple_New(0);
945
946 va_end(va);
947
Guido van Rossum717ce002001-09-14 16:58:08 +0000948 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000949 return NULL;
950
Guido van Rossum717ce002001-09-14 16:58:08 +0000951 assert(PyTuple_Check(args));
952 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000953
954 Py_DECREF(args);
955 Py_DECREF(func);
956
957 return retval;
958}
959
Tim Petersa91e9642001-11-14 23:32:33 +0000960static int
961fill_classic_mro(PyObject *mro, PyObject *cls)
962{
963 PyObject *bases, *base;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000964 Py_ssize_t i, n;
Tim Petersa91e9642001-11-14 23:32:33 +0000965
966 assert(PyList_Check(mro));
967 assert(PyClass_Check(cls));
968 i = PySequence_Contains(mro, cls);
969 if (i < 0)
970 return -1;
971 if (!i) {
972 if (PyList_Append(mro, cls) < 0)
973 return -1;
974 }
975 bases = ((PyClassObject *)cls)->cl_bases;
976 assert(bases && PyTuple_Check(bases));
977 n = PyTuple_GET_SIZE(bases);
978 for (i = 0; i < n; i++) {
979 base = PyTuple_GET_ITEM(bases, i);
980 if (fill_classic_mro(mro, base) < 0)
981 return -1;
982 }
983 return 0;
984}
985
986static PyObject *
987classic_mro(PyObject *cls)
988{
989 PyObject *mro;
990
991 assert(PyClass_Check(cls));
992 mro = PyList_New(0);
993 if (mro != NULL) {
994 if (fill_classic_mro(mro, cls) == 0)
995 return mro;
996 Py_DECREF(mro);
997 }
998 return NULL;
999}
1000
Tim Petersea7f75d2002-12-07 21:39:16 +00001001/*
Guido van Rossum1f121312002-11-14 19:49:16 +00001002 Method resolution order algorithm C3 described in
1003 "A Monotonic Superclass Linearization for Dylan",
1004 by Kim Barrett, Bob Cassel, Paul Haahr,
Tim Petersea7f75d2002-12-07 21:39:16 +00001005 David A. Moon, Keith Playford, and P. Tucker Withington.
Guido van Rossum1f121312002-11-14 19:49:16 +00001006 (OOPSLA 1996)
1007
Guido van Rossum98f33732002-11-25 21:36:54 +00001008 Some notes about the rules implied by C3:
1009
Tim Petersea7f75d2002-12-07 21:39:16 +00001010 No duplicate bases.
Guido van Rossum98f33732002-11-25 21:36:54 +00001011 It isn't legal to repeat a class in a list of base classes.
1012
1013 The next three properties are the 3 constraints in "C3".
1014
Tim Petersea7f75d2002-12-07 21:39:16 +00001015 Local precendece order.
Guido van Rossum98f33732002-11-25 21:36:54 +00001016 If A precedes B in C's MRO, then A will precede B in the MRO of all
1017 subclasses of C.
1018
1019 Monotonicity.
1020 The MRO of a class must be an extension without reordering of the
1021 MRO of each of its superclasses.
1022
1023 Extended Precedence Graph (EPG).
1024 Linearization is consistent if there is a path in the EPG from
1025 each class to all its successors in the linearization. See
1026 the paper for definition of EPG.
Guido van Rossum1f121312002-11-14 19:49:16 +00001027 */
1028
Tim Petersea7f75d2002-12-07 21:39:16 +00001029static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001030tail_contains(PyObject *list, int whence, PyObject *o) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001031 Py_ssize_t j, size;
Guido van Rossum1f121312002-11-14 19:49:16 +00001032 size = PyList_GET_SIZE(list);
1033
1034 for (j = whence+1; j < size; j++) {
1035 if (PyList_GET_ITEM(list, j) == o)
1036 return 1;
1037 }
1038 return 0;
1039}
1040
Guido van Rossum98f33732002-11-25 21:36:54 +00001041static PyObject *
1042class_name(PyObject *cls)
1043{
1044 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1045 if (name == NULL) {
1046 PyErr_Clear();
1047 Py_XDECREF(name);
1048 name = PyObject_Repr(cls);
1049 }
1050 if (name == NULL)
1051 return NULL;
1052 if (!PyString_Check(name)) {
1053 Py_DECREF(name);
1054 return NULL;
1055 }
1056 return name;
1057}
1058
1059static int
1060check_duplicates(PyObject *list)
1061{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001062 Py_ssize_t i, j, n;
Guido van Rossum98f33732002-11-25 21:36:54 +00001063 /* Let's use a quadratic time algorithm,
1064 assuming that the bases lists is short.
1065 */
1066 n = PyList_GET_SIZE(list);
1067 for (i = 0; i < n; i++) {
1068 PyObject *o = PyList_GET_ITEM(list, i);
1069 for (j = i + 1; j < n; j++) {
1070 if (PyList_GET_ITEM(list, j) == o) {
1071 o = class_name(o);
1072 PyErr_Format(PyExc_TypeError,
1073 "duplicate base class %s",
1074 o ? PyString_AS_STRING(o) : "?");
1075 Py_XDECREF(o);
1076 return -1;
1077 }
1078 }
1079 }
1080 return 0;
1081}
1082
1083/* Raise a TypeError for an MRO order disagreement.
1084
1085 It's hard to produce a good error message. In the absence of better
1086 insight into error reporting, report the classes that were candidates
1087 to be put next into the MRO. There is some conflict between the
1088 order in which they should be put in the MRO, but it's hard to
1089 diagnose what constraint can't be satisfied.
1090*/
1091
1092static void
1093set_mro_error(PyObject *to_merge, int *remain)
1094{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001095 Py_ssize_t i, n, off, to_merge_size;
Guido van Rossum98f33732002-11-25 21:36:54 +00001096 char buf[1000];
1097 PyObject *k, *v;
1098 PyObject *set = PyDict_New();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001099 if (!set) return;
Guido van Rossum98f33732002-11-25 21:36:54 +00001100
1101 to_merge_size = PyList_GET_SIZE(to_merge);
1102 for (i = 0; i < to_merge_size; i++) {
1103 PyObject *L = PyList_GET_ITEM(to_merge, i);
1104 if (remain[i] < PyList_GET_SIZE(L)) {
1105 PyObject *c = PyList_GET_ITEM(L, remain[i]);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001106 if (PyDict_SetItem(set, c, Py_None) < 0) {
1107 Py_DECREF(set);
Guido van Rossum98f33732002-11-25 21:36:54 +00001108 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001109 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001110 }
1111 }
1112 n = PyDict_Size(set);
1113
Raymond Hettingerf394df42003-04-06 19:13:41 +00001114 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1115consistent method resolution\norder (MRO) for bases");
Guido van Rossum98f33732002-11-25 21:36:54 +00001116 i = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001117 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001118 PyObject *name = class_name(k);
1119 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1120 name ? PyString_AS_STRING(name) : "?");
1121 Py_XDECREF(name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001122 if (--n && (size_t)(off+1) < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001123 buf[off++] = ',';
1124 buf[off] = '\0';
1125 }
1126 }
1127 PyErr_SetString(PyExc_TypeError, buf);
1128 Py_DECREF(set);
1129}
1130
Tim Petersea7f75d2002-12-07 21:39:16 +00001131static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001132pmerge(PyObject *acc, PyObject* to_merge) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001133 Py_ssize_t i, j, to_merge_size, empty_cnt;
Guido van Rossum1f121312002-11-14 19:49:16 +00001134 int *remain;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001135 int ok;
Tim Petersea7f75d2002-12-07 21:39:16 +00001136
Guido van Rossum1f121312002-11-14 19:49:16 +00001137 to_merge_size = PyList_GET_SIZE(to_merge);
1138
Guido van Rossum98f33732002-11-25 21:36:54 +00001139 /* remain stores an index into each sublist of to_merge.
1140 remain[i] is the index of the next base in to_merge[i]
1141 that is not included in acc.
1142 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
Guido van Rossum1f121312002-11-14 19:49:16 +00001144 if (remain == NULL)
1145 return -1;
1146 for (i = 0; i < to_merge_size; i++)
1147 remain[i] = 0;
1148
1149 again:
1150 empty_cnt = 0;
1151 for (i = 0; i < to_merge_size; i++) {
1152 PyObject *candidate;
Tim Petersea7f75d2002-12-07 21:39:16 +00001153
Guido van Rossum1f121312002-11-14 19:49:16 +00001154 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1155
1156 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1157 empty_cnt++;
1158 continue;
1159 }
1160
Guido van Rossum98f33732002-11-25 21:36:54 +00001161 /* Choose next candidate for MRO.
1162
1163 The input sequences alone can determine the choice.
1164 If not, choose the class which appears in the MRO
1165 of the earliest direct superclass of the new class.
1166 */
1167
Guido van Rossum1f121312002-11-14 19:49:16 +00001168 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1169 for (j = 0; j < to_merge_size; j++) {
1170 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum98f33732002-11-25 21:36:54 +00001171 if (tail_contains(j_lst, remain[j], candidate)) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001172 goto skip; /* continue outer loop */
Guido van Rossum98f33732002-11-25 21:36:54 +00001173 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001174 }
1175 ok = PyList_Append(acc, candidate);
1176 if (ok < 0) {
1177 PyMem_Free(remain);
1178 return -1;
1179 }
1180 for (j = 0; j < to_merge_size; j++) {
1181 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum768158c2002-12-31 16:33:01 +00001182 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1183 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001184 remain[j]++;
1185 }
1186 }
1187 goto again;
Tim Peters9a6b8d82002-11-14 23:22:33 +00001188 skip: ;
Guido van Rossum1f121312002-11-14 19:49:16 +00001189 }
1190
Guido van Rossum98f33732002-11-25 21:36:54 +00001191 if (empty_cnt == to_merge_size) {
1192 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001193 return 0;
Guido van Rossum98f33732002-11-25 21:36:54 +00001194 }
1195 set_mro_error(to_merge, remain);
1196 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001197 return -1;
1198}
1199
Tim Peters6d6c1a32001-08-02 04:15:00 +00001200static PyObject *
1201mro_implementation(PyTypeObject *type)
1202{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001203 Py_ssize_t i, n;
1204 int ok;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001205 PyObject *bases, *result;
Guido van Rossum1f121312002-11-14 19:49:16 +00001206 PyObject *to_merge, *bases_aslist;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001207
Guido van Rossum63517572002-06-18 16:44:57 +00001208 if(type->tp_dict == NULL) {
1209 if(PyType_Ready(type) < 0)
1210 return NULL;
1211 }
1212
Guido van Rossum98f33732002-11-25 21:36:54 +00001213 /* Find a superclass linearization that honors the constraints
1214 of the explicit lists of bases and the constraints implied by
Tim Petersea7f75d2002-12-07 21:39:16 +00001215 each base class.
Guido van Rossum98f33732002-11-25 21:36:54 +00001216
1217 to_merge is a list of lists, where each list is a superclass
1218 linearization implied by a base class. The last element of
1219 to_merge is the declared list of bases.
1220 */
1221
Tim Peters6d6c1a32001-08-02 04:15:00 +00001222 bases = type->tp_bases;
1223 n = PyTuple_GET_SIZE(bases);
Guido van Rossum1f121312002-11-14 19:49:16 +00001224
1225 to_merge = PyList_New(n+1);
1226 if (to_merge == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001227 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001228
Tim Peters6d6c1a32001-08-02 04:15:00 +00001229 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001230 PyObject *base = PyTuple_GET_ITEM(bases, i);
1231 PyObject *parentMRO;
1232 if (PyType_Check(base))
1233 parentMRO = PySequence_List(
1234 ((PyTypeObject*)base)->tp_mro);
1235 else
1236 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001237 if (parentMRO == NULL) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001238 Py_DECREF(to_merge);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001239 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001240 }
1241
1242 PyList_SET_ITEM(to_merge, i, parentMRO);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001243 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001244
1245 bases_aslist = PySequence_List(bases);
1246 if (bases_aslist == NULL) {
1247 Py_DECREF(to_merge);
1248 return NULL;
1249 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001250 /* This is just a basic sanity check. */
1251 if (check_duplicates(bases_aslist) < 0) {
1252 Py_DECREF(to_merge);
1253 Py_DECREF(bases_aslist);
1254 return NULL;
1255 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001256 PyList_SET_ITEM(to_merge, n, bases_aslist);
1257
1258 result = Py_BuildValue("[O]", (PyObject *)type);
1259 if (result == NULL) {
1260 Py_DECREF(to_merge);
1261 return NULL;
1262 }
1263
1264 ok = pmerge(result, to_merge);
1265 Py_DECREF(to_merge);
1266 if (ok < 0) {
1267 Py_DECREF(result);
1268 return NULL;
1269 }
1270
Tim Peters6d6c1a32001-08-02 04:15:00 +00001271 return result;
1272}
1273
1274static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001275mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001276{
1277 PyTypeObject *type = (PyTypeObject *)self;
1278
Tim Peters6d6c1a32001-08-02 04:15:00 +00001279 return mro_implementation(type);
1280}
1281
1282static int
1283mro_internal(PyTypeObject *type)
1284{
1285 PyObject *mro, *result, *tuple;
Armin Rigo037d1e02005-12-29 17:07:39 +00001286 int checkit = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001287
1288 if (type->ob_type == &PyType_Type) {
1289 result = mro_implementation(type);
1290 }
1291 else {
Guido van Rossum60718732001-08-28 17:47:51 +00001292 static PyObject *mro_str;
Armin Rigo037d1e02005-12-29 17:07:39 +00001293 checkit = 1;
Guido van Rossum60718732001-08-28 17:47:51 +00001294 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001295 if (mro == NULL)
1296 return -1;
1297 result = PyObject_CallObject(mro, NULL);
1298 Py_DECREF(mro);
1299 }
1300 if (result == NULL)
1301 return -1;
1302 tuple = PySequence_Tuple(result);
1303 Py_DECREF(result);
Armin Rigo037d1e02005-12-29 17:07:39 +00001304 if (tuple == NULL)
1305 return -1;
1306 if (checkit) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307 Py_ssize_t i, len;
Armin Rigo037d1e02005-12-29 17:07:39 +00001308 PyObject *cls;
1309 PyTypeObject *solid;
1310
1311 solid = solid_base(type);
1312
1313 len = PyTuple_GET_SIZE(tuple);
1314
1315 for (i = 0; i < len; i++) {
1316 PyTypeObject *t;
1317 cls = PyTuple_GET_ITEM(tuple, i);
1318 if (PyClass_Check(cls))
1319 continue;
1320 else if (!PyType_Check(cls)) {
1321 PyErr_Format(PyExc_TypeError,
1322 "mro() returned a non-class ('%.500s')",
1323 cls->ob_type->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001324 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001325 return -1;
1326 }
1327 t = (PyTypeObject*)cls;
1328 if (!PyType_IsSubtype(solid, solid_base(t))) {
1329 PyErr_Format(PyExc_TypeError,
1330 "mro() returned base with unsuitable layout ('%.500s')",
1331 t->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001332 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001333 return -1;
1334 }
1335 }
1336 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001337 type->tp_mro = tuple;
1338 return 0;
1339}
1340
1341
1342/* Calculate the best base amongst multiple base classes.
1343 This is the first one that's on the path to the "solid base". */
1344
1345static PyTypeObject *
1346best_base(PyObject *bases)
1347{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001348 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001349 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +00001350 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001351
1352 assert(PyTuple_Check(bases));
1353 n = PyTuple_GET_SIZE(bases);
1354 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +00001355 base = NULL;
1356 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001357 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001358 base_proto = PyTuple_GET_ITEM(bases, i);
1359 if (PyClass_Check(base_proto))
1360 continue;
1361 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001362 PyErr_SetString(
1363 PyExc_TypeError,
1364 "bases must be types");
1365 return NULL;
1366 }
Tim Petersa91e9642001-11-14 23:32:33 +00001367 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001368 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001369 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001370 return NULL;
1371 }
1372 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +00001373 if (winner == NULL) {
1374 winner = candidate;
1375 base = base_i;
1376 }
1377 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001378 ;
1379 else if (PyType_IsSubtype(candidate, winner)) {
1380 winner = candidate;
1381 base = base_i;
1382 }
1383 else {
1384 PyErr_SetString(
1385 PyExc_TypeError,
1386 "multiple bases have "
1387 "instance lay-out conflict");
1388 return NULL;
1389 }
1390 }
Guido van Rossume54616c2001-12-14 04:19:56 +00001391 if (base == NULL)
1392 PyErr_SetString(PyExc_TypeError,
1393 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001394 return base;
1395}
1396
1397static int
1398extra_ivars(PyTypeObject *type, PyTypeObject *base)
1399{
Neil Schemenauerc806c882001-08-29 23:54:54 +00001400 size_t t_size = type->tp_basicsize;
1401 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001402
Guido van Rossum9676b222001-08-17 20:32:36 +00001403 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001404 if (type->tp_itemsize || base->tp_itemsize) {
1405 /* If itemsize is involved, stricter rules */
1406 return t_size != b_size ||
1407 type->tp_itemsize != base->tp_itemsize;
1408 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001409 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1410 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1411 t_size -= sizeof(PyObject *);
1412 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1413 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1414 t_size -= sizeof(PyObject *);
1415
1416 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001417}
1418
1419static PyTypeObject *
1420solid_base(PyTypeObject *type)
1421{
1422 PyTypeObject *base;
1423
1424 if (type->tp_base)
1425 base = solid_base(type->tp_base);
1426 else
1427 base = &PyBaseObject_Type;
1428 if (extra_ivars(type, base))
1429 return type;
1430 else
1431 return base;
1432}
1433
Jeremy Hylton938ace62002-07-17 16:30:39 +00001434static void object_dealloc(PyObject *);
1435static int object_init(PyObject *, PyObject *, PyObject *);
1436static int update_slot(PyTypeObject *, PyObject *);
1437static void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001438
1439static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001440subtype_dict(PyObject *obj, void *context)
1441{
1442 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1443 PyObject *dict;
1444
1445 if (dictptr == NULL) {
1446 PyErr_SetString(PyExc_AttributeError,
1447 "This object has no __dict__");
1448 return NULL;
1449 }
1450 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +00001451 if (dict == NULL)
1452 *dictptr = dict = PyDict_New();
1453 Py_XINCREF(dict);
1454 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001455}
1456
Guido van Rossum6661be32001-10-26 04:26:12 +00001457static int
1458subtype_setdict(PyObject *obj, PyObject *value, void *context)
1459{
1460 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1461 PyObject *dict;
1462
1463 if (dictptr == NULL) {
1464 PyErr_SetString(PyExc_AttributeError,
1465 "This object has no __dict__");
1466 return -1;
1467 }
Guido van Rossumd331cb52001-12-05 19:46:42 +00001468 if (value != NULL && !PyDict_Check(value)) {
Guido van Rossum6661be32001-10-26 04:26:12 +00001469 PyErr_SetString(PyExc_TypeError,
1470 "__dict__ must be set to a dictionary");
1471 return -1;
1472 }
1473 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +00001474 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +00001475 *dictptr = value;
1476 Py_XDECREF(dict);
1477 return 0;
1478}
1479
Guido van Rossumad47da02002-08-12 19:05:44 +00001480static PyObject *
1481subtype_getweakref(PyObject *obj, void *context)
1482{
1483 PyObject **weaklistptr;
1484 PyObject *result;
1485
1486 if (obj->ob_type->tp_weaklistoffset == 0) {
1487 PyErr_SetString(PyExc_AttributeError,
1488 "This object has no __weaklist__");
1489 return NULL;
1490 }
1491 assert(obj->ob_type->tp_weaklistoffset > 0);
1492 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
Guido van Rossum3747a0f2002-08-12 19:25:08 +00001493 (size_t)(obj->ob_type->tp_basicsize));
Guido van Rossumad47da02002-08-12 19:05:44 +00001494 weaklistptr = (PyObject **)
Guido van Rossum3747a0f2002-08-12 19:25:08 +00001495 ((char *)obj + obj->ob_type->tp_weaklistoffset);
Guido van Rossumad47da02002-08-12 19:05:44 +00001496 if (*weaklistptr == NULL)
1497 result = Py_None;
1498 else
1499 result = *weaklistptr;
1500 Py_INCREF(result);
1501 return result;
1502}
1503
Guido van Rossum373c7412003-01-07 13:41:37 +00001504/* Three variants on the subtype_getsets list. */
1505
1506static PyGetSetDef subtype_getsets_full[] = {
Guido van Rossumad47da02002-08-12 19:05:44 +00001507 {"__dict__", subtype_dict, subtype_setdict,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001508 PyDoc_STR("dictionary for instance variables (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001509 {"__weakref__", subtype_getweakref, NULL,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001510 PyDoc_STR("list of weak references to the object (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001511 {0}
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001512};
1513
Guido van Rossum373c7412003-01-07 13:41:37 +00001514static PyGetSetDef subtype_getsets_dict_only[] = {
1515 {"__dict__", subtype_dict, subtype_setdict,
1516 PyDoc_STR("dictionary for instance variables (if defined)")},
1517 {0}
1518};
1519
1520static PyGetSetDef subtype_getsets_weakref_only[] = {
1521 {"__weakref__", subtype_getweakref, NULL,
1522 PyDoc_STR("list of weak references to the object (if defined)")},
1523 {0}
1524};
1525
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001526static int
1527valid_identifier(PyObject *s)
1528{
Guido van Rossum03013a02002-07-16 14:30:28 +00001529 unsigned char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001530 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001531
1532 if (!PyString_Check(s)) {
1533 PyErr_SetString(PyExc_TypeError,
1534 "__slots__ must be strings");
1535 return 0;
1536 }
Guido van Rossum03013a02002-07-16 14:30:28 +00001537 p = (unsigned char *) PyString_AS_STRING(s);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001538 n = PyString_GET_SIZE(s);
1539 /* We must reject an empty name. As a hack, we bump the
1540 length to 1 so that the loop will balk on the trailing \0. */
1541 if (n == 0)
1542 n = 1;
1543 for (i = 0; i < n; i++, p++) {
1544 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1545 PyErr_SetString(PyExc_TypeError,
1546 "__slots__ must be identifiers");
1547 return 0;
1548 }
1549 }
1550 return 1;
1551}
1552
Martin v. Löwisd919a592002-10-14 21:07:28 +00001553#ifdef Py_USING_UNICODE
1554/* Replace Unicode objects in slots. */
1555
1556static PyObject *
Martin v. Löwiseb079f12006-02-16 14:32:27 +00001557_unicode_to_string(PyObject *slots, Py_ssize_t nslots)
Martin v. Löwisd919a592002-10-14 21:07:28 +00001558{
1559 PyObject *tmp = slots;
1560 PyObject *o, *o1;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00001561 Py_ssize_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001562 ssizessizeargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
Martin v. Löwisd919a592002-10-14 21:07:28 +00001563 for (i = 0; i < nslots; i++) {
1564 if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
1565 if (tmp == slots) {
1566 tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
1567 if (tmp == NULL)
1568 return NULL;
1569 }
1570 o1 = _PyUnicode_AsDefaultEncodedString
1571 (o, NULL);
1572 if (o1 == NULL) {
1573 Py_DECREF(tmp);
1574 return 0;
1575 }
1576 Py_INCREF(o1);
1577 Py_DECREF(o);
1578 PyTuple_SET_ITEM(tmp, i, o1);
1579 }
1580 }
1581 return tmp;
1582}
1583#endif
1584
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001585static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001586type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1587{
1588 PyObject *name, *bases, *dict;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001589 static char *kwlist[] = {"name", "bases", "dict", 0};
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001590 PyObject *slots, *tmp, *newslots;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001591 PyTypeObject *type, *base, *tmptype, *winner;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001592 PyHeapTypeObject *et;
Guido van Rossum6f799372001-09-20 20:46:19 +00001593 PyMemberDef *mp;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001594 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
Guido van Rossumad47da02002-08-12 19:05:44 +00001595 int j, may_add_dict, may_add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001596
Tim Peters3abca122001-10-27 19:37:48 +00001597 assert(args != NULL && PyTuple_Check(args));
1598 assert(kwds == NULL || PyDict_Check(kwds));
1599
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001600 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +00001601 {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001602 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1603 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
Tim Peters3abca122001-10-27 19:37:48 +00001604
1605 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1606 PyObject *x = PyTuple_GET_ITEM(args, 0);
1607 Py_INCREF(x->ob_type);
1608 return (PyObject *) x->ob_type;
1609 }
1610
1611 /* SF bug 475327 -- if that didn't trigger, we need 3
1612 arguments. but PyArg_ParseTupleAndKeywords below may give
1613 a msg saying type() needs exactly 3. */
1614 if (nargs + nkwds != 3) {
1615 PyErr_SetString(PyExc_TypeError,
1616 "type() takes 1 or 3 arguments");
1617 return NULL;
1618 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001619 }
1620
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001621 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001622 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1623 &name,
1624 &PyTuple_Type, &bases,
1625 &PyDict_Type, &dict))
1626 return NULL;
1627
1628 /* Determine the proper metatype to deal with this,
1629 and check for metatype conflicts while we're at it.
1630 Note that if some other metatype wins to contract,
1631 it's possible that its instances are not types. */
1632 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001633 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001634 for (i = 0; i < nbases; i++) {
1635 tmp = PyTuple_GET_ITEM(bases, i);
1636 tmptype = tmp->ob_type;
Tim Petersa91e9642001-11-14 23:32:33 +00001637 if (tmptype == &PyClass_Type)
1638 continue; /* Special case classic classes */
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001639 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001640 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001641 if (PyType_IsSubtype(tmptype, winner)) {
1642 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001643 continue;
1644 }
1645 PyErr_SetString(PyExc_TypeError,
Guido van Rossum636688d2003-04-23 12:07:22 +00001646 "metaclass conflict: "
1647 "the metaclass of a derived class "
1648 "must be a (non-strict) subclass "
1649 "of the metaclasses of all its bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001650 return NULL;
1651 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001652 if (winner != metatype) {
1653 if (winner->tp_new != type_new) /* Pass it to the winner */
1654 return winner->tp_new(winner, args, kwds);
1655 metatype = winner;
1656 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001657
1658 /* Adjust for empty tuple bases */
1659 if (nbases == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001660 bases = PyTuple_Pack(1, &PyBaseObject_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001661 if (bases == NULL)
1662 return NULL;
1663 nbases = 1;
1664 }
1665 else
1666 Py_INCREF(bases);
1667
1668 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1669
1670 /* Calculate best base, and check that all bases are type objects */
1671 base = best_base(bases);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001672 if (base == NULL) {
1673 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001674 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001675 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001676 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1677 PyErr_Format(PyExc_TypeError,
1678 "type '%.100s' is not an acceptable base type",
1679 base->tp_name);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001680 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001681 return NULL;
1682 }
1683
Tim Peters6d6c1a32001-08-02 04:15:00 +00001684 /* Check for a __slots__ sequence variable in dict, and count it */
1685 slots = PyDict_GetItemString(dict, "__slots__");
1686 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001687 add_dict = 0;
1688 add_weak = 0;
Guido van Rossumad47da02002-08-12 19:05:44 +00001689 may_add_dict = base->tp_dictoffset == 0;
1690 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1691 if (slots == NULL) {
1692 if (may_add_dict) {
1693 add_dict++;
1694 }
1695 if (may_add_weak) {
1696 add_weak++;
1697 }
1698 }
1699 else {
1700 /* Have slots */
1701
Tim Peters6d6c1a32001-08-02 04:15:00 +00001702 /* Make it into a tuple */
1703 if (PyString_Check(slots))
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001704 slots = PyTuple_Pack(1, slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001705 else
1706 slots = PySequence_Tuple(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001707 if (slots == NULL) {
1708 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001709 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001710 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001711 assert(PyTuple_Check(slots));
1712
1713 /* Are slots allowed? */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001714 nslots = PyTuple_GET_SIZE(slots);
Jeremy Hylton1c7a0ea2003-07-16 16:08:23 +00001715 if (nslots > 0 && base->tp_itemsize != 0) {
Guido van Rossumc4141872001-08-30 04:43:35 +00001716 PyErr_Format(PyExc_TypeError,
1717 "nonempty __slots__ "
1718 "not supported for subtype of '%s'",
1719 base->tp_name);
Guido van Rossumad47da02002-08-12 19:05:44 +00001720 bad_slots:
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001721 Py_DECREF(bases);
Guido van Rossumad47da02002-08-12 19:05:44 +00001722 Py_DECREF(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00001723 return NULL;
1724 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001725
Martin v. Löwisd919a592002-10-14 21:07:28 +00001726#ifdef Py_USING_UNICODE
1727 tmp = _unicode_to_string(slots, nslots);
Martin v. Löwis13b1a5c2002-10-14 21:11:34 +00001728 if (tmp != slots) {
1729 Py_DECREF(slots);
1730 slots = tmp;
1731 }
Martin v. Löwisd919a592002-10-14 21:07:28 +00001732 if (!tmp)
1733 return NULL;
1734#endif
Guido van Rossumad47da02002-08-12 19:05:44 +00001735 /* Check for valid slot names and two special cases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001736 for (i = 0; i < nslots; i++) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001737 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1738 char *s;
1739 if (!valid_identifier(tmp))
1740 goto bad_slots;
1741 assert(PyString_Check(tmp));
1742 s = PyString_AS_STRING(tmp);
1743 if (strcmp(s, "__dict__") == 0) {
1744 if (!may_add_dict || add_dict) {
1745 PyErr_SetString(PyExc_TypeError,
1746 "__dict__ slot disallowed: "
1747 "we already got one");
1748 goto bad_slots;
1749 }
1750 add_dict++;
1751 }
1752 if (strcmp(s, "__weakref__") == 0) {
1753 if (!may_add_weak || add_weak) {
1754 PyErr_SetString(PyExc_TypeError,
1755 "__weakref__ slot disallowed: "
1756 "either we already got one, "
1757 "or __itemsize__ != 0");
1758 goto bad_slots;
1759 }
1760 add_weak++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001761 }
1762 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001763
Guido van Rossumad47da02002-08-12 19:05:44 +00001764 /* Copy slots into yet another tuple, demangling names */
1765 newslots = PyTuple_New(nslots - add_dict - add_weak);
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001766 if (newslots == NULL)
Guido van Rossumad47da02002-08-12 19:05:44 +00001767 goto bad_slots;
1768 for (i = j = 0; i < nslots; i++) {
1769 char *s;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001770 tmp = PyTuple_GET_ITEM(slots, i);
Guido van Rossumad47da02002-08-12 19:05:44 +00001771 s = PyString_AS_STRING(tmp);
1772 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1773 (add_weak && strcmp(s, "__weakref__") == 0))
1774 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 tmp =_Py_Mangle(name, tmp);
1776 if (!tmp)
1777 goto bad_slots;
Guido van Rossumad47da02002-08-12 19:05:44 +00001778 PyTuple_SET_ITEM(newslots, j, tmp);
1779 j++;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001780 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001781 assert(j == nslots - add_dict - add_weak);
1782 nslots = j;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001783 Py_DECREF(slots);
1784 slots = newslots;
1785
Guido van Rossumad47da02002-08-12 19:05:44 +00001786 /* Secondary bases may provide weakrefs or dict */
1787 if (nbases > 1 &&
1788 ((may_add_dict && !add_dict) ||
1789 (may_add_weak && !add_weak))) {
1790 for (i = 0; i < nbases; i++) {
1791 tmp = PyTuple_GET_ITEM(bases, i);
1792 if (tmp == (PyObject *)base)
1793 continue; /* Skip primary base */
1794 if (PyClass_Check(tmp)) {
1795 /* Classic base class provides both */
1796 if (may_add_dict && !add_dict)
1797 add_dict++;
1798 if (may_add_weak && !add_weak)
1799 add_weak++;
1800 break;
1801 }
1802 assert(PyType_Check(tmp));
1803 tmptype = (PyTypeObject *)tmp;
1804 if (may_add_dict && !add_dict &&
1805 tmptype->tp_dictoffset != 0)
1806 add_dict++;
1807 if (may_add_weak && !add_weak &&
1808 tmptype->tp_weaklistoffset != 0)
1809 add_weak++;
1810 if (may_add_dict && !add_dict)
1811 continue;
1812 if (may_add_weak && !add_weak)
1813 continue;
1814 /* Nothing more to check */
1815 break;
1816 }
1817 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001818 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001819
1820 /* XXX From here until type is safely allocated,
1821 "return NULL" may leak slots! */
1822
1823 /* Allocate the type object */
1824 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
Guido van Rossumad47da02002-08-12 19:05:44 +00001825 if (type == NULL) {
1826 Py_XDECREF(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001827 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001828 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00001829 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001830
1831 /* Keep name and slots alive in the extended type object */
Guido van Rossume5c691a2003-03-07 15:13:17 +00001832 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001833 Py_INCREF(name);
Georg Brandlc255c7b2006-02-20 22:27:28 +00001834 et->ht_name = name;
1835 et->ht_slots = slots;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001836
Guido van Rossumdc91b992001-08-08 22:26:22 +00001837 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001838 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1839 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001840 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1841 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001842
Guido van Rossumdc91b992001-08-08 22:26:22 +00001843 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001844 type->tp_as_number = &et->as_number;
1845 type->tp_as_sequence = &et->as_sequence;
1846 type->tp_as_mapping = &et->as_mapping;
1847 type->tp_as_buffer = &et->as_buffer;
1848 type->tp_name = PyString_AS_STRING(name);
1849
1850 /* Set tp_base and tp_bases */
1851 type->tp_bases = bases;
1852 Py_INCREF(base);
1853 type->tp_base = base;
1854
Guido van Rossum687ae002001-10-15 22:03:32 +00001855 /* Initialize tp_dict from passed-in dict */
1856 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001857 if (dict == NULL) {
1858 Py_DECREF(type);
1859 return NULL;
1860 }
1861
Guido van Rossumc3542212001-08-16 09:18:56 +00001862 /* Set __module__ in the dict */
1863 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1864 tmp = PyEval_GetGlobals();
1865 if (tmp != NULL) {
1866 tmp = PyDict_GetItemString(tmp, "__name__");
1867 if (tmp != NULL) {
1868 if (PyDict_SetItemString(dict, "__module__",
1869 tmp) < 0)
1870 return NULL;
1871 }
1872 }
1873 }
1874
Tim Peters2f93e282001-10-04 05:27:00 +00001875 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00001876 and is a string. The __doc__ accessor will first look for tp_doc;
1877 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00001878 */
1879 {
1880 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1881 if (doc != NULL && PyString_Check(doc)) {
1882 const size_t n = (size_t)PyString_GET_SIZE(doc);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001883 char *tp_doc = (char *)PyObject_MALLOC(n+1);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001884 if (tp_doc == NULL) {
Tim Peters2f93e282001-10-04 05:27:00 +00001885 Py_DECREF(type);
1886 return NULL;
1887 }
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001888 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
1889 type->tp_doc = tp_doc;
Tim Peters2f93e282001-10-04 05:27:00 +00001890 }
1891 }
1892
Tim Peters6d6c1a32001-08-02 04:15:00 +00001893 /* Special-case __new__: if it's a plain function,
1894 make it a static function */
1895 tmp = PyDict_GetItemString(dict, "__new__");
1896 if (tmp != NULL && PyFunction_Check(tmp)) {
1897 tmp = PyStaticMethod_New(tmp);
1898 if (tmp == NULL) {
1899 Py_DECREF(type);
1900 return NULL;
1901 }
1902 PyDict_SetItemString(dict, "__new__", tmp);
1903 Py_DECREF(tmp);
1904 }
1905
1906 /* Add descriptors for custom slots from __slots__, or for __dict__ */
Guido van Rossume5c691a2003-03-07 15:13:17 +00001907 mp = PyHeapType_GET_MEMBERS(et);
Neil Schemenauerc806c882001-08-29 23:54:54 +00001908 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001909 if (slots != NULL) {
1910 for (i = 0; i < nslots; i++, mp++) {
1911 mp->name = PyString_AS_STRING(
1912 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001913 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001914 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001915 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001916 strcmp(mp->name, "__weakref__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001917 add_weak++;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001918 mp->type = T_OBJECT;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001919 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001920 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001921 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001922 slotoffset += sizeof(PyObject *);
1923 }
1924 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001925 if (add_dict) {
1926 if (base->tp_itemsize)
1927 type->tp_dictoffset = -(long)sizeof(PyObject *);
1928 else
1929 type->tp_dictoffset = slotoffset;
1930 slotoffset += sizeof(PyObject *);
1931 }
1932 if (add_weak) {
1933 assert(!base->tp_itemsize);
1934 type->tp_weaklistoffset = slotoffset;
1935 slotoffset += sizeof(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001936 }
1937 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001938 type->tp_itemsize = base->tp_itemsize;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001939 type->tp_members = PyHeapType_GET_MEMBERS(et);
Guido van Rossum373c7412003-01-07 13:41:37 +00001940
1941 if (type->tp_weaklistoffset && type->tp_dictoffset)
1942 type->tp_getset = subtype_getsets_full;
1943 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1944 type->tp_getset = subtype_getsets_weakref_only;
1945 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1946 type->tp_getset = subtype_getsets_dict_only;
1947 else
1948 type->tp_getset = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001949
1950 /* Special case some slots */
1951 if (type->tp_dictoffset != 0 || nslots > 0) {
1952 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1953 type->tp_getattro = PyObject_GenericGetAttr;
1954 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1955 type->tp_setattro = PyObject_GenericSetAttr;
1956 }
1957 type->tp_dealloc = subtype_dealloc;
1958
Guido van Rossum9475a232001-10-05 20:51:39 +00001959 /* Enable GC unless there are really no instance variables possible */
1960 if (!(type->tp_basicsize == sizeof(PyObject) &&
1961 type->tp_itemsize == 0))
1962 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1963
Tim Peters6d6c1a32001-08-02 04:15:00 +00001964 /* Always override allocation strategy to use regular heap */
1965 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001966 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001967 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001968 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001969 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00001970 }
1971 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001972 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001973
1974 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001975 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001976 Py_DECREF(type);
1977 return NULL;
1978 }
1979
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001980 /* Put the proper slots in place */
1981 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001982
Tim Peters6d6c1a32001-08-02 04:15:00 +00001983 return (PyObject *)type;
1984}
1985
1986/* Internal API to look for a name through the MRO.
1987 This returns a borrowed reference, and doesn't set an exception! */
1988PyObject *
1989_PyType_Lookup(PyTypeObject *type, PyObject *name)
1990{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001991 Py_ssize_t i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001992 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001993
Guido van Rossum687ae002001-10-15 22:03:32 +00001994 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001995 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00001996
1997 /* If mro is NULL, the type is either not yet initialized
1998 by PyType_Ready(), or already cleared by type_clear().
1999 Either way the safest thing to do is to return NULL. */
2000 if (mro == NULL)
2001 return NULL;
2002
Tim Peters6d6c1a32001-08-02 04:15:00 +00002003 assert(PyTuple_Check(mro));
2004 n = PyTuple_GET_SIZE(mro);
2005 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002006 base = PyTuple_GET_ITEM(mro, i);
2007 if (PyClass_Check(base))
2008 dict = ((PyClassObject *)base)->cl_dict;
2009 else {
2010 assert(PyType_Check(base));
2011 dict = ((PyTypeObject *)base)->tp_dict;
2012 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002013 assert(dict && PyDict_Check(dict));
2014 res = PyDict_GetItem(dict, name);
2015 if (res != NULL)
2016 return res;
2017 }
2018 return NULL;
2019}
2020
2021/* This is similar to PyObject_GenericGetAttr(),
2022 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2023static PyObject *
2024type_getattro(PyTypeObject *type, PyObject *name)
2025{
2026 PyTypeObject *metatype = type->ob_type;
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002027 PyObject *meta_attribute, *attribute;
2028 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002029
2030 /* Initialize this type (we'll assume the metatype is initialized) */
2031 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002032 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002033 return NULL;
2034 }
2035
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002036 /* No readable descriptor found yet */
2037 meta_get = NULL;
Tim Peters34592512002-07-11 06:23:50 +00002038
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002039 /* Look for the attribute in the metatype */
2040 meta_attribute = _PyType_Lookup(metatype, name);
2041
2042 if (meta_attribute != NULL) {
2043 meta_get = meta_attribute->ob_type->tp_descr_get;
Tim Peters34592512002-07-11 06:23:50 +00002044
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002045 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2046 /* Data descriptors implement tp_descr_set to intercept
2047 * writes. Assume the attribute is not overridden in
2048 * type's tp_dict (and bases): call the descriptor now.
2049 */
2050 return meta_get(meta_attribute, (PyObject *)type,
2051 (PyObject *)metatype);
2052 }
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002053 Py_INCREF(meta_attribute);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002054 }
2055
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002056 /* No data descriptor found on metatype. Look in tp_dict of this
2057 * type and its bases */
2058 attribute = _PyType_Lookup(type, name);
2059 if (attribute != NULL) {
2060 /* Implement descriptor functionality, if any */
2061 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002062
2063 Py_XDECREF(meta_attribute);
2064
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002065 if (local_get != NULL) {
2066 /* NULL 2nd argument indicates the descriptor was
2067 * found on the target object itself (or a base) */
2068 return local_get(attribute, (PyObject *)NULL,
2069 (PyObject *)type);
2070 }
Tim Peters34592512002-07-11 06:23:50 +00002071
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002072 Py_INCREF(attribute);
2073 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002074 }
2075
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002076 /* No attribute found in local __dict__ (or bases): use the
2077 * descriptor from the metatype, if any */
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002078 if (meta_get != NULL) {
2079 PyObject *res;
2080 res = meta_get(meta_attribute, (PyObject *)type,
2081 (PyObject *)metatype);
2082 Py_DECREF(meta_attribute);
2083 return res;
2084 }
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002085
2086 /* If an ordinary attribute was found on the metatype, return it now */
2087 if (meta_attribute != NULL) {
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002088 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002089 }
2090
2091 /* Give up */
2092 PyErr_Format(PyExc_AttributeError,
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002093 "type object '%.50s' has no attribute '%.400s'",
2094 type->tp_name, PyString_AS_STRING(name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002095 return NULL;
2096}
2097
2098static int
2099type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2100{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002101 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2102 PyErr_Format(
2103 PyExc_TypeError,
2104 "can't set attributes of built-in/extension type '%s'",
2105 type->tp_name);
2106 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002107 }
Guido van Rossum8d24ee92003-03-24 23:49:49 +00002108 /* XXX Example of how I expect this to be used...
2109 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2110 return -1;
2111 */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002112 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2113 return -1;
2114 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002115}
2116
2117static void
2118type_dealloc(PyTypeObject *type)
2119{
Guido van Rossume5c691a2003-03-07 15:13:17 +00002120 PyHeapTypeObject *et;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002121
2122 /* Assert this is a heap-allocated type object */
2123 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002124 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00002125 PyObject_ClearWeakRefs((PyObject *)type);
Guido van Rossume5c691a2003-03-07 15:13:17 +00002126 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002127 Py_XDECREF(type->tp_base);
2128 Py_XDECREF(type->tp_dict);
2129 Py_XDECREF(type->tp_bases);
2130 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00002131 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00002132 Py_XDECREF(type->tp_subclasses);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002133 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2134 * of most other objects. It's okay to cast it to char *.
2135 */
2136 PyObject_Free((char *)type->tp_doc);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002137 Py_XDECREF(et->ht_name);
2138 Py_XDECREF(et->ht_slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002139 type->ob_type->tp_free((PyObject *)type);
2140}
2141
Guido van Rossum1c450732001-10-08 15:18:27 +00002142static PyObject *
2143type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2144{
2145 PyObject *list, *raw, *ref;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002146 Py_ssize_t i, n;
Guido van Rossum1c450732001-10-08 15:18:27 +00002147
2148 list = PyList_New(0);
2149 if (list == NULL)
2150 return NULL;
2151 raw = type->tp_subclasses;
2152 if (raw == NULL)
2153 return list;
2154 assert(PyList_Check(raw));
2155 n = PyList_GET_SIZE(raw);
2156 for (i = 0; i < n; i++) {
2157 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00002158 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00002159 ref = PyWeakref_GET_OBJECT(ref);
2160 if (ref != Py_None) {
2161 if (PyList_Append(list, ref) < 0) {
2162 Py_DECREF(list);
2163 return NULL;
2164 }
2165 }
2166 }
2167 return list;
2168}
2169
Tim Peters6d6c1a32001-08-02 04:15:00 +00002170static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002171 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002172 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
Guido van Rossum1c450732001-10-08 15:18:27 +00002173 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002174 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002175 {0}
2176};
2177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002178PyDoc_STRVAR(type_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002179"type(object) -> the object's type\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002180"type(name, bases, dict) -> a new type");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002181
Guido van Rossum048eb752001-10-02 21:24:57 +00002182static int
2183type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2184{
Guido van Rossuma3862092002-06-10 15:24:42 +00002185 /* Because of type_is_gc(), the collector only calls this
2186 for heaptypes. */
2187 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002188
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002189 Py_VISIT(type->tp_dict);
2190 Py_VISIT(type->tp_cache);
2191 Py_VISIT(type->tp_mro);
2192 Py_VISIT(type->tp_bases);
2193 Py_VISIT(type->tp_base);
Guido van Rossuma3862092002-06-10 15:24:42 +00002194
2195 /* There's no need to visit type->tp_subclasses or
Georg Brandlc255c7b2006-02-20 22:27:28 +00002196 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
Guido van Rossuma3862092002-06-10 15:24:42 +00002197 in cycles; tp_subclasses is a list of weak references,
2198 and slots is a tuple of strings. */
Guido van Rossum048eb752001-10-02 21:24:57 +00002199
Guido van Rossum048eb752001-10-02 21:24:57 +00002200 return 0;
2201}
2202
2203static int
2204type_clear(PyTypeObject *type)
2205{
Guido van Rossuma3862092002-06-10 15:24:42 +00002206 /* Because of type_is_gc(), the collector only calls this
2207 for heaptypes. */
2208 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002209
Guido van Rossuma3862092002-06-10 15:24:42 +00002210 /* The only field we need to clear is tp_mro, which is part of a
2211 hard cycle (its first element is the class itself) that won't
2212 be broken otherwise (it's a tuple and tuples don't have a
2213 tp_clear handler). None of the other fields need to be
2214 cleared, and here's why:
Guido van Rossum048eb752001-10-02 21:24:57 +00002215
Guido van Rossuma3862092002-06-10 15:24:42 +00002216 tp_dict:
2217 It is a dict, so the collector will call its tp_clear.
2218
2219 tp_cache:
2220 Not used; if it were, it would be a dict.
2221
2222 tp_bases, tp_base:
2223 If these are involved in a cycle, there must be at least
2224 one other, mutable object in the cycle, e.g. a base
2225 class's dict; the cycle will be broken that way.
2226
2227 tp_subclasses:
2228 A list of weak references can't be part of a cycle; and
2229 lists have their own tp_clear.
2230
Guido van Rossume5c691a2003-03-07 15:13:17 +00002231 slots (in PyHeapTypeObject):
Guido van Rossuma3862092002-06-10 15:24:42 +00002232 A tuple of strings can't be part of a cycle.
2233 */
2234
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002235 Py_CLEAR(type->tp_mro);
Guido van Rossum048eb752001-10-02 21:24:57 +00002236
2237 return 0;
2238}
2239
2240static int
2241type_is_gc(PyTypeObject *type)
2242{
2243 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2244}
2245
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002246PyTypeObject PyType_Type = {
2247 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002248 0, /* ob_size */
2249 "type", /* tp_name */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002250 sizeof(PyHeapTypeObject), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00002251 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002252 (destructor)type_dealloc, /* tp_dealloc */
2253 0, /* tp_print */
2254 0, /* tp_getattr */
2255 0, /* tp_setattr */
2256 type_compare, /* tp_compare */
2257 (reprfunc)type_repr, /* tp_repr */
2258 0, /* tp_as_number */
2259 0, /* tp_as_sequence */
2260 0, /* tp_as_mapping */
2261 (hashfunc)_Py_HashPointer, /* tp_hash */
2262 (ternaryfunc)type_call, /* tp_call */
2263 0, /* tp_str */
2264 (getattrofunc)type_getattro, /* tp_getattro */
2265 (setattrofunc)type_setattro, /* tp_setattro */
2266 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00002267 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2268 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002269 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00002270 (traverseproc)type_traverse, /* tp_traverse */
2271 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002272 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00002273 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002274 0, /* tp_iter */
2275 0, /* tp_iternext */
2276 type_methods, /* tp_methods */
2277 type_members, /* tp_members */
2278 type_getsets, /* tp_getset */
2279 0, /* tp_base */
2280 0, /* tp_dict */
2281 0, /* tp_descr_get */
2282 0, /* tp_descr_set */
2283 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2284 0, /* tp_init */
2285 0, /* tp_alloc */
2286 type_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002287 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00002288 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002289};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002290
2291
2292/* The base type of all types (eventually)... except itself. */
2293
2294static int
2295object_init(PyObject *self, PyObject *args, PyObject *kwds)
2296{
2297 return 0;
2298}
2299
Guido van Rossum298e4212003-02-13 16:30:16 +00002300/* If we don't have a tp_new for a new-style class, new will use this one.
2301 Therefore this should take no arguments/keywords. However, this new may
2302 also be inherited by objects that define a tp_init but no tp_new. These
2303 objects WILL pass argumets to tp_new, because it gets the same args as
2304 tp_init. So only allow arguments if we aren't using the default init, in
2305 which case we expect init to handle argument parsing. */
2306static PyObject *
2307object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2308{
2309 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2310 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2311 PyErr_SetString(PyExc_TypeError,
2312 "default __new__ takes no parameters");
2313 return NULL;
2314 }
2315 return type->tp_alloc(type, 0);
2316}
2317
Tim Peters6d6c1a32001-08-02 04:15:00 +00002318static void
2319object_dealloc(PyObject *self)
2320{
2321 self->ob_type->tp_free(self);
2322}
2323
Guido van Rossum8e248182001-08-12 05:17:56 +00002324static PyObject *
2325object_repr(PyObject *self)
2326{
Guido van Rossum76e69632001-08-16 18:52:43 +00002327 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00002328 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002329
Guido van Rossum76e69632001-08-16 18:52:43 +00002330 type = self->ob_type;
2331 mod = type_module(type, NULL);
2332 if (mod == NULL)
2333 PyErr_Clear();
2334 else if (!PyString_Check(mod)) {
2335 Py_DECREF(mod);
2336 mod = NULL;
2337 }
2338 name = type_name(type, NULL);
2339 if (name == NULL)
2340 return NULL;
2341 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00002342 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002343 PyString_AS_STRING(mod),
2344 PyString_AS_STRING(name),
2345 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002346 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00002347 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002348 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002349 Py_XDECREF(mod);
2350 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00002351 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002352}
2353
Guido van Rossumb8f63662001-08-15 23:57:02 +00002354static PyObject *
2355object_str(PyObject *self)
2356{
2357 unaryfunc f;
2358
2359 f = self->ob_type->tp_repr;
2360 if (f == NULL)
2361 f = object_repr;
2362 return f(self);
2363}
2364
Guido van Rossum8e248182001-08-12 05:17:56 +00002365static long
2366object_hash(PyObject *self)
2367{
2368 return _Py_HashPointer(self);
2369}
Guido van Rossum8e248182001-08-12 05:17:56 +00002370
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002371static PyObject *
2372object_get_class(PyObject *self, void *closure)
2373{
2374 Py_INCREF(self->ob_type);
2375 return (PyObject *)(self->ob_type);
2376}
2377
2378static int
2379equiv_structs(PyTypeObject *a, PyTypeObject *b)
2380{
2381 return a == b ||
2382 (a != NULL &&
2383 b != NULL &&
2384 a->tp_basicsize == b->tp_basicsize &&
2385 a->tp_itemsize == b->tp_itemsize &&
2386 a->tp_dictoffset == b->tp_dictoffset &&
2387 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2388 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2389 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2390}
2391
2392static int
2393same_slots_added(PyTypeObject *a, PyTypeObject *b)
2394{
2395 PyTypeObject *base = a->tp_base;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002396 Py_ssize_t size;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002397
2398 if (base != b->tp_base)
2399 return 0;
2400 if (equiv_structs(a, base) && equiv_structs(b, base))
2401 return 1;
2402 size = base->tp_basicsize;
2403 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2404 size += sizeof(PyObject *);
2405 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2406 size += sizeof(PyObject *);
2407 return size == a->tp_basicsize && size == b->tp_basicsize;
2408}
2409
2410static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002411compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002412{
2413 PyTypeObject *newbase, *oldbase;
2414
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002415 if (newto->tp_dealloc != oldto->tp_dealloc ||
2416 newto->tp_free != oldto->tp_free)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002417 {
2418 PyErr_Format(PyExc_TypeError,
2419 "%s assignment: "
2420 "'%s' deallocator differs from '%s'",
2421 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002422 newto->tp_name,
2423 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002424 return 0;
2425 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002426 newbase = newto;
2427 oldbase = oldto;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002428 while (equiv_structs(newbase, newbase->tp_base))
2429 newbase = newbase->tp_base;
2430 while (equiv_structs(oldbase, oldbase->tp_base))
2431 oldbase = oldbase->tp_base;
2432 if (newbase != oldbase &&
2433 (newbase->tp_base != oldbase->tp_base ||
2434 !same_slots_added(newbase, oldbase))) {
2435 PyErr_Format(PyExc_TypeError,
2436 "%s assignment: "
2437 "'%s' object layout differs from '%s'",
2438 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002439 newto->tp_name,
2440 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002441 return 0;
2442 }
Tim Petersea7f75d2002-12-07 21:39:16 +00002443
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002444 return 1;
2445}
2446
2447static int
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002448object_set_class(PyObject *self, PyObject *value, void *closure)
2449{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002450 PyTypeObject *oldto = self->ob_type;
2451 PyTypeObject *newto;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002452
Guido van Rossumb6b89422002-04-15 01:03:30 +00002453 if (value == NULL) {
2454 PyErr_SetString(PyExc_TypeError,
2455 "can't delete __class__ attribute");
2456 return -1;
2457 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002458 if (!PyType_Check(value)) {
2459 PyErr_Format(PyExc_TypeError,
2460 "__class__ must be set to new-style class, not '%s' object",
2461 value->ob_type->tp_name);
2462 return -1;
2463 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002464 newto = (PyTypeObject *)value;
2465 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2466 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
Guido van Rossum40af8892002-08-10 05:42:07 +00002467 {
2468 PyErr_Format(PyExc_TypeError,
2469 "__class__ assignment: only for heap types");
2470 return -1;
2471 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002472 if (compatible_for_assignment(newto, oldto, "__class__")) {
2473 Py_INCREF(newto);
2474 self->ob_type = newto;
2475 Py_DECREF(oldto);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002476 return 0;
2477 }
2478 else {
Guido van Rossum9ee4b942002-05-24 18:47:47 +00002479 return -1;
2480 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002481}
2482
2483static PyGetSetDef object_getsets[] = {
2484 {"__class__", object_get_class, object_set_class,
Neal Norwitz858e34f2002-08-13 17:18:45 +00002485 PyDoc_STR("the object's class")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002486 {0}
2487};
2488
Guido van Rossumc53f0092003-02-18 22:05:12 +00002489
Guido van Rossum036f9992003-02-21 22:02:54 +00002490/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2491 We fall back to helpers in copy_reg for:
2492 - pickle protocols < 2
2493 - calculating the list of slot names (done only once per class)
2494 - the __newobj__ function (which is used as a token but never called)
2495*/
2496
2497static PyObject *
2498import_copy_reg(void)
2499{
2500 static PyObject *copy_reg_str;
Guido van Rossum3926a632001-09-25 16:25:58 +00002501
2502 if (!copy_reg_str) {
2503 copy_reg_str = PyString_InternFromString("copy_reg");
2504 if (copy_reg_str == NULL)
2505 return NULL;
2506 }
Guido van Rossum036f9992003-02-21 22:02:54 +00002507
2508 return PyImport_Import(copy_reg_str);
2509}
2510
2511static PyObject *
2512slotnames(PyObject *cls)
2513{
2514 PyObject *clsdict;
2515 PyObject *copy_reg;
2516 PyObject *slotnames;
2517
2518 if (!PyType_Check(cls)) {
2519 Py_INCREF(Py_None);
2520 return Py_None;
2521 }
2522
2523 clsdict = ((PyTypeObject *)cls)->tp_dict;
2524 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
Armin Rigoec862b92005-09-24 22:58:41 +00002525 if (slotnames != NULL && PyList_Check(slotnames)) {
Guido van Rossum036f9992003-02-21 22:02:54 +00002526 Py_INCREF(slotnames);
2527 return slotnames;
2528 }
2529
2530 copy_reg = import_copy_reg();
2531 if (copy_reg == NULL)
2532 return NULL;
2533
2534 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2535 Py_DECREF(copy_reg);
2536 if (slotnames != NULL &&
2537 slotnames != Py_None &&
2538 !PyList_Check(slotnames))
2539 {
2540 PyErr_SetString(PyExc_TypeError,
2541 "copy_reg._slotnames didn't return a list or None");
2542 Py_DECREF(slotnames);
2543 slotnames = NULL;
2544 }
2545
2546 return slotnames;
2547}
2548
2549static PyObject *
2550reduce_2(PyObject *obj)
2551{
2552 PyObject *cls, *getnewargs;
2553 PyObject *args = NULL, *args2 = NULL;
2554 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2555 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2556 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002557 Py_ssize_t i, n;
Guido van Rossum036f9992003-02-21 22:02:54 +00002558
2559 cls = PyObject_GetAttrString(obj, "__class__");
2560 if (cls == NULL)
2561 return NULL;
2562
2563 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2564 if (getnewargs != NULL) {
2565 args = PyObject_CallObject(getnewargs, NULL);
2566 Py_DECREF(getnewargs);
2567 if (args != NULL && !PyTuple_Check(args)) {
2568 PyErr_SetString(PyExc_TypeError,
2569 "__getnewargs__ should return a tuple");
2570 goto end;
2571 }
2572 }
2573 else {
2574 PyErr_Clear();
2575 args = PyTuple_New(0);
2576 }
2577 if (args == NULL)
2578 goto end;
2579
2580 getstate = PyObject_GetAttrString(obj, "__getstate__");
2581 if (getstate != NULL) {
2582 state = PyObject_CallObject(getstate, NULL);
2583 Py_DECREF(getstate);
Neal Norwitze2fdc612003-06-08 13:19:58 +00002584 if (state == NULL)
2585 goto end;
Guido van Rossum036f9992003-02-21 22:02:54 +00002586 }
2587 else {
Jim Fulton8a1a5942004-02-08 04:21:26 +00002588 PyErr_Clear();
Guido van Rossum036f9992003-02-21 22:02:54 +00002589 state = PyObject_GetAttrString(obj, "__dict__");
2590 if (state == NULL) {
2591 PyErr_Clear();
2592 state = Py_None;
2593 Py_INCREF(state);
2594 }
2595 names = slotnames(cls);
2596 if (names == NULL)
2597 goto end;
2598 if (names != Py_None) {
2599 assert(PyList_Check(names));
2600 slots = PyDict_New();
2601 if (slots == NULL)
2602 goto end;
2603 n = 0;
2604 /* Can't pre-compute the list size; the list
2605 is stored on the class so accessible to other
2606 threads, which may be run by DECREF */
2607 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2608 PyObject *name, *value;
2609 name = PyList_GET_ITEM(names, i);
2610 value = PyObject_GetAttr(obj, name);
2611 if (value == NULL)
2612 PyErr_Clear();
2613 else {
2614 int err = PyDict_SetItem(slots, name,
2615 value);
2616 Py_DECREF(value);
2617 if (err)
2618 goto end;
2619 n++;
2620 }
2621 }
2622 if (n) {
2623 state = Py_BuildValue("(NO)", state, slots);
2624 if (state == NULL)
2625 goto end;
2626 }
2627 }
2628 }
2629
2630 if (!PyList_Check(obj)) {
2631 listitems = Py_None;
2632 Py_INCREF(listitems);
2633 }
2634 else {
2635 listitems = PyObject_GetIter(obj);
2636 if (listitems == NULL)
2637 goto end;
2638 }
2639
2640 if (!PyDict_Check(obj)) {
2641 dictitems = Py_None;
2642 Py_INCREF(dictitems);
2643 }
2644 else {
2645 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2646 if (dictitems == NULL)
2647 goto end;
2648 }
2649
2650 copy_reg = import_copy_reg();
2651 if (copy_reg == NULL)
2652 goto end;
2653 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2654 if (newobj == NULL)
2655 goto end;
2656
2657 n = PyTuple_GET_SIZE(args);
2658 args2 = PyTuple_New(n+1);
2659 if (args2 == NULL)
2660 goto end;
2661 PyTuple_SET_ITEM(args2, 0, cls);
2662 cls = NULL;
2663 for (i = 0; i < n; i++) {
2664 PyObject *v = PyTuple_GET_ITEM(args, i);
2665 Py_INCREF(v);
2666 PyTuple_SET_ITEM(args2, i+1, v);
2667 }
2668
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002669 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
Guido van Rossum036f9992003-02-21 22:02:54 +00002670
2671 end:
2672 Py_XDECREF(cls);
2673 Py_XDECREF(args);
2674 Py_XDECREF(args2);
Jeremy Hyltond06483c2003-04-09 21:01:42 +00002675 Py_XDECREF(slots);
Guido van Rossum036f9992003-02-21 22:02:54 +00002676 Py_XDECREF(state);
2677 Py_XDECREF(names);
2678 Py_XDECREF(listitems);
2679 Py_XDECREF(dictitems);
2680 Py_XDECREF(copy_reg);
2681 Py_XDECREF(newobj);
2682 return res;
2683}
2684
2685static PyObject *
2686object_reduce_ex(PyObject *self, PyObject *args)
2687{
2688 /* Call copy_reg._reduce_ex(self, proto) */
2689 PyObject *reduce, *copy_reg, *res;
2690 int proto = 0;
2691
2692 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2693 return NULL;
2694
2695 reduce = PyObject_GetAttrString(self, "__reduce__");
2696 if (reduce == NULL)
2697 PyErr_Clear();
2698 else {
2699 PyObject *cls, *clsreduce, *objreduce;
2700 int override;
2701 cls = PyObject_GetAttrString(self, "__class__");
2702 if (cls == NULL) {
2703 Py_DECREF(reduce);
2704 return NULL;
2705 }
2706 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2707 Py_DECREF(cls);
2708 if (clsreduce == NULL) {
2709 Py_DECREF(reduce);
2710 return NULL;
2711 }
2712 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2713 "__reduce__");
2714 override = (clsreduce != objreduce);
2715 Py_DECREF(clsreduce);
2716 if (override) {
2717 res = PyObject_CallObject(reduce, NULL);
2718 Py_DECREF(reduce);
2719 return res;
2720 }
2721 else
2722 Py_DECREF(reduce);
2723 }
2724
2725 if (proto >= 2)
2726 return reduce_2(self);
2727
2728 copy_reg = import_copy_reg();
Guido van Rossum3926a632001-09-25 16:25:58 +00002729 if (!copy_reg)
2730 return NULL;
Guido van Rossum036f9992003-02-21 22:02:54 +00002731
Guido van Rossumc53f0092003-02-18 22:05:12 +00002732 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
Guido van Rossum3926a632001-09-25 16:25:58 +00002733 Py_DECREF(copy_reg);
Guido van Rossum036f9992003-02-21 22:02:54 +00002734
Guido van Rossum3926a632001-09-25 16:25:58 +00002735 return res;
2736}
2737
2738static PyMethodDef object_methods[] = {
Guido van Rossumc53f0092003-02-18 22:05:12 +00002739 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2740 PyDoc_STR("helper for pickle")},
2741 {"__reduce__", object_reduce_ex, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002742 PyDoc_STR("helper for pickle")},
Guido van Rossum3926a632001-09-25 16:25:58 +00002743 {0}
2744};
2745
Guido van Rossum036f9992003-02-21 22:02:54 +00002746
Tim Peters6d6c1a32001-08-02 04:15:00 +00002747PyTypeObject PyBaseObject_Type = {
2748 PyObject_HEAD_INIT(&PyType_Type)
2749 0, /* ob_size */
2750 "object", /* tp_name */
2751 sizeof(PyObject), /* tp_basicsize */
2752 0, /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002753 object_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002754 0, /* tp_print */
2755 0, /* tp_getattr */
2756 0, /* tp_setattr */
2757 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002758 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002759 0, /* tp_as_number */
2760 0, /* tp_as_sequence */
2761 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002762 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002763 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002764 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002765 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00002766 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002767 0, /* tp_as_buffer */
2768 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002769 PyDoc_STR("The most base type"), /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002770 0, /* tp_traverse */
2771 0, /* tp_clear */
2772 0, /* tp_richcompare */
2773 0, /* tp_weaklistoffset */
2774 0, /* tp_iter */
2775 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00002776 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002777 0, /* tp_members */
2778 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002779 0, /* tp_base */
2780 0, /* tp_dict */
2781 0, /* tp_descr_get */
2782 0, /* tp_descr_set */
2783 0, /* tp_dictoffset */
2784 object_init, /* tp_init */
2785 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossum298e4212003-02-13 16:30:16 +00002786 object_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002787 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002788};
2789
2790
2791/* Initialize the __dict__ in a type object */
2792
2793static int
2794add_methods(PyTypeObject *type, PyMethodDef *meth)
2795{
Guido van Rossum687ae002001-10-15 22:03:32 +00002796 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002797
2798 for (; meth->ml_name != NULL; meth++) {
2799 PyObject *descr;
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002800 if (PyDict_GetItemString(dict, meth->ml_name) &&
2801 !(meth->ml_flags & METH_COEXIST))
2802 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00002803 if (meth->ml_flags & METH_CLASS) {
2804 if (meth->ml_flags & METH_STATIC) {
2805 PyErr_SetString(PyExc_ValueError,
2806 "method cannot be both class and static");
2807 return -1;
2808 }
Tim Petersbca1cbc2002-12-09 22:56:13 +00002809 descr = PyDescr_NewClassMethod(type, meth);
Fred Drake7bf97152002-03-28 05:33:33 +00002810 }
2811 else if (meth->ml_flags & METH_STATIC) {
Guido van Rossum9af48ff2003-02-11 17:12:46 +00002812 PyObject *cfunc = PyCFunction_New(meth, NULL);
2813 if (cfunc == NULL)
2814 return -1;
2815 descr = PyStaticMethod_New(cfunc);
2816 Py_DECREF(cfunc);
Fred Drake7bf97152002-03-28 05:33:33 +00002817 }
2818 else {
2819 descr = PyDescr_NewMethod(type, meth);
2820 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002821 if (descr == NULL)
2822 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00002823 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002824 return -1;
2825 Py_DECREF(descr);
2826 }
2827 return 0;
2828}
2829
2830static int
Guido van Rossum6f799372001-09-20 20:46:19 +00002831add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002832{
Guido van Rossum687ae002001-10-15 22:03:32 +00002833 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002834
2835 for (; memb->name != NULL; memb++) {
2836 PyObject *descr;
2837 if (PyDict_GetItemString(dict, memb->name))
2838 continue;
2839 descr = PyDescr_NewMember(type, memb);
2840 if (descr == NULL)
2841 return -1;
2842 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2843 return -1;
2844 Py_DECREF(descr);
2845 }
2846 return 0;
2847}
2848
2849static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00002850add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002851{
Guido van Rossum687ae002001-10-15 22:03:32 +00002852 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002853
2854 for (; gsp->name != NULL; gsp++) {
2855 PyObject *descr;
2856 if (PyDict_GetItemString(dict, gsp->name))
2857 continue;
2858 descr = PyDescr_NewGetSet(type, gsp);
2859
2860 if (descr == NULL)
2861 return -1;
2862 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2863 return -1;
2864 Py_DECREF(descr);
2865 }
2866 return 0;
2867}
2868
Guido van Rossum13d52f02001-08-10 21:24:08 +00002869static void
2870inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002871{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002872 Py_ssize_t oldsize, newsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002873
Guido van Rossum13d52f02001-08-10 21:24:08 +00002874 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00002875 oldsize = base->tp_basicsize;
2876 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2877 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2878 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00002879 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00002880 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00002881 if (type->tp_traverse == NULL)
2882 type->tp_traverse = base->tp_traverse;
2883 if (type->tp_clear == NULL)
2884 type->tp_clear = base->tp_clear;
2885 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002886 {
Guido van Rossumf884b742001-12-17 17:14:22 +00002887 /* The condition below could use some explanation.
2888 It appears that tp_new is not inherited for static types
2889 whose base class is 'object'; this seems to be a precaution
2890 so that old extension types don't suddenly become
2891 callable (object.__new__ wouldn't insure the invariants
2892 that the extension type's own factory function ensures).
2893 Heap types, of course, are under our control, so they do
2894 inherit tp_new; static extension types that specify some
2895 other built-in type as the default are considered
2896 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00002897 if (base != &PyBaseObject_Type ||
2898 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2899 if (type->tp_new == NULL)
2900 type->tp_new = base->tp_new;
2901 }
2902 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00002903 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00002904
2905 /* Copy other non-function slots */
2906
2907#undef COPYVAL
2908#define COPYVAL(SLOT) \
2909 if (type->SLOT == 0) type->SLOT = base->SLOT
2910
2911 COPYVAL(tp_itemsize);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002912 COPYVAL(tp_weaklistoffset);
2913 COPYVAL(tp_dictoffset);
Guido van Rossum13d52f02001-08-10 21:24:08 +00002914}
2915
2916static void
2917inherit_slots(PyTypeObject *type, PyTypeObject *base)
2918{
2919 PyTypeObject *basebase;
2920
2921#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00002922#undef COPYSLOT
2923#undef COPYNUM
2924#undef COPYSEQ
2925#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00002926#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00002927
2928#define SLOTDEFINED(SLOT) \
2929 (base->SLOT != 0 && \
2930 (basebase == NULL || base->SLOT != basebase->SLOT))
2931
Tim Peters6d6c1a32001-08-02 04:15:00 +00002932#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00002933 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00002934
2935#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
2936#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
2937#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00002938#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002939
Guido van Rossum13d52f02001-08-10 21:24:08 +00002940 /* This won't inherit indirect slots (from tp_as_number etc.)
2941 if type doesn't provide the space. */
2942
2943 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
2944 basebase = base->tp_base;
2945 if (basebase->tp_as_number == NULL)
2946 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002947 COPYNUM(nb_add);
2948 COPYNUM(nb_subtract);
2949 COPYNUM(nb_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002950 COPYNUM(nb_remainder);
2951 COPYNUM(nb_divmod);
2952 COPYNUM(nb_power);
2953 COPYNUM(nb_negative);
2954 COPYNUM(nb_positive);
2955 COPYNUM(nb_absolute);
2956 COPYNUM(nb_nonzero);
2957 COPYNUM(nb_invert);
2958 COPYNUM(nb_lshift);
2959 COPYNUM(nb_rshift);
2960 COPYNUM(nb_and);
2961 COPYNUM(nb_xor);
2962 COPYNUM(nb_or);
2963 COPYNUM(nb_coerce);
2964 COPYNUM(nb_int);
2965 COPYNUM(nb_long);
2966 COPYNUM(nb_float);
2967 COPYNUM(nb_oct);
2968 COPYNUM(nb_hex);
2969 COPYNUM(nb_inplace_add);
2970 COPYNUM(nb_inplace_subtract);
2971 COPYNUM(nb_inplace_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002972 COPYNUM(nb_inplace_remainder);
2973 COPYNUM(nb_inplace_power);
2974 COPYNUM(nb_inplace_lshift);
2975 COPYNUM(nb_inplace_rshift);
2976 COPYNUM(nb_inplace_and);
2977 COPYNUM(nb_inplace_xor);
2978 COPYNUM(nb_inplace_or);
Neal Norwitzbcc0db82006-03-24 08:14:36 +00002979 COPYNUM(nb_true_divide);
2980 COPYNUM(nb_floor_divide);
2981 COPYNUM(nb_inplace_true_divide);
2982 COPYNUM(nb_inplace_floor_divide);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002983 COPYNUM(nb_index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002984 }
2985
Guido van Rossum13d52f02001-08-10 21:24:08 +00002986 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
2987 basebase = base->tp_base;
2988 if (basebase->tp_as_sequence == NULL)
2989 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002990 COPYSEQ(sq_length);
2991 COPYSEQ(sq_concat);
2992 COPYSEQ(sq_repeat);
2993 COPYSEQ(sq_item);
2994 COPYSEQ(sq_slice);
2995 COPYSEQ(sq_ass_item);
2996 COPYSEQ(sq_ass_slice);
2997 COPYSEQ(sq_contains);
2998 COPYSEQ(sq_inplace_concat);
2999 COPYSEQ(sq_inplace_repeat);
3000 }
3001
Guido van Rossum13d52f02001-08-10 21:24:08 +00003002 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3003 basebase = base->tp_base;
3004 if (basebase->tp_as_mapping == NULL)
3005 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003006 COPYMAP(mp_length);
3007 COPYMAP(mp_subscript);
3008 COPYMAP(mp_ass_subscript);
3009 }
3010
Tim Petersfc57ccb2001-10-12 02:38:24 +00003011 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3012 basebase = base->tp_base;
3013 if (basebase->tp_as_buffer == NULL)
3014 basebase = NULL;
3015 COPYBUF(bf_getreadbuffer);
3016 COPYBUF(bf_getwritebuffer);
3017 COPYBUF(bf_getsegcount);
3018 COPYBUF(bf_getcharbuffer);
3019 }
3020
Guido van Rossum13d52f02001-08-10 21:24:08 +00003021 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003022
Tim Peters6d6c1a32001-08-02 04:15:00 +00003023 COPYSLOT(tp_dealloc);
3024 COPYSLOT(tp_print);
3025 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3026 type->tp_getattr = base->tp_getattr;
3027 type->tp_getattro = base->tp_getattro;
3028 }
3029 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3030 type->tp_setattr = base->tp_setattr;
3031 type->tp_setattro = base->tp_setattro;
3032 }
3033 /* tp_compare see tp_richcompare */
3034 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003035 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003036 COPYSLOT(tp_call);
3037 COPYSLOT(tp_str);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003038 {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003039 if (type->tp_compare == NULL &&
3040 type->tp_richcompare == NULL &&
3041 type->tp_hash == NULL)
3042 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003043 type->tp_compare = base->tp_compare;
3044 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003045 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003046 }
3047 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003048 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003049 COPYSLOT(tp_iter);
3050 COPYSLOT(tp_iternext);
3051 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003052 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003053 COPYSLOT(tp_descr_get);
3054 COPYSLOT(tp_descr_set);
3055 COPYSLOT(tp_dictoffset);
3056 COPYSLOT(tp_init);
3057 COPYSLOT(tp_alloc);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00003058 COPYSLOT(tp_is_gc);
Tim Peters3cfe7542003-05-21 21:29:48 +00003059 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3060 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3061 /* They agree about gc. */
3062 COPYSLOT(tp_free);
3063 }
3064 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3065 type->tp_free == NULL &&
3066 base->tp_free == _PyObject_Del) {
3067 /* A bit of magic to plug in the correct default
3068 * tp_free function when a derived class adds gc,
3069 * didn't define tp_free, and the base uses the
3070 * default non-gc tp_free.
3071 */
3072 type->tp_free = PyObject_GC_Del;
3073 }
3074 /* else they didn't agree about gc, and there isn't something
3075 * obvious to be done -- the type is on its own.
3076 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003077 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003078}
3079
Jeremy Hylton938ace62002-07-17 16:30:39 +00003080static int add_operators(PyTypeObject *);
Guido van Rossum13d52f02001-08-10 21:24:08 +00003081
Tim Peters6d6c1a32001-08-02 04:15:00 +00003082int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003083PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003084{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003085 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003086 PyTypeObject *base;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003087 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003088
Guido van Rossumcab05802002-06-10 15:29:03 +00003089 if (type->tp_flags & Py_TPFLAGS_READY) {
3090 assert(type->tp_dict != NULL);
Guido van Rossumd614f972001-08-10 17:39:49 +00003091 return 0;
Guido van Rossumcab05802002-06-10 15:29:03 +00003092 }
Guido van Rossumd614f972001-08-10 17:39:49 +00003093 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00003094
3095 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003096
Tim Peters36eb4df2003-03-23 03:33:13 +00003097#ifdef Py_TRACE_REFS
3098 /* PyType_Ready is the closest thing we have to a choke point
3099 * for type objects, so is the best place I can think of to try
3100 * to get type objects into the doubly-linked list of all objects.
3101 * Still, not all type objects go thru PyType_Ready.
3102 */
Tim Peters7571a0f2003-03-23 17:52:28 +00003103 _Py_AddToAllObjects((PyObject *)type, 0);
Tim Peters36eb4df2003-03-23 03:33:13 +00003104#endif
3105
Tim Peters6d6c1a32001-08-02 04:15:00 +00003106 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3107 base = type->tp_base;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003108 if (base == NULL && type != &PyBaseObject_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003109 base = type->tp_base = &PyBaseObject_Type;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003110 Py_INCREF(base);
3111 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003112
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003113 /* Now the only way base can still be NULL is if type is
3114 * &PyBaseObject_Type.
3115 */
3116
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003117 /* Initialize the base class */
3118 if (base && base->tp_dict == NULL) {
3119 if (PyType_Ready(base) < 0)
3120 goto error;
3121 }
3122
Guido van Rossum0986d822002-04-08 01:38:42 +00003123 /* Initialize ob_type if NULL. This means extensions that want to be
3124 compilable separately on Windows can call PyType_Ready() instead of
3125 initializing the ob_type field of their type objects. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003126 /* The test for base != NULL is really unnecessary, since base is only
3127 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3128 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3129 know that. */
3130 if (type->ob_type == NULL && base != NULL)
Guido van Rossum0986d822002-04-08 01:38:42 +00003131 type->ob_type = base->ob_type;
3132
Tim Peters6d6c1a32001-08-02 04:15:00 +00003133 /* Initialize tp_bases */
3134 bases = type->tp_bases;
3135 if (bases == NULL) {
3136 if (base == NULL)
3137 bases = PyTuple_New(0);
3138 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003139 bases = PyTuple_Pack(1, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003140 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003141 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003142 type->tp_bases = bases;
3143 }
3144
Guido van Rossum687ae002001-10-15 22:03:32 +00003145 /* Initialize tp_dict */
3146 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003147 if (dict == NULL) {
3148 dict = PyDict_New();
3149 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003150 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00003151 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003152 }
3153
Guido van Rossum687ae002001-10-15 22:03:32 +00003154 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003155 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003156 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003157 if (type->tp_methods != NULL) {
3158 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003159 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003160 }
3161 if (type->tp_members != NULL) {
3162 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003163 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003164 }
3165 if (type->tp_getset != NULL) {
3166 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003167 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003168 }
3169
Tim Peters6d6c1a32001-08-02 04:15:00 +00003170 /* Calculate method resolution order */
3171 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00003172 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003173 }
3174
Guido van Rossum13d52f02001-08-10 21:24:08 +00003175 /* Inherit special flags from dominant base */
3176 if (type->tp_base != NULL)
3177 inherit_special(type, type->tp_base);
3178
Tim Peters6d6c1a32001-08-02 04:15:00 +00003179 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003180 bases = type->tp_mro;
3181 assert(bases != NULL);
3182 assert(PyTuple_Check(bases));
3183 n = PyTuple_GET_SIZE(bases);
3184 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003185 PyObject *b = PyTuple_GET_ITEM(bases, i);
3186 if (PyType_Check(b))
3187 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003188 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003189
Tim Peters3cfe7542003-05-21 21:29:48 +00003190 /* Sanity check for tp_free. */
3191 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3192 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3193 /* This base class needs to call tp_free, but doesn't have
3194 * one, or its tp_free is for non-gc'ed objects.
3195 */
3196 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3197 "gc and is a base type but has inappropriate "
3198 "tp_free slot",
3199 type->tp_name);
3200 goto error;
3201 }
3202
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003203 /* if the type dictionary doesn't contain a __doc__, set it from
3204 the tp_doc slot.
3205 */
3206 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3207 if (type->tp_doc != NULL) {
3208 PyObject *doc = PyString_FromString(type->tp_doc);
3209 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3210 Py_DECREF(doc);
3211 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00003212 PyDict_SetItemString(type->tp_dict,
3213 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003214 }
3215 }
3216
Guido van Rossum13d52f02001-08-10 21:24:08 +00003217 /* Some more special stuff */
3218 base = type->tp_base;
3219 if (base != NULL) {
3220 if (type->tp_as_number == NULL)
3221 type->tp_as_number = base->tp_as_number;
3222 if (type->tp_as_sequence == NULL)
3223 type->tp_as_sequence = base->tp_as_sequence;
3224 if (type->tp_as_mapping == NULL)
3225 type->tp_as_mapping = base->tp_as_mapping;
Guido van Rossumeea47182003-02-11 20:39:59 +00003226 if (type->tp_as_buffer == NULL)
3227 type->tp_as_buffer = base->tp_as_buffer;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003228 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003229
Guido van Rossum1c450732001-10-08 15:18:27 +00003230 /* Link into each base class's list of subclasses */
3231 bases = type->tp_bases;
3232 n = PyTuple_GET_SIZE(bases);
3233 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003234 PyObject *b = PyTuple_GET_ITEM(bases, i);
3235 if (PyType_Check(b) &&
3236 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00003237 goto error;
3238 }
3239
Guido van Rossum13d52f02001-08-10 21:24:08 +00003240 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00003241 assert(type->tp_dict != NULL);
3242 type->tp_flags =
3243 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003244 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00003245
3246 error:
3247 type->tp_flags &= ~Py_TPFLAGS_READYING;
3248 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003249}
3250
Guido van Rossum1c450732001-10-08 15:18:27 +00003251static int
3252add_subclass(PyTypeObject *base, PyTypeObject *type)
3253{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003254 Py_ssize_t i;
3255 int result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003256 PyObject *list, *ref, *newobj;
Guido van Rossum1c450732001-10-08 15:18:27 +00003257
3258 list = base->tp_subclasses;
3259 if (list == NULL) {
3260 base->tp_subclasses = list = PyList_New(0);
3261 if (list == NULL)
3262 return -1;
3263 }
3264 assert(PyList_Check(list));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003265 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
Guido van Rossum1c450732001-10-08 15:18:27 +00003266 i = PyList_GET_SIZE(list);
3267 while (--i >= 0) {
3268 ref = PyList_GET_ITEM(list, i);
3269 assert(PyWeakref_CheckRef(ref));
Guido van Rossum3930bc32002-10-18 13:51:49 +00003270 if (PyWeakref_GET_OBJECT(ref) == Py_None)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003271 return PyList_SetItem(list, i, newobj);
Guido van Rossum1c450732001-10-08 15:18:27 +00003272 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003273 result = PyList_Append(list, newobj);
3274 Py_DECREF(newobj);
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003275 return result;
Guido van Rossum1c450732001-10-08 15:18:27 +00003276}
3277
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003278static void
3279remove_subclass(PyTypeObject *base, PyTypeObject *type)
3280{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003281 Py_ssize_t i;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003282 PyObject *list, *ref;
3283
3284 list = base->tp_subclasses;
3285 if (list == NULL) {
3286 return;
3287 }
3288 assert(PyList_Check(list));
3289 i = PyList_GET_SIZE(list);
3290 while (--i >= 0) {
3291 ref = PyList_GET_ITEM(list, i);
3292 assert(PyWeakref_CheckRef(ref));
3293 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3294 /* this can't fail, right? */
3295 PySequence_DelItem(list, i);
3296 return;
3297 }
3298 }
3299}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003300
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003301static int
3302check_num_args(PyObject *ob, int n)
3303{
3304 if (!PyTuple_CheckExact(ob)) {
3305 PyErr_SetString(PyExc_SystemError,
3306 "PyArg_UnpackTuple() argument list is not a tuple");
3307 return 0;
3308 }
3309 if (n == PyTuple_GET_SIZE(ob))
3310 return 1;
3311 PyErr_Format(
3312 PyExc_TypeError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00003313 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003314 return 0;
3315}
3316
Tim Peters6d6c1a32001-08-02 04:15:00 +00003317/* Generic wrappers for overloadable 'operators' such as __getitem__ */
3318
3319/* There's a wrapper *function* for each distinct function typedef used
3320 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3321 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3322 Most tables have only one entry; the tables for binary operators have two
3323 entries, one regular and one with reversed arguments. */
3324
3325static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003326wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003327{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003328 lenfunc func = (lenfunc)wrapped;
3329 Py_ssize_t res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003330
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003331 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003332 return NULL;
3333 res = (*func)(self);
3334 if (res == -1 && PyErr_Occurred())
3335 return NULL;
3336 return PyInt_FromLong((long)res);
3337}
3338
Tim Peters6d6c1a32001-08-02 04:15:00 +00003339static PyObject *
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003340wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3341{
3342 inquiry func = (inquiry)wrapped;
3343 int res;
3344
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003345 if (!check_num_args(args, 0))
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003346 return NULL;
3347 res = (*func)(self);
3348 if (res == -1 && PyErr_Occurred())
3349 return NULL;
3350 return PyBool_FromLong((long)res);
3351}
3352
3353static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003354wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3355{
3356 binaryfunc func = (binaryfunc)wrapped;
3357 PyObject *other;
3358
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003359 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003360 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003361 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003362 return (*func)(self, other);
3363}
3364
3365static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003366wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3367{
3368 binaryfunc func = (binaryfunc)wrapped;
3369 PyObject *other;
3370
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003371 if (!check_num_args(args, 1))
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003372 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003373 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003374 return (*func)(self, other);
3375}
3376
3377static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003378wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3379{
3380 binaryfunc func = (binaryfunc)wrapped;
3381 PyObject *other;
3382
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003383 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003384 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003385 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003386 if (!PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003387 Py_INCREF(Py_NotImplemented);
3388 return Py_NotImplemented;
3389 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003390 return (*func)(other, self);
3391}
3392
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003393static PyObject *
3394wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3395{
3396 coercion func = (coercion)wrapped;
3397 PyObject *other, *res;
3398 int ok;
3399
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003400 if (!check_num_args(args, 1))
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003401 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003402 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003403 ok = func(&self, &other);
3404 if (ok < 0)
3405 return NULL;
3406 if (ok > 0) {
3407 Py_INCREF(Py_NotImplemented);
3408 return Py_NotImplemented;
3409 }
3410 res = PyTuple_New(2);
3411 if (res == NULL) {
3412 Py_DECREF(self);
3413 Py_DECREF(other);
3414 return NULL;
3415 }
3416 PyTuple_SET_ITEM(res, 0, self);
3417 PyTuple_SET_ITEM(res, 1, other);
3418 return res;
3419}
3420
Tim Peters6d6c1a32001-08-02 04:15:00 +00003421static PyObject *
3422wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3423{
3424 ternaryfunc func = (ternaryfunc)wrapped;
3425 PyObject *other;
3426 PyObject *third = Py_None;
3427
3428 /* Note: This wrapper only works for __pow__() */
3429
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003430 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003431 return NULL;
3432 return (*func)(self, other, third);
3433}
3434
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00003435static PyObject *
3436wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3437{
3438 ternaryfunc func = (ternaryfunc)wrapped;
3439 PyObject *other;
3440 PyObject *third = Py_None;
3441
3442 /* Note: This wrapper only works for __pow__() */
3443
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003444 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00003445 return NULL;
3446 return (*func)(other, self, third);
3447}
3448
Tim Peters6d6c1a32001-08-02 04:15:00 +00003449static PyObject *
3450wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3451{
3452 unaryfunc func = (unaryfunc)wrapped;
3453
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003454 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003455 return NULL;
3456 return (*func)(self);
3457}
3458
Tim Peters6d6c1a32001-08-02 04:15:00 +00003459static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003460wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003461{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003462 ssizeargfunc func = (ssizeargfunc)wrapped;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003463 PyObject* o;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003464 Py_ssize_t i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003465
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003466 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
3467 return NULL;
3468 i = PyNumber_Index(o);
3469 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00003470 return NULL;
3471 return (*func)(self, i);
3472}
3473
Martin v. Löwis18e16552006-02-15 17:27:45 +00003474static Py_ssize_t
Guido van Rossum5d815f32001-08-17 21:57:47 +00003475getindex(PyObject *self, PyObject *arg)
3476{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003477 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003478
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003479 i = PyNumber_Index(arg);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003480 if (i == -1 && PyErr_Occurred())
3481 return -1;
3482 if (i < 0) {
3483 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3484 if (sq && sq->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00003485 Py_ssize_t n = (*sq->sq_length)(self);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003486 if (n < 0)
3487 return -1;
3488 i += n;
3489 }
3490 }
3491 return i;
3492}
3493
3494static PyObject *
3495wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3496{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003497 ssizeargfunc func = (ssizeargfunc)wrapped;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003498 PyObject *arg;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003499 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003500
Guido van Rossumf4593e02001-10-03 12:09:30 +00003501 if (PyTuple_GET_SIZE(args) == 1) {
3502 arg = PyTuple_GET_ITEM(args, 0);
3503 i = getindex(self, arg);
3504 if (i == -1 && PyErr_Occurred())
3505 return NULL;
3506 return (*func)(self, i);
3507 }
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003508 check_num_args(args, 1);
Guido van Rossumf4593e02001-10-03 12:09:30 +00003509 assert(PyErr_Occurred());
3510 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003511}
3512
Tim Peters6d6c1a32001-08-02 04:15:00 +00003513static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003514wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003515{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003516 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
3517 Py_ssize_t i, j;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003518
Martin v. Löwis18e16552006-02-15 17:27:45 +00003519 if (!PyArg_ParseTuple(args, "nn", &i, &j))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003520 return NULL;
3521 return (*func)(self, i, j);
3522}
3523
Tim Peters6d6c1a32001-08-02 04:15:00 +00003524static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00003525wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003526{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003527 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3528 Py_ssize_t i;
3529 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003530 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003531
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003532 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
Guido van Rossum5d815f32001-08-17 21:57:47 +00003533 return NULL;
3534 i = getindex(self, arg);
3535 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00003536 return NULL;
3537 res = (*func)(self, i, value);
3538 if (res == -1 && PyErr_Occurred())
3539 return NULL;
3540 Py_INCREF(Py_None);
3541 return Py_None;
3542}
3543
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003544static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00003545wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003546{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003547 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3548 Py_ssize_t i;
3549 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003550 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003551
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003552 if (!check_num_args(args, 1))
Guido van Rossum5d815f32001-08-17 21:57:47 +00003553 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003554 arg = PyTuple_GET_ITEM(args, 0);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003555 i = getindex(self, arg);
3556 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003557 return NULL;
3558 res = (*func)(self, i, NULL);
3559 if (res == -1 && PyErr_Occurred())
3560 return NULL;
3561 Py_INCREF(Py_None);
3562 return Py_None;
3563}
3564
Tim Peters6d6c1a32001-08-02 04:15:00 +00003565static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003566wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003567{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003568 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3569 Py_ssize_t i, j;
3570 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003571 PyObject *value;
3572
Martin v. Löwis18e16552006-02-15 17:27:45 +00003573 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003574 return NULL;
3575 res = (*func)(self, i, j, value);
3576 if (res == -1 && PyErr_Occurred())
3577 return NULL;
3578 Py_INCREF(Py_None);
3579 return Py_None;
3580}
3581
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003582static PyObject *
3583wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3584{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003585 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3586 Py_ssize_t i, j;
3587 int res;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003588
Martin v. Löwis18e16552006-02-15 17:27:45 +00003589 if (!PyArg_ParseTuple(args, "nn", &i, &j))
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003590 return NULL;
3591 res = (*func)(self, i, j, NULL);
3592 if (res == -1 && PyErr_Occurred())
3593 return NULL;
3594 Py_INCREF(Py_None);
3595 return Py_None;
3596}
3597
Tim Peters6d6c1a32001-08-02 04:15:00 +00003598/* XXX objobjproc is a misnomer; should be objargpred */
3599static PyObject *
3600wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3601{
3602 objobjproc func = (objobjproc)wrapped;
3603 int res;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00003604 PyObject *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003605
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003606 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003607 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003608 value = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003609 res = (*func)(self, value);
3610 if (res == -1 && PyErr_Occurred())
3611 return NULL;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00003612 else
3613 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003614}
3615
Tim Peters6d6c1a32001-08-02 04:15:00 +00003616static PyObject *
3617wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3618{
3619 objobjargproc func = (objobjargproc)wrapped;
3620 int res;
3621 PyObject *key, *value;
3622
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003623 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003624 return NULL;
3625 res = (*func)(self, key, value);
3626 if (res == -1 && PyErr_Occurred())
3627 return NULL;
3628 Py_INCREF(Py_None);
3629 return Py_None;
3630}
3631
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003632static PyObject *
3633wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3634{
3635 objobjargproc func = (objobjargproc)wrapped;
3636 int res;
3637 PyObject *key;
3638
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003639 if (!check_num_args(args, 1))
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003640 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003641 key = PyTuple_GET_ITEM(args, 0);
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003642 res = (*func)(self, key, NULL);
3643 if (res == -1 && PyErr_Occurred())
3644 return NULL;
3645 Py_INCREF(Py_None);
3646 return Py_None;
3647}
3648
Tim Peters6d6c1a32001-08-02 04:15:00 +00003649static PyObject *
3650wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3651{
3652 cmpfunc func = (cmpfunc)wrapped;
3653 int res;
3654 PyObject *other;
3655
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003656 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003657 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003658 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00003659 if (other->ob_type->tp_compare != func &&
3660 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00003661 PyErr_Format(
3662 PyExc_TypeError,
3663 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3664 self->ob_type->tp_name,
3665 self->ob_type->tp_name,
3666 other->ob_type->tp_name);
3667 return NULL;
3668 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003669 res = (*func)(self, other);
3670 if (PyErr_Occurred())
3671 return NULL;
3672 return PyInt_FromLong((long)res);
3673}
3674
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003675/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
Guido van Rossum52b27052003-04-15 20:05:10 +00003676 This is called the Carlo Verre hack after its discoverer. */
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003677static int
3678hackcheck(PyObject *self, setattrofunc func, char *what)
3679{
3680 PyTypeObject *type = self->ob_type;
3681 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3682 type = type->tp_base;
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003683 /* If type is NULL now, this is a really weird type.
3684 In the same of backwards compatibility (?), just shut up. */
3685 if (type && type->tp_setattro != func) {
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003686 PyErr_Format(PyExc_TypeError,
3687 "can't apply this %s to %s object",
3688 what,
3689 type->tp_name);
3690 return 0;
3691 }
3692 return 1;
3693}
3694
Tim Peters6d6c1a32001-08-02 04:15:00 +00003695static PyObject *
3696wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
3697{
3698 setattrofunc func = (setattrofunc)wrapped;
3699 int res;
3700 PyObject *name, *value;
3701
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003702 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003703 return NULL;
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003704 if (!hackcheck(self, func, "__setattr__"))
3705 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003706 res = (*func)(self, name, value);
3707 if (res < 0)
3708 return NULL;
3709 Py_INCREF(Py_None);
3710 return Py_None;
3711}
3712
3713static PyObject *
3714wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
3715{
3716 setattrofunc func = (setattrofunc)wrapped;
3717 int res;
3718 PyObject *name;
3719
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003720 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003721 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003722 name = PyTuple_GET_ITEM(args, 0);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003723 if (!hackcheck(self, func, "__delattr__"))
3724 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003725 res = (*func)(self, name, NULL);
3726 if (res < 0)
3727 return NULL;
3728 Py_INCREF(Py_None);
3729 return Py_None;
3730}
3731
Tim Peters6d6c1a32001-08-02 04:15:00 +00003732static PyObject *
3733wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3734{
3735 hashfunc func = (hashfunc)wrapped;
3736 long res;
3737
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003738 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003739 return NULL;
3740 res = (*func)(self);
3741 if (res == -1 && PyErr_Occurred())
3742 return NULL;
3743 return PyInt_FromLong(res);
3744}
3745
Tim Peters6d6c1a32001-08-02 04:15:00 +00003746static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00003747wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003748{
3749 ternaryfunc func = (ternaryfunc)wrapped;
3750
Guido van Rossumc8e56452001-10-22 00:43:43 +00003751 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003752}
3753
Tim Peters6d6c1a32001-08-02 04:15:00 +00003754static PyObject *
3755wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3756{
3757 richcmpfunc func = (richcmpfunc)wrapped;
3758 PyObject *other;
3759
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003760 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003761 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003762 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003763 return (*func)(self, other, op);
3764}
3765
3766#undef RICHCMP_WRAPPER
3767#define RICHCMP_WRAPPER(NAME, OP) \
3768static PyObject * \
3769richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3770{ \
3771 return wrap_richcmpfunc(self, args, wrapped, OP); \
3772}
3773
Jack Jansen8e938b42001-08-08 15:29:49 +00003774RICHCMP_WRAPPER(lt, Py_LT)
3775RICHCMP_WRAPPER(le, Py_LE)
3776RICHCMP_WRAPPER(eq, Py_EQ)
3777RICHCMP_WRAPPER(ne, Py_NE)
3778RICHCMP_WRAPPER(gt, Py_GT)
3779RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003780
Tim Peters6d6c1a32001-08-02 04:15:00 +00003781static PyObject *
3782wrap_next(PyObject *self, PyObject *args, void *wrapped)
3783{
3784 unaryfunc func = (unaryfunc)wrapped;
3785 PyObject *res;
3786
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003787 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003788 return NULL;
3789 res = (*func)(self);
3790 if (res == NULL && !PyErr_Occurred())
3791 PyErr_SetNone(PyExc_StopIteration);
3792 return res;
3793}
3794
Tim Peters6d6c1a32001-08-02 04:15:00 +00003795static PyObject *
3796wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3797{
3798 descrgetfunc func = (descrgetfunc)wrapped;
3799 PyObject *obj;
3800 PyObject *type = NULL;
3801
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003802 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003803 return NULL;
Guido van Rossum82ed25c2003-02-11 16:25:43 +00003804 if (obj == Py_None)
3805 obj = NULL;
3806 if (type == Py_None)
3807 type = NULL;
3808 if (type == NULL &&obj == NULL) {
3809 PyErr_SetString(PyExc_TypeError,
3810 "__get__(None, None) is invalid");
3811 return NULL;
3812 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003813 return (*func)(self, obj, type);
3814}
3815
Tim Peters6d6c1a32001-08-02 04:15:00 +00003816static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003817wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003818{
3819 descrsetfunc func = (descrsetfunc)wrapped;
3820 PyObject *obj, *value;
3821 int ret;
3822
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003823 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003824 return NULL;
3825 ret = (*func)(self, obj, value);
3826 if (ret < 0)
3827 return NULL;
3828 Py_INCREF(Py_None);
3829 return Py_None;
3830}
Guido van Rossum22b13872002-08-06 21:41:44 +00003831
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00003832static PyObject *
3833wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3834{
3835 descrsetfunc func = (descrsetfunc)wrapped;
3836 PyObject *obj;
3837 int ret;
3838
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003839 if (!check_num_args(args, 1))
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00003840 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003841 obj = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00003842 ret = (*func)(self, obj, NULL);
3843 if (ret < 0)
3844 return NULL;
3845 Py_INCREF(Py_None);
3846 return Py_None;
3847}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003848
Tim Peters6d6c1a32001-08-02 04:15:00 +00003849static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00003850wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003851{
3852 initproc func = (initproc)wrapped;
3853
Guido van Rossumc8e56452001-10-22 00:43:43 +00003854 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003855 return NULL;
3856 Py_INCREF(Py_None);
3857 return Py_None;
3858}
3859
Tim Peters6d6c1a32001-08-02 04:15:00 +00003860static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003861tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003862{
Barry Warsaw60f01882001-08-22 19:24:42 +00003863 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003864 PyObject *arg0, *res;
3865
3866 if (self == NULL || !PyType_Check(self))
3867 Py_FatalError("__new__() called with non-type 'self'");
3868 type = (PyTypeObject *)self;
3869 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003870 PyErr_Format(PyExc_TypeError,
3871 "%s.__new__(): not enough arguments",
3872 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003873 return NULL;
3874 }
3875 arg0 = PyTuple_GET_ITEM(args, 0);
3876 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003877 PyErr_Format(PyExc_TypeError,
3878 "%s.__new__(X): X is not a type object (%s)",
3879 type->tp_name,
3880 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003881 return NULL;
3882 }
3883 subtype = (PyTypeObject *)arg0;
3884 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003885 PyErr_Format(PyExc_TypeError,
3886 "%s.__new__(%s): %s is not a subtype of %s",
3887 type->tp_name,
3888 subtype->tp_name,
3889 subtype->tp_name,
3890 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003891 return NULL;
3892 }
Barry Warsaw60f01882001-08-22 19:24:42 +00003893
3894 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00003895 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00003896 most derived base that's not a heap type is this type. */
3897 staticbase = subtype;
3898 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
3899 staticbase = staticbase->tp_base;
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003900 /* If staticbase is NULL now, it is a really weird type.
3901 In the same of backwards compatibility (?), just shut up. */
3902 if (staticbase && staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003903 PyErr_Format(PyExc_TypeError,
3904 "%s.__new__(%s) is not safe, use %s.__new__()",
3905 type->tp_name,
3906 subtype->tp_name,
3907 staticbase == NULL ? "?" : staticbase->tp_name);
3908 return NULL;
3909 }
3910
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003911 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3912 if (args == NULL)
3913 return NULL;
3914 res = type->tp_new(subtype, args, kwds);
3915 Py_DECREF(args);
3916 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003917}
3918
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003919static struct PyMethodDef tp_new_methoddef[] = {
3920 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003921 PyDoc_STR("T.__new__(S, ...) -> "
3922 "a new object with type S, a subtype of T")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00003923 {0}
3924};
3925
3926static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003927add_tp_new_wrapper(PyTypeObject *type)
3928{
Guido van Rossumf040ede2001-08-07 16:40:56 +00003929 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003930
Guido van Rossum687ae002001-10-15 22:03:32 +00003931 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00003932 return 0;
3933 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003934 if (func == NULL)
3935 return -1;
Raymond Hettinger8d726ee2004-06-25 22:24:35 +00003936 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
Raymond Hettingerd56cbe52004-06-25 22:17:39 +00003937 Py_DECREF(func);
3938 return -1;
3939 }
3940 Py_DECREF(func);
3941 return 0;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003942}
3943
Guido van Rossumf040ede2001-08-07 16:40:56 +00003944/* Slot wrappers that call the corresponding __foo__ slot. See comments
3945 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003946
Guido van Rossumdc91b992001-08-08 22:26:22 +00003947#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003948static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003949FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003950{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00003951 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003952 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003953}
3954
Guido van Rossumdc91b992001-08-08 22:26:22 +00003955#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003956static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003957FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003958{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003959 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003960 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003961}
3962
Guido van Rossumcd118802003-01-06 22:57:47 +00003963/* Boolean helper for SLOT1BINFULL().
3964 right.__class__ is a nontrivial subclass of left.__class__. */
3965static int
3966method_is_overloaded(PyObject *left, PyObject *right, char *name)
3967{
3968 PyObject *a, *b;
3969 int ok;
3970
3971 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
3972 if (b == NULL) {
3973 PyErr_Clear();
3974 /* If right doesn't have it, it's not overloaded */
3975 return 0;
3976 }
3977
3978 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
3979 if (a == NULL) {
3980 PyErr_Clear();
3981 Py_DECREF(b);
3982 /* If right has it but left doesn't, it's overloaded */
3983 return 1;
3984 }
3985
3986 ok = PyObject_RichCompareBool(a, b, Py_NE);
3987 Py_DECREF(a);
3988 Py_DECREF(b);
3989 if (ok < 0) {
3990 PyErr_Clear();
3991 return 0;
3992 }
3993
3994 return ok;
3995}
3996
Guido van Rossumdc91b992001-08-08 22:26:22 +00003997
3998#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003999static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004000FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004001{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004002 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00004003 int do_other = self->ob_type != other->ob_type && \
4004 other->ob_type->tp_as_number != NULL && \
4005 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004006 if (self->ob_type->tp_as_number != NULL && \
4007 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
4008 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00004009 if (do_other && \
Guido van Rossumcd118802003-01-06 22:57:47 +00004010 PyType_IsSubtype(other->ob_type, self->ob_type) && \
4011 method_is_overloaded(self, other, ROPSTR)) { \
Guido van Rossum55f20992001-10-01 17:18:22 +00004012 r = call_maybe( \
4013 other, ROPSTR, &rcache_str, "(O)", self); \
4014 if (r != Py_NotImplemented) \
4015 return r; \
4016 Py_DECREF(r); \
4017 do_other = 0; \
4018 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004019 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004020 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004021 if (r != Py_NotImplemented || \
4022 other->ob_type == self->ob_type) \
4023 return r; \
4024 Py_DECREF(r); \
4025 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00004026 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00004027 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00004028 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00004029 } \
4030 Py_INCREF(Py_NotImplemented); \
4031 return Py_NotImplemented; \
4032}
4033
4034#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
4035 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
4036
4037#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4038static PyObject * \
4039FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4040{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004041 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004042 return call_method(self, OPSTR, &cache_str, \
4043 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004044}
4045
Martin v. Löwis18e16552006-02-15 17:27:45 +00004046static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +00004047slot_sq_length(PyObject *self)
4048{
Guido van Rossum2730b132001-08-28 18:22:14 +00004049 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004050 PyObject *res = call_method(self, "__len__", &len_str, "()");
Martin v. Löwis18e16552006-02-15 17:27:45 +00004051 Py_ssize_t temp;
4052 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004053
4054 if (res == NULL)
4055 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004056 temp = PyInt_AsSsize_t(res);
Guido van Rossum630db602005-09-20 18:49:54 +00004057 len = (int)temp;
Guido van Rossum26111622001-10-01 16:42:49 +00004058 Py_DECREF(res);
Jeremy Hylton73a088e2002-07-25 16:43:29 +00004059 if (len == -1 && PyErr_Occurred())
4060 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004061#if SIZEOF_SIZE_T < SIZEOF_LONG
4062 /* Overflow check -- range of PyInt is more than C ssize_t */
Guido van Rossum630db602005-09-20 18:49:54 +00004063 if (len != temp) {
4064 PyErr_SetString(PyExc_OverflowError,
4065 "__len__() should return 0 <= outcome < 2**31");
4066 return -1;
4067 }
4068#endif
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004069 if (len < 0) {
Guido van Rossum22b13872002-08-06 21:41:44 +00004070 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004071 "__len__() should return >= 0");
4072 return -1;
4073 }
Guido van Rossum26111622001-10-01 16:42:49 +00004074 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004075}
4076
Guido van Rossumf4593e02001-10-03 12:09:30 +00004077/* Super-optimized version of slot_sq_item.
4078 Other slots could do the same... */
4079static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00004080slot_sq_item(PyObject *self, Py_ssize_t i)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004081{
4082 static PyObject *getitem_str;
4083 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4084 descrgetfunc f;
4085
4086 if (getitem_str == NULL) {
4087 getitem_str = PyString_InternFromString("__getitem__");
4088 if (getitem_str == NULL)
4089 return NULL;
4090 }
4091 func = _PyType_Lookup(self->ob_type, getitem_str);
4092 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00004093 if ((f = func->ob_type->tp_descr_get) == NULL)
4094 Py_INCREF(func);
Neal Norwitz673cd822002-10-18 16:33:13 +00004095 else {
Guido van Rossumf4593e02001-10-03 12:09:30 +00004096 func = f(func, self, (PyObject *)(self->ob_type));
Neal Norwitz673cd822002-10-18 16:33:13 +00004097 if (func == NULL) {
4098 return NULL;
4099 }
4100 }
Martin v. Löwiseb079f12006-02-16 14:32:27 +00004101 ival = PyInt_FromSsize_t(i);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004102 if (ival != NULL) {
4103 args = PyTuple_New(1);
4104 if (args != NULL) {
4105 PyTuple_SET_ITEM(args, 0, ival);
4106 retval = PyObject_Call(func, args, NULL);
4107 Py_XDECREF(args);
4108 Py_XDECREF(func);
4109 return retval;
4110 }
4111 }
4112 }
4113 else {
4114 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4115 }
4116 Py_XDECREF(args);
4117 Py_XDECREF(ival);
4118 Py_XDECREF(func);
4119 return NULL;
4120}
4121
Martin v. Löwis18e16552006-02-15 17:27:45 +00004122SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004123
4124static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004125slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004126{
4127 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004128 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004129
4130 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004131 res = call_method(self, "__delitem__", &delitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004132 "(n)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004133 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004134 res = call_method(self, "__setitem__", &setitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004135 "(nO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004136 if (res == NULL)
4137 return -1;
4138 Py_DECREF(res);
4139 return 0;
4140}
4141
4142static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004143slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004144{
4145 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004146 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004147
4148 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004149 res = call_method(self, "__delslice__", &delslice_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004150 "(nn)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004151 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004152 res = call_method(self, "__setslice__", &setslice_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004153 "(nnO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004154 if (res == NULL)
4155 return -1;
4156 Py_DECREF(res);
4157 return 0;
4158}
4159
4160static int
4161slot_sq_contains(PyObject *self, PyObject *value)
4162{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004163 PyObject *func, *res, *args;
Tim Petersbf9b2442003-03-23 05:35:36 +00004164 int result = -1;
4165
Guido van Rossum60718732001-08-28 17:47:51 +00004166 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004167
Guido van Rossum55f20992001-10-01 17:18:22 +00004168 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004169 if (func != NULL) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004170 args = PyTuple_Pack(1, value);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004171 if (args == NULL)
4172 res = NULL;
4173 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004174 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004175 Py_DECREF(args);
4176 }
4177 Py_DECREF(func);
Tim Petersbf9b2442003-03-23 05:35:36 +00004178 if (res != NULL) {
4179 result = PyObject_IsTrue(res);
4180 Py_DECREF(res);
4181 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004182 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004183 else if (! PyErr_Occurred()) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00004184 /* Possible results: -1 and 1 */
4185 result = (int)_PySequence_IterSearch(self, value,
Tim Petersbf9b2442003-03-23 05:35:36 +00004186 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004187 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004188 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004189}
4190
Tim Peters6d6c1a32001-08-02 04:15:00 +00004191#define slot_mp_length slot_sq_length
4192
Guido van Rossumdc91b992001-08-08 22:26:22 +00004193SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004194
4195static int
4196slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4197{
4198 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004199 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004200
4201 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004202 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004203 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004204 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004205 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004206 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004207 if (res == NULL)
4208 return -1;
4209 Py_DECREF(res);
4210 return 0;
4211}
4212
Guido van Rossumdc91b992001-08-08 22:26:22 +00004213SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4214SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4215SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004216SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4217SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4218
Jeremy Hylton938ace62002-07-17 16:30:39 +00004219static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
Guido van Rossumdc91b992001-08-08 22:26:22 +00004220
4221SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4222 nb_power, "__pow__", "__rpow__")
4223
4224static PyObject *
4225slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4226{
Guido van Rossum2730b132001-08-28 18:22:14 +00004227 static PyObject *pow_str;
4228
Guido van Rossumdc91b992001-08-08 22:26:22 +00004229 if (modulus == Py_None)
4230 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00004231 /* Three-arg power doesn't use __rpow__. But ternary_op
4232 can call this when the second argument's type uses
4233 slot_nb_power, so check before calling self.__pow__. */
4234 if (self->ob_type->tp_as_number != NULL &&
4235 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4236 return call_method(self, "__pow__", &pow_str,
4237 "(OO)", other, modulus);
4238 }
4239 Py_INCREF(Py_NotImplemented);
4240 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00004241}
4242
4243SLOT0(slot_nb_negative, "__neg__")
4244SLOT0(slot_nb_positive, "__pos__")
4245SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004246
4247static int
4248slot_nb_nonzero(PyObject *self)
4249{
Tim Petersea7f75d2002-12-07 21:39:16 +00004250 PyObject *func, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00004251 static PyObject *nonzero_str, *len_str;
Tim Petersea7f75d2002-12-07 21:39:16 +00004252 int result = -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004253
Guido van Rossum55f20992001-10-01 17:18:22 +00004254 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004255 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00004256 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00004257 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00004258 func = lookup_maybe(self, "__len__", &len_str);
Tim Petersea7f75d2002-12-07 21:39:16 +00004259 if (func == NULL)
4260 return PyErr_Occurred() ? -1 : 1;
4261 }
4262 args = PyTuple_New(0);
4263 if (args != NULL) {
4264 PyObject *temp = PyObject_Call(func, args, NULL);
4265 Py_DECREF(args);
4266 if (temp != NULL) {
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004267 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
Jeremy Hylton090a3492003-06-27 16:46:45 +00004268 result = PyObject_IsTrue(temp);
4269 else {
4270 PyErr_Format(PyExc_TypeError,
4271 "__nonzero__ should return "
4272 "bool or int, returned %s",
4273 temp->ob_type->tp_name);
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004274 result = -1;
Jeremy Hylton090a3492003-06-27 16:46:45 +00004275 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004276 Py_DECREF(temp);
Guido van Rossum55f20992001-10-01 17:18:22 +00004277 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004278 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004279 Py_DECREF(func);
Tim Petersea7f75d2002-12-07 21:39:16 +00004280 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004281}
4282
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004283
4284static Py_ssize_t
4285slot_nb_index(PyObject *self)
4286{
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004287 static PyObject *index_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004288 PyObject *temp = call_method(self, "__index__", &index_str, "()");
4289 Py_ssize_t result;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004291 if (temp == NULL)
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004292 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004293 if (PyInt_CheckExact(temp) || PyLong_CheckExact(temp)) {
4294 result = temp->ob_type->tp_as_number->nb_index(temp);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004295 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004296 else {
4297 PyErr_SetString(PyExc_TypeError,
4298 "__index__ must return an int or a long");
4299 result = -1;
4300 }
4301 Py_DECREF(temp);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004302 return result;
4303}
4304
4305
Guido van Rossumdc91b992001-08-08 22:26:22 +00004306SLOT0(slot_nb_invert, "__invert__")
4307SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4308SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4309SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4310SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4311SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004312
4313static int
4314slot_nb_coerce(PyObject **a, PyObject **b)
4315{
4316 static PyObject *coerce_str;
4317 PyObject *self = *a, *other = *b;
4318
4319 if (self->ob_type->tp_as_number != NULL &&
4320 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4321 PyObject *r;
4322 r = call_maybe(
4323 self, "__coerce__", &coerce_str, "(O)", other);
4324 if (r == NULL)
4325 return -1;
4326 if (r == Py_NotImplemented) {
4327 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004328 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004329 else {
4330 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4331 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004332 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00004333 Py_DECREF(r);
4334 return -1;
4335 }
4336 *a = PyTuple_GET_ITEM(r, 0);
4337 Py_INCREF(*a);
4338 *b = PyTuple_GET_ITEM(r, 1);
4339 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004340 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00004341 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004342 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004343 }
4344 if (other->ob_type->tp_as_number != NULL &&
4345 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4346 PyObject *r;
4347 r = call_maybe(
4348 other, "__coerce__", &coerce_str, "(O)", self);
4349 if (r == NULL)
4350 return -1;
4351 if (r == Py_NotImplemented) {
4352 Py_DECREF(r);
4353 return 1;
4354 }
4355 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4356 PyErr_SetString(PyExc_TypeError,
4357 "__coerce__ didn't return a 2-tuple");
4358 Py_DECREF(r);
4359 return -1;
4360 }
4361 *a = PyTuple_GET_ITEM(r, 1);
4362 Py_INCREF(*a);
4363 *b = PyTuple_GET_ITEM(r, 0);
4364 Py_INCREF(*b);
4365 Py_DECREF(r);
4366 return 0;
4367 }
4368 return 1;
4369}
4370
Guido van Rossumdc91b992001-08-08 22:26:22 +00004371SLOT0(slot_nb_int, "__int__")
4372SLOT0(slot_nb_long, "__long__")
4373SLOT0(slot_nb_float, "__float__")
4374SLOT0(slot_nb_oct, "__oct__")
4375SLOT0(slot_nb_hex, "__hex__")
4376SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4377SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4378SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004379SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
Guido van Rossum6e5680f2002-10-15 01:01:53 +00004380SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004381SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4382SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4383SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4384SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4385SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4386SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4387 "__floordiv__", "__rfloordiv__")
4388SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4389SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4390SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004391
4392static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00004393half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004394{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004395 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004396 static PyObject *cmp_str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004397 Py_ssize_t c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004398
Guido van Rossum60718732001-08-28 17:47:51 +00004399 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004400 if (func == NULL) {
4401 PyErr_Clear();
4402 }
4403 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004404 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004405 if (args == NULL)
4406 res = NULL;
4407 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004408 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004409 Py_DECREF(args);
4410 }
Raymond Hettingerab5dae32002-06-24 13:08:16 +00004411 Py_DECREF(func);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004412 if (res != Py_NotImplemented) {
4413 if (res == NULL)
4414 return -2;
4415 c = PyInt_AsLong(res);
4416 Py_DECREF(res);
4417 if (c == -1 && PyErr_Occurred())
4418 return -2;
4419 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4420 }
4421 Py_DECREF(res);
4422 }
4423 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004424}
4425
Guido van Rossumab3b0342001-09-18 20:38:53 +00004426/* This slot is published for the benefit of try_3way_compare in object.c */
4427int
4428_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00004429{
4430 int c;
4431
Guido van Rossumab3b0342001-09-18 20:38:53 +00004432 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004433 c = half_compare(self, other);
4434 if (c <= 1)
4435 return c;
4436 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00004437 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004438 c = half_compare(other, self);
4439 if (c < -1)
4440 return -2;
4441 if (c <= 1)
4442 return -c;
4443 }
4444 return (void *)self < (void *)other ? -1 :
4445 (void *)self > (void *)other ? 1 : 0;
4446}
4447
4448static PyObject *
4449slot_tp_repr(PyObject *self)
4450{
4451 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004452 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004453
Guido van Rossum60718732001-08-28 17:47:51 +00004454 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004455 if (func != NULL) {
4456 res = PyEval_CallObject(func, NULL);
4457 Py_DECREF(func);
4458 return res;
4459 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00004460 PyErr_Clear();
4461 return PyString_FromFormat("<%s object at %p>",
4462 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004463}
4464
4465static PyObject *
4466slot_tp_str(PyObject *self)
4467{
4468 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004469 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004470
Guido van Rossum60718732001-08-28 17:47:51 +00004471 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004472 if (func != NULL) {
4473 res = PyEval_CallObject(func, NULL);
4474 Py_DECREF(func);
4475 return res;
4476 }
4477 else {
4478 PyErr_Clear();
4479 return slot_tp_repr(self);
4480 }
4481}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004482
4483static long
4484slot_tp_hash(PyObject *self)
4485{
Tim Peters61ce0a92002-12-06 23:38:02 +00004486 PyObject *func;
Guido van Rossum60718732001-08-28 17:47:51 +00004487 static PyObject *hash_str, *eq_str, *cmp_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004488 long h;
4489
Guido van Rossum60718732001-08-28 17:47:51 +00004490 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004491
4492 if (func != NULL) {
Tim Peters61ce0a92002-12-06 23:38:02 +00004493 PyObject *res = PyEval_CallObject(func, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004494 Py_DECREF(func);
4495 if (res == NULL)
4496 return -1;
4497 h = PyInt_AsLong(res);
Tim Peters61ce0a92002-12-06 23:38:02 +00004498 Py_DECREF(res);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004499 }
4500 else {
4501 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004502 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004503 if (func == NULL) {
4504 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004505 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004506 }
4507 if (func != NULL) {
4508 Py_DECREF(func);
4509 PyErr_SetString(PyExc_TypeError, "unhashable type");
4510 return -1;
4511 }
4512 PyErr_Clear();
4513 h = _Py_HashPointer((void *)self);
4514 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004515 if (h == -1 && !PyErr_Occurred())
4516 h = -2;
4517 return h;
4518}
4519
4520static PyObject *
4521slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4522{
Guido van Rossum60718732001-08-28 17:47:51 +00004523 static PyObject *call_str;
4524 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004525 PyObject *res;
4526
4527 if (meth == NULL)
4528 return NULL;
4529 res = PyObject_Call(meth, args, kwds);
4530 Py_DECREF(meth);
4531 return res;
4532}
4533
Guido van Rossum14a6f832001-10-17 13:59:09 +00004534/* There are two slot dispatch functions for tp_getattro.
4535
4536 - slot_tp_getattro() is used when __getattribute__ is overridden
4537 but no __getattr__ hook is present;
4538
4539 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4540
Guido van Rossumc334df52002-04-04 23:44:47 +00004541 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4542 detects the absence of __getattr__ and then installs the simpler slot if
4543 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00004544
Tim Peters6d6c1a32001-08-02 04:15:00 +00004545static PyObject *
4546slot_tp_getattro(PyObject *self, PyObject *name)
4547{
Guido van Rossum14a6f832001-10-17 13:59:09 +00004548 static PyObject *getattribute_str = NULL;
4549 return call_method(self, "__getattribute__", &getattribute_str,
4550 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004551}
4552
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004553static PyObject *
4554slot_tp_getattr_hook(PyObject *self, PyObject *name)
4555{
4556 PyTypeObject *tp = self->ob_type;
4557 PyObject *getattr, *getattribute, *res;
4558 static PyObject *getattribute_str = NULL;
4559 static PyObject *getattr_str = NULL;
4560
4561 if (getattr_str == NULL) {
4562 getattr_str = PyString_InternFromString("__getattr__");
4563 if (getattr_str == NULL)
4564 return NULL;
4565 }
4566 if (getattribute_str == NULL) {
4567 getattribute_str =
4568 PyString_InternFromString("__getattribute__");
4569 if (getattribute_str == NULL)
4570 return NULL;
4571 }
4572 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004573 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004574 /* No __getattr__ hook: use a simpler dispatcher */
4575 tp->tp_getattro = slot_tp_getattro;
4576 return slot_tp_getattro(self, name);
4577 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004578 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004579 if (getattribute == NULL ||
4580 (getattribute->ob_type == &PyWrapperDescr_Type &&
4581 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4582 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004583 res = PyObject_GenericGetAttr(self, name);
4584 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00004585 res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004586 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004587 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00004588 res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004589 }
4590 return res;
4591}
4592
Tim Peters6d6c1a32001-08-02 04:15:00 +00004593static int
4594slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4595{
4596 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004597 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004598
4599 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004600 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004601 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004602 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004603 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004604 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004605 if (res == NULL)
4606 return -1;
4607 Py_DECREF(res);
4608 return 0;
4609}
4610
4611/* Map rich comparison operators to their __xx__ namesakes */
4612static char *name_op[] = {
4613 "__lt__",
4614 "__le__",
4615 "__eq__",
4616 "__ne__",
4617 "__gt__",
4618 "__ge__",
4619};
4620
4621static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00004622half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004623{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004624 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004625 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00004626
Guido van Rossum60718732001-08-28 17:47:51 +00004627 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004628 if (func == NULL) {
4629 PyErr_Clear();
4630 Py_INCREF(Py_NotImplemented);
4631 return Py_NotImplemented;
4632 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004633 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004634 if (args == NULL)
4635 res = NULL;
4636 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004637 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004638 Py_DECREF(args);
4639 }
4640 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004641 return res;
4642}
4643
Guido van Rossumb8f63662001-08-15 23:57:02 +00004644static PyObject *
4645slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4646{
4647 PyObject *res;
4648
4649 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4650 res = half_richcompare(self, other, op);
4651 if (res != Py_NotImplemented)
4652 return res;
4653 Py_DECREF(res);
4654 }
4655 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
Tim Petersf4aca752004-09-23 02:39:37 +00004656 res = half_richcompare(other, self, _Py_SwappedOp[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004657 if (res != Py_NotImplemented) {
4658 return res;
4659 }
4660 Py_DECREF(res);
4661 }
4662 Py_INCREF(Py_NotImplemented);
4663 return Py_NotImplemented;
4664}
4665
4666static PyObject *
4667slot_tp_iter(PyObject *self)
4668{
4669 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004670 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004671
Guido van Rossum60718732001-08-28 17:47:51 +00004672 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004673 if (func != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +00004674 PyObject *args;
4675 args = res = PyTuple_New(0);
4676 if (args != NULL) {
4677 res = PyObject_Call(func, args, NULL);
4678 Py_DECREF(args);
4679 }
4680 Py_DECREF(func);
4681 return res;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004682 }
4683 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004684 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004685 if (func == NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00004686 PyErr_SetString(PyExc_TypeError,
4687 "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00004688 return NULL;
4689 }
4690 Py_DECREF(func);
4691 return PySeqIter_New(self);
4692}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004693
4694static PyObject *
4695slot_tp_iternext(PyObject *self)
4696{
Guido van Rossum2730b132001-08-28 18:22:14 +00004697 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004698 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00004699}
4700
Guido van Rossum1a493502001-08-17 16:47:50 +00004701static PyObject *
4702slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4703{
4704 PyTypeObject *tp = self->ob_type;
4705 PyObject *get;
4706 static PyObject *get_str = NULL;
4707
4708 if (get_str == NULL) {
4709 get_str = PyString_InternFromString("__get__");
4710 if (get_str == NULL)
4711 return NULL;
4712 }
4713 get = _PyType_Lookup(tp, get_str);
4714 if (get == NULL) {
4715 /* Avoid further slowdowns */
4716 if (tp->tp_descr_get == slot_tp_descr_get)
4717 tp->tp_descr_get = NULL;
4718 Py_INCREF(self);
4719 return self;
4720 }
Guido van Rossum2c252392001-08-24 10:13:31 +00004721 if (obj == NULL)
4722 obj = Py_None;
4723 if (type == NULL)
4724 type = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00004725 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
Guido van Rossum1a493502001-08-17 16:47:50 +00004726}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004727
4728static int
4729slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4730{
Guido van Rossum2c252392001-08-24 10:13:31 +00004731 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004732 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00004733
4734 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00004735 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004736 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00004737 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004738 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004739 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004740 if (res == NULL)
4741 return -1;
4742 Py_DECREF(res);
4743 return 0;
4744}
4745
4746static int
4747slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4748{
Guido van Rossum60718732001-08-28 17:47:51 +00004749 static PyObject *init_str;
4750 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004751 PyObject *res;
4752
4753 if (meth == NULL)
4754 return -1;
4755 res = PyObject_Call(meth, args, kwds);
4756 Py_DECREF(meth);
4757 if (res == NULL)
4758 return -1;
Raymond Hettingerb67cc802005-03-03 16:45:19 +00004759 if (res != Py_None) {
4760 PyErr_SetString(PyExc_TypeError,
4761 "__init__() should return None");
4762 Py_DECREF(res);
4763 return -1;
4764 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004765 Py_DECREF(res);
4766 return 0;
4767}
4768
4769static PyObject *
4770slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4771{
Guido van Rossum7bed2132002-08-08 21:57:53 +00004772 static PyObject *new_str;
4773 PyObject *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004774 PyObject *newargs, *x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004775 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004776
Guido van Rossum7bed2132002-08-08 21:57:53 +00004777 if (new_str == NULL) {
4778 new_str = PyString_InternFromString("__new__");
4779 if (new_str == NULL)
4780 return NULL;
4781 }
4782 func = PyObject_GetAttr((PyObject *)type, new_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004783 if (func == NULL)
4784 return NULL;
4785 assert(PyTuple_Check(args));
4786 n = PyTuple_GET_SIZE(args);
4787 newargs = PyTuple_New(n+1);
4788 if (newargs == NULL)
4789 return NULL;
4790 Py_INCREF(type);
4791 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4792 for (i = 0; i < n; i++) {
4793 x = PyTuple_GET_ITEM(args, i);
4794 Py_INCREF(x);
4795 PyTuple_SET_ITEM(newargs, i+1, x);
4796 }
4797 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00004798 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004799 Py_DECREF(func);
4800 return x;
4801}
4802
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004803static void
4804slot_tp_del(PyObject *self)
4805{
4806 static PyObject *del_str = NULL;
4807 PyObject *del, *res;
4808 PyObject *error_type, *error_value, *error_traceback;
4809
4810 /* Temporarily resurrect the object. */
4811 assert(self->ob_refcnt == 0);
4812 self->ob_refcnt = 1;
4813
4814 /* Save the current exception, if any. */
4815 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4816
4817 /* Execute __del__ method, if any. */
4818 del = lookup_maybe(self, "__del__", &del_str);
4819 if (del != NULL) {
4820 res = PyEval_CallObject(del, NULL);
4821 if (res == NULL)
4822 PyErr_WriteUnraisable(del);
4823 else
4824 Py_DECREF(res);
4825 Py_DECREF(del);
4826 }
4827
4828 /* Restore the saved exception. */
4829 PyErr_Restore(error_type, error_value, error_traceback);
4830
4831 /* Undo the temporary resurrection; can't use DECREF here, it would
4832 * cause a recursive call.
4833 */
4834 assert(self->ob_refcnt > 0);
4835 if (--self->ob_refcnt == 0)
4836 return; /* this is the normal path out */
4837
4838 /* __del__ resurrected it! Make it look like the original Py_DECREF
4839 * never happened.
4840 */
4841 {
Martin v. Löwis725507b2006-03-07 12:08:51 +00004842 Py_ssize_t refcnt = self->ob_refcnt;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004843 _Py_NewReference(self);
4844 self->ob_refcnt = refcnt;
4845 }
4846 assert(!PyType_IS_GC(self->ob_type) ||
4847 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
Michael W. Hudson3f3b6682004-08-03 10:21:03 +00004848 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
4849 * we need to undo that. */
4850 _Py_DEC_REFTOTAL;
4851 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4852 * chain, so no more to do there.
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004853 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4854 * _Py_NewReference bumped tp_allocs: both of those need to be
4855 * undone.
4856 */
4857#ifdef COUNT_ALLOCS
4858 --self->ob_type->tp_frees;
4859 --self->ob_type->tp_allocs;
4860#endif
4861}
4862
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004863
4864/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
Tim Petersbf9b2442003-03-23 05:35:36 +00004865 functions. The offsets here are relative to the 'PyHeapTypeObject'
Guido van Rossume5c691a2003-03-07 15:13:17 +00004866 structure, which incorporates the additional structures used for numbers,
4867 sequences and mappings.
4868 Note that multiple names may map to the same slot (e.g. __eq__,
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004869 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00004870 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4871 terminated with an all-zero entry. (This table is further initialized and
4872 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004873
Guido van Rossum6d204072001-10-21 00:44:31 +00004874typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004875
4876#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00004877#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004878#undef ETSLOT
4879#undef SQSLOT
4880#undef MPSLOT
4881#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00004882#undef UNSLOT
4883#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004884#undef BINSLOT
4885#undef RBINSLOT
4886
Guido van Rossum6d204072001-10-21 00:44:31 +00004887#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004888 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4889 PyDoc_STR(DOC)}
Guido van Rossumc8e56452001-10-22 00:43:43 +00004890#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4891 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004892 PyDoc_STR(DOC), FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00004893#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Guido van Rossume5c691a2003-03-07 15:13:17 +00004894 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004895 PyDoc_STR(DOC)}
Guido van Rossum6d204072001-10-21 00:44:31 +00004896#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4897 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
4898#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4899 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
4900#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4901 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
4902#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4903 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4904 "x." NAME "() <==> " DOC)
4905#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4906 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4907 "x." NAME "(y) <==> x" DOC "y")
4908#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
4909 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4910 "x." NAME "(y) <==> x" DOC "y")
4911#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
4912 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4913 "x." NAME "(y) <==> y" DOC "x")
Anthony Baxter56616992005-06-03 14:12:21 +00004914#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4915 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4916 "x." NAME "(y) <==> " DOC)
4917#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4918 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4919 "x." NAME "(y) <==> " DOC)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004920
4921static slotdef slotdefs[] = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00004922 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00004923 "x.__len__() <==> len(x)"),
Armin Rigofd163f92005-12-29 15:59:19 +00004924 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
4925 The logic in abstract.c always falls back to nb_add/nb_multiply in
4926 this case. Defining both the nb_* and the sq_* slots to call the
4927 user-defined methods has unexpected side-effects, as shown by
4928 test_descr.notimplemented() */
4929 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
4930 "x.__add__(y) <==> x+y"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004931 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
Armin Rigofd163f92005-12-29 15:59:19 +00004932 "x.__mul__(n) <==> x*n"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004933 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
Armin Rigofd163f92005-12-29 15:59:19 +00004934 "x.__rmul__(n) <==> n*x"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004935 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
4936 "x.__getitem__(y) <==> x[y]"),
Martin v. Löwis18e16552006-02-15 17:27:45 +00004937 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
Brett Cannon154da9b2003-05-20 02:30:04 +00004938 "x.__getslice__(i, j) <==> x[i:j]\n\
4939 \n\
4940 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004941 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00004942 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004943 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00004944 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004945 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Martin v. Löwis18e16552006-02-15 17:27:45 +00004946 wrap_ssizessizeobjargproc,
Brett Cannonbe67d872003-05-20 02:40:12 +00004947 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
4948 \n\
4949 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004950 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
Brett Cannonbe67d872003-05-20 02:40:12 +00004951 "x.__delslice__(i, j) <==> del x[i:j]\n\
4952 \n\
4953 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004954 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
4955 "x.__contains__(y) <==> y in x"),
Armin Rigofd163f92005-12-29 15:59:19 +00004956 SQSLOT("__iadd__", sq_inplace_concat, NULL,
4957 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
4958 SQSLOT("__imul__", sq_inplace_repeat, NULL,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004959 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004960
Martin v. Löwis18e16552006-02-15 17:27:45 +00004961 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00004962 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00004963 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004964 wrap_binaryfunc,
4965 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004966 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004967 wrap_objobjargproc,
4968 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004969 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004970 wrap_delitem,
4971 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004972
Guido van Rossum6d204072001-10-21 00:44:31 +00004973 BINSLOT("__add__", nb_add, slot_nb_add,
4974 "+"),
4975 RBINSLOT("__radd__", nb_add, slot_nb_add,
4976 "+"),
4977 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
4978 "-"),
4979 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
4980 "-"),
4981 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
4982 "*"),
4983 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
4984 "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004985 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
4986 "%"),
4987 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
4988 "%"),
Anthony Baxter56616992005-06-03 14:12:21 +00004989 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00004990 "divmod(x, y)"),
Anthony Baxter56616992005-06-03 14:12:21 +00004991 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00004992 "divmod(y, x)"),
4993 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
4994 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
4995 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
4996 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
4997 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
4998 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
4999 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
5000 "abs(x)"),
Raymond Hettingerf34f2642003-10-11 17:29:04 +00005001 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
Guido van Rossum6d204072001-10-21 00:44:31 +00005002 "x != 0"),
5003 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
5004 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
5005 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
5006 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
5007 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
5008 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
5009 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
5010 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
5011 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
5012 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
5013 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
5014 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
5015 "x.__coerce__(y) <==> coerce(x, y)"),
5016 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
5017 "int(x)"),
5018 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
5019 "long(x)"),
5020 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
5021 "float(x)"),
5022 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
5023 "oct(x)"),
5024 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
5025 "hex(x)"),
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005026 NBSLOT("__index__", nb_index, slot_nb_index, wrap_lenfunc,
5027 "x[y:z] <==> x[y.__index__():z.__index__()]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005028 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
5029 wrap_binaryfunc, "+"),
5030 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
5031 wrap_binaryfunc, "-"),
5032 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
5033 wrap_binaryfunc, "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005034 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
5035 wrap_binaryfunc, "%"),
5036 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
Guido van Rossum6e5680f2002-10-15 01:01:53 +00005037 wrap_binaryfunc, "**"),
Guido van Rossum6d204072001-10-21 00:44:31 +00005038 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
5039 wrap_binaryfunc, "<<"),
5040 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
5041 wrap_binaryfunc, ">>"),
5042 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
5043 wrap_binaryfunc, "&"),
5044 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
5045 wrap_binaryfunc, "^"),
5046 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
5047 wrap_binaryfunc, "|"),
5048 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5049 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
5050 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
5051 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
5052 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
5053 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
5054 IBSLOT("__itruediv__", nb_inplace_true_divide,
5055 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005056
Guido van Rossum6d204072001-10-21 00:44:31 +00005057 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5058 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00005059 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00005060 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5061 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00005062 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00005063 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5064 "x.__cmp__(y) <==> cmp(x,y)"),
5065 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5066 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005067 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5068 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005069 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00005070 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5071 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5072 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5073 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5074 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5075 "x.__setattr__('name', value) <==> x.name = value"),
5076 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5077 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5078 "x.__delattr__('name') <==> del x.name"),
5079 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5080 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5081 "x.__lt__(y) <==> x<y"),
5082 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
5083 "x.__le__(y) <==> x<=y"),
5084 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5085 "x.__eq__(y) <==> x==y"),
5086 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5087 "x.__ne__(y) <==> x!=y"),
5088 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5089 "x.__gt__(y) <==> x>y"),
5090 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5091 "x.__ge__(y) <==> x>=y"),
5092 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5093 "x.__iter__() <==> iter(x)"),
5094 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
5095 "x.next() -> the next value, or raise StopIteration"),
5096 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5097 "descr.__get__(obj[, type]) -> value"),
5098 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5099 "descr.__set__(obj, value)"),
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00005100 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5101 wrap_descr_delete, "descr.__delete__(obj)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005102 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00005103 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00005104 "see x.__class__.__doc__ for signature",
5105 PyWrapperFlag_KEYWORDS),
5106 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005107 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005108 {NULL}
5109};
5110
Guido van Rossumc334df52002-04-04 23:44:47 +00005111/* Given a type pointer and an offset gotten from a slotdef entry, return a
5112 pointer to the actual slot. This is not quite the same as simply adding
5113 the offset to the type pointer, since it takes care to indirect through the
5114 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5115 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005116static void **
Martin v. Löwis18e16552006-02-15 17:27:45 +00005117slotptr(PyTypeObject *type, int ioffset)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005118{
5119 char *ptr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005120 long offset = ioffset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005121
Guido van Rossume5c691a2003-03-07 15:13:17 +00005122 /* Note: this depends on the order of the members of PyHeapTypeObject! */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005123 assert(offset >= 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005124 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5125 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5126 ptr = (char *)type->tp_as_sequence;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005127 offset -= offsetof(PyHeapTypeObject, as_sequence);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005128 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005129 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5130 ptr = (char *)type->tp_as_mapping;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005131 offset -= offsetof(PyHeapTypeObject, as_mapping);
Guido van Rossum09638c12002-06-13 19:17:46 +00005132 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005133 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
5134 ptr = (char *)type->tp_as_number;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005135 offset -= offsetof(PyHeapTypeObject, as_number);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005136 }
5137 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005138 ptr = (char *)type;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005139 }
5140 if (ptr != NULL)
5141 ptr += offset;
5142 return (void **)ptr;
5143}
Guido van Rossumf040ede2001-08-07 16:40:56 +00005144
Guido van Rossumc334df52002-04-04 23:44:47 +00005145/* Length of array of slotdef pointers used to store slots with the
5146 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5147 the same __name__, for any __name__. Since that's a static property, it is
5148 appropriate to declare fixed-size arrays for this. */
5149#define MAX_EQUIV 10
5150
5151/* Return a slot pointer for a given name, but ONLY if the attribute has
5152 exactly one slot function. The name must be an interned string. */
5153static void **
5154resolve_slotdups(PyTypeObject *type, PyObject *name)
5155{
5156 /* XXX Maybe this could be optimized more -- but is it worth it? */
5157
5158 /* pname and ptrs act as a little cache */
5159 static PyObject *pname;
5160 static slotdef *ptrs[MAX_EQUIV];
5161 slotdef *p, **pp;
5162 void **res, **ptr;
5163
5164 if (pname != name) {
5165 /* Collect all slotdefs that match name into ptrs. */
5166 pname = name;
5167 pp = ptrs;
5168 for (p = slotdefs; p->name_strobj; p++) {
5169 if (p->name_strobj == name)
5170 *pp++ = p;
5171 }
5172 *pp = NULL;
5173 }
5174
5175 /* Look in all matching slots of the type; if exactly one of these has
5176 a filled-in slot, return its value. Otherwise return NULL. */
5177 res = NULL;
5178 for (pp = ptrs; *pp; pp++) {
5179 ptr = slotptr(type, (*pp)->offset);
5180 if (ptr == NULL || *ptr == NULL)
5181 continue;
5182 if (res != NULL)
5183 return NULL;
5184 res = ptr;
5185 }
5186 return res;
5187}
5188
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005189/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
Guido van Rossumc334df52002-04-04 23:44:47 +00005190 does some incredibly complex thinking and then sticks something into the
5191 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5192 interests, and then stores a generic wrapper or a specific function into
5193 the slot.) Return a pointer to the next slotdef with a different offset,
5194 because that's convenient for fixup_slot_dispatchers(). */
5195static slotdef *
5196update_one_slot(PyTypeObject *type, slotdef *p)
5197{
5198 PyObject *descr;
5199 PyWrapperDescrObject *d;
5200 void *generic = NULL, *specific = NULL;
5201 int use_generic = 0;
5202 int offset = p->offset;
5203 void **ptr = slotptr(type, offset);
5204
5205 if (ptr == NULL) {
5206 do {
5207 ++p;
5208 } while (p->offset == offset);
5209 return p;
5210 }
5211 do {
5212 descr = _PyType_Lookup(type, p->name_strobj);
5213 if (descr == NULL)
5214 continue;
5215 if (descr->ob_type == &PyWrapperDescr_Type) {
5216 void **tptr = resolve_slotdups(type, p->name_strobj);
5217 if (tptr == NULL || tptr == ptr)
5218 generic = p->function;
5219 d = (PyWrapperDescrObject *)descr;
5220 if (d->d_base->wrapper == p->wrapper &&
5221 PyType_IsSubtype(type, d->d_type))
5222 {
5223 if (specific == NULL ||
5224 specific == d->d_wrapped)
5225 specific = d->d_wrapped;
5226 else
5227 use_generic = 1;
5228 }
5229 }
Guido van Rossum721f62e2002-08-09 02:14:34 +00005230 else if (descr->ob_type == &PyCFunction_Type &&
5231 PyCFunction_GET_FUNCTION(descr) ==
5232 (PyCFunction)tp_new_wrapper &&
5233 strcmp(p->name, "__new__") == 0)
5234 {
5235 /* The __new__ wrapper is not a wrapper descriptor,
5236 so must be special-cased differently.
5237 If we don't do this, creating an instance will
5238 always use slot_tp_new which will look up
5239 __new__ in the MRO which will call tp_new_wrapper
5240 which will look through the base classes looking
5241 for a static base and call its tp_new (usually
5242 PyType_GenericNew), after performing various
5243 sanity checks and constructing a new argument
5244 list. Cut all that nonsense short -- this speeds
5245 up instance creation tremendously. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005246 specific = (void *)type->tp_new;
Guido van Rossum721f62e2002-08-09 02:14:34 +00005247 /* XXX I'm not 100% sure that there isn't a hole
5248 in this reasoning that requires additional
5249 sanity checks. I'll buy the first person to
5250 point out a bug in this reasoning a beer. */
5251 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005252 else {
5253 use_generic = 1;
5254 generic = p->function;
5255 }
5256 } while ((++p)->offset == offset);
5257 if (specific && !use_generic)
5258 *ptr = specific;
5259 else
5260 *ptr = generic;
5261 return p;
5262}
5263
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005264/* In the type, update the slots whose slotdefs are gathered in the pp array.
5265 This is a callback for update_subclasses(). */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005266static int
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005267update_slots_callback(PyTypeObject *type, void *data)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005268{
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005269 slotdef **pp = (slotdef **)data;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005270
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005271 for (; *pp; pp++)
Guido van Rossumc334df52002-04-04 23:44:47 +00005272 update_one_slot(type, *pp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005273 return 0;
5274}
5275
Guido van Rossumc334df52002-04-04 23:44:47 +00005276/* Comparison function for qsort() to compare slotdefs by their offset, and
5277 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005278static int
5279slotdef_cmp(const void *aa, const void *bb)
5280{
5281 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5282 int c = a->offset - b->offset;
5283 if (c != 0)
5284 return c;
5285 else
Martin v. Löwis18e16552006-02-15 17:27:45 +00005286 /* Cannot use a-b, as this gives off_t,
5287 which may lose precision when converted to int. */
5288 return (a > b) ? 1 : (a < b) ? -1 : 0;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005289}
5290
Guido van Rossumc334df52002-04-04 23:44:47 +00005291/* Initialize the slotdefs table by adding interned string objects for the
5292 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005293static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005294init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005295{
5296 slotdef *p;
5297 static int initialized = 0;
5298
5299 if (initialized)
5300 return;
5301 for (p = slotdefs; p->name; p++) {
5302 p->name_strobj = PyString_InternFromString(p->name);
5303 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00005304 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005305 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005306 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5307 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005308 initialized = 1;
5309}
5310
Guido van Rossumc334df52002-04-04 23:44:47 +00005311/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005312static int
5313update_slot(PyTypeObject *type, PyObject *name)
5314{
Guido van Rossumc334df52002-04-04 23:44:47 +00005315 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005316 slotdef *p;
5317 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005318 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005319
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005320 init_slotdefs();
5321 pp = ptrs;
5322 for (p = slotdefs; p->name; p++) {
5323 /* XXX assume name is interned! */
5324 if (p->name_strobj == name)
5325 *pp++ = p;
5326 }
5327 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005328 for (pp = ptrs; *pp; pp++) {
5329 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005330 offset = p->offset;
5331 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005332 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005333 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005334 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005335 if (ptrs[0] == NULL)
5336 return 0; /* Not an attribute that affects any slots */
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005337 return update_subclasses(type, name,
5338 update_slots_callback, (void *)ptrs);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005339}
5340
Guido van Rossumc334df52002-04-04 23:44:47 +00005341/* Store the proper functions in the slot dispatches at class (type)
5342 definition time, based upon which operations the class overrides in its
5343 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00005344static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005345fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005346{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005347 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005348
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005349 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00005350 for (p = slotdefs; p->name; )
5351 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005352}
Guido van Rossum705f0f52001-08-24 16:47:00 +00005353
Michael W. Hudson98bbc492002-11-26 14:47:27 +00005354static void
5355update_all_slots(PyTypeObject* type)
5356{
5357 slotdef *p;
5358
5359 init_slotdefs();
5360 for (p = slotdefs; p->name; p++) {
5361 /* update_slot returns int but can't actually fail */
5362 update_slot(type, p->name_strobj);
5363 }
5364}
5365
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005366/* recurse_down_subclasses() and update_subclasses() are mutually
5367 recursive functions to call a callback for all subclasses,
5368 but refraining from recursing into subclasses that define 'name'. */
5369
5370static int
5371update_subclasses(PyTypeObject *type, PyObject *name,
5372 update_callback callback, void *data)
5373{
5374 if (callback(type, data) < 0)
5375 return -1;
5376 return recurse_down_subclasses(type, name, callback, data);
5377}
5378
5379static int
5380recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5381 update_callback callback, void *data)
5382{
5383 PyTypeObject *subclass;
5384 PyObject *ref, *subclasses, *dict;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005385 Py_ssize_t i, n;
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005386
5387 subclasses = type->tp_subclasses;
5388 if (subclasses == NULL)
5389 return 0;
5390 assert(PyList_Check(subclasses));
5391 n = PyList_GET_SIZE(subclasses);
5392 for (i = 0; i < n; i++) {
5393 ref = PyList_GET_ITEM(subclasses, i);
5394 assert(PyWeakref_CheckRef(ref));
5395 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5396 assert(subclass != NULL);
5397 if ((PyObject *)subclass == Py_None)
5398 continue;
5399 assert(PyType_Check(subclass));
5400 /* Avoid recursing down into unaffected classes */
5401 dict = subclass->tp_dict;
5402 if (dict != NULL && PyDict_Check(dict) &&
5403 PyDict_GetItem(dict, name) != NULL)
5404 continue;
5405 if (update_subclasses(subclass, name, callback, data) < 0)
5406 return -1;
5407 }
5408 return 0;
5409}
5410
Guido van Rossum6d204072001-10-21 00:44:31 +00005411/* This function is called by PyType_Ready() to populate the type's
5412 dictionary with method descriptors for function slots. For each
Guido van Rossum09638c12002-06-13 19:17:46 +00005413 function slot (like tp_repr) that's defined in the type, one or more
5414 corresponding descriptors are added in the type's tp_dict dictionary
5415 under the appropriate name (like __repr__). Some function slots
5416 cause more than one descriptor to be added (for example, the nb_add
5417 slot adds both __add__ and __radd__ descriptors) and some function
5418 slots compete for the same descriptor (for example both sq_item and
5419 mp_subscript generate a __getitem__ descriptor).
5420
5421 In the latter case, the first slotdef entry encoutered wins. Since
Tim Petersbf9b2442003-03-23 05:35:36 +00005422 slotdef entries are sorted by the offset of the slot in the
Guido van Rossume5c691a2003-03-07 15:13:17 +00005423 PyHeapTypeObject, this gives us some control over disambiguating
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005424 between competing slots: the members of PyHeapTypeObject are listed
5425 from most general to least general, so the most general slot is
5426 preferred. In particular, because as_mapping comes before as_sequence,
5427 for a type that defines both mp_subscript and sq_item, mp_subscript
5428 wins.
Guido van Rossum09638c12002-06-13 19:17:46 +00005429
5430 This only adds new descriptors and doesn't overwrite entries in
5431 tp_dict that were previously defined. The descriptors contain a
5432 reference to the C function they must call, so that it's safe if they
5433 are copied into a subtype's __dict__ and the subtype has a different
5434 C function in its slot -- calling the method defined by the
5435 descriptor will call the C function that was used to create it,
5436 rather than the C function present in the slot when it is called.
5437 (This is important because a subtype may have a C function in the
5438 slot that calls the method from the dictionary, and we want to avoid
5439 infinite recursion here.) */
Guido van Rossum6d204072001-10-21 00:44:31 +00005440
5441static int
5442add_operators(PyTypeObject *type)
5443{
5444 PyObject *dict = type->tp_dict;
5445 slotdef *p;
5446 PyObject *descr;
5447 void **ptr;
5448
5449 init_slotdefs();
5450 for (p = slotdefs; p->name; p++) {
5451 if (p->wrapper == NULL)
5452 continue;
5453 ptr = slotptr(type, p->offset);
5454 if (!ptr || !*ptr)
5455 continue;
5456 if (PyDict_GetItem(dict, p->name_strobj))
5457 continue;
5458 descr = PyDescr_NewWrapper(type, p, *ptr);
5459 if (descr == NULL)
5460 return -1;
5461 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5462 return -1;
5463 Py_DECREF(descr);
5464 }
5465 if (type->tp_new != NULL) {
5466 if (add_tp_new_wrapper(type) < 0)
5467 return -1;
5468 }
5469 return 0;
5470}
5471
Guido van Rossum705f0f52001-08-24 16:47:00 +00005472
5473/* Cooperative 'super' */
5474
5475typedef struct {
5476 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00005477 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005478 PyObject *obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005479 PyTypeObject *obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005480} superobject;
5481
Guido van Rossum6f799372001-09-20 20:46:19 +00005482static PyMemberDef super_members[] = {
5483 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5484 "the class invoking super()"},
5485 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5486 "the instance invoking super(); may be None"},
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005487 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005488 "the type of the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005489 {0}
5490};
5491
Guido van Rossum705f0f52001-08-24 16:47:00 +00005492static void
5493super_dealloc(PyObject *self)
5494{
5495 superobject *su = (superobject *)self;
5496
Guido van Rossum048eb752001-10-02 21:24:57 +00005497 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005498 Py_XDECREF(su->obj);
5499 Py_XDECREF(su->type);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005500 Py_XDECREF(su->obj_type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005501 self->ob_type->tp_free(self);
5502}
5503
5504static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005505super_repr(PyObject *self)
5506{
5507 superobject *su = (superobject *)self;
5508
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005509 if (su->obj_type)
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005510 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005511 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005512 su->type ? su->type->tp_name : "NULL",
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005513 su->obj_type->tp_name);
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005514 else
5515 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005516 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005517 su->type ? su->type->tp_name : "NULL");
5518}
5519
5520static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00005521super_getattro(PyObject *self, PyObject *name)
5522{
5523 superobject *su = (superobject *)self;
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005524 int skip = su->obj_type == NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005525
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005526 if (!skip) {
5527 /* We want __class__ to return the class of the super object
5528 (i.e. super, or a subclass), not the class of su->obj. */
5529 skip = (PyString_Check(name) &&
5530 PyString_GET_SIZE(name) == 9 &&
5531 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5532 }
5533
5534 if (!skip) {
Tim Petersa91e9642001-11-14 23:32:33 +00005535 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005536 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005537 descrgetfunc f;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005538 Py_ssize_t i, n;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005539
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005540 starttype = su->obj_type;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005541 mro = starttype->tp_mro;
5542
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005543 if (mro == NULL)
5544 n = 0;
5545 else {
5546 assert(PyTuple_Check(mro));
5547 n = PyTuple_GET_SIZE(mro);
5548 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005549 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00005550 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00005551 break;
5552 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005553 i++;
5554 res = NULL;
5555 for (; i < n; i++) {
5556 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00005557 if (PyType_Check(tmp))
5558 dict = ((PyTypeObject *)tmp)->tp_dict;
5559 else if (PyClass_Check(tmp))
5560 dict = ((PyClassObject *)tmp)->cl_dict;
5561 else
5562 continue;
5563 res = PyDict_GetItem(dict, name);
Guido van Rossum6cc5bb62003-04-16 20:01:36 +00005564 if (res != NULL) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00005565 Py_INCREF(res);
5566 f = res->ob_type->tp_descr_get;
5567 if (f != NULL) {
Phillip J. Eby91a968a2004-03-25 02:19:34 +00005568 tmp = f(res,
5569 /* Only pass 'obj' param if
5570 this is instance-mode super
5571 (See SF ID #743627)
5572 */
Hye-Shik Changff365c92004-03-25 16:37:03 +00005573 (su->obj == (PyObject *)
5574 su->obj_type
Phillip J. Eby91a968a2004-03-25 02:19:34 +00005575 ? (PyObject *)NULL
5576 : su->obj),
Guido van Rossumd4641072002-04-03 02:13:37 +00005577 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005578 Py_DECREF(res);
5579 res = tmp;
5580 }
5581 return res;
5582 }
5583 }
5584 }
5585 return PyObject_GenericGetAttr(self, name);
5586}
5587
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005588static PyTypeObject *
Guido van Rossum5b443c62001-12-03 15:38:28 +00005589supercheck(PyTypeObject *type, PyObject *obj)
5590{
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005591 /* Check that a super() call makes sense. Return a type object.
5592
5593 obj can be a new-style class, or an instance of one:
5594
5595 - If it is a class, it must be a subclass of 'type'. This case is
5596 used for class methods; the return value is obj.
5597
5598 - If it is an instance, it must be an instance of 'type'. This is
5599 the normal case; the return value is obj.__class__.
5600
5601 But... when obj is an instance, we want to allow for the case where
5602 obj->ob_type is not a subclass of type, but obj.__class__ is!
5603 This will allow using super() with a proxy for obj.
5604 */
5605
Guido van Rossum8e80a722003-02-18 19:22:22 +00005606 /* Check for first bullet above (special case) */
5607 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5608 Py_INCREF(obj);
5609 return (PyTypeObject *)obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005610 }
Guido van Rossum8e80a722003-02-18 19:22:22 +00005611
5612 /* Normal case */
5613 if (PyType_IsSubtype(obj->ob_type, type)) {
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005614 Py_INCREF(obj->ob_type);
5615 return obj->ob_type;
5616 }
5617 else {
5618 /* Try the slow way */
5619 static PyObject *class_str = NULL;
5620 PyObject *class_attr;
5621
5622 if (class_str == NULL) {
5623 class_str = PyString_FromString("__class__");
5624 if (class_str == NULL)
5625 return NULL;
5626 }
5627
5628 class_attr = PyObject_GetAttr(obj, class_str);
5629
5630 if (class_attr != NULL &&
5631 PyType_Check(class_attr) &&
5632 (PyTypeObject *)class_attr != obj->ob_type)
5633 {
5634 int ok = PyType_IsSubtype(
5635 (PyTypeObject *)class_attr, type);
5636 if (ok)
5637 return (PyTypeObject *)class_attr;
5638 }
5639
5640 if (class_attr == NULL)
5641 PyErr_Clear();
5642 else
5643 Py_DECREF(class_attr);
5644 }
5645
Tim Peters97e5ff52003-02-18 19:32:50 +00005646 PyErr_SetString(PyExc_TypeError,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005647 "super(type, obj): "
5648 "obj must be an instance or subtype of type");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005649 return NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00005650}
5651
Guido van Rossum705f0f52001-08-24 16:47:00 +00005652static PyObject *
5653super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5654{
5655 superobject *su = (superobject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005656 superobject *newobj;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005657
5658 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5659 /* Not binding to an object, or already bound */
5660 Py_INCREF(self);
5661 return self;
5662 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00005663 if (su->ob_type != &PySuper_Type)
Armin Rigo7726dc02005-05-15 15:32:08 +00005664 /* If su is an instance of a (strict) subclass of super,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005665 call its type */
Thomas Wouters477c8d52006-05-27 19:21:47 +00005666 return PyObject_CallFunctionObjArgs((PyObject *)su->ob_type,
5667 su->type, obj, NULL);
Guido van Rossum5b443c62001-12-03 15:38:28 +00005668 else {
5669 /* Inline the common case */
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005670 PyTypeObject *obj_type = supercheck(su->type, obj);
5671 if (obj_type == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00005672 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005673 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005674 NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005675 if (newobj == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00005676 return NULL;
5677 Py_INCREF(su->type);
5678 Py_INCREF(obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005679 newobj->type = su->type;
5680 newobj->obj = obj;
5681 newobj->obj_type = obj_type;
5682 return (PyObject *)newobj;
Guido van Rossum5b443c62001-12-03 15:38:28 +00005683 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005684}
5685
5686static int
5687super_init(PyObject *self, PyObject *args, PyObject *kwds)
5688{
5689 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00005690 PyTypeObject *type;
5691 PyObject *obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005692 PyTypeObject *obj_type = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005693
5694 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
5695 return -1;
5696 if (obj == Py_None)
5697 obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005698 if (obj != NULL) {
5699 obj_type = supercheck(type, obj);
5700 if (obj_type == NULL)
5701 return -1;
5702 Py_INCREF(obj);
5703 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005704 Py_INCREF(type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005705 su->type = type;
5706 su->obj = obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005707 su->obj_type = obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005708 return 0;
5709}
5710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005711PyDoc_STRVAR(super_doc,
Guido van Rossum705f0f52001-08-24 16:47:00 +00005712"super(type) -> unbound super object\n"
5713"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00005714"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00005715"Typical use to call a cooperative superclass method:\n"
5716"class C(B):\n"
5717" def meth(self, arg):\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005718" super(C, self).meth(arg)");
Guido van Rossum705f0f52001-08-24 16:47:00 +00005719
Guido van Rossum048eb752001-10-02 21:24:57 +00005720static int
5721super_traverse(PyObject *self, visitproc visit, void *arg)
5722{
5723 superobject *su = (superobject *)self;
Guido van Rossum048eb752001-10-02 21:24:57 +00005724
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005725 Py_VISIT(su->obj);
5726 Py_VISIT(su->type);
5727 Py_VISIT(su->obj_type);
Guido van Rossum048eb752001-10-02 21:24:57 +00005728
5729 return 0;
5730}
5731
Guido van Rossum705f0f52001-08-24 16:47:00 +00005732PyTypeObject PySuper_Type = {
5733 PyObject_HEAD_INIT(&PyType_Type)
5734 0, /* ob_size */
5735 "super", /* tp_name */
5736 sizeof(superobject), /* tp_basicsize */
5737 0, /* tp_itemsize */
5738 /* methods */
5739 super_dealloc, /* tp_dealloc */
5740 0, /* tp_print */
5741 0, /* tp_getattr */
5742 0, /* tp_setattr */
5743 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005744 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005745 0, /* tp_as_number */
5746 0, /* tp_as_sequence */
5747 0, /* tp_as_mapping */
5748 0, /* tp_hash */
5749 0, /* tp_call */
5750 0, /* tp_str */
5751 super_getattro, /* tp_getattro */
5752 0, /* tp_setattro */
5753 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00005754 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
5755 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005756 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00005757 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005758 0, /* tp_clear */
5759 0, /* tp_richcompare */
5760 0, /* tp_weaklistoffset */
5761 0, /* tp_iter */
5762 0, /* tp_iternext */
5763 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005764 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005765 0, /* tp_getset */
5766 0, /* tp_base */
5767 0, /* tp_dict */
5768 super_descr_get, /* tp_descr_get */
5769 0, /* tp_descr_set */
5770 0, /* tp_dictoffset */
5771 super_init, /* tp_init */
5772 PyType_GenericAlloc, /* tp_alloc */
5773 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00005774 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005775};