blob: cc844ad6ddb1e2467627634ea79627ec4159b134 [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{
24 char *s;
25
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000026 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Guido van Rossume5c691a2003-03-07 15:13:17 +000027 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
Tim Petersea7f75d2002-12-07 21:39:16 +000028
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000029 Py_INCREF(et->name);
30 return et->name;
31 }
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
74 Py_DECREF(et->name);
75 et->name = value;
76
77 type->tp_name = PyString_AS_STRING(value);
78
79 return 0;
80}
81
Guido van Rossumc3542212001-08-16 09:18:56 +000082static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000083type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000084{
Guido van Rossumc3542212001-08-16 09:18:56 +000085 PyObject *mod;
86 char *s;
87
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000088 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
89 mod = PyDict_GetItemString(type->tp_dict, "__module__");
90 Py_XINCREF(mod);
Guido van Rossumc3542212001-08-16 09:18:56 +000091 return mod;
92 }
Michael W. Hudsonade8c8b2002-11-27 16:29:26 +000093 else {
94 s = strrchr(type->tp_name, '.');
95 if (s != NULL)
96 return PyString_FromStringAndSize(
97 type->tp_name, (int)(s - type->tp_name));
98 return PyString_FromString("__builtin__");
99 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000100}
101
Guido van Rossum3926a632001-09-25 16:25:58 +0000102static int
103type_set_module(PyTypeObject *type, PyObject *value, void *context)
104{
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000105 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
Guido van Rossum3926a632001-09-25 16:25:58 +0000106 PyErr_Format(PyExc_TypeError,
107 "can't set %s.__module__", type->tp_name);
108 return -1;
109 }
110 if (!value) {
111 PyErr_Format(PyExc_TypeError,
112 "can't delete %s.__module__", type->tp_name);
113 return -1;
114 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000115
Guido van Rossum3926a632001-09-25 16:25:58 +0000116 return PyDict_SetItemString(type->tp_dict, "__module__", value);
117}
118
Tim Peters6d6c1a32001-08-02 04:15:00 +0000119static PyObject *
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000120type_get_bases(PyTypeObject *type, void *context)
121{
122 Py_INCREF(type->tp_bases);
123 return type->tp_bases;
124}
125
126static PyTypeObject *best_base(PyObject *);
127static int mro_internal(PyTypeObject *);
128static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
129static int add_subclass(PyTypeObject*, PyTypeObject*);
130static void remove_subclass(PyTypeObject *, PyTypeObject *);
131static void update_all_slots(PyTypeObject *);
132
Guido van Rossum8d24ee92003-03-24 23:49:49 +0000133typedef int (*update_callback)(PyTypeObject *, void *);
134static int update_subclasses(PyTypeObject *type, PyObject *name,
135 update_callback callback, void *data);
136static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
137 update_callback callback, void *data);
138
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000139static int
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000140mro_subclasses(PyTypeObject *type, PyObject* temp)
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000141{
142 PyTypeObject *subclass;
143 PyObject *ref, *subclasses, *old_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000144 int i, n;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000145
146 subclasses = type->tp_subclasses;
147 if (subclasses == NULL)
148 return 0;
149 assert(PyList_Check(subclasses));
150 n = PyList_GET_SIZE(subclasses);
151 for (i = 0; i < n; i++) {
152 ref = PyList_GET_ITEM(subclasses, i);
153 assert(PyWeakref_CheckRef(ref));
154 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
155 assert(subclass != NULL);
156 if ((PyObject *)subclass == Py_None)
157 continue;
158 assert(PyType_Check(subclass));
159 old_mro = subclass->tp_mro;
160 if (mro_internal(subclass) < 0) {
161 subclass->tp_mro = old_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000162 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000163 }
164 else {
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000165 PyObject* tuple;
166 tuple = Py_BuildValue("OO", subclass, old_mro);
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000167 Py_DECREF(old_mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000168 if (!tuple)
169 return -1;
170 if (PyList_Append(temp, tuple) < 0)
171 return -1;
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000172 Py_DECREF(tuple);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000173 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000174 if (mro_subclasses(subclass, temp) < 0)
175 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000176 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000177 return 0;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000178}
179
180static int
181type_set_bases(PyTypeObject *type, PyObject *value, void *context)
182{
183 int i, r = 0;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000184 PyObject *ob, *temp;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000185 PyTypeObject *new_base, *old_base;
186 PyObject *old_bases, *old_mro;
187
188 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
189 PyErr_Format(PyExc_TypeError,
190 "can't set %s.__bases__", type->tp_name);
191 return -1;
192 }
193 if (!value) {
194 PyErr_Format(PyExc_TypeError,
195 "can't delete %s.__bases__", type->tp_name);
196 return -1;
197 }
198 if (!PyTuple_Check(value)) {
199 PyErr_Format(PyExc_TypeError,
200 "can only assign tuple to %s.__bases__, not %s",
201 type->tp_name, value->ob_type->tp_name);
202 return -1;
203 }
Guido van Rossum3bbc0ee2002-12-13 17:49:38 +0000204 if (PyTuple_GET_SIZE(value) == 0) {
205 PyErr_Format(PyExc_TypeError,
206 "can only assign non-empty tuple to %s.__bases__, not ()",
207 type->tp_name);
208 return -1;
209 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000210 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
211 ob = PyTuple_GET_ITEM(value, i);
212 if (!PyClass_Check(ob) && !PyType_Check(ob)) {
213 PyErr_Format(
214 PyExc_TypeError,
215 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
216 type->tp_name, ob->ob_type->tp_name);
217 return -1;
218 }
Michael W. Hudsoncaf17be2002-11-27 10:24:44 +0000219 if (PyType_Check(ob)) {
220 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
221 PyErr_SetString(PyExc_TypeError,
222 "a __bases__ item causes an inheritance cycle");
223 return -1;
224 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000225 }
226 }
227
228 new_base = best_base(value);
229
230 if (!new_base) {
231 return -1;
232 }
233
234 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
235 return -1;
236
237 Py_INCREF(new_base);
238 Py_INCREF(value);
239
240 old_bases = type->tp_bases;
241 old_base = type->tp_base;
242 old_mro = type->tp_mro;
243
244 type->tp_bases = value;
245 type->tp_base = new_base;
246
247 if (mro_internal(type) < 0) {
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000248 goto bail;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000249 }
250
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000251 temp = PyList_New(0);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000252 if (!temp)
253 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000254
255 r = mro_subclasses(type, temp);
256
257 if (r < 0) {
258 for (i = 0; i < PyList_Size(temp); i++) {
259 PyTypeObject* cls;
260 PyObject* mro;
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000261 PyArg_ParseTuple(PyList_GET_ITEM(temp, i),
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000262 "OO", &cls, &mro);
263 Py_DECREF(cls->tp_mro);
264 cls->tp_mro = mro;
265 Py_INCREF(cls->tp_mro);
266 }
267 Py_DECREF(temp);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000268 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000269 }
270
271 Py_DECREF(temp);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000272
273 /* any base that was in __bases__ but now isn't, we
Raymond Hettingera8285862002-12-14 17:17:56 +0000274 need to remove |type| from its tp_subclasses.
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000275 conversely, any class now in __bases__ that wasn't
Raymond Hettingera8285862002-12-14 17:17:56 +0000276 needs to have |type| added to its subclasses. */
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000277
278 /* for now, sod that: just remove from all old_bases,
279 add to all new_bases */
280
281 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
282 ob = PyTuple_GET_ITEM(old_bases, i);
283 if (PyType_Check(ob)) {
284 remove_subclass(
285 (PyTypeObject*)ob, type);
286 }
287 }
288
289 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
290 ob = PyTuple_GET_ITEM(value, i);
291 if (PyType_Check(ob)) {
292 if (add_subclass((PyTypeObject*)ob, type) < 0)
293 r = -1;
294 }
295 }
296
297 update_all_slots(type);
298
299 Py_DECREF(old_bases);
300 Py_DECREF(old_base);
301 Py_DECREF(old_mro);
302
303 return r;
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000304
305 bail:
Michael W. Hudsone723e452003-08-07 14:58:10 +0000306 Py_DECREF(type->tp_bases);
307 Py_DECREF(type->tp_base);
308 if (type->tp_mro != old_mro) {
309 Py_DECREF(type->tp_mro);
310 }
311
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000312 type->tp_bases = old_bases;
313 type->tp_base = old_base;
314 type->tp_mro = old_mro;
Tim Petersea7f75d2002-12-07 21:39:16 +0000315
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000316 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000317}
318
319static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000320type_dict(PyTypeObject *type, void *context)
321{
322 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323 Py_INCREF(Py_None);
324 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000325 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000326 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000327}
328
Tim Peters24008312002-03-17 18:56:20 +0000329static PyObject *
330type_get_doc(PyTypeObject *type, void *context)
331{
332 PyObject *result;
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000333 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Tim Peters24008312002-03-17 18:56:20 +0000334 return PyString_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +0000335 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000336 if (result == NULL) {
337 result = Py_None;
338 Py_INCREF(result);
339 }
340 else if (result->ob_type->tp_descr_get) {
Tim Peters2b858972002-04-18 04:12:28 +0000341 result = result->ob_type->tp_descr_get(result, NULL,
342 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000343 }
344 else {
345 Py_INCREF(result);
346 }
Tim Peters24008312002-03-17 18:56:20 +0000347 return result;
348}
349
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000350static PyGetSetDef type_getsets[] = {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000351 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
352 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000353 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000354 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000355 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000356 {0}
357};
358
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000359static int
360type_compare(PyObject *v, PyObject *w)
361{
362 /* This is called with type objects only. So we
363 can just compare the addresses. */
364 Py_uintptr_t vv = (Py_uintptr_t)v;
365 Py_uintptr_t ww = (Py_uintptr_t)w;
366 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
367}
368
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000370type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000372 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000373 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000374
375 mod = type_module(type, NULL);
376 if (mod == NULL)
377 PyErr_Clear();
378 else if (!PyString_Check(mod)) {
379 Py_DECREF(mod);
380 mod = NULL;
381 }
382 name = type_name(type, NULL);
383 if (name == NULL)
384 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000385
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000386 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
387 kind = "class";
388 else
389 kind = "type";
390
Barry Warsaw7ce36942001-08-24 18:34:26 +0000391 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000392 rtn = PyString_FromFormat("<%s '%s.%s'>",
393 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000394 PyString_AS_STRING(mod),
395 PyString_AS_STRING(name));
396 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000397 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000398 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000399
Guido van Rossumc3542212001-08-16 09:18:56 +0000400 Py_XDECREF(mod);
401 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000402 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403}
404
Tim Peters6d6c1a32001-08-02 04:15:00 +0000405static PyObject *
406type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
407{
408 PyObject *obj;
409
410 if (type->tp_new == NULL) {
411 PyErr_Format(PyExc_TypeError,
412 "cannot create '%.100s' instances",
413 type->tp_name);
414 return NULL;
415 }
416
Tim Peters3f996e72001-09-13 19:18:27 +0000417 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000418 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000419 /* Ugly exception: when the call was type(something),
420 don't call tp_init on the result. */
421 if (type == &PyType_Type &&
422 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
423 (kwds == NULL ||
424 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
425 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000426 /* If the returned object is not an instance of type,
427 it won't be initialized. */
428 if (!PyType_IsSubtype(obj->ob_type, type))
429 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000430 type = obj->ob_type;
Jeremy Hylton719841e2002-07-16 19:39:38 +0000431 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
432 type->tp_init != NULL &&
Tim Peters6d6c1a32001-08-02 04:15:00 +0000433 type->tp_init(obj, args, kwds) < 0) {
434 Py_DECREF(obj);
435 obj = NULL;
436 }
437 }
438 return obj;
439}
440
441PyObject *
442PyType_GenericAlloc(PyTypeObject *type, int nitems)
443{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000444 PyObject *obj;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000445 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
446 /* note that we need to add one, for the sentinel */
Tim Peters406fe3b2001-10-06 19:04:01 +0000447
448 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000449 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000450 else
Neil Schemenauerc806c882001-08-29 23:54:54 +0000451 obj = PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000452
Neil Schemenauerc806c882001-08-29 23:54:54 +0000453 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000454 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000455
Neil Schemenauerc806c882001-08-29 23:54:54 +0000456 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000457
Tim Peters6d6c1a32001-08-02 04:15:00 +0000458 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
459 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000460
Tim Peters6d6c1a32001-08-02 04:15:00 +0000461 if (type->tp_itemsize == 0)
462 PyObject_INIT(obj, type);
463 else
464 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000465
Tim Peters6d6c1a32001-08-02 04:15:00 +0000466 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000467 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000468 return obj;
469}
470
471PyObject *
472PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
473{
474 return type->tp_alloc(type, 0);
475}
476
Guido van Rossum9475a232001-10-05 20:51:39 +0000477/* Helpers for subtyping */
478
479static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000480traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
481{
482 int i, n;
483 PyMemberDef *mp;
484
485 n = type->ob_size;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000486 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000487 for (i = 0; i < n; i++, mp++) {
488 if (mp->type == T_OBJECT_EX) {
489 char *addr = (char *)self + mp->offset;
490 PyObject *obj = *(PyObject **)addr;
491 if (obj != NULL) {
492 int err = visit(obj, arg);
493 if (err)
494 return err;
495 }
496 }
497 }
498 return 0;
499}
500
501static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000502subtype_traverse(PyObject *self, visitproc visit, void *arg)
503{
504 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000505 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000506
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000507 /* Find the nearest base with a different tp_traverse,
508 and traverse slots while we're at it */
Guido van Rossum9475a232001-10-05 20:51:39 +0000509 type = self->ob_type;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000510 base = type;
511 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
512 if (base->ob_size) {
513 int err = traverse_slots(base, self, visit, arg);
514 if (err)
515 return err;
516 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000517 base = base->tp_base;
518 assert(base);
519 }
520
521 if (type->tp_dictoffset != base->tp_dictoffset) {
522 PyObject **dictptr = _PyObject_GetDictPtr(self);
523 if (dictptr && *dictptr) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000524 int err = visit(*dictptr, arg);
Guido van Rossum9475a232001-10-05 20:51:39 +0000525 if (err)
526 return err;
527 }
528 }
529
Guido van Rossuma3862092002-06-10 15:24:42 +0000530 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
531 /* For a heaptype, the instances count as references
532 to the type. Traverse the type so the collector
533 can find cycles involving this link. */
534 int err = visit((PyObject *)type, arg);
535 if (err)
536 return err;
537 }
538
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000539 if (basetraverse)
540 return basetraverse(self, visit, arg);
541 return 0;
542}
543
544static void
545clear_slots(PyTypeObject *type, PyObject *self)
546{
547 int i, n;
548 PyMemberDef *mp;
549
550 n = type->ob_size;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000551 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000552 for (i = 0; i < n; i++, mp++) {
553 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
554 char *addr = (char *)self + mp->offset;
555 PyObject *obj = *(PyObject **)addr;
556 if (obj != NULL) {
557 Py_DECREF(obj);
558 *(PyObject **)addr = NULL;
559 }
560 }
561 }
562}
563
564static int
565subtype_clear(PyObject *self)
566{
567 PyTypeObject *type, *base;
568 inquiry baseclear;
569
570 /* Find the nearest base with a different tp_clear
571 and clear slots while we're at it */
572 type = self->ob_type;
573 base = type;
574 while ((baseclear = base->tp_clear) == subtype_clear) {
575 if (base->ob_size)
576 clear_slots(base, self);
577 base = base->tp_base;
578 assert(base);
579 }
580
Guido van Rossuma3862092002-06-10 15:24:42 +0000581 /* There's no need to clear the instance dict (if any);
582 the collector will call its tp_clear handler. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000583
584 if (baseclear)
585 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000586 return 0;
587}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000588
589static void
590subtype_dealloc(PyObject *self)
591{
Guido van Rossum14227b42001-12-06 02:35:58 +0000592 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000593 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000594
Guido van Rossum22b13872002-08-06 21:41:44 +0000595 /* Extract the type; we expect it to be a heap type */
596 type = self->ob_type;
597 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000598
Guido van Rossum22b13872002-08-06 21:41:44 +0000599 /* Test whether the type has GC exactly once */
600
601 if (!PyType_IS_GC(type)) {
602 /* It's really rare to find a dynamic type that doesn't have
603 GC; it can only happen when deriving from 'object' and not
604 adding any slots or instance variables. This allows
605 certain simplifications: there's no need to call
606 clear_slots(), or DECREF the dict, or clear weakrefs. */
607
608 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000609 if (type->tp_del) {
610 type->tp_del(self);
611 if (self->ob_refcnt > 0)
612 return;
613 }
Guido van Rossum22b13872002-08-06 21:41:44 +0000614
615 /* Find the nearest base with a different tp_dealloc */
616 base = type;
617 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
618 assert(base->ob_size == 0);
619 base = base->tp_base;
620 assert(base);
621 }
622
623 /* Call the base tp_dealloc() */
624 assert(basedealloc);
625 basedealloc(self);
626
627 /* Can't reference self beyond this point */
628 Py_DECREF(type);
629
630 /* Done */
631 return;
632 }
633
634 /* We get here only if the type has GC */
635
636 /* UnTrack and re-Track around the trashcan macro, alas */
Andrew M. Kuchlingc9172d32003-02-06 15:22:49 +0000637 /* See explanation at end of function for full disclosure */
Guido van Rossum0906e072002-08-07 20:42:09 +0000638 PyObject_GC_UnTrack(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000639 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000640 Py_TRASHCAN_SAFE_BEGIN(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000641 --_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000642 _PyObject_GC_TRACK(self); /* We'll untrack for real later */
643
Guido van Rossum59195fd2003-06-13 20:54:40 +0000644 /* Find the nearest base with a different tp_dealloc */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000645 base = type;
646 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647 base = base->tp_base;
648 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000649 }
650
Guido van Rossum1987c662003-05-29 14:29:23 +0000651 /* If we added a weaklist, we clear it. Do this *before* calling
Guido van Rossum59195fd2003-06-13 20:54:40 +0000652 the finalizer (__del__), clearing slots, or clearing the instance
653 dict. */
654
Guido van Rossum1987c662003-05-29 14:29:23 +0000655 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
656 PyObject_ClearWeakRefs(self);
657
658 /* Maybe call finalizer; exit early if resurrected */
659 if (type->tp_del) {
660 type->tp_del(self);
661 if (self->ob_refcnt > 0)
662 goto endlabel;
663 }
664
Guido van Rossum59195fd2003-06-13 20:54:40 +0000665 /* Clear slots up to the nearest base with a different tp_dealloc */
666 base = type;
667 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
668 if (base->ob_size)
669 clear_slots(base, self);
670 base = base->tp_base;
671 assert(base);
672 }
673
Tim Peters6d6c1a32001-08-02 04:15:00 +0000674 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000675 if (type->tp_dictoffset && !base->tp_dictoffset) {
676 PyObject **dictptr = _PyObject_GetDictPtr(self);
677 if (dictptr != NULL) {
678 PyObject *dict = *dictptr;
679 if (dict != NULL) {
680 Py_DECREF(dict);
681 *dictptr = NULL;
682 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000683 }
684 }
685
686 /* Finalize GC if the base doesn't do GC and we do */
Guido van Rossum22b13872002-08-06 21:41:44 +0000687 if (!PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000688 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689
690 /* Call the base tp_dealloc() */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000691 assert(basedealloc);
692 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000693
694 /* Can't reference self beyond this point */
Guido van Rossum22b13872002-08-06 21:41:44 +0000695 Py_DECREF(type);
696
Guido van Rossum0906e072002-08-07 20:42:09 +0000697 endlabel:
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000698 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000699 Py_TRASHCAN_SAFE_END(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000700 --_PyTrash_delete_nesting;
701
702 /* Explanation of the weirdness around the trashcan macros:
703
704 Q. What do the trashcan macros do?
705
706 A. Read the comment titled "Trashcan mechanism" in object.h.
707 For one, this explains why there must be a call to GC-untrack
708 before the trashcan begin macro. Without understanding the
709 trashcan code, the answers to the following questions don't make
710 sense.
711
712 Q. Why do we GC-untrack before the trashcan and then immediately
713 GC-track again afterward?
714
715 A. In the case that the base class is GC-aware, the base class
716 probably GC-untracks the object. If it does that using the
717 UNTRACK macro, this will crash when the object is already
718 untracked. Because we don't know what the base class does, the
719 only safe thing is to make sure the object is tracked when we
720 call the base class dealloc. But... The trashcan begin macro
721 requires that the object is *untracked* before it is called. So
722 the dance becomes:
723
724 GC untrack
725 trashcan begin
726 GC track
727
728 Q. Why the bizarre (net-zero) manipulation of
729 _PyTrash_delete_nesting around the trashcan macros?
730
731 A. Some base classes (e.g. list) also use the trashcan mechanism.
732 The following scenario used to be possible:
733
734 - suppose the trashcan level is one below the trashcan limit
735
736 - subtype_dealloc() is called
737
738 - the trashcan limit is not yet reached, so the trashcan level
739 is incremented and the code between trashcan begin and end is
740 executed
741
742 - this destroys much of the object's contents, including its
743 slots and __dict__
744
745 - basedealloc() is called; this is really list_dealloc(), or
746 some other type which also uses the trashcan macros
747
748 - the trashcan limit is now reached, so the object is put on the
749 trashcan's to-be-deleted-later list
750
751 - basedealloc() returns
752
753 - subtype_dealloc() decrefs the object's type
754
755 - subtype_dealloc() returns
756
757 - later, the trashcan code starts deleting the objects from its
758 to-be-deleted-later list
759
760 - subtype_dealloc() is called *AGAIN* for the same object
761
762 - at the very least (if the destroyed slots and __dict__ don't
763 cause problems) the object's type gets decref'ed a second
764 time, which is *BAD*!!!
765
766 The remedy is to make sure that if the code between trashcan
767 begin and end in subtype_dealloc() is called, the code between
768 trashcan begin and end in basedealloc() will also be called.
769 This is done by decrementing the level after passing into the
770 trashcan block, and incrementing it just before leaving the
771 block.
772
773 But now it's possible that a chain of objects consisting solely
774 of objects whose deallocator is subtype_dealloc() will defeat
775 the trashcan mechanism completely: the decremented level means
776 that the effective level never reaches the limit. Therefore, we
777 *increment* the level *before* entering the trashcan block, and
778 matchingly decrement it after leaving. This means the trashcan
779 code will trigger a little early, but that's no big deal.
780
781 Q. Are there any live examples of code in need of all this
782 complexity?
783
784 A. Yes. See SF bug 668433 for code that crashed (when Python was
785 compiled in debug mode) before the trashcan level manipulations
786 were added. For more discussion, see SF patches 581742, 575073
787 and bug 574207.
788 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000789}
790
Jeremy Hylton938ace62002-07-17 16:30:39 +0000791static PyTypeObject *solid_base(PyTypeObject *type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000792
Tim Peters6d6c1a32001-08-02 04:15:00 +0000793/* type test with subclassing support */
794
795int
796PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
797{
798 PyObject *mro;
799
Guido van Rossum9478d072001-09-07 18:52:13 +0000800 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
801 return b == a || b == &PyBaseObject_Type;
802
Tim Peters6d6c1a32001-08-02 04:15:00 +0000803 mro = a->tp_mro;
804 if (mro != NULL) {
805 /* Deal with multiple inheritance without recursion
806 by walking the MRO tuple */
807 int i, n;
808 assert(PyTuple_Check(mro));
809 n = PyTuple_GET_SIZE(mro);
810 for (i = 0; i < n; i++) {
811 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
812 return 1;
813 }
814 return 0;
815 }
816 else {
817 /* a is not completely initilized yet; follow tp_base */
818 do {
819 if (a == b)
820 return 1;
821 a = a->tp_base;
822 } while (a != NULL);
823 return b == &PyBaseObject_Type;
824 }
825}
826
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000827/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000828 without looking in the instance dictionary
829 (so we can't use PyObject_GetAttr) but still binding
830 it to the instance. The arguments are the object,
831 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000832 static variable used to cache the interned Python string.
833
834 Two variants:
835
836 - lookup_maybe() returns NULL without raising an exception
837 when the _PyType_Lookup() call fails;
838
839 - lookup_method() always raises an exception upon errors.
840*/
Guido van Rossum60718732001-08-28 17:47:51 +0000841
842static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000843lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000844{
845 PyObject *res;
846
847 if (*attrobj == NULL) {
848 *attrobj = PyString_InternFromString(attrstr);
849 if (*attrobj == NULL)
850 return NULL;
851 }
852 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000853 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000854 descrgetfunc f;
855 if ((f = res->ob_type->tp_descr_get) == NULL)
856 Py_INCREF(res);
857 else
858 res = f(res, self, (PyObject *)(self->ob_type));
859 }
860 return res;
861}
862
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000863static PyObject *
864lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
865{
866 PyObject *res = lookup_maybe(self, attrstr, attrobj);
867 if (res == NULL && !PyErr_Occurred())
868 PyErr_SetObject(PyExc_AttributeError, *attrobj);
869 return res;
870}
871
Guido van Rossum2730b132001-08-28 18:22:14 +0000872/* A variation of PyObject_CallMethod that uses lookup_method()
873 instead of PyObject_GetAttrString(). This uses the same convention
874 as lookup_method to cache the interned name string object. */
875
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000876static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000877call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
878{
879 va_list va;
880 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000881 va_start(va, format);
882
Guido van Rossumda21c012001-10-03 00:50:18 +0000883 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000884 if (func == NULL) {
885 va_end(va);
886 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000887 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000888 return NULL;
889 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000890
891 if (format && *format)
892 args = Py_VaBuildValue(format, va);
893 else
894 args = PyTuple_New(0);
895
896 va_end(va);
897
898 if (args == NULL)
899 return NULL;
900
901 assert(PyTuple_Check(args));
902 retval = PyObject_Call(func, args, NULL);
903
904 Py_DECREF(args);
905 Py_DECREF(func);
906
907 return retval;
908}
909
910/* Clone of call_method() that returns NotImplemented when the lookup fails. */
911
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000912static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000913call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
914{
915 va_list va;
916 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000917 va_start(va, format);
918
Guido van Rossumda21c012001-10-03 00:50:18 +0000919 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000920 if (func == NULL) {
921 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000922 if (!PyErr_Occurred()) {
923 Py_INCREF(Py_NotImplemented);
924 return Py_NotImplemented;
925 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000926 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000927 }
928
929 if (format && *format)
930 args = Py_VaBuildValue(format, va);
931 else
932 args = PyTuple_New(0);
933
934 va_end(va);
935
Guido van Rossum717ce002001-09-14 16:58:08 +0000936 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000937 return NULL;
938
Guido van Rossum717ce002001-09-14 16:58:08 +0000939 assert(PyTuple_Check(args));
940 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000941
942 Py_DECREF(args);
943 Py_DECREF(func);
944
945 return retval;
946}
947
Tim Petersa91e9642001-11-14 23:32:33 +0000948static int
949fill_classic_mro(PyObject *mro, PyObject *cls)
950{
951 PyObject *bases, *base;
952 int i, n;
953
954 assert(PyList_Check(mro));
955 assert(PyClass_Check(cls));
956 i = PySequence_Contains(mro, cls);
957 if (i < 0)
958 return -1;
959 if (!i) {
960 if (PyList_Append(mro, cls) < 0)
961 return -1;
962 }
963 bases = ((PyClassObject *)cls)->cl_bases;
964 assert(bases && PyTuple_Check(bases));
965 n = PyTuple_GET_SIZE(bases);
966 for (i = 0; i < n; i++) {
967 base = PyTuple_GET_ITEM(bases, i);
968 if (fill_classic_mro(mro, base) < 0)
969 return -1;
970 }
971 return 0;
972}
973
974static PyObject *
975classic_mro(PyObject *cls)
976{
977 PyObject *mro;
978
979 assert(PyClass_Check(cls));
980 mro = PyList_New(0);
981 if (mro != NULL) {
982 if (fill_classic_mro(mro, cls) == 0)
983 return mro;
984 Py_DECREF(mro);
985 }
986 return NULL;
987}
988
Tim Petersea7f75d2002-12-07 21:39:16 +0000989/*
Guido van Rossum1f121312002-11-14 19:49:16 +0000990 Method resolution order algorithm C3 described in
991 "A Monotonic Superclass Linearization for Dylan",
992 by Kim Barrett, Bob Cassel, Paul Haahr,
Tim Petersea7f75d2002-12-07 21:39:16 +0000993 David A. Moon, Keith Playford, and P. Tucker Withington.
Guido van Rossum1f121312002-11-14 19:49:16 +0000994 (OOPSLA 1996)
995
Guido van Rossum98f33732002-11-25 21:36:54 +0000996 Some notes about the rules implied by C3:
997
Tim Petersea7f75d2002-12-07 21:39:16 +0000998 No duplicate bases.
Guido van Rossum98f33732002-11-25 21:36:54 +0000999 It isn't legal to repeat a class in a list of base classes.
1000
1001 The next three properties are the 3 constraints in "C3".
1002
Tim Petersea7f75d2002-12-07 21:39:16 +00001003 Local precendece order.
Guido van Rossum98f33732002-11-25 21:36:54 +00001004 If A precedes B in C's MRO, then A will precede B in the MRO of all
1005 subclasses of C.
1006
1007 Monotonicity.
1008 The MRO of a class must be an extension without reordering of the
1009 MRO of each of its superclasses.
1010
1011 Extended Precedence Graph (EPG).
1012 Linearization is consistent if there is a path in the EPG from
1013 each class to all its successors in the linearization. See
1014 the paper for definition of EPG.
Guido van Rossum1f121312002-11-14 19:49:16 +00001015 */
1016
Tim Petersea7f75d2002-12-07 21:39:16 +00001017static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001018tail_contains(PyObject *list, int whence, PyObject *o) {
1019 int j, size;
1020 size = PyList_GET_SIZE(list);
1021
1022 for (j = whence+1; j < size; j++) {
1023 if (PyList_GET_ITEM(list, j) == o)
1024 return 1;
1025 }
1026 return 0;
1027}
1028
Guido van Rossum98f33732002-11-25 21:36:54 +00001029static PyObject *
1030class_name(PyObject *cls)
1031{
1032 PyObject *name = PyObject_GetAttrString(cls, "__name__");
1033 if (name == NULL) {
1034 PyErr_Clear();
1035 Py_XDECREF(name);
1036 name = PyObject_Repr(cls);
1037 }
1038 if (name == NULL)
1039 return NULL;
1040 if (!PyString_Check(name)) {
1041 Py_DECREF(name);
1042 return NULL;
1043 }
1044 return name;
1045}
1046
1047static int
1048check_duplicates(PyObject *list)
1049{
1050 int i, j, n;
1051 /* Let's use a quadratic time algorithm,
1052 assuming that the bases lists is short.
1053 */
1054 n = PyList_GET_SIZE(list);
1055 for (i = 0; i < n; i++) {
1056 PyObject *o = PyList_GET_ITEM(list, i);
1057 for (j = i + 1; j < n; j++) {
1058 if (PyList_GET_ITEM(list, j) == o) {
1059 o = class_name(o);
1060 PyErr_Format(PyExc_TypeError,
1061 "duplicate base class %s",
1062 o ? PyString_AS_STRING(o) : "?");
1063 Py_XDECREF(o);
1064 return -1;
1065 }
1066 }
1067 }
1068 return 0;
1069}
1070
1071/* Raise a TypeError for an MRO order disagreement.
1072
1073 It's hard to produce a good error message. In the absence of better
1074 insight into error reporting, report the classes that were candidates
1075 to be put next into the MRO. There is some conflict between the
1076 order in which they should be put in the MRO, but it's hard to
1077 diagnose what constraint can't be satisfied.
1078*/
1079
1080static void
1081set_mro_error(PyObject *to_merge, int *remain)
1082{
1083 int i, n, off, to_merge_size;
1084 char buf[1000];
1085 PyObject *k, *v;
1086 PyObject *set = PyDict_New();
1087
1088 to_merge_size = PyList_GET_SIZE(to_merge);
1089 for (i = 0; i < to_merge_size; i++) {
1090 PyObject *L = PyList_GET_ITEM(to_merge, i);
1091 if (remain[i] < PyList_GET_SIZE(L)) {
1092 PyObject *c = PyList_GET_ITEM(L, remain[i]);
1093 if (PyDict_SetItem(set, c, Py_None) < 0)
1094 return;
1095 }
1096 }
1097 n = PyDict_Size(set);
1098
Raymond Hettingerf394df42003-04-06 19:13:41 +00001099 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1100consistent method resolution\norder (MRO) for bases");
Guido van Rossum98f33732002-11-25 21:36:54 +00001101 i = 0;
1102 while (PyDict_Next(set, &i, &k, &v) && off < sizeof(buf)) {
1103 PyObject *name = class_name(k);
1104 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1105 name ? PyString_AS_STRING(name) : "?");
1106 Py_XDECREF(name);
1107 if (--n && off+1 < sizeof(buf)) {
1108 buf[off++] = ',';
1109 buf[off] = '\0';
1110 }
1111 }
1112 PyErr_SetString(PyExc_TypeError, buf);
1113 Py_DECREF(set);
1114}
1115
Tim Petersea7f75d2002-12-07 21:39:16 +00001116static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001117pmerge(PyObject *acc, PyObject* to_merge) {
1118 int i, j, to_merge_size;
1119 int *remain;
1120 int ok, empty_cnt;
Tim Petersea7f75d2002-12-07 21:39:16 +00001121
Guido van Rossum1f121312002-11-14 19:49:16 +00001122 to_merge_size = PyList_GET_SIZE(to_merge);
1123
Guido van Rossum98f33732002-11-25 21:36:54 +00001124 /* remain stores an index into each sublist of to_merge.
1125 remain[i] is the index of the next base in to_merge[i]
1126 that is not included in acc.
1127 */
Guido van Rossum1f121312002-11-14 19:49:16 +00001128 remain = PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1129 if (remain == NULL)
1130 return -1;
1131 for (i = 0; i < to_merge_size; i++)
1132 remain[i] = 0;
1133
1134 again:
1135 empty_cnt = 0;
1136 for (i = 0; i < to_merge_size; i++) {
1137 PyObject *candidate;
Tim Petersea7f75d2002-12-07 21:39:16 +00001138
Guido van Rossum1f121312002-11-14 19:49:16 +00001139 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1140
1141 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1142 empty_cnt++;
1143 continue;
1144 }
1145
Guido van Rossum98f33732002-11-25 21:36:54 +00001146 /* Choose next candidate for MRO.
1147
1148 The input sequences alone can determine the choice.
1149 If not, choose the class which appears in the MRO
1150 of the earliest direct superclass of the new class.
1151 */
1152
Guido van Rossum1f121312002-11-14 19:49:16 +00001153 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1154 for (j = 0; j < to_merge_size; j++) {
1155 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum98f33732002-11-25 21:36:54 +00001156 if (tail_contains(j_lst, remain[j], candidate)) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001157 goto skip; /* continue outer loop */
Guido van Rossum98f33732002-11-25 21:36:54 +00001158 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001159 }
1160 ok = PyList_Append(acc, candidate);
1161 if (ok < 0) {
1162 PyMem_Free(remain);
1163 return -1;
1164 }
1165 for (j = 0; j < to_merge_size; j++) {
1166 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum768158c2002-12-31 16:33:01 +00001167 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1168 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001169 remain[j]++;
1170 }
1171 }
1172 goto again;
Tim Peters9a6b8d82002-11-14 23:22:33 +00001173 skip: ;
Guido van Rossum1f121312002-11-14 19:49:16 +00001174 }
1175
Guido van Rossum98f33732002-11-25 21:36:54 +00001176 if (empty_cnt == to_merge_size) {
1177 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001178 return 0;
Guido van Rossum98f33732002-11-25 21:36:54 +00001179 }
1180 set_mro_error(to_merge, remain);
1181 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001182 return -1;
1183}
1184
Tim Peters6d6c1a32001-08-02 04:15:00 +00001185static PyObject *
1186mro_implementation(PyTypeObject *type)
1187{
1188 int i, n, ok;
1189 PyObject *bases, *result;
Guido van Rossum1f121312002-11-14 19:49:16 +00001190 PyObject *to_merge, *bases_aslist;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001191
Guido van Rossum63517572002-06-18 16:44:57 +00001192 if(type->tp_dict == NULL) {
1193 if(PyType_Ready(type) < 0)
1194 return NULL;
1195 }
1196
Guido van Rossum98f33732002-11-25 21:36:54 +00001197 /* Find a superclass linearization that honors the constraints
1198 of the explicit lists of bases and the constraints implied by
Tim Petersea7f75d2002-12-07 21:39:16 +00001199 each base class.
Guido van Rossum98f33732002-11-25 21:36:54 +00001200
1201 to_merge is a list of lists, where each list is a superclass
1202 linearization implied by a base class. The last element of
1203 to_merge is the declared list of bases.
1204 */
1205
Tim Peters6d6c1a32001-08-02 04:15:00 +00001206 bases = type->tp_bases;
1207 n = PyTuple_GET_SIZE(bases);
Guido van Rossum1f121312002-11-14 19:49:16 +00001208
1209 to_merge = PyList_New(n+1);
1210 if (to_merge == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001211 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001212
Tim Peters6d6c1a32001-08-02 04:15:00 +00001213 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001214 PyObject *base = PyTuple_GET_ITEM(bases, i);
1215 PyObject *parentMRO;
1216 if (PyType_Check(base))
1217 parentMRO = PySequence_List(
1218 ((PyTypeObject*)base)->tp_mro);
1219 else
1220 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001221 if (parentMRO == NULL) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001222 Py_DECREF(to_merge);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001223 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001224 }
1225
1226 PyList_SET_ITEM(to_merge, i, parentMRO);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001227 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001228
1229 bases_aslist = PySequence_List(bases);
1230 if (bases_aslist == NULL) {
1231 Py_DECREF(to_merge);
1232 return NULL;
1233 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001234 /* This is just a basic sanity check. */
1235 if (check_duplicates(bases_aslist) < 0) {
1236 Py_DECREF(to_merge);
1237 Py_DECREF(bases_aslist);
1238 return NULL;
1239 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001240 PyList_SET_ITEM(to_merge, n, bases_aslist);
1241
1242 result = Py_BuildValue("[O]", (PyObject *)type);
1243 if (result == NULL) {
1244 Py_DECREF(to_merge);
1245 return NULL;
1246 }
1247
1248 ok = pmerge(result, to_merge);
1249 Py_DECREF(to_merge);
1250 if (ok < 0) {
1251 Py_DECREF(result);
1252 return NULL;
1253 }
1254
Tim Peters6d6c1a32001-08-02 04:15:00 +00001255 return result;
1256}
1257
1258static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001259mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001260{
1261 PyTypeObject *type = (PyTypeObject *)self;
1262
Tim Peters6d6c1a32001-08-02 04:15:00 +00001263 return mro_implementation(type);
1264}
1265
1266static int
1267mro_internal(PyTypeObject *type)
1268{
1269 PyObject *mro, *result, *tuple;
1270
1271 if (type->ob_type == &PyType_Type) {
1272 result = mro_implementation(type);
1273 }
1274 else {
Guido van Rossum60718732001-08-28 17:47:51 +00001275 static PyObject *mro_str;
1276 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001277 if (mro == NULL)
1278 return -1;
1279 result = PyObject_CallObject(mro, NULL);
1280 Py_DECREF(mro);
1281 }
1282 if (result == NULL)
1283 return -1;
1284 tuple = PySequence_Tuple(result);
1285 Py_DECREF(result);
1286 type->tp_mro = tuple;
1287 return 0;
1288}
1289
1290
1291/* Calculate the best base amongst multiple base classes.
1292 This is the first one that's on the path to the "solid base". */
1293
1294static PyTypeObject *
1295best_base(PyObject *bases)
1296{
1297 int i, n;
1298 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +00001299 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001300
1301 assert(PyTuple_Check(bases));
1302 n = PyTuple_GET_SIZE(bases);
1303 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +00001304 base = NULL;
1305 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001306 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001307 base_proto = PyTuple_GET_ITEM(bases, i);
1308 if (PyClass_Check(base_proto))
1309 continue;
1310 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001311 PyErr_SetString(
1312 PyExc_TypeError,
1313 "bases must be types");
1314 return NULL;
1315 }
Tim Petersa91e9642001-11-14 23:32:33 +00001316 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001317 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001318 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001319 return NULL;
1320 }
1321 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +00001322 if (winner == NULL) {
1323 winner = candidate;
1324 base = base_i;
1325 }
1326 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001327 ;
1328 else if (PyType_IsSubtype(candidate, winner)) {
1329 winner = candidate;
1330 base = base_i;
1331 }
1332 else {
1333 PyErr_SetString(
1334 PyExc_TypeError,
1335 "multiple bases have "
1336 "instance lay-out conflict");
1337 return NULL;
1338 }
1339 }
Guido van Rossume54616c2001-12-14 04:19:56 +00001340 if (base == NULL)
1341 PyErr_SetString(PyExc_TypeError,
1342 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001343 return base;
1344}
1345
1346static int
1347extra_ivars(PyTypeObject *type, PyTypeObject *base)
1348{
Neil Schemenauerc806c882001-08-29 23:54:54 +00001349 size_t t_size = type->tp_basicsize;
1350 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001351
Guido van Rossum9676b222001-08-17 20:32:36 +00001352 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001353 if (type->tp_itemsize || base->tp_itemsize) {
1354 /* If itemsize is involved, stricter rules */
1355 return t_size != b_size ||
1356 type->tp_itemsize != base->tp_itemsize;
1357 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001358 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1359 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1360 t_size -= sizeof(PyObject *);
1361 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1362 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1363 t_size -= sizeof(PyObject *);
1364
1365 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001366}
1367
1368static PyTypeObject *
1369solid_base(PyTypeObject *type)
1370{
1371 PyTypeObject *base;
1372
1373 if (type->tp_base)
1374 base = solid_base(type->tp_base);
1375 else
1376 base = &PyBaseObject_Type;
1377 if (extra_ivars(type, base))
1378 return type;
1379 else
1380 return base;
1381}
1382
Jeremy Hylton938ace62002-07-17 16:30:39 +00001383static void object_dealloc(PyObject *);
1384static int object_init(PyObject *, PyObject *, PyObject *);
1385static int update_slot(PyTypeObject *, PyObject *);
1386static void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001387
1388static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001389subtype_dict(PyObject *obj, void *context)
1390{
1391 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1392 PyObject *dict;
1393
1394 if (dictptr == NULL) {
1395 PyErr_SetString(PyExc_AttributeError,
1396 "This object has no __dict__");
1397 return NULL;
1398 }
1399 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +00001400 if (dict == NULL)
1401 *dictptr = dict = PyDict_New();
1402 Py_XINCREF(dict);
1403 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001404}
1405
Guido van Rossum6661be32001-10-26 04:26:12 +00001406static int
1407subtype_setdict(PyObject *obj, PyObject *value, void *context)
1408{
1409 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1410 PyObject *dict;
1411
1412 if (dictptr == NULL) {
1413 PyErr_SetString(PyExc_AttributeError,
1414 "This object has no __dict__");
1415 return -1;
1416 }
Guido van Rossumd331cb52001-12-05 19:46:42 +00001417 if (value != NULL && !PyDict_Check(value)) {
Guido van Rossum6661be32001-10-26 04:26:12 +00001418 PyErr_SetString(PyExc_TypeError,
1419 "__dict__ must be set to a dictionary");
1420 return -1;
1421 }
1422 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +00001423 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +00001424 *dictptr = value;
1425 Py_XDECREF(dict);
1426 return 0;
1427}
1428
Guido van Rossumad47da02002-08-12 19:05:44 +00001429static PyObject *
1430subtype_getweakref(PyObject *obj, void *context)
1431{
1432 PyObject **weaklistptr;
1433 PyObject *result;
1434
1435 if (obj->ob_type->tp_weaklistoffset == 0) {
1436 PyErr_SetString(PyExc_AttributeError,
1437 "This object has no __weaklist__");
1438 return NULL;
1439 }
1440 assert(obj->ob_type->tp_weaklistoffset > 0);
1441 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
Guido van Rossum3747a0f2002-08-12 19:25:08 +00001442 (size_t)(obj->ob_type->tp_basicsize));
Guido van Rossumad47da02002-08-12 19:05:44 +00001443 weaklistptr = (PyObject **)
Guido van Rossum3747a0f2002-08-12 19:25:08 +00001444 ((char *)obj + obj->ob_type->tp_weaklistoffset);
Guido van Rossumad47da02002-08-12 19:05:44 +00001445 if (*weaklistptr == NULL)
1446 result = Py_None;
1447 else
1448 result = *weaklistptr;
1449 Py_INCREF(result);
1450 return result;
1451}
1452
Guido van Rossum373c7412003-01-07 13:41:37 +00001453/* Three variants on the subtype_getsets list. */
1454
1455static PyGetSetDef subtype_getsets_full[] = {
Guido van Rossumad47da02002-08-12 19:05:44 +00001456 {"__dict__", subtype_dict, subtype_setdict,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001457 PyDoc_STR("dictionary for instance variables (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001458 {"__weakref__", subtype_getweakref, NULL,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001459 PyDoc_STR("list of weak references to the object (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001460 {0}
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001461};
1462
Guido van Rossum373c7412003-01-07 13:41:37 +00001463static PyGetSetDef subtype_getsets_dict_only[] = {
1464 {"__dict__", subtype_dict, subtype_setdict,
1465 PyDoc_STR("dictionary for instance variables (if defined)")},
1466 {0}
1467};
1468
1469static PyGetSetDef subtype_getsets_weakref_only[] = {
1470 {"__weakref__", subtype_getweakref, NULL,
1471 PyDoc_STR("list of weak references to the object (if defined)")},
1472 {0}
1473};
1474
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001475static int
1476valid_identifier(PyObject *s)
1477{
Guido van Rossum03013a02002-07-16 14:30:28 +00001478 unsigned char *p;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001479 int i, n;
1480
1481 if (!PyString_Check(s)) {
1482 PyErr_SetString(PyExc_TypeError,
1483 "__slots__ must be strings");
1484 return 0;
1485 }
Guido van Rossum03013a02002-07-16 14:30:28 +00001486 p = (unsigned char *) PyString_AS_STRING(s);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001487 n = PyString_GET_SIZE(s);
1488 /* We must reject an empty name. As a hack, we bump the
1489 length to 1 so that the loop will balk on the trailing \0. */
1490 if (n == 0)
1491 n = 1;
1492 for (i = 0; i < n; i++, p++) {
1493 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1494 PyErr_SetString(PyExc_TypeError,
1495 "__slots__ must be identifiers");
1496 return 0;
1497 }
1498 }
1499 return 1;
1500}
1501
Martin v. Löwisd919a592002-10-14 21:07:28 +00001502#ifdef Py_USING_UNICODE
1503/* Replace Unicode objects in slots. */
1504
1505static PyObject *
1506_unicode_to_string(PyObject *slots, int nslots)
1507{
1508 PyObject *tmp = slots;
1509 PyObject *o, *o1;
1510 int i;
1511 intintargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
1512 for (i = 0; i < nslots; i++) {
1513 if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
1514 if (tmp == slots) {
1515 tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
1516 if (tmp == NULL)
1517 return NULL;
1518 }
1519 o1 = _PyUnicode_AsDefaultEncodedString
1520 (o, NULL);
1521 if (o1 == NULL) {
1522 Py_DECREF(tmp);
1523 return 0;
1524 }
1525 Py_INCREF(o1);
1526 Py_DECREF(o);
1527 PyTuple_SET_ITEM(tmp, i, o1);
1528 }
1529 }
1530 return tmp;
1531}
1532#endif
1533
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001534static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001535type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1536{
1537 PyObject *name, *bases, *dict;
1538 static char *kwlist[] = {"name", "bases", "dict", 0};
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001539 PyObject *slots, *tmp, *newslots;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001540 PyTypeObject *type, *base, *tmptype, *winner;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001541 PyHeapTypeObject *et;
Guido van Rossum6f799372001-09-20 20:46:19 +00001542 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001543 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Guido van Rossumad47da02002-08-12 19:05:44 +00001544 int j, may_add_dict, may_add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001545
Tim Peters3abca122001-10-27 19:37:48 +00001546 assert(args != NULL && PyTuple_Check(args));
1547 assert(kwds == NULL || PyDict_Check(kwds));
1548
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001549 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +00001550 {
1551 const int nargs = PyTuple_GET_SIZE(args);
1552 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1553
1554 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1555 PyObject *x = PyTuple_GET_ITEM(args, 0);
1556 Py_INCREF(x->ob_type);
1557 return (PyObject *) x->ob_type;
1558 }
1559
1560 /* SF bug 475327 -- if that didn't trigger, we need 3
1561 arguments. but PyArg_ParseTupleAndKeywords below may give
1562 a msg saying type() needs exactly 3. */
1563 if (nargs + nkwds != 3) {
1564 PyErr_SetString(PyExc_TypeError,
1565 "type() takes 1 or 3 arguments");
1566 return NULL;
1567 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001568 }
1569
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001570 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001571 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1572 &name,
1573 &PyTuple_Type, &bases,
1574 &PyDict_Type, &dict))
1575 return NULL;
1576
1577 /* Determine the proper metatype to deal with this,
1578 and check for metatype conflicts while we're at it.
1579 Note that if some other metatype wins to contract,
1580 it's possible that its instances are not types. */
1581 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001582 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001583 for (i = 0; i < nbases; i++) {
1584 tmp = PyTuple_GET_ITEM(bases, i);
1585 tmptype = tmp->ob_type;
Tim Petersa91e9642001-11-14 23:32:33 +00001586 if (tmptype == &PyClass_Type)
1587 continue; /* Special case classic classes */
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001588 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001589 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001590 if (PyType_IsSubtype(tmptype, winner)) {
1591 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001592 continue;
1593 }
1594 PyErr_SetString(PyExc_TypeError,
Guido van Rossum636688d2003-04-23 12:07:22 +00001595 "metaclass conflict: "
1596 "the metaclass of a derived class "
1597 "must be a (non-strict) subclass "
1598 "of the metaclasses of all its bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001599 return NULL;
1600 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001601 if (winner != metatype) {
1602 if (winner->tp_new != type_new) /* Pass it to the winner */
1603 return winner->tp_new(winner, args, kwds);
1604 metatype = winner;
1605 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001606
1607 /* Adjust for empty tuple bases */
1608 if (nbases == 0) {
1609 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
1610 if (bases == NULL)
1611 return NULL;
1612 nbases = 1;
1613 }
1614 else
1615 Py_INCREF(bases);
1616
1617 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1618
1619 /* Calculate best base, and check that all bases are type objects */
1620 base = best_base(bases);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001621 if (base == NULL) {
1622 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001623 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001624 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001625 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1626 PyErr_Format(PyExc_TypeError,
1627 "type '%.100s' is not an acceptable base type",
1628 base->tp_name);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001629 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001630 return NULL;
1631 }
1632
Tim Peters6d6c1a32001-08-02 04:15:00 +00001633 /* Check for a __slots__ sequence variable in dict, and count it */
1634 slots = PyDict_GetItemString(dict, "__slots__");
1635 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001636 add_dict = 0;
1637 add_weak = 0;
Guido van Rossumad47da02002-08-12 19:05:44 +00001638 may_add_dict = base->tp_dictoffset == 0;
1639 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1640 if (slots == NULL) {
1641 if (may_add_dict) {
1642 add_dict++;
1643 }
1644 if (may_add_weak) {
1645 add_weak++;
1646 }
1647 }
1648 else {
1649 /* Have slots */
1650
Tim Peters6d6c1a32001-08-02 04:15:00 +00001651 /* Make it into a tuple */
1652 if (PyString_Check(slots))
1653 slots = Py_BuildValue("(O)", slots);
1654 else
1655 slots = PySequence_Tuple(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001656 if (slots == NULL) {
1657 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001658 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001659 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001660 assert(PyTuple_Check(slots));
1661
1662 /* Are slots allowed? */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001663 nslots = PyTuple_GET_SIZE(slots);
Jeremy Hylton1c7a0ea2003-07-16 16:08:23 +00001664 if (nslots > 0 && base->tp_itemsize != 0) {
Guido van Rossumc4141872001-08-30 04:43:35 +00001665 PyErr_Format(PyExc_TypeError,
1666 "nonempty __slots__ "
1667 "not supported for subtype of '%s'",
1668 base->tp_name);
Guido van Rossumad47da02002-08-12 19:05:44 +00001669 bad_slots:
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001670 Py_DECREF(bases);
Guido van Rossumad47da02002-08-12 19:05:44 +00001671 Py_DECREF(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00001672 return NULL;
1673 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001674
Martin v. Löwisd919a592002-10-14 21:07:28 +00001675#ifdef Py_USING_UNICODE
1676 tmp = _unicode_to_string(slots, nslots);
Martin v. Löwis13b1a5c2002-10-14 21:11:34 +00001677 if (tmp != slots) {
1678 Py_DECREF(slots);
1679 slots = tmp;
1680 }
Martin v. Löwisd919a592002-10-14 21:07:28 +00001681 if (!tmp)
1682 return NULL;
1683#endif
Guido van Rossumad47da02002-08-12 19:05:44 +00001684 /* Check for valid slot names and two special cases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001685 for (i = 0; i < nslots; i++) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001686 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1687 char *s;
1688 if (!valid_identifier(tmp))
1689 goto bad_slots;
1690 assert(PyString_Check(tmp));
1691 s = PyString_AS_STRING(tmp);
1692 if (strcmp(s, "__dict__") == 0) {
1693 if (!may_add_dict || add_dict) {
1694 PyErr_SetString(PyExc_TypeError,
1695 "__dict__ slot disallowed: "
1696 "we already got one");
1697 goto bad_slots;
1698 }
1699 add_dict++;
1700 }
1701 if (strcmp(s, "__weakref__") == 0) {
1702 if (!may_add_weak || add_weak) {
1703 PyErr_SetString(PyExc_TypeError,
1704 "__weakref__ slot disallowed: "
1705 "either we already got one, "
1706 "or __itemsize__ != 0");
1707 goto bad_slots;
1708 }
1709 add_weak++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001710 }
1711 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001712
Guido van Rossumad47da02002-08-12 19:05:44 +00001713 /* Copy slots into yet another tuple, demangling names */
1714 newslots = PyTuple_New(nslots - add_dict - add_weak);
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001715 if (newslots == NULL)
Guido van Rossumad47da02002-08-12 19:05:44 +00001716 goto bad_slots;
1717 for (i = j = 0; i < nslots; i++) {
1718 char *s;
Guido van Rossum8e829202002-08-16 03:47:49 +00001719 char buffer[256];
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001720 tmp = PyTuple_GET_ITEM(slots, i);
Guido van Rossumad47da02002-08-12 19:05:44 +00001721 s = PyString_AS_STRING(tmp);
1722 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1723 (add_weak && strcmp(s, "__weakref__") == 0))
1724 continue;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001725 if (_Py_Mangle(PyString_AS_STRING(name),
Guido van Rossumad47da02002-08-12 19:05:44 +00001726 PyString_AS_STRING(tmp),
1727 buffer, sizeof(buffer)))
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001728 {
1729 tmp = PyString_FromString(buffer);
1730 } else {
1731 Py_INCREF(tmp);
1732 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001733 PyTuple_SET_ITEM(newslots, j, tmp);
1734 j++;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001735 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001736 assert(j == nslots - add_dict - add_weak);
1737 nslots = j;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001738 Py_DECREF(slots);
1739 slots = newslots;
1740
Guido van Rossumad47da02002-08-12 19:05:44 +00001741 /* Secondary bases may provide weakrefs or dict */
1742 if (nbases > 1 &&
1743 ((may_add_dict && !add_dict) ||
1744 (may_add_weak && !add_weak))) {
1745 for (i = 0; i < nbases; i++) {
1746 tmp = PyTuple_GET_ITEM(bases, i);
1747 if (tmp == (PyObject *)base)
1748 continue; /* Skip primary base */
1749 if (PyClass_Check(tmp)) {
1750 /* Classic base class provides both */
1751 if (may_add_dict && !add_dict)
1752 add_dict++;
1753 if (may_add_weak && !add_weak)
1754 add_weak++;
1755 break;
1756 }
1757 assert(PyType_Check(tmp));
1758 tmptype = (PyTypeObject *)tmp;
1759 if (may_add_dict && !add_dict &&
1760 tmptype->tp_dictoffset != 0)
1761 add_dict++;
1762 if (may_add_weak && !add_weak &&
1763 tmptype->tp_weaklistoffset != 0)
1764 add_weak++;
1765 if (may_add_dict && !add_dict)
1766 continue;
1767 if (may_add_weak && !add_weak)
1768 continue;
1769 /* Nothing more to check */
1770 break;
1771 }
1772 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001773 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001774
1775 /* XXX From here until type is safely allocated,
1776 "return NULL" may leak slots! */
1777
1778 /* Allocate the type object */
1779 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
Guido van Rossumad47da02002-08-12 19:05:44 +00001780 if (type == NULL) {
1781 Py_XDECREF(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001782 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001783 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00001784 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001785
1786 /* Keep name and slots alive in the extended type object */
Guido van Rossume5c691a2003-03-07 15:13:17 +00001787 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001788 Py_INCREF(name);
1789 et->name = name;
1790 et->slots = slots;
1791
Guido van Rossumdc91b992001-08-08 22:26:22 +00001792 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001793 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1794 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001795 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1796 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001797
1798 /* It's a new-style number unless it specifically inherits any
1799 old-style numeric behavior */
1800 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1801 (base->tp_as_number == NULL))
1802 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1803
1804 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001805 type->tp_as_number = &et->as_number;
1806 type->tp_as_sequence = &et->as_sequence;
1807 type->tp_as_mapping = &et->as_mapping;
1808 type->tp_as_buffer = &et->as_buffer;
1809 type->tp_name = PyString_AS_STRING(name);
1810
1811 /* Set tp_base and tp_bases */
1812 type->tp_bases = bases;
1813 Py_INCREF(base);
1814 type->tp_base = base;
1815
Guido van Rossum687ae002001-10-15 22:03:32 +00001816 /* Initialize tp_dict from passed-in dict */
1817 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001818 if (dict == NULL) {
1819 Py_DECREF(type);
1820 return NULL;
1821 }
1822
Guido van Rossumc3542212001-08-16 09:18:56 +00001823 /* Set __module__ in the dict */
1824 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1825 tmp = PyEval_GetGlobals();
1826 if (tmp != NULL) {
1827 tmp = PyDict_GetItemString(tmp, "__name__");
1828 if (tmp != NULL) {
1829 if (PyDict_SetItemString(dict, "__module__",
1830 tmp) < 0)
1831 return NULL;
1832 }
1833 }
1834 }
1835
Tim Peters2f93e282001-10-04 05:27:00 +00001836 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00001837 and is a string. The __doc__ accessor will first look for tp_doc;
1838 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00001839 */
1840 {
1841 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1842 if (doc != NULL && PyString_Check(doc)) {
1843 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +00001844 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +00001845 if (type->tp_doc == NULL) {
1846 Py_DECREF(type);
1847 return NULL;
1848 }
1849 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
1850 }
1851 }
1852
Tim Peters6d6c1a32001-08-02 04:15:00 +00001853 /* Special-case __new__: if it's a plain function,
1854 make it a static function */
1855 tmp = PyDict_GetItemString(dict, "__new__");
1856 if (tmp != NULL && PyFunction_Check(tmp)) {
1857 tmp = PyStaticMethod_New(tmp);
1858 if (tmp == NULL) {
1859 Py_DECREF(type);
1860 return NULL;
1861 }
1862 PyDict_SetItemString(dict, "__new__", tmp);
1863 Py_DECREF(tmp);
1864 }
1865
1866 /* Add descriptors for custom slots from __slots__, or for __dict__ */
Guido van Rossume5c691a2003-03-07 15:13:17 +00001867 mp = PyHeapType_GET_MEMBERS(et);
Neil Schemenauerc806c882001-08-29 23:54:54 +00001868 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001869 if (slots != NULL) {
1870 for (i = 0; i < nslots; i++, mp++) {
1871 mp->name = PyString_AS_STRING(
1872 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001873 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001874 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001875 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001876 strcmp(mp->name, "__weakref__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001877 add_weak++;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001878 mp->type = T_OBJECT;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001879 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001880 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001881 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001882 slotoffset += sizeof(PyObject *);
1883 }
1884 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001885 if (add_dict) {
1886 if (base->tp_itemsize)
1887 type->tp_dictoffset = -(long)sizeof(PyObject *);
1888 else
1889 type->tp_dictoffset = slotoffset;
1890 slotoffset += sizeof(PyObject *);
1891 }
1892 if (add_weak) {
1893 assert(!base->tp_itemsize);
1894 type->tp_weaklistoffset = slotoffset;
1895 slotoffset += sizeof(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001896 }
1897 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001898 type->tp_itemsize = base->tp_itemsize;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001899 type->tp_members = PyHeapType_GET_MEMBERS(et);
Guido van Rossum373c7412003-01-07 13:41:37 +00001900
1901 if (type->tp_weaklistoffset && type->tp_dictoffset)
1902 type->tp_getset = subtype_getsets_full;
1903 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1904 type->tp_getset = subtype_getsets_weakref_only;
1905 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1906 type->tp_getset = subtype_getsets_dict_only;
1907 else
1908 type->tp_getset = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001909
1910 /* Special case some slots */
1911 if (type->tp_dictoffset != 0 || nslots > 0) {
1912 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1913 type->tp_getattro = PyObject_GenericGetAttr;
1914 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1915 type->tp_setattro = PyObject_GenericSetAttr;
1916 }
1917 type->tp_dealloc = subtype_dealloc;
1918
Guido van Rossum9475a232001-10-05 20:51:39 +00001919 /* Enable GC unless there are really no instance variables possible */
1920 if (!(type->tp_basicsize == sizeof(PyObject) &&
1921 type->tp_itemsize == 0))
1922 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1923
Tim Peters6d6c1a32001-08-02 04:15:00 +00001924 /* Always override allocation strategy to use regular heap */
1925 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001926 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001927 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001928 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001929 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00001930 }
1931 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001932 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001933
1934 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001935 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001936 Py_DECREF(type);
1937 return NULL;
1938 }
1939
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001940 /* Put the proper slots in place */
1941 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001942
Tim Peters6d6c1a32001-08-02 04:15:00 +00001943 return (PyObject *)type;
1944}
1945
1946/* Internal API to look for a name through the MRO.
1947 This returns a borrowed reference, and doesn't set an exception! */
1948PyObject *
1949_PyType_Lookup(PyTypeObject *type, PyObject *name)
1950{
1951 int i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001952 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001953
Guido van Rossum687ae002001-10-15 22:03:32 +00001954 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001955 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00001956
1957 /* If mro is NULL, the type is either not yet initialized
1958 by PyType_Ready(), or already cleared by type_clear().
1959 Either way the safest thing to do is to return NULL. */
1960 if (mro == NULL)
1961 return NULL;
1962
Tim Peters6d6c1a32001-08-02 04:15:00 +00001963 assert(PyTuple_Check(mro));
1964 n = PyTuple_GET_SIZE(mro);
1965 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001966 base = PyTuple_GET_ITEM(mro, i);
1967 if (PyClass_Check(base))
1968 dict = ((PyClassObject *)base)->cl_dict;
1969 else {
1970 assert(PyType_Check(base));
1971 dict = ((PyTypeObject *)base)->tp_dict;
1972 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001973 assert(dict && PyDict_Check(dict));
1974 res = PyDict_GetItem(dict, name);
1975 if (res != NULL)
1976 return res;
1977 }
1978 return NULL;
1979}
1980
1981/* This is similar to PyObject_GenericGetAttr(),
1982 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1983static PyObject *
1984type_getattro(PyTypeObject *type, PyObject *name)
1985{
1986 PyTypeObject *metatype = type->ob_type;
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001987 PyObject *meta_attribute, *attribute;
1988 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001989
1990 /* Initialize this type (we'll assume the metatype is initialized) */
1991 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001992 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001993 return NULL;
1994 }
1995
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001996 /* No readable descriptor found yet */
1997 meta_get = NULL;
Tim Peters34592512002-07-11 06:23:50 +00001998
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001999 /* Look for the attribute in the metatype */
2000 meta_attribute = _PyType_Lookup(metatype, name);
2001
2002 if (meta_attribute != NULL) {
2003 meta_get = meta_attribute->ob_type->tp_descr_get;
Tim Peters34592512002-07-11 06:23:50 +00002004
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002005 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2006 /* Data descriptors implement tp_descr_set to intercept
2007 * writes. Assume the attribute is not overridden in
2008 * type's tp_dict (and bases): call the descriptor now.
2009 */
2010 return meta_get(meta_attribute, (PyObject *)type,
2011 (PyObject *)metatype);
2012 }
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002013 Py_INCREF(meta_attribute);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002014 }
2015
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002016 /* No data descriptor found on metatype. Look in tp_dict of this
2017 * type and its bases */
2018 attribute = _PyType_Lookup(type, name);
2019 if (attribute != NULL) {
2020 /* Implement descriptor functionality, if any */
2021 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002022
2023 Py_XDECREF(meta_attribute);
2024
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002025 if (local_get != NULL) {
2026 /* NULL 2nd argument indicates the descriptor was
2027 * found on the target object itself (or a base) */
2028 return local_get(attribute, (PyObject *)NULL,
2029 (PyObject *)type);
2030 }
Tim Peters34592512002-07-11 06:23:50 +00002031
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002032 Py_INCREF(attribute);
2033 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002034 }
2035
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002036 /* No attribute found in local __dict__ (or bases): use the
2037 * descriptor from the metatype, if any */
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002038 if (meta_get != NULL) {
2039 PyObject *res;
2040 res = meta_get(meta_attribute, (PyObject *)type,
2041 (PyObject *)metatype);
2042 Py_DECREF(meta_attribute);
2043 return res;
2044 }
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002045
2046 /* If an ordinary attribute was found on the metatype, return it now */
2047 if (meta_attribute != NULL) {
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002048 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002049 }
2050
2051 /* Give up */
2052 PyErr_Format(PyExc_AttributeError,
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002053 "type object '%.50s' has no attribute '%.400s'",
2054 type->tp_name, PyString_AS_STRING(name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002055 return NULL;
2056}
2057
2058static int
2059type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2060{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002061 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2062 PyErr_Format(
2063 PyExc_TypeError,
2064 "can't set attributes of built-in/extension type '%s'",
2065 type->tp_name);
2066 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002067 }
Guido van Rossum8d24ee92003-03-24 23:49:49 +00002068 /* XXX Example of how I expect this to be used...
2069 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2070 return -1;
2071 */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002072 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2073 return -1;
2074 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002075}
2076
2077static void
2078type_dealloc(PyTypeObject *type)
2079{
Guido van Rossume5c691a2003-03-07 15:13:17 +00002080 PyHeapTypeObject *et;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002081
2082 /* Assert this is a heap-allocated type object */
2083 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002084 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00002085 PyObject_ClearWeakRefs((PyObject *)type);
Guido van Rossume5c691a2003-03-07 15:13:17 +00002086 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002087 Py_XDECREF(type->tp_base);
2088 Py_XDECREF(type->tp_dict);
2089 Py_XDECREF(type->tp_bases);
2090 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00002091 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00002092 Py_XDECREF(type->tp_subclasses);
Neal Norwitzcee5ca02002-07-30 00:42:06 +00002093 PyObject_Free(type->tp_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002094 Py_XDECREF(et->name);
2095 Py_XDECREF(et->slots);
2096 type->ob_type->tp_free((PyObject *)type);
2097}
2098
Guido van Rossum1c450732001-10-08 15:18:27 +00002099static PyObject *
2100type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2101{
2102 PyObject *list, *raw, *ref;
2103 int i, n;
2104
2105 list = PyList_New(0);
2106 if (list == NULL)
2107 return NULL;
2108 raw = type->tp_subclasses;
2109 if (raw == NULL)
2110 return list;
2111 assert(PyList_Check(raw));
2112 n = PyList_GET_SIZE(raw);
2113 for (i = 0; i < n; i++) {
2114 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00002115 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00002116 ref = PyWeakref_GET_OBJECT(ref);
2117 if (ref != Py_None) {
2118 if (PyList_Append(list, ref) < 0) {
2119 Py_DECREF(list);
2120 return NULL;
2121 }
2122 }
2123 }
2124 return list;
2125}
2126
Tim Peters6d6c1a32001-08-02 04:15:00 +00002127static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002128 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002129 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
Guido van Rossum1c450732001-10-08 15:18:27 +00002130 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002131 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002132 {0}
2133};
2134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002135PyDoc_STRVAR(type_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002136"type(object) -> the object's type\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002137"type(name, bases, dict) -> a new type");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002138
Guido van Rossum048eb752001-10-02 21:24:57 +00002139static int
2140type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2141{
Guido van Rossum048eb752001-10-02 21:24:57 +00002142 int err;
2143
Guido van Rossuma3862092002-06-10 15:24:42 +00002144 /* Because of type_is_gc(), the collector only calls this
2145 for heaptypes. */
2146 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002147
2148#define VISIT(SLOT) \
2149 if (SLOT) { \
2150 err = visit((PyObject *)(SLOT), arg); \
2151 if (err) \
2152 return err; \
2153 }
2154
2155 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00002156 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00002157 VISIT(type->tp_mro);
2158 VISIT(type->tp_bases);
2159 VISIT(type->tp_base);
Guido van Rossuma3862092002-06-10 15:24:42 +00002160
2161 /* There's no need to visit type->tp_subclasses or
Guido van Rossume5c691a2003-03-07 15:13:17 +00002162 ((PyHeapTypeObject *)type)->slots, because they can't be involved
Guido van Rossuma3862092002-06-10 15:24:42 +00002163 in cycles; tp_subclasses is a list of weak references,
2164 and slots is a tuple of strings. */
Guido van Rossum048eb752001-10-02 21:24:57 +00002165
2166#undef VISIT
2167
2168 return 0;
2169}
2170
2171static int
2172type_clear(PyTypeObject *type)
2173{
Guido van Rossum048eb752001-10-02 21:24:57 +00002174 PyObject *tmp;
2175
Guido van Rossuma3862092002-06-10 15:24:42 +00002176 /* Because of type_is_gc(), the collector only calls this
2177 for heaptypes. */
2178 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002179
2180#define CLEAR(SLOT) \
2181 if (SLOT) { \
2182 tmp = (PyObject *)(SLOT); \
2183 SLOT = NULL; \
2184 Py_DECREF(tmp); \
2185 }
2186
Guido van Rossuma3862092002-06-10 15:24:42 +00002187 /* The only field we need to clear is tp_mro, which is part of a
2188 hard cycle (its first element is the class itself) that won't
2189 be broken otherwise (it's a tuple and tuples don't have a
2190 tp_clear handler). None of the other fields need to be
2191 cleared, and here's why:
Guido van Rossum048eb752001-10-02 21:24:57 +00002192
Guido van Rossuma3862092002-06-10 15:24:42 +00002193 tp_dict:
2194 It is a dict, so the collector will call its tp_clear.
2195
2196 tp_cache:
2197 Not used; if it were, it would be a dict.
2198
2199 tp_bases, tp_base:
2200 If these are involved in a cycle, there must be at least
2201 one other, mutable object in the cycle, e.g. a base
2202 class's dict; the cycle will be broken that way.
2203
2204 tp_subclasses:
2205 A list of weak references can't be part of a cycle; and
2206 lists have their own tp_clear.
2207
Guido van Rossume5c691a2003-03-07 15:13:17 +00002208 slots (in PyHeapTypeObject):
Guido van Rossuma3862092002-06-10 15:24:42 +00002209 A tuple of strings can't be part of a cycle.
2210 */
2211
2212 CLEAR(type->tp_mro);
Tim Peters2f93e282001-10-04 05:27:00 +00002213
Guido van Rossum048eb752001-10-02 21:24:57 +00002214#undef CLEAR
2215
2216 return 0;
2217}
2218
2219static int
2220type_is_gc(PyTypeObject *type)
2221{
2222 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2223}
2224
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002225PyTypeObject PyType_Type = {
2226 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002227 0, /* ob_size */
2228 "type", /* tp_name */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002229 sizeof(PyHeapTypeObject), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00002230 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002231 (destructor)type_dealloc, /* tp_dealloc */
2232 0, /* tp_print */
2233 0, /* tp_getattr */
2234 0, /* tp_setattr */
2235 type_compare, /* tp_compare */
2236 (reprfunc)type_repr, /* tp_repr */
2237 0, /* tp_as_number */
2238 0, /* tp_as_sequence */
2239 0, /* tp_as_mapping */
2240 (hashfunc)_Py_HashPointer, /* tp_hash */
2241 (ternaryfunc)type_call, /* tp_call */
2242 0, /* tp_str */
2243 (getattrofunc)type_getattro, /* tp_getattro */
2244 (setattrofunc)type_setattro, /* tp_setattro */
2245 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00002246 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2247 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002248 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00002249 (traverseproc)type_traverse, /* tp_traverse */
2250 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002251 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00002252 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002253 0, /* tp_iter */
2254 0, /* tp_iternext */
2255 type_methods, /* tp_methods */
2256 type_members, /* tp_members */
2257 type_getsets, /* tp_getset */
2258 0, /* tp_base */
2259 0, /* tp_dict */
2260 0, /* tp_descr_get */
2261 0, /* tp_descr_set */
2262 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2263 0, /* tp_init */
2264 0, /* tp_alloc */
2265 type_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002266 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00002267 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002268};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002269
2270
2271/* The base type of all types (eventually)... except itself. */
2272
2273static int
2274object_init(PyObject *self, PyObject *args, PyObject *kwds)
2275{
2276 return 0;
2277}
2278
Guido van Rossum298e4212003-02-13 16:30:16 +00002279/* If we don't have a tp_new for a new-style class, new will use this one.
2280 Therefore this should take no arguments/keywords. However, this new may
2281 also be inherited by objects that define a tp_init but no tp_new. These
2282 objects WILL pass argumets to tp_new, because it gets the same args as
2283 tp_init. So only allow arguments if we aren't using the default init, in
2284 which case we expect init to handle argument parsing. */
2285static PyObject *
2286object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2287{
2288 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2289 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2290 PyErr_SetString(PyExc_TypeError,
2291 "default __new__ takes no parameters");
2292 return NULL;
2293 }
2294 return type->tp_alloc(type, 0);
2295}
2296
Tim Peters6d6c1a32001-08-02 04:15:00 +00002297static void
2298object_dealloc(PyObject *self)
2299{
2300 self->ob_type->tp_free(self);
2301}
2302
Guido van Rossum8e248182001-08-12 05:17:56 +00002303static PyObject *
2304object_repr(PyObject *self)
2305{
Guido van Rossum76e69632001-08-16 18:52:43 +00002306 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00002307 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002308
Guido van Rossum76e69632001-08-16 18:52:43 +00002309 type = self->ob_type;
2310 mod = type_module(type, NULL);
2311 if (mod == NULL)
2312 PyErr_Clear();
2313 else if (!PyString_Check(mod)) {
2314 Py_DECREF(mod);
2315 mod = NULL;
2316 }
2317 name = type_name(type, NULL);
2318 if (name == NULL)
2319 return NULL;
2320 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00002321 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002322 PyString_AS_STRING(mod),
2323 PyString_AS_STRING(name),
2324 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002325 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00002326 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002327 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002328 Py_XDECREF(mod);
2329 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00002330 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002331}
2332
Guido van Rossumb8f63662001-08-15 23:57:02 +00002333static PyObject *
2334object_str(PyObject *self)
2335{
2336 unaryfunc f;
2337
2338 f = self->ob_type->tp_repr;
2339 if (f == NULL)
2340 f = object_repr;
2341 return f(self);
2342}
2343
Guido van Rossum8e248182001-08-12 05:17:56 +00002344static long
2345object_hash(PyObject *self)
2346{
2347 return _Py_HashPointer(self);
2348}
Guido van Rossum8e248182001-08-12 05:17:56 +00002349
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002350static PyObject *
2351object_get_class(PyObject *self, void *closure)
2352{
2353 Py_INCREF(self->ob_type);
2354 return (PyObject *)(self->ob_type);
2355}
2356
2357static int
2358equiv_structs(PyTypeObject *a, PyTypeObject *b)
2359{
2360 return a == b ||
2361 (a != NULL &&
2362 b != NULL &&
2363 a->tp_basicsize == b->tp_basicsize &&
2364 a->tp_itemsize == b->tp_itemsize &&
2365 a->tp_dictoffset == b->tp_dictoffset &&
2366 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2367 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2368 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2369}
2370
2371static int
2372same_slots_added(PyTypeObject *a, PyTypeObject *b)
2373{
2374 PyTypeObject *base = a->tp_base;
2375 int size;
2376
2377 if (base != b->tp_base)
2378 return 0;
2379 if (equiv_structs(a, base) && equiv_structs(b, base))
2380 return 1;
2381 size = base->tp_basicsize;
2382 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2383 size += sizeof(PyObject *);
2384 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2385 size += sizeof(PyObject *);
2386 return size == a->tp_basicsize && size == b->tp_basicsize;
2387}
2388
2389static int
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002390compatible_for_assignment(PyTypeObject* old, PyTypeObject* new, char* attr)
2391{
2392 PyTypeObject *newbase, *oldbase;
2393
2394 if (new->tp_dealloc != old->tp_dealloc ||
2395 new->tp_free != old->tp_free)
2396 {
2397 PyErr_Format(PyExc_TypeError,
2398 "%s assignment: "
2399 "'%s' deallocator differs from '%s'",
2400 attr,
2401 new->tp_name,
2402 old->tp_name);
2403 return 0;
2404 }
2405 newbase = new;
2406 oldbase = old;
2407 while (equiv_structs(newbase, newbase->tp_base))
2408 newbase = newbase->tp_base;
2409 while (equiv_structs(oldbase, oldbase->tp_base))
2410 oldbase = oldbase->tp_base;
2411 if (newbase != oldbase &&
2412 (newbase->tp_base != oldbase->tp_base ||
2413 !same_slots_added(newbase, oldbase))) {
2414 PyErr_Format(PyExc_TypeError,
2415 "%s assignment: "
2416 "'%s' object layout differs from '%s'",
2417 attr,
2418 new->tp_name,
2419 old->tp_name);
2420 return 0;
2421 }
Tim Petersea7f75d2002-12-07 21:39:16 +00002422
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002423 return 1;
2424}
2425
2426static int
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002427object_set_class(PyObject *self, PyObject *value, void *closure)
2428{
2429 PyTypeObject *old = self->ob_type;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002430 PyTypeObject *new;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002431
Guido van Rossumb6b89422002-04-15 01:03:30 +00002432 if (value == NULL) {
2433 PyErr_SetString(PyExc_TypeError,
2434 "can't delete __class__ attribute");
2435 return -1;
2436 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002437 if (!PyType_Check(value)) {
2438 PyErr_Format(PyExc_TypeError,
2439 "__class__ must be set to new-style class, not '%s' object",
2440 value->ob_type->tp_name);
2441 return -1;
2442 }
2443 new = (PyTypeObject *)value;
Guido van Rossum40af8892002-08-10 05:42:07 +00002444 if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2445 !(old->tp_flags & Py_TPFLAGS_HEAPTYPE))
2446 {
2447 PyErr_Format(PyExc_TypeError,
2448 "__class__ assignment: only for heap types");
2449 return -1;
2450 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002451 if (compatible_for_assignment(new, old, "__class__")) {
2452 Py_INCREF(new);
2453 self->ob_type = new;
2454 Py_DECREF(old);
2455 return 0;
2456 }
2457 else {
Guido van Rossum9ee4b942002-05-24 18:47:47 +00002458 return -1;
2459 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002460}
2461
2462static PyGetSetDef object_getsets[] = {
2463 {"__class__", object_get_class, object_set_class,
Neal Norwitz858e34f2002-08-13 17:18:45 +00002464 PyDoc_STR("the object's class")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002465 {0}
2466};
2467
Guido van Rossumc53f0092003-02-18 22:05:12 +00002468
Guido van Rossum036f9992003-02-21 22:02:54 +00002469/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2470 We fall back to helpers in copy_reg for:
2471 - pickle protocols < 2
2472 - calculating the list of slot names (done only once per class)
2473 - the __newobj__ function (which is used as a token but never called)
2474*/
2475
2476static PyObject *
2477import_copy_reg(void)
2478{
2479 static PyObject *copy_reg_str;
Guido van Rossum3926a632001-09-25 16:25:58 +00002480
2481 if (!copy_reg_str) {
2482 copy_reg_str = PyString_InternFromString("copy_reg");
2483 if (copy_reg_str == NULL)
2484 return NULL;
2485 }
Guido van Rossum036f9992003-02-21 22:02:54 +00002486
2487 return PyImport_Import(copy_reg_str);
2488}
2489
2490static PyObject *
2491slotnames(PyObject *cls)
2492{
2493 PyObject *clsdict;
2494 PyObject *copy_reg;
2495 PyObject *slotnames;
2496
2497 if (!PyType_Check(cls)) {
2498 Py_INCREF(Py_None);
2499 return Py_None;
2500 }
2501
2502 clsdict = ((PyTypeObject *)cls)->tp_dict;
2503 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
2504 if (slotnames != NULL) {
2505 Py_INCREF(slotnames);
2506 return slotnames;
2507 }
2508
2509 copy_reg = import_copy_reg();
2510 if (copy_reg == NULL)
2511 return NULL;
2512
2513 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2514 Py_DECREF(copy_reg);
2515 if (slotnames != NULL &&
2516 slotnames != Py_None &&
2517 !PyList_Check(slotnames))
2518 {
2519 PyErr_SetString(PyExc_TypeError,
2520 "copy_reg._slotnames didn't return a list or None");
2521 Py_DECREF(slotnames);
2522 slotnames = NULL;
2523 }
2524
2525 return slotnames;
2526}
2527
2528static PyObject *
2529reduce_2(PyObject *obj)
2530{
2531 PyObject *cls, *getnewargs;
2532 PyObject *args = NULL, *args2 = NULL;
2533 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2534 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2535 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
2536 int i, n;
2537
2538 cls = PyObject_GetAttrString(obj, "__class__");
2539 if (cls == NULL)
2540 return NULL;
2541
2542 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2543 if (getnewargs != NULL) {
2544 args = PyObject_CallObject(getnewargs, NULL);
2545 Py_DECREF(getnewargs);
2546 if (args != NULL && !PyTuple_Check(args)) {
2547 PyErr_SetString(PyExc_TypeError,
2548 "__getnewargs__ should return a tuple");
2549 goto end;
2550 }
2551 }
2552 else {
2553 PyErr_Clear();
2554 args = PyTuple_New(0);
2555 }
2556 if (args == NULL)
2557 goto end;
2558
2559 getstate = PyObject_GetAttrString(obj, "__getstate__");
2560 if (getstate != NULL) {
2561 state = PyObject_CallObject(getstate, NULL);
2562 Py_DECREF(getstate);
Neal Norwitze2fdc612003-06-08 13:19:58 +00002563 if (state == NULL)
2564 goto end;
Guido van Rossum036f9992003-02-21 22:02:54 +00002565 }
2566 else {
2567 state = PyObject_GetAttrString(obj, "__dict__");
2568 if (state == NULL) {
2569 PyErr_Clear();
2570 state = Py_None;
2571 Py_INCREF(state);
2572 }
2573 names = slotnames(cls);
2574 if (names == NULL)
2575 goto end;
2576 if (names != Py_None) {
2577 assert(PyList_Check(names));
2578 slots = PyDict_New();
2579 if (slots == NULL)
2580 goto end;
2581 n = 0;
2582 /* Can't pre-compute the list size; the list
2583 is stored on the class so accessible to other
2584 threads, which may be run by DECREF */
2585 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2586 PyObject *name, *value;
2587 name = PyList_GET_ITEM(names, i);
2588 value = PyObject_GetAttr(obj, name);
2589 if (value == NULL)
2590 PyErr_Clear();
2591 else {
2592 int err = PyDict_SetItem(slots, name,
2593 value);
2594 Py_DECREF(value);
2595 if (err)
2596 goto end;
2597 n++;
2598 }
2599 }
2600 if (n) {
2601 state = Py_BuildValue("(NO)", state, slots);
2602 if (state == NULL)
2603 goto end;
2604 }
2605 }
2606 }
2607
2608 if (!PyList_Check(obj)) {
2609 listitems = Py_None;
2610 Py_INCREF(listitems);
2611 }
2612 else {
2613 listitems = PyObject_GetIter(obj);
2614 if (listitems == NULL)
2615 goto end;
2616 }
2617
2618 if (!PyDict_Check(obj)) {
2619 dictitems = Py_None;
2620 Py_INCREF(dictitems);
2621 }
2622 else {
2623 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2624 if (dictitems == NULL)
2625 goto end;
2626 }
2627
2628 copy_reg = import_copy_reg();
2629 if (copy_reg == NULL)
2630 goto end;
2631 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2632 if (newobj == NULL)
2633 goto end;
2634
2635 n = PyTuple_GET_SIZE(args);
2636 args2 = PyTuple_New(n+1);
2637 if (args2 == NULL)
2638 goto end;
2639 PyTuple_SET_ITEM(args2, 0, cls);
2640 cls = NULL;
2641 for (i = 0; i < n; i++) {
2642 PyObject *v = PyTuple_GET_ITEM(args, i);
2643 Py_INCREF(v);
2644 PyTuple_SET_ITEM(args2, i+1, v);
2645 }
2646
2647 res = Py_BuildValue("(OOOOO)",
2648 newobj, args2, state, listitems, dictitems);
2649
2650 end:
2651 Py_XDECREF(cls);
2652 Py_XDECREF(args);
2653 Py_XDECREF(args2);
Jeremy Hyltond06483c2003-04-09 21:01:42 +00002654 Py_XDECREF(slots);
Guido van Rossum036f9992003-02-21 22:02:54 +00002655 Py_XDECREF(state);
2656 Py_XDECREF(names);
2657 Py_XDECREF(listitems);
2658 Py_XDECREF(dictitems);
2659 Py_XDECREF(copy_reg);
2660 Py_XDECREF(newobj);
2661 return res;
2662}
2663
2664static PyObject *
2665object_reduce_ex(PyObject *self, PyObject *args)
2666{
2667 /* Call copy_reg._reduce_ex(self, proto) */
2668 PyObject *reduce, *copy_reg, *res;
2669 int proto = 0;
2670
2671 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2672 return NULL;
2673
2674 reduce = PyObject_GetAttrString(self, "__reduce__");
2675 if (reduce == NULL)
2676 PyErr_Clear();
2677 else {
2678 PyObject *cls, *clsreduce, *objreduce;
2679 int override;
2680 cls = PyObject_GetAttrString(self, "__class__");
2681 if (cls == NULL) {
2682 Py_DECREF(reduce);
2683 return NULL;
2684 }
2685 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2686 Py_DECREF(cls);
2687 if (clsreduce == NULL) {
2688 Py_DECREF(reduce);
2689 return NULL;
2690 }
2691 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2692 "__reduce__");
2693 override = (clsreduce != objreduce);
2694 Py_DECREF(clsreduce);
2695 if (override) {
2696 res = PyObject_CallObject(reduce, NULL);
2697 Py_DECREF(reduce);
2698 return res;
2699 }
2700 else
2701 Py_DECREF(reduce);
2702 }
2703
2704 if (proto >= 2)
2705 return reduce_2(self);
2706
2707 copy_reg = import_copy_reg();
Guido van Rossum3926a632001-09-25 16:25:58 +00002708 if (!copy_reg)
2709 return NULL;
Guido van Rossum036f9992003-02-21 22:02:54 +00002710
Guido van Rossumc53f0092003-02-18 22:05:12 +00002711 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
Guido van Rossum3926a632001-09-25 16:25:58 +00002712 Py_DECREF(copy_reg);
Guido van Rossum036f9992003-02-21 22:02:54 +00002713
Guido van Rossum3926a632001-09-25 16:25:58 +00002714 return res;
2715}
2716
2717static PyMethodDef object_methods[] = {
Guido van Rossumc53f0092003-02-18 22:05:12 +00002718 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2719 PyDoc_STR("helper for pickle")},
2720 {"__reduce__", object_reduce_ex, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002721 PyDoc_STR("helper for pickle")},
Guido van Rossum3926a632001-09-25 16:25:58 +00002722 {0}
2723};
2724
Guido van Rossum036f9992003-02-21 22:02:54 +00002725
Tim Peters6d6c1a32001-08-02 04:15:00 +00002726PyTypeObject PyBaseObject_Type = {
2727 PyObject_HEAD_INIT(&PyType_Type)
2728 0, /* ob_size */
2729 "object", /* tp_name */
2730 sizeof(PyObject), /* tp_basicsize */
2731 0, /* tp_itemsize */
2732 (destructor)object_dealloc, /* tp_dealloc */
2733 0, /* tp_print */
2734 0, /* tp_getattr */
2735 0, /* tp_setattr */
2736 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002737 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002738 0, /* tp_as_number */
2739 0, /* tp_as_sequence */
2740 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002741 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002742 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002743 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002744 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00002745 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002746 0, /* tp_as_buffer */
2747 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002748 PyDoc_STR("The most base type"), /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002749 0, /* tp_traverse */
2750 0, /* tp_clear */
2751 0, /* tp_richcompare */
2752 0, /* tp_weaklistoffset */
2753 0, /* tp_iter */
2754 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00002755 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002756 0, /* tp_members */
2757 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002758 0, /* tp_base */
2759 0, /* tp_dict */
2760 0, /* tp_descr_get */
2761 0, /* tp_descr_set */
2762 0, /* tp_dictoffset */
2763 object_init, /* tp_init */
2764 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossum298e4212003-02-13 16:30:16 +00002765 object_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002766 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002767};
2768
2769
2770/* Initialize the __dict__ in a type object */
2771
2772static int
2773add_methods(PyTypeObject *type, PyMethodDef *meth)
2774{
Guido van Rossum687ae002001-10-15 22:03:32 +00002775 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002776
2777 for (; meth->ml_name != NULL; meth++) {
2778 PyObject *descr;
2779 if (PyDict_GetItemString(dict, meth->ml_name))
2780 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00002781 if (meth->ml_flags & METH_CLASS) {
2782 if (meth->ml_flags & METH_STATIC) {
2783 PyErr_SetString(PyExc_ValueError,
2784 "method cannot be both class and static");
2785 return -1;
2786 }
Tim Petersbca1cbc2002-12-09 22:56:13 +00002787 descr = PyDescr_NewClassMethod(type, meth);
Fred Drake7bf97152002-03-28 05:33:33 +00002788 }
2789 else if (meth->ml_flags & METH_STATIC) {
Guido van Rossum9af48ff2003-02-11 17:12:46 +00002790 PyObject *cfunc = PyCFunction_New(meth, NULL);
2791 if (cfunc == NULL)
2792 return -1;
2793 descr = PyStaticMethod_New(cfunc);
2794 Py_DECREF(cfunc);
Fred Drake7bf97152002-03-28 05:33:33 +00002795 }
2796 else {
2797 descr = PyDescr_NewMethod(type, meth);
2798 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002799 if (descr == NULL)
2800 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00002801 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002802 return -1;
2803 Py_DECREF(descr);
2804 }
2805 return 0;
2806}
2807
2808static int
Guido van Rossum6f799372001-09-20 20:46:19 +00002809add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002810{
Guido van Rossum687ae002001-10-15 22:03:32 +00002811 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002812
2813 for (; memb->name != NULL; memb++) {
2814 PyObject *descr;
2815 if (PyDict_GetItemString(dict, memb->name))
2816 continue;
2817 descr = PyDescr_NewMember(type, memb);
2818 if (descr == NULL)
2819 return -1;
2820 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2821 return -1;
2822 Py_DECREF(descr);
2823 }
2824 return 0;
2825}
2826
2827static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00002828add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002829{
Guido van Rossum687ae002001-10-15 22:03:32 +00002830 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002831
2832 for (; gsp->name != NULL; gsp++) {
2833 PyObject *descr;
2834 if (PyDict_GetItemString(dict, gsp->name))
2835 continue;
2836 descr = PyDescr_NewGetSet(type, gsp);
2837
2838 if (descr == NULL)
2839 return -1;
2840 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2841 return -1;
2842 Py_DECREF(descr);
2843 }
2844 return 0;
2845}
2846
Guido van Rossum13d52f02001-08-10 21:24:08 +00002847static void
2848inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002849{
2850 int oldsize, newsize;
2851
Guido van Rossum13d52f02001-08-10 21:24:08 +00002852 /* Special flag magic */
2853 if (!type->tp_as_buffer && base->tp_as_buffer) {
2854 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
2855 type->tp_flags |=
2856 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
2857 }
2858 if (!type->tp_as_sequence && base->tp_as_sequence) {
2859 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
2860 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
2861 }
2862 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
2863 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
2864 if ((!type->tp_as_number && base->tp_as_number) ||
2865 (!type->tp_as_sequence && base->tp_as_sequence)) {
2866 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
2867 if (!type->tp_as_number && !type->tp_as_sequence) {
2868 type->tp_flags |= base->tp_flags &
2869 Py_TPFLAGS_HAVE_INPLACEOPS;
2870 }
2871 }
2872 /* Wow */
2873 }
2874 if (!type->tp_as_number && base->tp_as_number) {
2875 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
2876 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
2877 }
2878
2879 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00002880 oldsize = base->tp_basicsize;
2881 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2882 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2883 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00002884 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
2885 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00002886 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00002887 if (type->tp_traverse == NULL)
2888 type->tp_traverse = base->tp_traverse;
2889 if (type->tp_clear == NULL)
2890 type->tp_clear = base->tp_clear;
2891 }
2892 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00002893 /* The condition below could use some explanation.
2894 It appears that tp_new is not inherited for static types
2895 whose base class is 'object'; this seems to be a precaution
2896 so that old extension types don't suddenly become
2897 callable (object.__new__ wouldn't insure the invariants
2898 that the extension type's own factory function ensures).
2899 Heap types, of course, are under our control, so they do
2900 inherit tp_new; static extension types that specify some
2901 other built-in type as the default are considered
2902 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00002903 if (base != &PyBaseObject_Type ||
2904 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2905 if (type->tp_new == NULL)
2906 type->tp_new = base->tp_new;
2907 }
2908 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00002909 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00002910
2911 /* Copy other non-function slots */
2912
2913#undef COPYVAL
2914#define COPYVAL(SLOT) \
2915 if (type->SLOT == 0) type->SLOT = base->SLOT
2916
2917 COPYVAL(tp_itemsize);
2918 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
2919 COPYVAL(tp_weaklistoffset);
2920 }
2921 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2922 COPYVAL(tp_dictoffset);
2923 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00002924}
2925
2926static void
2927inherit_slots(PyTypeObject *type, PyTypeObject *base)
2928{
2929 PyTypeObject *basebase;
2930
2931#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00002932#undef COPYSLOT
2933#undef COPYNUM
2934#undef COPYSEQ
2935#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00002936#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00002937
2938#define SLOTDEFINED(SLOT) \
2939 (base->SLOT != 0 && \
2940 (basebase == NULL || base->SLOT != basebase->SLOT))
2941
Tim Peters6d6c1a32001-08-02 04:15:00 +00002942#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00002943 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00002944
2945#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
2946#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
2947#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00002948#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002949
Guido van Rossum13d52f02001-08-10 21:24:08 +00002950 /* This won't inherit indirect slots (from tp_as_number etc.)
2951 if type doesn't provide the space. */
2952
2953 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
2954 basebase = base->tp_base;
2955 if (basebase->tp_as_number == NULL)
2956 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002957 COPYNUM(nb_add);
2958 COPYNUM(nb_subtract);
2959 COPYNUM(nb_multiply);
2960 COPYNUM(nb_divide);
2961 COPYNUM(nb_remainder);
2962 COPYNUM(nb_divmod);
2963 COPYNUM(nb_power);
2964 COPYNUM(nb_negative);
2965 COPYNUM(nb_positive);
2966 COPYNUM(nb_absolute);
2967 COPYNUM(nb_nonzero);
2968 COPYNUM(nb_invert);
2969 COPYNUM(nb_lshift);
2970 COPYNUM(nb_rshift);
2971 COPYNUM(nb_and);
2972 COPYNUM(nb_xor);
2973 COPYNUM(nb_or);
2974 COPYNUM(nb_coerce);
2975 COPYNUM(nb_int);
2976 COPYNUM(nb_long);
2977 COPYNUM(nb_float);
2978 COPYNUM(nb_oct);
2979 COPYNUM(nb_hex);
2980 COPYNUM(nb_inplace_add);
2981 COPYNUM(nb_inplace_subtract);
2982 COPYNUM(nb_inplace_multiply);
2983 COPYNUM(nb_inplace_divide);
2984 COPYNUM(nb_inplace_remainder);
2985 COPYNUM(nb_inplace_power);
2986 COPYNUM(nb_inplace_lshift);
2987 COPYNUM(nb_inplace_rshift);
2988 COPYNUM(nb_inplace_and);
2989 COPYNUM(nb_inplace_xor);
2990 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002991 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
2992 COPYNUM(nb_true_divide);
2993 COPYNUM(nb_floor_divide);
2994 COPYNUM(nb_inplace_true_divide);
2995 COPYNUM(nb_inplace_floor_divide);
2996 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002997 }
2998
Guido van Rossum13d52f02001-08-10 21:24:08 +00002999 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3000 basebase = base->tp_base;
3001 if (basebase->tp_as_sequence == NULL)
3002 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003003 COPYSEQ(sq_length);
3004 COPYSEQ(sq_concat);
3005 COPYSEQ(sq_repeat);
3006 COPYSEQ(sq_item);
3007 COPYSEQ(sq_slice);
3008 COPYSEQ(sq_ass_item);
3009 COPYSEQ(sq_ass_slice);
3010 COPYSEQ(sq_contains);
3011 COPYSEQ(sq_inplace_concat);
3012 COPYSEQ(sq_inplace_repeat);
3013 }
3014
Guido van Rossum13d52f02001-08-10 21:24:08 +00003015 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3016 basebase = base->tp_base;
3017 if (basebase->tp_as_mapping == NULL)
3018 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003019 COPYMAP(mp_length);
3020 COPYMAP(mp_subscript);
3021 COPYMAP(mp_ass_subscript);
3022 }
3023
Tim Petersfc57ccb2001-10-12 02:38:24 +00003024 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3025 basebase = base->tp_base;
3026 if (basebase->tp_as_buffer == NULL)
3027 basebase = NULL;
3028 COPYBUF(bf_getreadbuffer);
3029 COPYBUF(bf_getwritebuffer);
3030 COPYBUF(bf_getsegcount);
3031 COPYBUF(bf_getcharbuffer);
3032 }
3033
Guido van Rossum13d52f02001-08-10 21:24:08 +00003034 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003035
Tim Peters6d6c1a32001-08-02 04:15:00 +00003036 COPYSLOT(tp_dealloc);
3037 COPYSLOT(tp_print);
3038 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3039 type->tp_getattr = base->tp_getattr;
3040 type->tp_getattro = base->tp_getattro;
3041 }
3042 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3043 type->tp_setattr = base->tp_setattr;
3044 type->tp_setattro = base->tp_setattro;
3045 }
3046 /* tp_compare see tp_richcompare */
3047 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003048 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003049 COPYSLOT(tp_call);
3050 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003051 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003052 if (type->tp_compare == NULL &&
3053 type->tp_richcompare == NULL &&
3054 type->tp_hash == NULL)
3055 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003056 type->tp_compare = base->tp_compare;
3057 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003058 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003059 }
3060 }
3061 else {
3062 COPYSLOT(tp_compare);
3063 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003064 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3065 COPYSLOT(tp_iter);
3066 COPYSLOT(tp_iternext);
3067 }
3068 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3069 COPYSLOT(tp_descr_get);
3070 COPYSLOT(tp_descr_set);
3071 COPYSLOT(tp_dictoffset);
3072 COPYSLOT(tp_init);
3073 COPYSLOT(tp_alloc);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00003074 COPYSLOT(tp_is_gc);
Tim Peters3cfe7542003-05-21 21:29:48 +00003075 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3076 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3077 /* They agree about gc. */
3078 COPYSLOT(tp_free);
3079 }
3080 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3081 type->tp_free == NULL &&
3082 base->tp_free == _PyObject_Del) {
3083 /* A bit of magic to plug in the correct default
3084 * tp_free function when a derived class adds gc,
3085 * didn't define tp_free, and the base uses the
3086 * default non-gc tp_free.
3087 */
3088 type->tp_free = PyObject_GC_Del;
3089 }
3090 /* else they didn't agree about gc, and there isn't something
3091 * obvious to be done -- the type is on its own.
3092 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003093 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003094}
3095
Jeremy Hylton938ace62002-07-17 16:30:39 +00003096static int add_operators(PyTypeObject *);
Guido van Rossum13d52f02001-08-10 21:24:08 +00003097
Tim Peters6d6c1a32001-08-02 04:15:00 +00003098int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003099PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003100{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003101 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003102 PyTypeObject *base;
3103 int i, n;
3104
Guido van Rossumcab05802002-06-10 15:29:03 +00003105 if (type->tp_flags & Py_TPFLAGS_READY) {
3106 assert(type->tp_dict != NULL);
Guido van Rossumd614f972001-08-10 17:39:49 +00003107 return 0;
Guido van Rossumcab05802002-06-10 15:29:03 +00003108 }
Guido van Rossumd614f972001-08-10 17:39:49 +00003109 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00003110
3111 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003112
Tim Peters36eb4df2003-03-23 03:33:13 +00003113#ifdef Py_TRACE_REFS
3114 /* PyType_Ready is the closest thing we have to a choke point
3115 * for type objects, so is the best place I can think of to try
3116 * to get type objects into the doubly-linked list of all objects.
3117 * Still, not all type objects go thru PyType_Ready.
3118 */
Tim Peters7571a0f2003-03-23 17:52:28 +00003119 _Py_AddToAllObjects((PyObject *)type, 0);
Tim Peters36eb4df2003-03-23 03:33:13 +00003120#endif
3121
Tim Peters6d6c1a32001-08-02 04:15:00 +00003122 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3123 base = type->tp_base;
3124 if (base == NULL && type != &PyBaseObject_Type)
3125 base = type->tp_base = &PyBaseObject_Type;
3126
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003127 /* Initialize the base class */
3128 if (base && base->tp_dict == NULL) {
3129 if (PyType_Ready(base) < 0)
3130 goto error;
3131 }
3132
Guido van Rossum0986d822002-04-08 01:38:42 +00003133 /* Initialize ob_type if NULL. This means extensions that want to be
3134 compilable separately on Windows can call PyType_Ready() instead of
3135 initializing the ob_type field of their type objects. */
3136 if (type->ob_type == NULL)
3137 type->ob_type = base->ob_type;
3138
Tim Peters6d6c1a32001-08-02 04:15:00 +00003139 /* Initialize tp_bases */
3140 bases = type->tp_bases;
3141 if (bases == NULL) {
3142 if (base == NULL)
3143 bases = PyTuple_New(0);
3144 else
3145 bases = Py_BuildValue("(O)", base);
3146 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003147 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003148 type->tp_bases = bases;
3149 }
3150
Guido van Rossum687ae002001-10-15 22:03:32 +00003151 /* Initialize tp_dict */
3152 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003153 if (dict == NULL) {
3154 dict = PyDict_New();
3155 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003156 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00003157 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003158 }
3159
Guido van Rossum687ae002001-10-15 22:03:32 +00003160 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003161 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003162 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003163 if (type->tp_methods != NULL) {
3164 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003165 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003166 }
3167 if (type->tp_members != NULL) {
3168 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003169 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003170 }
3171 if (type->tp_getset != NULL) {
3172 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003173 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003174 }
3175
Tim Peters6d6c1a32001-08-02 04:15:00 +00003176 /* Calculate method resolution order */
3177 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00003178 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003179 }
3180
Guido van Rossum13d52f02001-08-10 21:24:08 +00003181 /* Inherit special flags from dominant base */
3182 if (type->tp_base != NULL)
3183 inherit_special(type, type->tp_base);
3184
Tim Peters6d6c1a32001-08-02 04:15:00 +00003185 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003186 bases = type->tp_mro;
3187 assert(bases != NULL);
3188 assert(PyTuple_Check(bases));
3189 n = PyTuple_GET_SIZE(bases);
3190 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003191 PyObject *b = PyTuple_GET_ITEM(bases, i);
3192 if (PyType_Check(b))
3193 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003194 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003195
Tim Peters3cfe7542003-05-21 21:29:48 +00003196 /* Sanity check for tp_free. */
3197 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3198 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3199 /* This base class needs to call tp_free, but doesn't have
3200 * one, or its tp_free is for non-gc'ed objects.
3201 */
3202 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3203 "gc and is a base type but has inappropriate "
3204 "tp_free slot",
3205 type->tp_name);
3206 goto error;
3207 }
3208
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003209 /* if the type dictionary doesn't contain a __doc__, set it from
3210 the tp_doc slot.
3211 */
3212 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3213 if (type->tp_doc != NULL) {
3214 PyObject *doc = PyString_FromString(type->tp_doc);
3215 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3216 Py_DECREF(doc);
3217 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00003218 PyDict_SetItemString(type->tp_dict,
3219 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003220 }
3221 }
3222
Guido van Rossum13d52f02001-08-10 21:24:08 +00003223 /* Some more special stuff */
3224 base = type->tp_base;
3225 if (base != NULL) {
3226 if (type->tp_as_number == NULL)
3227 type->tp_as_number = base->tp_as_number;
3228 if (type->tp_as_sequence == NULL)
3229 type->tp_as_sequence = base->tp_as_sequence;
3230 if (type->tp_as_mapping == NULL)
3231 type->tp_as_mapping = base->tp_as_mapping;
Guido van Rossumeea47182003-02-11 20:39:59 +00003232 if (type->tp_as_buffer == NULL)
3233 type->tp_as_buffer = base->tp_as_buffer;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003234 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003235
Guido van Rossum1c450732001-10-08 15:18:27 +00003236 /* Link into each base class's list of subclasses */
3237 bases = type->tp_bases;
3238 n = PyTuple_GET_SIZE(bases);
3239 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003240 PyObject *b = PyTuple_GET_ITEM(bases, i);
3241 if (PyType_Check(b) &&
3242 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00003243 goto error;
3244 }
3245
Guido van Rossum13d52f02001-08-10 21:24:08 +00003246 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00003247 assert(type->tp_dict != NULL);
3248 type->tp_flags =
3249 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003250 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00003251
3252 error:
3253 type->tp_flags &= ~Py_TPFLAGS_READYING;
3254 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003255}
3256
Guido van Rossum1c450732001-10-08 15:18:27 +00003257static int
3258add_subclass(PyTypeObject *base, PyTypeObject *type)
3259{
3260 int i;
3261 PyObject *list, *ref, *new;
3262
3263 list = base->tp_subclasses;
3264 if (list == NULL) {
3265 base->tp_subclasses = list = PyList_New(0);
3266 if (list == NULL)
3267 return -1;
3268 }
3269 assert(PyList_Check(list));
3270 new = PyWeakref_NewRef((PyObject *)type, NULL);
3271 i = PyList_GET_SIZE(list);
3272 while (--i >= 0) {
3273 ref = PyList_GET_ITEM(list, i);
3274 assert(PyWeakref_CheckRef(ref));
Guido van Rossum3930bc32002-10-18 13:51:49 +00003275 if (PyWeakref_GET_OBJECT(ref) == Py_None)
3276 return PyList_SetItem(list, i, new);
Guido van Rossum1c450732001-10-08 15:18:27 +00003277 }
3278 i = PyList_Append(list, new);
3279 Py_DECREF(new);
3280 return i;
3281}
3282
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003283static void
3284remove_subclass(PyTypeObject *base, PyTypeObject *type)
3285{
3286 int i;
3287 PyObject *list, *ref;
3288
3289 list = base->tp_subclasses;
3290 if (list == NULL) {
3291 return;
3292 }
3293 assert(PyList_Check(list));
3294 i = PyList_GET_SIZE(list);
3295 while (--i >= 0) {
3296 ref = PyList_GET_ITEM(list, i);
3297 assert(PyWeakref_CheckRef(ref));
3298 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3299 /* this can't fail, right? */
3300 PySequence_DelItem(list, i);
3301 return;
3302 }
3303 }
3304}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003305
3306/* Generic wrappers for overloadable 'operators' such as __getitem__ */
3307
3308/* There's a wrapper *function* for each distinct function typedef used
3309 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3310 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3311 Most tables have only one entry; the tables for binary operators have two
3312 entries, one regular and one with reversed arguments. */
3313
3314static PyObject *
3315wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
3316{
3317 inquiry func = (inquiry)wrapped;
3318 int res;
3319
3320 if (!PyArg_ParseTuple(args, ""))
3321 return NULL;
3322 res = (*func)(self);
3323 if (res == -1 && PyErr_Occurred())
3324 return NULL;
3325 return PyInt_FromLong((long)res);
3326}
3327
Tim Peters6d6c1a32001-08-02 04:15:00 +00003328static PyObject *
3329wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3330{
3331 binaryfunc func = (binaryfunc)wrapped;
3332 PyObject *other;
3333
3334 if (!PyArg_ParseTuple(args, "O", &other))
3335 return NULL;
3336 return (*func)(self, other);
3337}
3338
3339static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003340wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3341{
3342 binaryfunc func = (binaryfunc)wrapped;
3343 PyObject *other;
3344
3345 if (!PyArg_ParseTuple(args, "O", &other))
3346 return NULL;
3347 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003348 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003349 Py_INCREF(Py_NotImplemented);
3350 return Py_NotImplemented;
3351 }
3352 return (*func)(self, other);
3353}
3354
3355static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003356wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3357{
3358 binaryfunc func = (binaryfunc)wrapped;
3359 PyObject *other;
3360
3361 if (!PyArg_ParseTuple(args, "O", &other))
3362 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003363 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003364 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003365 Py_INCREF(Py_NotImplemented);
3366 return Py_NotImplemented;
3367 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003368 return (*func)(other, self);
3369}
3370
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003371static PyObject *
3372wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
3373{
3374 coercion func = (coercion)wrapped;
3375 PyObject *other, *res;
3376 int ok;
3377
3378 if (!PyArg_ParseTuple(args, "O", &other))
3379 return NULL;
3380 ok = func(&self, &other);
3381 if (ok < 0)
3382 return NULL;
3383 if (ok > 0) {
3384 Py_INCREF(Py_NotImplemented);
3385 return Py_NotImplemented;
3386 }
3387 res = PyTuple_New(2);
3388 if (res == NULL) {
3389 Py_DECREF(self);
3390 Py_DECREF(other);
3391 return NULL;
3392 }
3393 PyTuple_SET_ITEM(res, 0, self);
3394 PyTuple_SET_ITEM(res, 1, other);
3395 return res;
3396}
3397
Tim Peters6d6c1a32001-08-02 04:15:00 +00003398static PyObject *
3399wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3400{
3401 ternaryfunc func = (ternaryfunc)wrapped;
3402 PyObject *other;
3403 PyObject *third = Py_None;
3404
3405 /* Note: This wrapper only works for __pow__() */
3406
3407 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
3408 return NULL;
3409 return (*func)(self, other, third);
3410}
3411
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00003412static PyObject *
3413wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3414{
3415 ternaryfunc func = (ternaryfunc)wrapped;
3416 PyObject *other;
3417 PyObject *third = Py_None;
3418
3419 /* Note: This wrapper only works for __pow__() */
3420
3421 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
3422 return NULL;
3423 return (*func)(other, self, third);
3424}
3425
Tim Peters6d6c1a32001-08-02 04:15:00 +00003426static PyObject *
3427wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3428{
3429 unaryfunc func = (unaryfunc)wrapped;
3430
3431 if (!PyArg_ParseTuple(args, ""))
3432 return NULL;
3433 return (*func)(self);
3434}
3435
Tim Peters6d6c1a32001-08-02 04:15:00 +00003436static PyObject *
3437wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
3438{
3439 intargfunc func = (intargfunc)wrapped;
3440 int i;
3441
3442 if (!PyArg_ParseTuple(args, "i", &i))
3443 return NULL;
3444 return (*func)(self, i);
3445}
3446
Guido van Rossum5d815f32001-08-17 21:57:47 +00003447static int
3448getindex(PyObject *self, PyObject *arg)
3449{
3450 int i;
3451
3452 i = PyInt_AsLong(arg);
3453 if (i == -1 && PyErr_Occurred())
3454 return -1;
3455 if (i < 0) {
3456 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3457 if (sq && sq->sq_length) {
3458 int n = (*sq->sq_length)(self);
3459 if (n < 0)
3460 return -1;
3461 i += n;
3462 }
3463 }
3464 return i;
3465}
3466
3467static PyObject *
3468wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3469{
3470 intargfunc func = (intargfunc)wrapped;
3471 PyObject *arg;
3472 int i;
3473
Guido van Rossumf4593e02001-10-03 12:09:30 +00003474 if (PyTuple_GET_SIZE(args) == 1) {
3475 arg = PyTuple_GET_ITEM(args, 0);
3476 i = getindex(self, arg);
3477 if (i == -1 && PyErr_Occurred())
3478 return NULL;
3479 return (*func)(self, i);
3480 }
3481 PyArg_ParseTuple(args, "O", &arg);
3482 assert(PyErr_Occurred());
3483 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003484}
3485
Tim Peters6d6c1a32001-08-02 04:15:00 +00003486static PyObject *
3487wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
3488{
3489 intintargfunc func = (intintargfunc)wrapped;
3490 int i, j;
3491
3492 if (!PyArg_ParseTuple(args, "ii", &i, &j))
3493 return NULL;
3494 return (*func)(self, i, j);
3495}
3496
Tim Peters6d6c1a32001-08-02 04:15:00 +00003497static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00003498wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003499{
3500 intobjargproc func = (intobjargproc)wrapped;
3501 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003502 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003503
Guido van Rossum5d815f32001-08-17 21:57:47 +00003504 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
3505 return NULL;
3506 i = getindex(self, arg);
3507 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00003508 return NULL;
3509 res = (*func)(self, i, value);
3510 if (res == -1 && PyErr_Occurred())
3511 return NULL;
3512 Py_INCREF(Py_None);
3513 return Py_None;
3514}
3515
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003516static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00003517wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003518{
3519 intobjargproc func = (intobjargproc)wrapped;
3520 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003521 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003522
Guido van Rossum5d815f32001-08-17 21:57:47 +00003523 if (!PyArg_ParseTuple(args, "O", &arg))
3524 return NULL;
3525 i = getindex(self, arg);
3526 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003527 return NULL;
3528 res = (*func)(self, i, NULL);
3529 if (res == -1 && PyErr_Occurred())
3530 return NULL;
3531 Py_INCREF(Py_None);
3532 return Py_None;
3533}
3534
Tim Peters6d6c1a32001-08-02 04:15:00 +00003535static PyObject *
3536wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
3537{
3538 intintobjargproc func = (intintobjargproc)wrapped;
3539 int i, j, res;
3540 PyObject *value;
3541
3542 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
3543 return NULL;
3544 res = (*func)(self, i, j, value);
3545 if (res == -1 && PyErr_Occurred())
3546 return NULL;
3547 Py_INCREF(Py_None);
3548 return Py_None;
3549}
3550
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003551static PyObject *
3552wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3553{
3554 intintobjargproc func = (intintobjargproc)wrapped;
3555 int i, j, res;
3556
3557 if (!PyArg_ParseTuple(args, "ii", &i, &j))
3558 return NULL;
3559 res = (*func)(self, i, j, NULL);
3560 if (res == -1 && PyErr_Occurred())
3561 return NULL;
3562 Py_INCREF(Py_None);
3563 return Py_None;
3564}
3565
Tim Peters6d6c1a32001-08-02 04:15:00 +00003566/* XXX objobjproc is a misnomer; should be objargpred */
3567static PyObject *
3568wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3569{
3570 objobjproc func = (objobjproc)wrapped;
3571 int res;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00003572 PyObject *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003573
3574 if (!PyArg_ParseTuple(args, "O", &value))
3575 return NULL;
3576 res = (*func)(self, value);
3577 if (res == -1 && PyErr_Occurred())
3578 return NULL;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00003579 else
3580 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003581}
3582
Tim Peters6d6c1a32001-08-02 04:15:00 +00003583static PyObject *
3584wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3585{
3586 objobjargproc func = (objobjargproc)wrapped;
3587 int res;
3588 PyObject *key, *value;
3589
3590 if (!PyArg_ParseTuple(args, "OO", &key, &value))
3591 return NULL;
3592 res = (*func)(self, key, value);
3593 if (res == -1 && PyErr_Occurred())
3594 return NULL;
3595 Py_INCREF(Py_None);
3596 return Py_None;
3597}
3598
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003599static PyObject *
3600wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3601{
3602 objobjargproc func = (objobjargproc)wrapped;
3603 int res;
3604 PyObject *key;
3605
3606 if (!PyArg_ParseTuple(args, "O", &key))
3607 return NULL;
3608 res = (*func)(self, key, NULL);
3609 if (res == -1 && PyErr_Occurred())
3610 return NULL;
3611 Py_INCREF(Py_None);
3612 return Py_None;
3613}
3614
Tim Peters6d6c1a32001-08-02 04:15:00 +00003615static PyObject *
3616wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3617{
3618 cmpfunc func = (cmpfunc)wrapped;
3619 int res;
3620 PyObject *other;
3621
3622 if (!PyArg_ParseTuple(args, "O", &other))
3623 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00003624 if (other->ob_type->tp_compare != func &&
3625 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00003626 PyErr_Format(
3627 PyExc_TypeError,
3628 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3629 self->ob_type->tp_name,
3630 self->ob_type->tp_name,
3631 other->ob_type->tp_name);
3632 return NULL;
3633 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003634 res = (*func)(self, other);
3635 if (PyErr_Occurred())
3636 return NULL;
3637 return PyInt_FromLong((long)res);
3638}
3639
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003640/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
Guido van Rossum52b27052003-04-15 20:05:10 +00003641 This is called the Carlo Verre hack after its discoverer. */
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003642static int
3643hackcheck(PyObject *self, setattrofunc func, char *what)
3644{
3645 PyTypeObject *type = self->ob_type;
3646 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3647 type = type->tp_base;
3648 if (type->tp_setattro != func) {
3649 PyErr_Format(PyExc_TypeError,
3650 "can't apply this %s to %s object",
3651 what,
3652 type->tp_name);
3653 return 0;
3654 }
3655 return 1;
3656}
3657
Tim Peters6d6c1a32001-08-02 04:15:00 +00003658static PyObject *
3659wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
3660{
3661 setattrofunc func = (setattrofunc)wrapped;
3662 int res;
3663 PyObject *name, *value;
3664
3665 if (!PyArg_ParseTuple(args, "OO", &name, &value))
3666 return NULL;
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003667 if (!hackcheck(self, func, "__setattr__"))
3668 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003669 res = (*func)(self, name, value);
3670 if (res < 0)
3671 return NULL;
3672 Py_INCREF(Py_None);
3673 return Py_None;
3674}
3675
3676static PyObject *
3677wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
3678{
3679 setattrofunc func = (setattrofunc)wrapped;
3680 int res;
3681 PyObject *name;
3682
3683 if (!PyArg_ParseTuple(args, "O", &name))
3684 return NULL;
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003685 if (!hackcheck(self, func, "__delattr__"))
3686 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003687 res = (*func)(self, name, NULL);
3688 if (res < 0)
3689 return NULL;
3690 Py_INCREF(Py_None);
3691 return Py_None;
3692}
3693
Tim Peters6d6c1a32001-08-02 04:15:00 +00003694static PyObject *
3695wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3696{
3697 hashfunc func = (hashfunc)wrapped;
3698 long res;
3699
3700 if (!PyArg_ParseTuple(args, ""))
3701 return NULL;
3702 res = (*func)(self);
3703 if (res == -1 && PyErr_Occurred())
3704 return NULL;
3705 return PyInt_FromLong(res);
3706}
3707
Tim Peters6d6c1a32001-08-02 04:15:00 +00003708static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00003709wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003710{
3711 ternaryfunc func = (ternaryfunc)wrapped;
3712
Guido van Rossumc8e56452001-10-22 00:43:43 +00003713 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003714}
3715
Tim Peters6d6c1a32001-08-02 04:15:00 +00003716static PyObject *
3717wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3718{
3719 richcmpfunc func = (richcmpfunc)wrapped;
3720 PyObject *other;
3721
3722 if (!PyArg_ParseTuple(args, "O", &other))
3723 return NULL;
3724 return (*func)(self, other, op);
3725}
3726
3727#undef RICHCMP_WRAPPER
3728#define RICHCMP_WRAPPER(NAME, OP) \
3729static PyObject * \
3730richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3731{ \
3732 return wrap_richcmpfunc(self, args, wrapped, OP); \
3733}
3734
Jack Jansen8e938b42001-08-08 15:29:49 +00003735RICHCMP_WRAPPER(lt, Py_LT)
3736RICHCMP_WRAPPER(le, Py_LE)
3737RICHCMP_WRAPPER(eq, Py_EQ)
3738RICHCMP_WRAPPER(ne, Py_NE)
3739RICHCMP_WRAPPER(gt, Py_GT)
3740RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003741
Tim Peters6d6c1a32001-08-02 04:15:00 +00003742static PyObject *
3743wrap_next(PyObject *self, PyObject *args, void *wrapped)
3744{
3745 unaryfunc func = (unaryfunc)wrapped;
3746 PyObject *res;
3747
3748 if (!PyArg_ParseTuple(args, ""))
3749 return NULL;
3750 res = (*func)(self);
3751 if (res == NULL && !PyErr_Occurred())
3752 PyErr_SetNone(PyExc_StopIteration);
3753 return res;
3754}
3755
Tim Peters6d6c1a32001-08-02 04:15:00 +00003756static PyObject *
3757wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3758{
3759 descrgetfunc func = (descrgetfunc)wrapped;
3760 PyObject *obj;
3761 PyObject *type = NULL;
3762
3763 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
3764 return NULL;
Guido van Rossum82ed25c2003-02-11 16:25:43 +00003765 if (obj == Py_None)
3766 obj = NULL;
3767 if (type == Py_None)
3768 type = NULL;
3769 if (type == NULL &&obj == NULL) {
3770 PyErr_SetString(PyExc_TypeError,
3771 "__get__(None, None) is invalid");
3772 return NULL;
3773 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003774 return (*func)(self, obj, type);
3775}
3776
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003778wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003779{
3780 descrsetfunc func = (descrsetfunc)wrapped;
3781 PyObject *obj, *value;
3782 int ret;
3783
3784 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
3785 return NULL;
3786 ret = (*func)(self, obj, value);
3787 if (ret < 0)
3788 return NULL;
3789 Py_INCREF(Py_None);
3790 return Py_None;
3791}
Guido van Rossum22b13872002-08-06 21:41:44 +00003792
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00003793static PyObject *
3794wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3795{
3796 descrsetfunc func = (descrsetfunc)wrapped;
3797 PyObject *obj;
3798 int ret;
3799
3800 if (!PyArg_ParseTuple(args, "O", &obj))
3801 return NULL;
3802 ret = (*func)(self, obj, NULL);
3803 if (ret < 0)
3804 return NULL;
3805 Py_INCREF(Py_None);
3806 return Py_None;
3807}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003808
Tim Peters6d6c1a32001-08-02 04:15:00 +00003809static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00003810wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003811{
3812 initproc func = (initproc)wrapped;
3813
Guido van Rossumc8e56452001-10-22 00:43:43 +00003814 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003815 return NULL;
3816 Py_INCREF(Py_None);
3817 return Py_None;
3818}
3819
Tim Peters6d6c1a32001-08-02 04:15:00 +00003820static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003821tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003822{
Barry Warsaw60f01882001-08-22 19:24:42 +00003823 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003824 PyObject *arg0, *res;
3825
3826 if (self == NULL || !PyType_Check(self))
3827 Py_FatalError("__new__() called with non-type 'self'");
3828 type = (PyTypeObject *)self;
3829 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003830 PyErr_Format(PyExc_TypeError,
3831 "%s.__new__(): not enough arguments",
3832 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003833 return NULL;
3834 }
3835 arg0 = PyTuple_GET_ITEM(args, 0);
3836 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003837 PyErr_Format(PyExc_TypeError,
3838 "%s.__new__(X): X is not a type object (%s)",
3839 type->tp_name,
3840 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003841 return NULL;
3842 }
3843 subtype = (PyTypeObject *)arg0;
3844 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003845 PyErr_Format(PyExc_TypeError,
3846 "%s.__new__(%s): %s is not a subtype of %s",
3847 type->tp_name,
3848 subtype->tp_name,
3849 subtype->tp_name,
3850 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003851 return NULL;
3852 }
Barry Warsaw60f01882001-08-22 19:24:42 +00003853
3854 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00003855 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00003856 most derived base that's not a heap type is this type. */
3857 staticbase = subtype;
3858 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
3859 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00003860 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003861 PyErr_Format(PyExc_TypeError,
3862 "%s.__new__(%s) is not safe, use %s.__new__()",
3863 type->tp_name,
3864 subtype->tp_name,
3865 staticbase == NULL ? "?" : staticbase->tp_name);
3866 return NULL;
3867 }
3868
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003869 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3870 if (args == NULL)
3871 return NULL;
3872 res = type->tp_new(subtype, args, kwds);
3873 Py_DECREF(args);
3874 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003875}
3876
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003877static struct PyMethodDef tp_new_methoddef[] = {
3878 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003879 PyDoc_STR("T.__new__(S, ...) -> "
3880 "a new object with type S, a subtype of T")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00003881 {0}
3882};
3883
3884static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003885add_tp_new_wrapper(PyTypeObject *type)
3886{
Guido van Rossumf040ede2001-08-07 16:40:56 +00003887 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003888
Guido van Rossum687ae002001-10-15 22:03:32 +00003889 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00003890 return 0;
3891 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003892 if (func == NULL)
3893 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00003894 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003895}
3896
Guido van Rossumf040ede2001-08-07 16:40:56 +00003897/* Slot wrappers that call the corresponding __foo__ slot. See comments
3898 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003899
Guido van Rossumdc91b992001-08-08 22:26:22 +00003900#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003901static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003902FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003903{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00003904 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003905 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003906}
3907
Guido van Rossumdc91b992001-08-08 22:26:22 +00003908#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003909static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003910FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003911{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003912 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003913 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003914}
3915
Guido van Rossumcd118802003-01-06 22:57:47 +00003916/* Boolean helper for SLOT1BINFULL().
3917 right.__class__ is a nontrivial subclass of left.__class__. */
3918static int
3919method_is_overloaded(PyObject *left, PyObject *right, char *name)
3920{
3921 PyObject *a, *b;
3922 int ok;
3923
3924 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
3925 if (b == NULL) {
3926 PyErr_Clear();
3927 /* If right doesn't have it, it's not overloaded */
3928 return 0;
3929 }
3930
3931 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
3932 if (a == NULL) {
3933 PyErr_Clear();
3934 Py_DECREF(b);
3935 /* If right has it but left doesn't, it's overloaded */
3936 return 1;
3937 }
3938
3939 ok = PyObject_RichCompareBool(a, b, Py_NE);
3940 Py_DECREF(a);
3941 Py_DECREF(b);
3942 if (ok < 0) {
3943 PyErr_Clear();
3944 return 0;
3945 }
3946
3947 return ok;
3948}
3949
Guido van Rossumdc91b992001-08-08 22:26:22 +00003950
3951#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003952static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003953FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003954{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003955 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00003956 int do_other = self->ob_type != other->ob_type && \
3957 other->ob_type->tp_as_number != NULL && \
3958 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003959 if (self->ob_type->tp_as_number != NULL && \
3960 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
3961 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00003962 if (do_other && \
Guido van Rossumcd118802003-01-06 22:57:47 +00003963 PyType_IsSubtype(other->ob_type, self->ob_type) && \
3964 method_is_overloaded(self, other, ROPSTR)) { \
Guido van Rossum55f20992001-10-01 17:18:22 +00003965 r = call_maybe( \
3966 other, ROPSTR, &rcache_str, "(O)", self); \
3967 if (r != Py_NotImplemented) \
3968 return r; \
3969 Py_DECREF(r); \
3970 do_other = 0; \
3971 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00003972 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00003973 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003974 if (r != Py_NotImplemented || \
3975 other->ob_type == self->ob_type) \
3976 return r; \
3977 Py_DECREF(r); \
3978 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00003979 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00003980 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00003981 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003982 } \
3983 Py_INCREF(Py_NotImplemented); \
3984 return Py_NotImplemented; \
3985}
3986
3987#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
3988 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
3989
3990#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
3991static PyObject * \
3992FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
3993{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003994 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003995 return call_method(self, OPSTR, &cache_str, \
3996 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003997}
3998
3999static int
4000slot_sq_length(PyObject *self)
4001{
Guido van Rossum2730b132001-08-28 18:22:14 +00004002 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004003 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00004004 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004005
4006 if (res == NULL)
4007 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00004008 len = (int)PyInt_AsLong(res);
4009 Py_DECREF(res);
Jeremy Hylton73a088e2002-07-25 16:43:29 +00004010 if (len == -1 && PyErr_Occurred())
4011 return -1;
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004012 if (len < 0) {
Guido van Rossum22b13872002-08-06 21:41:44 +00004013 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004014 "__len__() should return >= 0");
4015 return -1;
4016 }
Guido van Rossum26111622001-10-01 16:42:49 +00004017 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004018}
4019
Guido van Rossumdc91b992001-08-08 22:26:22 +00004020SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
4021SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00004022
4023/* Super-optimized version of slot_sq_item.
4024 Other slots could do the same... */
4025static PyObject *
4026slot_sq_item(PyObject *self, int i)
4027{
4028 static PyObject *getitem_str;
4029 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4030 descrgetfunc f;
4031
4032 if (getitem_str == NULL) {
4033 getitem_str = PyString_InternFromString("__getitem__");
4034 if (getitem_str == NULL)
4035 return NULL;
4036 }
4037 func = _PyType_Lookup(self->ob_type, getitem_str);
4038 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00004039 if ((f = func->ob_type->tp_descr_get) == NULL)
4040 Py_INCREF(func);
Neal Norwitz673cd822002-10-18 16:33:13 +00004041 else {
Guido van Rossumf4593e02001-10-03 12:09:30 +00004042 func = f(func, self, (PyObject *)(self->ob_type));
Neal Norwitz673cd822002-10-18 16:33:13 +00004043 if (func == NULL) {
4044 return NULL;
4045 }
4046 }
Guido van Rossumf4593e02001-10-03 12:09:30 +00004047 ival = PyInt_FromLong(i);
4048 if (ival != NULL) {
4049 args = PyTuple_New(1);
4050 if (args != NULL) {
4051 PyTuple_SET_ITEM(args, 0, ival);
4052 retval = PyObject_Call(func, args, NULL);
4053 Py_XDECREF(args);
4054 Py_XDECREF(func);
4055 return retval;
4056 }
4057 }
4058 }
4059 else {
4060 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4061 }
4062 Py_XDECREF(args);
4063 Py_XDECREF(ival);
4064 Py_XDECREF(func);
4065 return NULL;
4066}
4067
Guido van Rossumdc91b992001-08-08 22:26:22 +00004068SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004069
4070static int
4071slot_sq_ass_item(PyObject *self, int index, PyObject *value)
4072{
4073 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004074 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004075
4076 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004077 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004078 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004079 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004080 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004081 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004082 if (res == NULL)
4083 return -1;
4084 Py_DECREF(res);
4085 return 0;
4086}
4087
4088static int
4089slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
4090{
4091 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004092 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004093
4094 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004095 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004096 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004097 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004098 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004099 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004100 if (res == NULL)
4101 return -1;
4102 Py_DECREF(res);
4103 return 0;
4104}
4105
4106static int
4107slot_sq_contains(PyObject *self, PyObject *value)
4108{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004109 PyObject *func, *res, *args;
Tim Petersbf9b2442003-03-23 05:35:36 +00004110 int result = -1;
4111
Guido van Rossum60718732001-08-28 17:47:51 +00004112 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004113
Guido van Rossum55f20992001-10-01 17:18:22 +00004114 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004115 if (func != NULL) {
4116 args = Py_BuildValue("(O)", value);
4117 if (args == NULL)
4118 res = NULL;
4119 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004120 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004121 Py_DECREF(args);
4122 }
4123 Py_DECREF(func);
Tim Petersbf9b2442003-03-23 05:35:36 +00004124 if (res != NULL) {
4125 result = PyObject_IsTrue(res);
4126 Py_DECREF(res);
4127 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004128 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004129 else if (! PyErr_Occurred()) {
4130 result = _PySequence_IterSearch(self, value,
4131 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004132 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004133 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004134}
4135
Guido van Rossumdc91b992001-08-08 22:26:22 +00004136SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
4137SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004138
4139#define slot_mp_length slot_sq_length
4140
Guido van Rossumdc91b992001-08-08 22:26:22 +00004141SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004142
4143static int
4144slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4145{
4146 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004147 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004148
4149 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004150 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004151 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004152 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004153 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004154 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004155 if (res == NULL)
4156 return -1;
4157 Py_DECREF(res);
4158 return 0;
4159}
4160
Guido van Rossumdc91b992001-08-08 22:26:22 +00004161SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4162SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4163SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
4164SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
4165SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4166SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4167
Jeremy Hylton938ace62002-07-17 16:30:39 +00004168static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
Guido van Rossumdc91b992001-08-08 22:26:22 +00004169
4170SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4171 nb_power, "__pow__", "__rpow__")
4172
4173static PyObject *
4174slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4175{
Guido van Rossum2730b132001-08-28 18:22:14 +00004176 static PyObject *pow_str;
4177
Guido van Rossumdc91b992001-08-08 22:26:22 +00004178 if (modulus == Py_None)
4179 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00004180 /* Three-arg power doesn't use __rpow__. But ternary_op
4181 can call this when the second argument's type uses
4182 slot_nb_power, so check before calling self.__pow__. */
4183 if (self->ob_type->tp_as_number != NULL &&
4184 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4185 return call_method(self, "__pow__", &pow_str,
4186 "(OO)", other, modulus);
4187 }
4188 Py_INCREF(Py_NotImplemented);
4189 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00004190}
4191
4192SLOT0(slot_nb_negative, "__neg__")
4193SLOT0(slot_nb_positive, "__pos__")
4194SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004195
4196static int
4197slot_nb_nonzero(PyObject *self)
4198{
Tim Petersea7f75d2002-12-07 21:39:16 +00004199 PyObject *func, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00004200 static PyObject *nonzero_str, *len_str;
Tim Petersea7f75d2002-12-07 21:39:16 +00004201 int result = -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004202
Guido van Rossum55f20992001-10-01 17:18:22 +00004203 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004204 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00004205 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00004206 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00004207 func = lookup_maybe(self, "__len__", &len_str);
Tim Petersea7f75d2002-12-07 21:39:16 +00004208 if (func == NULL)
4209 return PyErr_Occurred() ? -1 : 1;
4210 }
4211 args = PyTuple_New(0);
4212 if (args != NULL) {
4213 PyObject *temp = PyObject_Call(func, args, NULL);
4214 Py_DECREF(args);
4215 if (temp != NULL) {
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004216 if (PyInt_CheckExact(temp) || PyBool_Check(temp))
Jeremy Hylton090a3492003-06-27 16:46:45 +00004217 result = PyObject_IsTrue(temp);
4218 else {
4219 PyErr_Format(PyExc_TypeError,
4220 "__nonzero__ should return "
4221 "bool or int, returned %s",
4222 temp->ob_type->tp_name);
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004223 result = -1;
Jeremy Hylton090a3492003-06-27 16:46:45 +00004224 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004225 Py_DECREF(temp);
Guido van Rossum55f20992001-10-01 17:18:22 +00004226 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004227 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004228 Py_DECREF(func);
Tim Petersea7f75d2002-12-07 21:39:16 +00004229 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004230}
4231
Guido van Rossumdc91b992001-08-08 22:26:22 +00004232SLOT0(slot_nb_invert, "__invert__")
4233SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4234SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4235SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4236SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4237SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004238
4239static int
4240slot_nb_coerce(PyObject **a, PyObject **b)
4241{
4242 static PyObject *coerce_str;
4243 PyObject *self = *a, *other = *b;
4244
4245 if (self->ob_type->tp_as_number != NULL &&
4246 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4247 PyObject *r;
4248 r = call_maybe(
4249 self, "__coerce__", &coerce_str, "(O)", other);
4250 if (r == NULL)
4251 return -1;
4252 if (r == Py_NotImplemented) {
4253 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004254 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004255 else {
4256 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4257 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004258 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00004259 Py_DECREF(r);
4260 return -1;
4261 }
4262 *a = PyTuple_GET_ITEM(r, 0);
4263 Py_INCREF(*a);
4264 *b = PyTuple_GET_ITEM(r, 1);
4265 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004266 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00004267 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004268 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004269 }
4270 if (other->ob_type->tp_as_number != NULL &&
4271 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
4272 PyObject *r;
4273 r = call_maybe(
4274 other, "__coerce__", &coerce_str, "(O)", self);
4275 if (r == NULL)
4276 return -1;
4277 if (r == Py_NotImplemented) {
4278 Py_DECREF(r);
4279 return 1;
4280 }
4281 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
4282 PyErr_SetString(PyExc_TypeError,
4283 "__coerce__ didn't return a 2-tuple");
4284 Py_DECREF(r);
4285 return -1;
4286 }
4287 *a = PyTuple_GET_ITEM(r, 1);
4288 Py_INCREF(*a);
4289 *b = PyTuple_GET_ITEM(r, 0);
4290 Py_INCREF(*b);
4291 Py_DECREF(r);
4292 return 0;
4293 }
4294 return 1;
4295}
4296
Guido van Rossumdc91b992001-08-08 22:26:22 +00004297SLOT0(slot_nb_int, "__int__")
4298SLOT0(slot_nb_long, "__long__")
4299SLOT0(slot_nb_float, "__float__")
4300SLOT0(slot_nb_oct, "__oct__")
4301SLOT0(slot_nb_hex, "__hex__")
4302SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4303SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4304SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
4305SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
4306SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
Guido van Rossum6e5680f2002-10-15 01:01:53 +00004307SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004308SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4309SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4310SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4311SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4312SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4313SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4314 "__floordiv__", "__rfloordiv__")
4315SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4316SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4317SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004318
4319static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00004320half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004321{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004322 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004323 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004324 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004325
Guido van Rossum60718732001-08-28 17:47:51 +00004326 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004327 if (func == NULL) {
4328 PyErr_Clear();
4329 }
4330 else {
4331 args = Py_BuildValue("(O)", other);
4332 if (args == NULL)
4333 res = NULL;
4334 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004335 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004336 Py_DECREF(args);
4337 }
Raymond Hettingerab5dae32002-06-24 13:08:16 +00004338 Py_DECREF(func);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004339 if (res != Py_NotImplemented) {
4340 if (res == NULL)
4341 return -2;
4342 c = PyInt_AsLong(res);
4343 Py_DECREF(res);
4344 if (c == -1 && PyErr_Occurred())
4345 return -2;
4346 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4347 }
4348 Py_DECREF(res);
4349 }
4350 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004351}
4352
Guido van Rossumab3b0342001-09-18 20:38:53 +00004353/* This slot is published for the benefit of try_3way_compare in object.c */
4354int
4355_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00004356{
4357 int c;
4358
Guido van Rossumab3b0342001-09-18 20:38:53 +00004359 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004360 c = half_compare(self, other);
4361 if (c <= 1)
4362 return c;
4363 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00004364 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004365 c = half_compare(other, self);
4366 if (c < -1)
4367 return -2;
4368 if (c <= 1)
4369 return -c;
4370 }
4371 return (void *)self < (void *)other ? -1 :
4372 (void *)self > (void *)other ? 1 : 0;
4373}
4374
4375static PyObject *
4376slot_tp_repr(PyObject *self)
4377{
4378 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004379 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004380
Guido van Rossum60718732001-08-28 17:47:51 +00004381 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004382 if (func != NULL) {
4383 res = PyEval_CallObject(func, NULL);
4384 Py_DECREF(func);
4385 return res;
4386 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00004387 PyErr_Clear();
4388 return PyString_FromFormat("<%s object at %p>",
4389 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004390}
4391
4392static PyObject *
4393slot_tp_str(PyObject *self)
4394{
4395 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004396 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004397
Guido van Rossum60718732001-08-28 17:47:51 +00004398 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004399 if (func != NULL) {
4400 res = PyEval_CallObject(func, NULL);
4401 Py_DECREF(func);
4402 return res;
4403 }
4404 else {
4405 PyErr_Clear();
4406 return slot_tp_repr(self);
4407 }
4408}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004409
4410static long
4411slot_tp_hash(PyObject *self)
4412{
Tim Peters61ce0a92002-12-06 23:38:02 +00004413 PyObject *func;
Guido van Rossum60718732001-08-28 17:47:51 +00004414 static PyObject *hash_str, *eq_str, *cmp_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004415 long h;
4416
Guido van Rossum60718732001-08-28 17:47:51 +00004417 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004418
4419 if (func != NULL) {
Tim Peters61ce0a92002-12-06 23:38:02 +00004420 PyObject *res = PyEval_CallObject(func, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004421 Py_DECREF(func);
4422 if (res == NULL)
4423 return -1;
4424 h = PyInt_AsLong(res);
Tim Peters61ce0a92002-12-06 23:38:02 +00004425 Py_DECREF(res);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004426 }
4427 else {
4428 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004429 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004430 if (func == NULL) {
4431 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004432 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004433 }
4434 if (func != NULL) {
4435 Py_DECREF(func);
4436 PyErr_SetString(PyExc_TypeError, "unhashable type");
4437 return -1;
4438 }
4439 PyErr_Clear();
4440 h = _Py_HashPointer((void *)self);
4441 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004442 if (h == -1 && !PyErr_Occurred())
4443 h = -2;
4444 return h;
4445}
4446
4447static PyObject *
4448slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4449{
Guido van Rossum60718732001-08-28 17:47:51 +00004450 static PyObject *call_str;
4451 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004452 PyObject *res;
4453
4454 if (meth == NULL)
4455 return NULL;
4456 res = PyObject_Call(meth, args, kwds);
4457 Py_DECREF(meth);
4458 return res;
4459}
4460
Guido van Rossum14a6f832001-10-17 13:59:09 +00004461/* There are two slot dispatch functions for tp_getattro.
4462
4463 - slot_tp_getattro() is used when __getattribute__ is overridden
4464 but no __getattr__ hook is present;
4465
4466 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4467
Guido van Rossumc334df52002-04-04 23:44:47 +00004468 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4469 detects the absence of __getattr__ and then installs the simpler slot if
4470 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00004471
Tim Peters6d6c1a32001-08-02 04:15:00 +00004472static PyObject *
4473slot_tp_getattro(PyObject *self, PyObject *name)
4474{
Guido van Rossum14a6f832001-10-17 13:59:09 +00004475 static PyObject *getattribute_str = NULL;
4476 return call_method(self, "__getattribute__", &getattribute_str,
4477 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004478}
4479
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004480static PyObject *
4481slot_tp_getattr_hook(PyObject *self, PyObject *name)
4482{
4483 PyTypeObject *tp = self->ob_type;
4484 PyObject *getattr, *getattribute, *res;
4485 static PyObject *getattribute_str = NULL;
4486 static PyObject *getattr_str = NULL;
4487
4488 if (getattr_str == NULL) {
4489 getattr_str = PyString_InternFromString("__getattr__");
4490 if (getattr_str == NULL)
4491 return NULL;
4492 }
4493 if (getattribute_str == NULL) {
4494 getattribute_str =
4495 PyString_InternFromString("__getattribute__");
4496 if (getattribute_str == NULL)
4497 return NULL;
4498 }
4499 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004500 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004501 /* No __getattr__ hook: use a simpler dispatcher */
4502 tp->tp_getattro = slot_tp_getattro;
4503 return slot_tp_getattro(self, name);
4504 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004505 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004506 if (getattribute == NULL ||
4507 (getattribute->ob_type == &PyWrapperDescr_Type &&
4508 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4509 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004510 res = PyObject_GenericGetAttr(self, name);
4511 else
4512 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004513 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004514 PyErr_Clear();
4515 res = PyObject_CallFunction(getattr, "OO", self, name);
4516 }
4517 return res;
4518}
4519
Tim Peters6d6c1a32001-08-02 04:15:00 +00004520static int
4521slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4522{
4523 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004524 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004525
4526 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004527 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004528 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004529 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004530 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004531 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004532 if (res == NULL)
4533 return -1;
4534 Py_DECREF(res);
4535 return 0;
4536}
4537
4538/* Map rich comparison operators to their __xx__ namesakes */
4539static char *name_op[] = {
4540 "__lt__",
4541 "__le__",
4542 "__eq__",
4543 "__ne__",
4544 "__gt__",
4545 "__ge__",
4546};
4547
4548static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00004549half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004550{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004551 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004552 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00004553
Guido van Rossum60718732001-08-28 17:47:51 +00004554 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004555 if (func == NULL) {
4556 PyErr_Clear();
4557 Py_INCREF(Py_NotImplemented);
4558 return Py_NotImplemented;
4559 }
4560 args = Py_BuildValue("(O)", other);
4561 if (args == NULL)
4562 res = NULL;
4563 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004564 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004565 Py_DECREF(args);
4566 }
4567 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004568 return res;
4569}
4570
Guido van Rossumb8f63662001-08-15 23:57:02 +00004571/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
4572static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
4573
4574static PyObject *
4575slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4576{
4577 PyObject *res;
4578
4579 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4580 res = half_richcompare(self, other, op);
4581 if (res != Py_NotImplemented)
4582 return res;
4583 Py_DECREF(res);
4584 }
4585 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
4586 res = half_richcompare(other, self, swapped_op[op]);
4587 if (res != Py_NotImplemented) {
4588 return res;
4589 }
4590 Py_DECREF(res);
4591 }
4592 Py_INCREF(Py_NotImplemented);
4593 return Py_NotImplemented;
4594}
4595
4596static PyObject *
4597slot_tp_iter(PyObject *self)
4598{
4599 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004600 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004601
Guido van Rossum60718732001-08-28 17:47:51 +00004602 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004603 if (func != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +00004604 PyObject *args;
4605 args = res = PyTuple_New(0);
4606 if (args != NULL) {
4607 res = PyObject_Call(func, args, NULL);
4608 Py_DECREF(args);
4609 }
4610 Py_DECREF(func);
4611 return res;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004612 }
4613 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004614 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004615 if (func == NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00004616 PyErr_SetString(PyExc_TypeError,
4617 "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00004618 return NULL;
4619 }
4620 Py_DECREF(func);
4621 return PySeqIter_New(self);
4622}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004623
4624static PyObject *
4625slot_tp_iternext(PyObject *self)
4626{
Guido van Rossum2730b132001-08-28 18:22:14 +00004627 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004628 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00004629}
4630
Guido van Rossum1a493502001-08-17 16:47:50 +00004631static PyObject *
4632slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4633{
4634 PyTypeObject *tp = self->ob_type;
4635 PyObject *get;
4636 static PyObject *get_str = NULL;
4637
4638 if (get_str == NULL) {
4639 get_str = PyString_InternFromString("__get__");
4640 if (get_str == NULL)
4641 return NULL;
4642 }
4643 get = _PyType_Lookup(tp, get_str);
4644 if (get == NULL) {
4645 /* Avoid further slowdowns */
4646 if (tp->tp_descr_get == slot_tp_descr_get)
4647 tp->tp_descr_get = NULL;
4648 Py_INCREF(self);
4649 return self;
4650 }
Guido van Rossum2c252392001-08-24 10:13:31 +00004651 if (obj == NULL)
4652 obj = Py_None;
4653 if (type == NULL)
4654 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00004655 return PyObject_CallFunction(get, "OOO", self, obj, type);
4656}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004657
4658static int
4659slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4660{
Guido van Rossum2c252392001-08-24 10:13:31 +00004661 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004662 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00004663
4664 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00004665 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004666 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00004667 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004668 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004669 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004670 if (res == NULL)
4671 return -1;
4672 Py_DECREF(res);
4673 return 0;
4674}
4675
4676static int
4677slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4678{
Guido van Rossum60718732001-08-28 17:47:51 +00004679 static PyObject *init_str;
4680 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004681 PyObject *res;
4682
4683 if (meth == NULL)
4684 return -1;
4685 res = PyObject_Call(meth, args, kwds);
4686 Py_DECREF(meth);
4687 if (res == NULL)
4688 return -1;
4689 Py_DECREF(res);
4690 return 0;
4691}
4692
4693static PyObject *
4694slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4695{
Guido van Rossum7bed2132002-08-08 21:57:53 +00004696 static PyObject *new_str;
4697 PyObject *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004698 PyObject *newargs, *x;
4699 int i, n;
4700
Guido van Rossum7bed2132002-08-08 21:57:53 +00004701 if (new_str == NULL) {
4702 new_str = PyString_InternFromString("__new__");
4703 if (new_str == NULL)
4704 return NULL;
4705 }
4706 func = PyObject_GetAttr((PyObject *)type, new_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004707 if (func == NULL)
4708 return NULL;
4709 assert(PyTuple_Check(args));
4710 n = PyTuple_GET_SIZE(args);
4711 newargs = PyTuple_New(n+1);
4712 if (newargs == NULL)
4713 return NULL;
4714 Py_INCREF(type);
4715 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4716 for (i = 0; i < n; i++) {
4717 x = PyTuple_GET_ITEM(args, i);
4718 Py_INCREF(x);
4719 PyTuple_SET_ITEM(newargs, i+1, x);
4720 }
4721 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00004722 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004723 Py_DECREF(func);
4724 return x;
4725}
4726
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004727static void
4728slot_tp_del(PyObject *self)
4729{
4730 static PyObject *del_str = NULL;
4731 PyObject *del, *res;
4732 PyObject *error_type, *error_value, *error_traceback;
4733
4734 /* Temporarily resurrect the object. */
4735 assert(self->ob_refcnt == 0);
4736 self->ob_refcnt = 1;
4737
4738 /* Save the current exception, if any. */
4739 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4740
4741 /* Execute __del__ method, if any. */
4742 del = lookup_maybe(self, "__del__", &del_str);
4743 if (del != NULL) {
4744 res = PyEval_CallObject(del, NULL);
4745 if (res == NULL)
4746 PyErr_WriteUnraisable(del);
4747 else
4748 Py_DECREF(res);
4749 Py_DECREF(del);
4750 }
4751
4752 /* Restore the saved exception. */
4753 PyErr_Restore(error_type, error_value, error_traceback);
4754
4755 /* Undo the temporary resurrection; can't use DECREF here, it would
4756 * cause a recursive call.
4757 */
4758 assert(self->ob_refcnt > 0);
4759 if (--self->ob_refcnt == 0)
4760 return; /* this is the normal path out */
4761
4762 /* __del__ resurrected it! Make it look like the original Py_DECREF
4763 * never happened.
4764 */
4765 {
4766 int refcnt = self->ob_refcnt;
4767 _Py_NewReference(self);
4768 self->ob_refcnt = refcnt;
4769 }
4770 assert(!PyType_IS_GC(self->ob_type) ||
4771 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
4772 /* If Py_REF_DEBUG, the original decref dropped _Py_RefTotal, but
4773 * _Py_NewReference bumped it again, so that's a wash.
4774 * If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4775 * chain, so no more to do there either.
4776 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4777 * _Py_NewReference bumped tp_allocs: both of those need to be
4778 * undone.
4779 */
4780#ifdef COUNT_ALLOCS
4781 --self->ob_type->tp_frees;
4782 --self->ob_type->tp_allocs;
4783#endif
4784}
4785
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004786
4787/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
Tim Petersbf9b2442003-03-23 05:35:36 +00004788 functions. The offsets here are relative to the 'PyHeapTypeObject'
Guido van Rossume5c691a2003-03-07 15:13:17 +00004789 structure, which incorporates the additional structures used for numbers,
4790 sequences and mappings.
4791 Note that multiple names may map to the same slot (e.g. __eq__,
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004792 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00004793 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4794 terminated with an all-zero entry. (This table is further initialized and
4795 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004796
Guido van Rossum6d204072001-10-21 00:44:31 +00004797typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004798
4799#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00004800#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004801#undef ETSLOT
4802#undef SQSLOT
4803#undef MPSLOT
4804#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00004805#undef UNSLOT
4806#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004807#undef BINSLOT
4808#undef RBINSLOT
4809
Guido van Rossum6d204072001-10-21 00:44:31 +00004810#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004811 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4812 PyDoc_STR(DOC)}
Guido van Rossumc8e56452001-10-22 00:43:43 +00004813#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4814 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004815 PyDoc_STR(DOC), FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00004816#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Guido van Rossume5c691a2003-03-07 15:13:17 +00004817 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004818 PyDoc_STR(DOC)}
Guido van Rossum6d204072001-10-21 00:44:31 +00004819#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4820 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
4821#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4822 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
4823#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4824 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
4825#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4826 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4827 "x." NAME "() <==> " DOC)
4828#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4829 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4830 "x." NAME "(y) <==> x" DOC "y")
4831#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
4832 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4833 "x." NAME "(y) <==> x" DOC "y")
4834#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
4835 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4836 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004837
4838static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00004839 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
4840 "x.__len__() <==> len(x)"),
4841 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
4842 "x.__add__(y) <==> x+y"),
4843 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
4844 "x.__mul__(n) <==> x*n"),
4845 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
4846 "x.__rmul__(n) <==> n*x"),
4847 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
4848 "x.__getitem__(y) <==> x[y]"),
4849 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
Brett Cannon154da9b2003-05-20 02:30:04 +00004850 "x.__getslice__(i, j) <==> x[i:j]\n\
4851 \n\
4852 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004853 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00004854 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004855 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00004856 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004857 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00004858 wrap_intintobjargproc,
Brett Cannonbe67d872003-05-20 02:40:12 +00004859 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
4860 \n\
4861 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004862 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
Brett Cannonbe67d872003-05-20 02:40:12 +00004863 "x.__delslice__(i, j) <==> del x[i:j]\n\
4864 \n\
4865 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004866 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
4867 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004868 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00004869 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004870 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00004871 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004872
Guido van Rossum6d204072001-10-21 00:44:31 +00004873 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
4874 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00004875 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004876 wrap_binaryfunc,
4877 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004878 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004879 wrap_objobjargproc,
4880 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004881 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004882 wrap_delitem,
4883 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004884
Guido van Rossum6d204072001-10-21 00:44:31 +00004885 BINSLOT("__add__", nb_add, slot_nb_add,
4886 "+"),
4887 RBINSLOT("__radd__", nb_add, slot_nb_add,
4888 "+"),
4889 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
4890 "-"),
4891 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
4892 "-"),
4893 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
4894 "*"),
4895 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
4896 "*"),
4897 BINSLOT("__div__", nb_divide, slot_nb_divide,
4898 "/"),
4899 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
4900 "/"),
4901 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
4902 "%"),
4903 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
4904 "%"),
4905 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
4906 "divmod(x, y)"),
4907 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
4908 "divmod(y, x)"),
4909 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
4910 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
4911 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
4912 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
4913 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
4914 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
4915 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
4916 "abs(x)"),
Guido van Rossumdfce3bf2002-03-10 14:11:16 +00004917 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry,
Guido van Rossum6d204072001-10-21 00:44:31 +00004918 "x != 0"),
4919 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
4920 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
4921 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
4922 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
4923 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
4924 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
4925 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
4926 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
4927 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
4928 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
4929 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
4930 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
4931 "x.__coerce__(y) <==> coerce(x, y)"),
4932 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
4933 "int(x)"),
4934 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
4935 "long(x)"),
4936 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
4937 "float(x)"),
4938 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
4939 "oct(x)"),
4940 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
4941 "hex(x)"),
4942 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
4943 wrap_binaryfunc, "+"),
4944 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
4945 wrap_binaryfunc, "-"),
4946 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
4947 wrap_binaryfunc, "*"),
4948 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
4949 wrap_binaryfunc, "/"),
4950 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
4951 wrap_binaryfunc, "%"),
4952 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
Guido van Rossum6e5680f2002-10-15 01:01:53 +00004953 wrap_binaryfunc, "**"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004954 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
4955 wrap_binaryfunc, "<<"),
4956 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
4957 wrap_binaryfunc, ">>"),
4958 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
4959 wrap_binaryfunc, "&"),
4960 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
4961 wrap_binaryfunc, "^"),
4962 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
4963 wrap_binaryfunc, "|"),
4964 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
4965 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
4966 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
4967 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
4968 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
4969 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
4970 IBSLOT("__itruediv__", nb_inplace_true_divide,
4971 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004972
Guido van Rossum6d204072001-10-21 00:44:31 +00004973 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
4974 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00004975 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00004976 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
4977 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00004978 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00004979 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
4980 "x.__cmp__(y) <==> cmp(x,y)"),
4981 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
4982 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00004983 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
4984 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004985 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00004986 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
4987 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
4988 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
4989 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
4990 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
4991 "x.__setattr__('name', value) <==> x.name = value"),
4992 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
4993 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
4994 "x.__delattr__('name') <==> del x.name"),
4995 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
4996 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
4997 "x.__lt__(y) <==> x<y"),
4998 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
4999 "x.__le__(y) <==> x<=y"),
5000 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
5001 "x.__eq__(y) <==> x==y"),
5002 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
5003 "x.__ne__(y) <==> x!=y"),
5004 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
5005 "x.__gt__(y) <==> x>y"),
5006 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
5007 "x.__ge__(y) <==> x>=y"),
5008 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
5009 "x.__iter__() <==> iter(x)"),
5010 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
5011 "x.next() -> the next value, or raise StopIteration"),
5012 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
5013 "descr.__get__(obj[, type]) -> value"),
5014 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
5015 "descr.__set__(obj, value)"),
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00005016 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
5017 wrap_descr_delete, "descr.__delete__(obj)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00005018 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00005019 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00005020 "see x.__class__.__doc__ for signature",
5021 PyWrapperFlag_KEYWORDS),
5022 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossumfebd61d2002-08-08 20:55:20 +00005023 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005024 {NULL}
5025};
5026
Guido van Rossumc334df52002-04-04 23:44:47 +00005027/* Given a type pointer and an offset gotten from a slotdef entry, return a
5028 pointer to the actual slot. This is not quite the same as simply adding
5029 the offset to the type pointer, since it takes care to indirect through the
5030 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5031 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005032static void **
5033slotptr(PyTypeObject *type, int offset)
5034{
5035 char *ptr;
5036
Guido van Rossume5c691a2003-03-07 15:13:17 +00005037 /* Note: this depends on the order of the members of PyHeapTypeObject! */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005038 assert(offset >= 0);
Guido van Rossume5c691a2003-03-07 15:13:17 +00005039 assert(offset < offsetof(PyHeapTypeObject, as_buffer));
5040 if (offset >= offsetof(PyHeapTypeObject, as_sequence)) {
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005041 ptr = (void *)type->tp_as_sequence;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005042 offset -= offsetof(PyHeapTypeObject, as_sequence);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005043 }
Guido van Rossume5c691a2003-03-07 15:13:17 +00005044 else if (offset >= offsetof(PyHeapTypeObject, as_mapping)) {
Guido van Rossum09638c12002-06-13 19:17:46 +00005045 ptr = (void *)type->tp_as_mapping;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005046 offset -= offsetof(PyHeapTypeObject, as_mapping);
Guido van Rossum09638c12002-06-13 19:17:46 +00005047 }
Guido van Rossume5c691a2003-03-07 15:13:17 +00005048 else if (offset >= offsetof(PyHeapTypeObject, as_number)) {
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005049 ptr = (void *)type->tp_as_number;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005050 offset -= offsetof(PyHeapTypeObject, as_number);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005051 }
5052 else {
5053 ptr = (void *)type;
5054 }
5055 if (ptr != NULL)
5056 ptr += offset;
5057 return (void **)ptr;
5058}
Guido van Rossumf040ede2001-08-07 16:40:56 +00005059
Guido van Rossumc334df52002-04-04 23:44:47 +00005060/* Length of array of slotdef pointers used to store slots with the
5061 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5062 the same __name__, for any __name__. Since that's a static property, it is
5063 appropriate to declare fixed-size arrays for this. */
5064#define MAX_EQUIV 10
5065
5066/* Return a slot pointer for a given name, but ONLY if the attribute has
5067 exactly one slot function. The name must be an interned string. */
5068static void **
5069resolve_slotdups(PyTypeObject *type, PyObject *name)
5070{
5071 /* XXX Maybe this could be optimized more -- but is it worth it? */
5072
5073 /* pname and ptrs act as a little cache */
5074 static PyObject *pname;
5075 static slotdef *ptrs[MAX_EQUIV];
5076 slotdef *p, **pp;
5077 void **res, **ptr;
5078
5079 if (pname != name) {
5080 /* Collect all slotdefs that match name into ptrs. */
5081 pname = name;
5082 pp = ptrs;
5083 for (p = slotdefs; p->name_strobj; p++) {
5084 if (p->name_strobj == name)
5085 *pp++ = p;
5086 }
5087 *pp = NULL;
5088 }
5089
5090 /* Look in all matching slots of the type; if exactly one of these has
5091 a filled-in slot, return its value. Otherwise return NULL. */
5092 res = NULL;
5093 for (pp = ptrs; *pp; pp++) {
5094 ptr = slotptr(type, (*pp)->offset);
5095 if (ptr == NULL || *ptr == NULL)
5096 continue;
5097 if (res != NULL)
5098 return NULL;
5099 res = ptr;
5100 }
5101 return res;
5102}
5103
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005104/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
Guido van Rossumc334df52002-04-04 23:44:47 +00005105 does some incredibly complex thinking and then sticks something into the
5106 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5107 interests, and then stores a generic wrapper or a specific function into
5108 the slot.) Return a pointer to the next slotdef with a different offset,
5109 because that's convenient for fixup_slot_dispatchers(). */
5110static slotdef *
5111update_one_slot(PyTypeObject *type, slotdef *p)
5112{
5113 PyObject *descr;
5114 PyWrapperDescrObject *d;
5115 void *generic = NULL, *specific = NULL;
5116 int use_generic = 0;
5117 int offset = p->offset;
5118 void **ptr = slotptr(type, offset);
5119
5120 if (ptr == NULL) {
5121 do {
5122 ++p;
5123 } while (p->offset == offset);
5124 return p;
5125 }
5126 do {
5127 descr = _PyType_Lookup(type, p->name_strobj);
5128 if (descr == NULL)
5129 continue;
5130 if (descr->ob_type == &PyWrapperDescr_Type) {
5131 void **tptr = resolve_slotdups(type, p->name_strobj);
5132 if (tptr == NULL || tptr == ptr)
5133 generic = p->function;
5134 d = (PyWrapperDescrObject *)descr;
5135 if (d->d_base->wrapper == p->wrapper &&
5136 PyType_IsSubtype(type, d->d_type))
5137 {
5138 if (specific == NULL ||
5139 specific == d->d_wrapped)
5140 specific = d->d_wrapped;
5141 else
5142 use_generic = 1;
5143 }
5144 }
Guido van Rossum721f62e2002-08-09 02:14:34 +00005145 else if (descr->ob_type == &PyCFunction_Type &&
5146 PyCFunction_GET_FUNCTION(descr) ==
5147 (PyCFunction)tp_new_wrapper &&
5148 strcmp(p->name, "__new__") == 0)
5149 {
5150 /* The __new__ wrapper is not a wrapper descriptor,
5151 so must be special-cased differently.
5152 If we don't do this, creating an instance will
5153 always use slot_tp_new which will look up
5154 __new__ in the MRO which will call tp_new_wrapper
5155 which will look through the base classes looking
5156 for a static base and call its tp_new (usually
5157 PyType_GenericNew), after performing various
5158 sanity checks and constructing a new argument
5159 list. Cut all that nonsense short -- this speeds
5160 up instance creation tremendously. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005161 specific = (void *)type->tp_new;
Guido van Rossum721f62e2002-08-09 02:14:34 +00005162 /* XXX I'm not 100% sure that there isn't a hole
5163 in this reasoning that requires additional
5164 sanity checks. I'll buy the first person to
5165 point out a bug in this reasoning a beer. */
5166 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005167 else {
5168 use_generic = 1;
5169 generic = p->function;
5170 }
5171 } while ((++p)->offset == offset);
5172 if (specific && !use_generic)
5173 *ptr = specific;
5174 else
5175 *ptr = generic;
5176 return p;
5177}
5178
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005179/* In the type, update the slots whose slotdefs are gathered in the pp array.
5180 This is a callback for update_subclasses(). */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005181static int
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005182update_slots_callback(PyTypeObject *type, void *data)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005183{
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005184 slotdef **pp = (slotdef **)data;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005185
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005186 for (; *pp; pp++)
Guido van Rossumc334df52002-04-04 23:44:47 +00005187 update_one_slot(type, *pp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005188 return 0;
5189}
5190
Guido van Rossumc334df52002-04-04 23:44:47 +00005191/* Comparison function for qsort() to compare slotdefs by their offset, and
5192 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005193static int
5194slotdef_cmp(const void *aa, const void *bb)
5195{
5196 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5197 int c = a->offset - b->offset;
5198 if (c != 0)
5199 return c;
5200 else
5201 return a - b;
5202}
5203
Guido van Rossumc334df52002-04-04 23:44:47 +00005204/* Initialize the slotdefs table by adding interned string objects for the
5205 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005206static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005207init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005208{
5209 slotdef *p;
5210 static int initialized = 0;
5211
5212 if (initialized)
5213 return;
5214 for (p = slotdefs; p->name; p++) {
5215 p->name_strobj = PyString_InternFromString(p->name);
5216 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00005217 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005218 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005219 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5220 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005221 initialized = 1;
5222}
5223
Guido van Rossumc334df52002-04-04 23:44:47 +00005224/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005225static int
5226update_slot(PyTypeObject *type, PyObject *name)
5227{
Guido van Rossumc334df52002-04-04 23:44:47 +00005228 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005229 slotdef *p;
5230 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005231 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005232
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005233 init_slotdefs();
5234 pp = ptrs;
5235 for (p = slotdefs; p->name; p++) {
5236 /* XXX assume name is interned! */
5237 if (p->name_strobj == name)
5238 *pp++ = p;
5239 }
5240 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005241 for (pp = ptrs; *pp; pp++) {
5242 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005243 offset = p->offset;
5244 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005245 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005246 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005247 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005248 if (ptrs[0] == NULL)
5249 return 0; /* Not an attribute that affects any slots */
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005250 return update_subclasses(type, name,
5251 update_slots_callback, (void *)ptrs);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005252}
5253
Guido van Rossumc334df52002-04-04 23:44:47 +00005254/* Store the proper functions in the slot dispatches at class (type)
5255 definition time, based upon which operations the class overrides in its
5256 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00005257static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005258fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005259{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005260 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005261
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005262 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00005263 for (p = slotdefs; p->name; )
5264 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005265}
Guido van Rossum705f0f52001-08-24 16:47:00 +00005266
Michael W. Hudson98bbc492002-11-26 14:47:27 +00005267static void
5268update_all_slots(PyTypeObject* type)
5269{
5270 slotdef *p;
5271
5272 init_slotdefs();
5273 for (p = slotdefs; p->name; p++) {
5274 /* update_slot returns int but can't actually fail */
5275 update_slot(type, p->name_strobj);
5276 }
5277}
5278
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005279/* recurse_down_subclasses() and update_subclasses() are mutually
5280 recursive functions to call a callback for all subclasses,
5281 but refraining from recursing into subclasses that define 'name'. */
5282
5283static int
5284update_subclasses(PyTypeObject *type, PyObject *name,
5285 update_callback callback, void *data)
5286{
5287 if (callback(type, data) < 0)
5288 return -1;
5289 return recurse_down_subclasses(type, name, callback, data);
5290}
5291
5292static int
5293recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5294 update_callback callback, void *data)
5295{
5296 PyTypeObject *subclass;
5297 PyObject *ref, *subclasses, *dict;
5298 int i, n;
5299
5300 subclasses = type->tp_subclasses;
5301 if (subclasses == NULL)
5302 return 0;
5303 assert(PyList_Check(subclasses));
5304 n = PyList_GET_SIZE(subclasses);
5305 for (i = 0; i < n; i++) {
5306 ref = PyList_GET_ITEM(subclasses, i);
5307 assert(PyWeakref_CheckRef(ref));
5308 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5309 assert(subclass != NULL);
5310 if ((PyObject *)subclass == Py_None)
5311 continue;
5312 assert(PyType_Check(subclass));
5313 /* Avoid recursing down into unaffected classes */
5314 dict = subclass->tp_dict;
5315 if (dict != NULL && PyDict_Check(dict) &&
5316 PyDict_GetItem(dict, name) != NULL)
5317 continue;
5318 if (update_subclasses(subclass, name, callback, data) < 0)
5319 return -1;
5320 }
5321 return 0;
5322}
5323
Guido van Rossum6d204072001-10-21 00:44:31 +00005324/* This function is called by PyType_Ready() to populate the type's
5325 dictionary with method descriptors for function slots. For each
Guido van Rossum09638c12002-06-13 19:17:46 +00005326 function slot (like tp_repr) that's defined in the type, one or more
5327 corresponding descriptors are added in the type's tp_dict dictionary
5328 under the appropriate name (like __repr__). Some function slots
5329 cause more than one descriptor to be added (for example, the nb_add
5330 slot adds both __add__ and __radd__ descriptors) and some function
5331 slots compete for the same descriptor (for example both sq_item and
5332 mp_subscript generate a __getitem__ descriptor).
5333
5334 In the latter case, the first slotdef entry encoutered wins. Since
Tim Petersbf9b2442003-03-23 05:35:36 +00005335 slotdef entries are sorted by the offset of the slot in the
Guido van Rossume5c691a2003-03-07 15:13:17 +00005336 PyHeapTypeObject, this gives us some control over disambiguating
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005337 between competing slots: the members of PyHeapTypeObject are listed
5338 from most general to least general, so the most general slot is
5339 preferred. In particular, because as_mapping comes before as_sequence,
5340 for a type that defines both mp_subscript and sq_item, mp_subscript
5341 wins.
Guido van Rossum09638c12002-06-13 19:17:46 +00005342
5343 This only adds new descriptors and doesn't overwrite entries in
5344 tp_dict that were previously defined. The descriptors contain a
5345 reference to the C function they must call, so that it's safe if they
5346 are copied into a subtype's __dict__ and the subtype has a different
5347 C function in its slot -- calling the method defined by the
5348 descriptor will call the C function that was used to create it,
5349 rather than the C function present in the slot when it is called.
5350 (This is important because a subtype may have a C function in the
5351 slot that calls the method from the dictionary, and we want to avoid
5352 infinite recursion here.) */
Guido van Rossum6d204072001-10-21 00:44:31 +00005353
5354static int
5355add_operators(PyTypeObject *type)
5356{
5357 PyObject *dict = type->tp_dict;
5358 slotdef *p;
5359 PyObject *descr;
5360 void **ptr;
5361
5362 init_slotdefs();
5363 for (p = slotdefs; p->name; p++) {
5364 if (p->wrapper == NULL)
5365 continue;
5366 ptr = slotptr(type, p->offset);
5367 if (!ptr || !*ptr)
5368 continue;
5369 if (PyDict_GetItem(dict, p->name_strobj))
5370 continue;
5371 descr = PyDescr_NewWrapper(type, p, *ptr);
5372 if (descr == NULL)
5373 return -1;
5374 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5375 return -1;
5376 Py_DECREF(descr);
5377 }
5378 if (type->tp_new != NULL) {
5379 if (add_tp_new_wrapper(type) < 0)
5380 return -1;
5381 }
5382 return 0;
5383}
5384
Guido van Rossum705f0f52001-08-24 16:47:00 +00005385
5386/* Cooperative 'super' */
5387
5388typedef struct {
5389 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00005390 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005391 PyObject *obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005392 PyTypeObject *obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005393} superobject;
5394
Guido van Rossum6f799372001-09-20 20:46:19 +00005395static PyMemberDef super_members[] = {
5396 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5397 "the class invoking super()"},
5398 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5399 "the instance invoking super(); may be None"},
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005400 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
5401 "the type of the the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005402 {0}
5403};
5404
Guido van Rossum705f0f52001-08-24 16:47:00 +00005405static void
5406super_dealloc(PyObject *self)
5407{
5408 superobject *su = (superobject *)self;
5409
Guido van Rossum048eb752001-10-02 21:24:57 +00005410 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005411 Py_XDECREF(su->obj);
5412 Py_XDECREF(su->type);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005413 Py_XDECREF(su->obj_type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005414 self->ob_type->tp_free(self);
5415}
5416
5417static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005418super_repr(PyObject *self)
5419{
5420 superobject *su = (superobject *)self;
5421
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005422 if (su->obj_type)
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005423 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005424 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005425 su->type ? su->type->tp_name : "NULL",
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005426 su->obj_type->tp_name);
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005427 else
5428 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005429 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005430 su->type ? su->type->tp_name : "NULL");
5431}
5432
5433static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00005434super_getattro(PyObject *self, PyObject *name)
5435{
5436 superobject *su = (superobject *)self;
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005437 int skip = su->obj_type == NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005438
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005439 if (!skip) {
5440 /* We want __class__ to return the class of the super object
5441 (i.e. super, or a subclass), not the class of su->obj. */
5442 skip = (PyString_Check(name) &&
5443 PyString_GET_SIZE(name) == 9 &&
5444 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5445 }
5446
5447 if (!skip) {
Tim Petersa91e9642001-11-14 23:32:33 +00005448 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005449 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005450 descrgetfunc f;
5451 int i, n;
5452
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005453 starttype = su->obj_type;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005454 mro = starttype->tp_mro;
5455
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005456 if (mro == NULL)
5457 n = 0;
5458 else {
5459 assert(PyTuple_Check(mro));
5460 n = PyTuple_GET_SIZE(mro);
5461 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005462 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00005463 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00005464 break;
5465 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005466 i++;
5467 res = NULL;
5468 for (; i < n; i++) {
5469 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00005470 if (PyType_Check(tmp))
5471 dict = ((PyTypeObject *)tmp)->tp_dict;
5472 else if (PyClass_Check(tmp))
5473 dict = ((PyClassObject *)tmp)->cl_dict;
5474 else
5475 continue;
5476 res = PyDict_GetItem(dict, name);
Guido van Rossum6cc5bb62003-04-16 20:01:36 +00005477 if (res != NULL) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00005478 Py_INCREF(res);
5479 f = res->ob_type->tp_descr_get;
5480 if (f != NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00005481 tmp = f(res, su->obj,
5482 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005483 Py_DECREF(res);
5484 res = tmp;
5485 }
5486 return res;
5487 }
5488 }
5489 }
5490 return PyObject_GenericGetAttr(self, name);
5491}
5492
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005493static PyTypeObject *
Guido van Rossum5b443c62001-12-03 15:38:28 +00005494supercheck(PyTypeObject *type, PyObject *obj)
5495{
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005496 /* Check that a super() call makes sense. Return a type object.
5497
5498 obj can be a new-style class, or an instance of one:
5499
5500 - If it is a class, it must be a subclass of 'type'. This case is
5501 used for class methods; the return value is obj.
5502
5503 - If it is an instance, it must be an instance of 'type'. This is
5504 the normal case; the return value is obj.__class__.
5505
5506 But... when obj is an instance, we want to allow for the case where
5507 obj->ob_type is not a subclass of type, but obj.__class__ is!
5508 This will allow using super() with a proxy for obj.
5509 */
5510
Guido van Rossum8e80a722003-02-18 19:22:22 +00005511 /* Check for first bullet above (special case) */
5512 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5513 Py_INCREF(obj);
5514 return (PyTypeObject *)obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005515 }
Guido van Rossum8e80a722003-02-18 19:22:22 +00005516
5517 /* Normal case */
5518 if (PyType_IsSubtype(obj->ob_type, type)) {
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005519 Py_INCREF(obj->ob_type);
5520 return obj->ob_type;
5521 }
5522 else {
5523 /* Try the slow way */
5524 static PyObject *class_str = NULL;
5525 PyObject *class_attr;
5526
5527 if (class_str == NULL) {
5528 class_str = PyString_FromString("__class__");
5529 if (class_str == NULL)
5530 return NULL;
5531 }
5532
5533 class_attr = PyObject_GetAttr(obj, class_str);
5534
5535 if (class_attr != NULL &&
5536 PyType_Check(class_attr) &&
5537 (PyTypeObject *)class_attr != obj->ob_type)
5538 {
5539 int ok = PyType_IsSubtype(
5540 (PyTypeObject *)class_attr, type);
5541 if (ok)
5542 return (PyTypeObject *)class_attr;
5543 }
5544
5545 if (class_attr == NULL)
5546 PyErr_Clear();
5547 else
5548 Py_DECREF(class_attr);
5549 }
5550
Tim Peters97e5ff52003-02-18 19:32:50 +00005551 PyErr_SetString(PyExc_TypeError,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005552 "super(type, obj): "
5553 "obj must be an instance or subtype of type");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005554 return NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00005555}
5556
Guido van Rossum705f0f52001-08-24 16:47:00 +00005557static PyObject *
5558super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5559{
5560 superobject *su = (superobject *)self;
5561 superobject *new;
5562
5563 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5564 /* Not binding to an object, or already bound */
5565 Py_INCREF(self);
5566 return self;
5567 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00005568 if (su->ob_type != &PySuper_Type)
Brett Cannon10147f72003-06-11 20:50:33 +00005569 /* If su is not an instance of a subclass of super,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005570 call its type */
5571 return PyObject_CallFunction((PyObject *)su->ob_type,
5572 "OO", su->type, obj);
5573 else {
5574 /* Inline the common case */
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005575 PyTypeObject *obj_type = supercheck(su->type, obj);
5576 if (obj_type == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00005577 return NULL;
5578 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
5579 NULL, NULL);
5580 if (new == NULL)
5581 return NULL;
5582 Py_INCREF(su->type);
5583 Py_INCREF(obj);
5584 new->type = su->type;
5585 new->obj = obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005586 new->obj_type = obj_type;
Guido van Rossum5b443c62001-12-03 15:38:28 +00005587 return (PyObject *)new;
5588 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005589}
5590
5591static int
5592super_init(PyObject *self, PyObject *args, PyObject *kwds)
5593{
5594 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00005595 PyTypeObject *type;
5596 PyObject *obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005597 PyTypeObject *obj_type = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005598
5599 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
5600 return -1;
5601 if (obj == Py_None)
5602 obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005603 if (obj != NULL) {
5604 obj_type = supercheck(type, obj);
5605 if (obj_type == NULL)
5606 return -1;
5607 Py_INCREF(obj);
5608 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005609 Py_INCREF(type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005610 su->type = type;
5611 su->obj = obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005612 su->obj_type = obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005613 return 0;
5614}
5615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005616PyDoc_STRVAR(super_doc,
Guido van Rossum705f0f52001-08-24 16:47:00 +00005617"super(type) -> unbound super object\n"
5618"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00005619"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00005620"Typical use to call a cooperative superclass method:\n"
5621"class C(B):\n"
5622" def meth(self, arg):\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005623" super(C, self).meth(arg)");
Guido van Rossum705f0f52001-08-24 16:47:00 +00005624
Guido van Rossum048eb752001-10-02 21:24:57 +00005625static int
5626super_traverse(PyObject *self, visitproc visit, void *arg)
5627{
5628 superobject *su = (superobject *)self;
5629 int err;
5630
5631#define VISIT(SLOT) \
5632 if (SLOT) { \
5633 err = visit((PyObject *)(SLOT), arg); \
5634 if (err) \
5635 return err; \
5636 }
5637
5638 VISIT(su->obj);
5639 VISIT(su->type);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005640 VISIT(su->obj_type);
Guido van Rossum048eb752001-10-02 21:24:57 +00005641
5642#undef VISIT
5643
5644 return 0;
5645}
5646
Guido van Rossum705f0f52001-08-24 16:47:00 +00005647PyTypeObject PySuper_Type = {
5648 PyObject_HEAD_INIT(&PyType_Type)
5649 0, /* ob_size */
5650 "super", /* tp_name */
5651 sizeof(superobject), /* tp_basicsize */
5652 0, /* tp_itemsize */
5653 /* methods */
5654 super_dealloc, /* tp_dealloc */
5655 0, /* tp_print */
5656 0, /* tp_getattr */
5657 0, /* tp_setattr */
5658 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005659 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005660 0, /* tp_as_number */
5661 0, /* tp_as_sequence */
5662 0, /* tp_as_mapping */
5663 0, /* tp_hash */
5664 0, /* tp_call */
5665 0, /* tp_str */
5666 super_getattro, /* tp_getattro */
5667 0, /* tp_setattro */
5668 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00005669 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
5670 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005671 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00005672 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005673 0, /* tp_clear */
5674 0, /* tp_richcompare */
5675 0, /* tp_weaklistoffset */
5676 0, /* tp_iter */
5677 0, /* tp_iternext */
5678 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005679 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005680 0, /* tp_getset */
5681 0, /* tp_base */
5682 0, /* tp_dict */
5683 super_descr_get, /* tp_descr_get */
5684 0, /* tp_descr_set */
5685 0, /* tp_dictoffset */
5686 super_init, /* tp_init */
5687 PyType_GenericAlloc, /* tp_alloc */
5688 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00005689 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005690};