blob: 1bef2088c9e31da2be42cc092bd7eab2bd45c76b [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Type object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00004#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum9923ffe2002-06-04 19:52:53 +00006#include <ctype.h>
7
Guido van Rossum6f799372001-09-20 20:46:19 +00008static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +00009 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
10 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
11 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
Guido van Rossum9676b222001-08-17 20:32:36 +000012 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000013 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
15 {"__dictoffset__", T_LONG,
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
Tim Peters6d6c1a32001-08-02 04:15:00 +000017 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
18 {0}
19};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossumc0b618a1997-05-02 03:12:38 +000021static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +000022type_name(PyTypeObject *type, void *context)
23{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000024 const char *s;
Guido van Rossumc3542212001-08-16 09:18:56 +000025
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +000026 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Guido van Rossume5c691a2003-03-07 15:13:17 +000027 PyHeapTypeObject* et = (PyHeapTypeObject*)type;
Tim Petersea7f75d2002-12-07 21:39:16 +000028
Georg Brandlc255c7b2006-02-20 22:27:28 +000029 Py_INCREF(et->ht_name);
30 return et->ht_name;
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +000031 }
32 else {
33 s = strrchr(type->tp_name, '.');
34 if (s == NULL)
35 s = type->tp_name;
36 else
37 s++;
38 return PyString_FromString(s);
39 }
Guido van Rossumc3542212001-08-16 09:18:56 +000040}
41
Michael W. Hudson98bbc492002-11-26 14:47:27 +000042static int
43type_set_name(PyTypeObject *type, PyObject *value, void *context)
44{
Guido van Rossume5c691a2003-03-07 15:13:17 +000045 PyHeapTypeObject* et;
Michael W. Hudson98bbc492002-11-26 14:47:27 +000046
47 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
48 PyErr_Format(PyExc_TypeError,
49 "can't set %s.__name__", type->tp_name);
50 return -1;
51 }
52 if (!value) {
53 PyErr_Format(PyExc_TypeError,
54 "can't delete %s.__name__", type->tp_name);
55 return -1;
56 }
57 if (!PyString_Check(value)) {
58 PyErr_Format(PyExc_TypeError,
59 "can only assign string to %s.__name__, not '%s'",
60 type->tp_name, value->ob_type->tp_name);
61 return -1;
62 }
Tim Petersea7f75d2002-12-07 21:39:16 +000063 if (strlen(PyString_AS_STRING(value))
Michael W. Hudson98bbc492002-11-26 14:47:27 +000064 != (size_t)PyString_GET_SIZE(value)) {
65 PyErr_Format(PyExc_ValueError,
66 "__name__ must not contain null bytes");
67 return -1;
68 }
69
Guido van Rossume5c691a2003-03-07 15:13:17 +000070 et = (PyHeapTypeObject*)type;
Michael W. Hudson98bbc492002-11-26 14:47:27 +000071
72 Py_INCREF(value);
73
Georg Brandlc255c7b2006-02-20 22:27:28 +000074 Py_DECREF(et->ht_name);
75 et->ht_name = value;
Michael W. Hudson98bbc492002-11-26 14:47:27 +000076
77 type->tp_name = PyString_AS_STRING(value);
78
79 return 0;
80}
81
Guido van Rossumc3542212001-08-16 09:18:56 +000082static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000083type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000084{
Guido van Rossumc3542212001-08-16 09:18:56 +000085 PyObject *mod;
86 char *s;
87
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +000088 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
89 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Anthony Baxter3ecdb252004-06-11 14:41:18 +000090 if (!mod) {
91 PyErr_Format(PyExc_AttributeError, "__module__");
92 return 0;
93 }
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +000094 Py_XINCREF(mod);
Guido van Rossumc3542212001-08-16 09:18:56 +000095 return mod;
96 }
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +000097 else {
98 s = strrchr(type->tp_name, '.');
99 if (s != NULL)
100 return PyString_FromStringAndSize(
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101 type->tp_name, (Py_ssize_t)(s - type->tp_name));
Michael W. Hudsonade8c8b22002-11-27 16:29:26 +0000102 return PyString_FromString("__builtin__");
103 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000104}
105
Guido van Rossum3926a632001-09-25 16:25:58 +0000106static int
107type_set_module(PyTypeObject *type, PyObject *value, void *context)
108{
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000109 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
Guido van Rossum3926a632001-09-25 16:25:58 +0000110 PyErr_Format(PyExc_TypeError,
111 "can't set %s.__module__", type->tp_name);
112 return -1;
113 }
114 if (!value) {
115 PyErr_Format(PyExc_TypeError,
116 "can't delete %s.__module__", type->tp_name);
117 return -1;
118 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000119
Guido van Rossum3926a632001-09-25 16:25:58 +0000120 return PyDict_SetItemString(type->tp_dict, "__module__", value);
121}
122
Tim Peters6d6c1a32001-08-02 04:15:00 +0000123static PyObject *
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000124type_get_bases(PyTypeObject *type, void *context)
125{
126 Py_INCREF(type->tp_bases);
127 return type->tp_bases;
128}
129
130static PyTypeObject *best_base(PyObject *);
131static int mro_internal(PyTypeObject *);
132static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
133static int add_subclass(PyTypeObject*, PyTypeObject*);
134static void remove_subclass(PyTypeObject *, PyTypeObject *);
135static void update_all_slots(PyTypeObject *);
136
Guido van Rossum8d24ee92003-03-24 23:49:49 +0000137typedef int (*update_callback)(PyTypeObject *, void *);
138static int update_subclasses(PyTypeObject *type, PyObject *name,
139 update_callback callback, void *data);
140static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
141 update_callback callback, void *data);
142
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000143static int
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000144mro_subclasses(PyTypeObject *type, PyObject* temp)
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000145{
146 PyTypeObject *subclass;
147 PyObject *ref, *subclasses, *old_mro;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000148 Py_ssize_t i, n;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000149
150 subclasses = type->tp_subclasses;
151 if (subclasses == NULL)
152 return 0;
153 assert(PyList_Check(subclasses));
154 n = PyList_GET_SIZE(subclasses);
155 for (i = 0; i < n; i++) {
156 ref = PyList_GET_ITEM(subclasses, i);
157 assert(PyWeakref_CheckRef(ref));
158 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
159 assert(subclass != NULL);
160 if ((PyObject *)subclass == Py_None)
161 continue;
162 assert(PyType_Check(subclass));
163 old_mro = subclass->tp_mro;
164 if (mro_internal(subclass) < 0) {
165 subclass->tp_mro = old_mro;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000166 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000167 }
168 else {
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000169 PyObject* tuple;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000170 tuple = PyTuple_Pack(2, subclass, old_mro);
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000171 Py_DECREF(old_mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000172 if (!tuple)
173 return -1;
174 if (PyList_Append(temp, tuple) < 0)
175 return -1;
Guido van Rossum19a02ba2003-04-15 22:09:45 +0000176 Py_DECREF(tuple);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000177 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000178 if (mro_subclasses(subclass, temp) < 0)
179 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000180 }
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000181 return 0;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000182}
183
184static int
185type_set_bases(PyTypeObject *type, PyObject *value, void *context)
186{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000187 Py_ssize_t i;
188 int r = 0;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000189 PyObject *ob, *temp;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000190 PyTypeObject *new_base, *old_base;
191 PyObject *old_bases, *old_mro;
192
193 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
194 PyErr_Format(PyExc_TypeError,
195 "can't set %s.__bases__", type->tp_name);
196 return -1;
197 }
198 if (!value) {
199 PyErr_Format(PyExc_TypeError,
200 "can't delete %s.__bases__", type->tp_name);
201 return -1;
202 }
203 if (!PyTuple_Check(value)) {
204 PyErr_Format(PyExc_TypeError,
205 "can only assign tuple to %s.__bases__, not %s",
206 type->tp_name, value->ob_type->tp_name);
207 return -1;
208 }
Guido van Rossum3bbc0ee2002-12-13 17:49:38 +0000209 if (PyTuple_GET_SIZE(value) == 0) {
210 PyErr_Format(PyExc_TypeError,
211 "can only assign non-empty tuple to %s.__bases__, not ()",
212 type->tp_name);
213 return -1;
214 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000215 for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
216 ob = PyTuple_GET_ITEM(value, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +0000217 if (!PyType_Check(ob)) {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000218 PyErr_Format(
219 PyExc_TypeError,
220 "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
221 type->tp_name, ob->ob_type->tp_name);
222 return -1;
223 }
Michael W. Hudsoncaf17be2002-11-27 10:24:44 +0000224 if (PyType_Check(ob)) {
225 if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
226 PyErr_SetString(PyExc_TypeError,
227 "a __bases__ item causes an inheritance cycle");
228 return -1;
229 }
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000230 }
231 }
232
233 new_base = best_base(value);
234
235 if (!new_base) {
236 return -1;
237 }
238
239 if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
240 return -1;
241
242 Py_INCREF(new_base);
243 Py_INCREF(value);
244
245 old_bases = type->tp_bases;
246 old_base = type->tp_base;
247 old_mro = type->tp_mro;
248
249 type->tp_bases = value;
250 type->tp_base = new_base;
251
252 if (mro_internal(type) < 0) {
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000253 goto bail;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000254 }
255
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000256 temp = PyList_New(0);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000257 if (!temp)
258 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000259
260 r = mro_subclasses(type, temp);
261
262 if (r < 0) {
263 for (i = 0; i < PyList_Size(temp); i++) {
264 PyTypeObject* cls;
265 PyObject* mro;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000266 PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
267 "", 2, 2, &cls, &mro);
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000268 Py_DECREF(cls->tp_mro);
269 cls->tp_mro = mro;
270 Py_INCREF(cls->tp_mro);
271 }
272 Py_DECREF(temp);
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000273 goto bail;
Michael W. Hudson586da8f2002-11-27 15:20:19 +0000274 }
275
276 Py_DECREF(temp);
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000277
278 /* any base that was in __bases__ but now isn't, we
Raymond Hettingera8285862002-12-14 17:17:56 +0000279 need to remove |type| from its tp_subclasses.
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000280 conversely, any class now in __bases__ that wasn't
Raymond Hettingera8285862002-12-14 17:17:56 +0000281 needs to have |type| added to its subclasses. */
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000282
283 /* for now, sod that: just remove from all old_bases,
284 add to all new_bases */
285
286 for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
287 ob = PyTuple_GET_ITEM(old_bases, i);
288 if (PyType_Check(ob)) {
289 remove_subclass(
290 (PyTypeObject*)ob, type);
291 }
292 }
293
294 for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
295 ob = PyTuple_GET_ITEM(value, i);
296 if (PyType_Check(ob)) {
297 if (add_subclass((PyTypeObject*)ob, type) < 0)
298 r = -1;
299 }
300 }
301
302 update_all_slots(type);
303
304 Py_DECREF(old_bases);
305 Py_DECREF(old_base);
306 Py_DECREF(old_mro);
307
308 return r;
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000309
310 bail:
Michael W. Hudsone723e452003-08-07 14:58:10 +0000311 Py_DECREF(type->tp_bases);
312 Py_DECREF(type->tp_base);
313 if (type->tp_mro != old_mro) {
314 Py_DECREF(type->tp_mro);
315 }
316
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000317 type->tp_bases = old_bases;
318 type->tp_base = old_base;
319 type->tp_mro = old_mro;
Tim Petersea7f75d2002-12-07 21:39:16 +0000320
Michael W. Hudson7e7c00d2002-11-27 15:40:09 +0000321 return -1;
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000322}
323
324static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000325type_dict(PyTypeObject *type, void *context)
326{
327 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328 Py_INCREF(Py_None);
329 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000330 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000331 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +0000332}
333
Tim Peters24008312002-03-17 18:56:20 +0000334static PyObject *
335type_get_doc(PyTypeObject *type, void *context)
336{
337 PyObject *result;
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000338 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Tim Peters24008312002-03-17 18:56:20 +0000339 return PyString_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +0000340 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000341 if (result == NULL) {
342 result = Py_None;
343 Py_INCREF(result);
344 }
345 else if (result->ob_type->tp_descr_get) {
Tim Peters2b858972002-04-18 04:12:28 +0000346 result = result->ob_type->tp_descr_get(result, NULL,
347 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000348 }
349 else {
350 Py_INCREF(result);
351 }
Tim Peters24008312002-03-17 18:56:20 +0000352 return result;
353}
354
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000355static PyGetSetDef type_getsets[] = {
Michael W. Hudson98bbc492002-11-26 14:47:27 +0000356 {"__name__", (getter)type_name, (setter)type_set_name, NULL},
357 {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000358 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000359 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000360 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000361 {0}
362};
363
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000365type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000367 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000368 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000369
370 mod = type_module(type, NULL);
371 if (mod == NULL)
372 PyErr_Clear();
373 else if (!PyString_Check(mod)) {
374 Py_DECREF(mod);
375 mod = NULL;
376 }
377 name = type_name(type, NULL);
378 if (name == NULL)
379 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000380
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000381 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
382 kind = "class";
383 else
384 kind = "type";
385
Barry Warsaw7ce36942001-08-24 18:34:26 +0000386 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000387 rtn = PyString_FromFormat("<%s '%s.%s'>",
388 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000389 PyString_AS_STRING(mod),
390 PyString_AS_STRING(name));
391 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000392 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000393 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000394
Guido van Rossumc3542212001-08-16 09:18:56 +0000395 Py_XDECREF(mod);
396 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000397 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398}
399
Tim Peters6d6c1a32001-08-02 04:15:00 +0000400static PyObject *
401type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
402{
403 PyObject *obj;
404
405 if (type->tp_new == NULL) {
406 PyErr_Format(PyExc_TypeError,
407 "cannot create '%.100s' instances",
408 type->tp_name);
409 return NULL;
410 }
411
Tim Peters3f996e72001-09-13 19:18:27 +0000412 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000413 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000414 /* Ugly exception: when the call was type(something),
415 don't call tp_init on the result. */
416 if (type == &PyType_Type &&
417 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
418 (kwds == NULL ||
419 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
420 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000421 /* If the returned object is not an instance of type,
422 it won't be initialized. */
423 if (!PyType_IsSubtype(obj->ob_type, type))
424 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000425 type = obj->ob_type;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000426 if (type->tp_init != NULL &&
Tim Peters6d6c1a32001-08-02 04:15:00 +0000427 type->tp_init(obj, args, kwds) < 0) {
428 Py_DECREF(obj);
429 obj = NULL;
430 }
431 }
432 return obj;
433}
434
435PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000436PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000437{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000438 PyObject *obj;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000439 const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
440 /* note that we need to add one, for the sentinel */
Tim Peters406fe3b2001-10-06 19:04:01 +0000441
442 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000443 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000444 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445 obj = (PyObject *)PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000446
Neil Schemenauerc806c882001-08-29 23:54:54 +0000447 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000448 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000449
Neil Schemenauerc806c882001-08-29 23:54:54 +0000450 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000451
Tim Peters6d6c1a32001-08-02 04:15:00 +0000452 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
453 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000454
Tim Peters6d6c1a32001-08-02 04:15:00 +0000455 if (type->tp_itemsize == 0)
456 PyObject_INIT(obj, type);
457 else
458 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000459
Tim Peters6d6c1a32001-08-02 04:15:00 +0000460 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000461 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000462 return obj;
463}
464
465PyObject *
466PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
467{
468 return type->tp_alloc(type, 0);
469}
470
Guido van Rossum9475a232001-10-05 20:51:39 +0000471/* Helpers for subtyping */
472
473static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000474traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
475{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000476 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000477 PyMemberDef *mp;
478
479 n = type->ob_size;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000480 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000481 for (i = 0; i < n; i++, mp++) {
482 if (mp->type == T_OBJECT_EX) {
483 char *addr = (char *)self + mp->offset;
484 PyObject *obj = *(PyObject **)addr;
485 if (obj != NULL) {
486 int err = visit(obj, arg);
487 if (err)
488 return err;
489 }
490 }
491 }
492 return 0;
493}
494
495static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000496subtype_traverse(PyObject *self, visitproc visit, void *arg)
497{
498 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000499 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000500
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000501 /* Find the nearest base with a different tp_traverse,
502 and traverse slots while we're at it */
Guido van Rossum9475a232001-10-05 20:51:39 +0000503 type = self->ob_type;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000504 base = type;
505 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
506 if (base->ob_size) {
507 int err = traverse_slots(base, self, visit, arg);
508 if (err)
509 return err;
510 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000511 base = base->tp_base;
512 assert(base);
513 }
514
515 if (type->tp_dictoffset != base->tp_dictoffset) {
516 PyObject **dictptr = _PyObject_GetDictPtr(self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517 if (dictptr && *dictptr)
518 Py_VISIT(*dictptr);
Guido van Rossum9475a232001-10-05 20:51:39 +0000519 }
520
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000521 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
Guido van Rossuma3862092002-06-10 15:24:42 +0000522 /* For a heaptype, the instances count as references
523 to the type. Traverse the type so the collector
524 can find cycles involving this link. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000525 Py_VISIT(type);
Guido van Rossuma3862092002-06-10 15:24:42 +0000526
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000527 if (basetraverse)
528 return basetraverse(self, visit, arg);
529 return 0;
530}
531
532static void
533clear_slots(PyTypeObject *type, PyObject *self)
534{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000535 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000536 PyMemberDef *mp;
537
538 n = type->ob_size;
Guido van Rossume5c691a2003-03-07 15:13:17 +0000539 mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000540 for (i = 0; i < n; i++, mp++) {
541 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
542 char *addr = (char *)self + mp->offset;
543 PyObject *obj = *(PyObject **)addr;
544 if (obj != NULL) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000545 *(PyObject **)addr = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546 Py_DECREF(obj);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000547 }
548 }
549 }
550}
551
552static int
553subtype_clear(PyObject *self)
554{
555 PyTypeObject *type, *base;
556 inquiry baseclear;
557
558 /* Find the nearest base with a different tp_clear
559 and clear slots while we're at it */
560 type = self->ob_type;
561 base = type;
562 while ((baseclear = base->tp_clear) == subtype_clear) {
563 if (base->ob_size)
564 clear_slots(base, self);
565 base = base->tp_base;
566 assert(base);
567 }
568
Guido van Rossuma3862092002-06-10 15:24:42 +0000569 /* There's no need to clear the instance dict (if any);
570 the collector will call its tp_clear handler. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000571
572 if (baseclear)
573 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000574 return 0;
575}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000576
577static void
578subtype_dealloc(PyObject *self)
579{
Guido van Rossum14227b42001-12-06 02:35:58 +0000580 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000581 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000582
Guido van Rossum22b13872002-08-06 21:41:44 +0000583 /* Extract the type; we expect it to be a heap type */
584 type = self->ob_type;
585 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000586
Guido van Rossum22b13872002-08-06 21:41:44 +0000587 /* Test whether the type has GC exactly once */
588
589 if (!PyType_IS_GC(type)) {
590 /* It's really rare to find a dynamic type that doesn't have
591 GC; it can only happen when deriving from 'object' and not
592 adding any slots or instance variables. This allows
593 certain simplifications: there's no need to call
594 clear_slots(), or DECREF the dict, or clear weakrefs. */
595
596 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000597 if (type->tp_del) {
598 type->tp_del(self);
599 if (self->ob_refcnt > 0)
600 return;
601 }
Guido van Rossum22b13872002-08-06 21:41:44 +0000602
603 /* Find the nearest base with a different tp_dealloc */
604 base = type;
605 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
606 assert(base->ob_size == 0);
607 base = base->tp_base;
608 assert(base);
609 }
610
611 /* Call the base tp_dealloc() */
612 assert(basedealloc);
613 basedealloc(self);
614
615 /* Can't reference self beyond this point */
616 Py_DECREF(type);
617
618 /* Done */
619 return;
620 }
621
622 /* We get here only if the type has GC */
623
624 /* UnTrack and re-Track around the trashcan macro, alas */
Andrew M. Kuchlingc9172d32003-02-06 15:22:49 +0000625 /* See explanation at end of function for full disclosure */
Guido van Rossum0906e072002-08-07 20:42:09 +0000626 PyObject_GC_UnTrack(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000627 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000628 Py_TRASHCAN_SAFE_BEGIN(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000629 --_PyTrash_delete_nesting;
Tim Petersf7f9e992003-11-13 21:59:32 +0000630 /* DO NOT restore GC tracking at this point. weakref callbacks
631 * (if any, and whether directly here or indirectly in something we
632 * call) may trigger GC, and if self is tracked at that point, it
633 * will look like trash to GC and GC will try to delete self again.
Tim Petersadd09b42003-11-12 20:43:28 +0000634 */
Guido van Rossum22b13872002-08-06 21:41:44 +0000635
Guido van Rossum59195fd2003-06-13 20:54:40 +0000636 /* Find the nearest base with a different tp_dealloc */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000637 base = type;
638 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639 base = base->tp_base;
640 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000641 }
642
Guido van Rossum1987c662003-05-29 14:29:23 +0000643 /* If we added a weaklist, we clear it. Do this *before* calling
Guido van Rossum59195fd2003-06-13 20:54:40 +0000644 the finalizer (__del__), clearing slots, or clearing the instance
645 dict. */
646
Guido van Rossum1987c662003-05-29 14:29:23 +0000647 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
648 PyObject_ClearWeakRefs(self);
649
650 /* Maybe call finalizer; exit early if resurrected */
651 if (type->tp_del) {
Tim Petersf7f9e992003-11-13 21:59:32 +0000652 _PyObject_GC_TRACK(self);
Guido van Rossum1987c662003-05-29 14:29:23 +0000653 type->tp_del(self);
654 if (self->ob_refcnt > 0)
Tim Petersf7f9e992003-11-13 21:59:32 +0000655 goto endlabel; /* resurrected */
656 else
657 _PyObject_GC_UNTRACK(self);
Guido van Rossum1987c662003-05-29 14:29:23 +0000658 }
659
Guido van Rossum59195fd2003-06-13 20:54:40 +0000660 /* Clear slots up to the nearest base with a different tp_dealloc */
661 base = type;
662 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
663 if (base->ob_size)
664 clear_slots(base, self);
665 base = base->tp_base;
666 assert(base);
667 }
668
Tim Peters6d6c1a32001-08-02 04:15:00 +0000669 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000670 if (type->tp_dictoffset && !base->tp_dictoffset) {
671 PyObject **dictptr = _PyObject_GetDictPtr(self);
672 if (dictptr != NULL) {
673 PyObject *dict = *dictptr;
674 if (dict != NULL) {
675 Py_DECREF(dict);
676 *dictptr = NULL;
677 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000678 }
679 }
680
Tim Peters0bd743c2003-11-13 22:50:00 +0000681 /* Call the base tp_dealloc(); first retrack self if
682 * basedealloc knows about gc.
683 */
684 if (PyType_IS_GC(base))
685 _PyObject_GC_TRACK(self);
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000686 assert(basedealloc);
687 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000688
689 /* Can't reference self beyond this point */
Guido van Rossum22b13872002-08-06 21:41:44 +0000690 Py_DECREF(type);
691
Guido van Rossum0906e072002-08-07 20:42:09 +0000692 endlabel:
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000693 ++_PyTrash_delete_nesting;
Guido van Rossum22b13872002-08-06 21:41:44 +0000694 Py_TRASHCAN_SAFE_END(self);
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000695 --_PyTrash_delete_nesting;
696
697 /* Explanation of the weirdness around the trashcan macros:
698
699 Q. What do the trashcan macros do?
700
701 A. Read the comment titled "Trashcan mechanism" in object.h.
702 For one, this explains why there must be a call to GC-untrack
703 before the trashcan begin macro. Without understanding the
704 trashcan code, the answers to the following questions don't make
705 sense.
706
707 Q. Why do we GC-untrack before the trashcan and then immediately
708 GC-track again afterward?
709
710 A. In the case that the base class is GC-aware, the base class
711 probably GC-untracks the object. If it does that using the
712 UNTRACK macro, this will crash when the object is already
713 untracked. Because we don't know what the base class does, the
714 only safe thing is to make sure the object is tracked when we
715 call the base class dealloc. But... The trashcan begin macro
716 requires that the object is *untracked* before it is called. So
717 the dance becomes:
718
719 GC untrack
720 trashcan begin
721 GC track
722
Tim Petersf7f9e992003-11-13 21:59:32 +0000723 Q. Why did the last question say "immediately GC-track again"?
724 It's nowhere near immediately.
725
726 A. Because the code *used* to re-track immediately. Bad Idea.
727 self has a refcount of 0, and if gc ever gets its hands on it
728 (which can happen if any weakref callback gets invoked), it
729 looks like trash to gc too, and gc also tries to delete self
730 then. But we're already deleting self. Double dealloction is
731 a subtle disaster.
732
Guido van Rossumce8bcd82003-02-05 22:39:45 +0000733 Q. Why the bizarre (net-zero) manipulation of
734 _PyTrash_delete_nesting around the trashcan macros?
735
736 A. Some base classes (e.g. list) also use the trashcan mechanism.
737 The following scenario used to be possible:
738
739 - suppose the trashcan level is one below the trashcan limit
740
741 - subtype_dealloc() is called
742
743 - the trashcan limit is not yet reached, so the trashcan level
744 is incremented and the code between trashcan begin and end is
745 executed
746
747 - this destroys much of the object's contents, including its
748 slots and __dict__
749
750 - basedealloc() is called; this is really list_dealloc(), or
751 some other type which also uses the trashcan macros
752
753 - the trashcan limit is now reached, so the object is put on the
754 trashcan's to-be-deleted-later list
755
756 - basedealloc() returns
757
758 - subtype_dealloc() decrefs the object's type
759
760 - subtype_dealloc() returns
761
762 - later, the trashcan code starts deleting the objects from its
763 to-be-deleted-later list
764
765 - subtype_dealloc() is called *AGAIN* for the same object
766
767 - at the very least (if the destroyed slots and __dict__ don't
768 cause problems) the object's type gets decref'ed a second
769 time, which is *BAD*!!!
770
771 The remedy is to make sure that if the code between trashcan
772 begin and end in subtype_dealloc() is called, the code between
773 trashcan begin and end in basedealloc() will also be called.
774 This is done by decrementing the level after passing into the
775 trashcan block, and incrementing it just before leaving the
776 block.
777
778 But now it's possible that a chain of objects consisting solely
779 of objects whose deallocator is subtype_dealloc() will defeat
780 the trashcan mechanism completely: the decremented level means
781 that the effective level never reaches the limit. Therefore, we
782 *increment* the level *before* entering the trashcan block, and
783 matchingly decrement it after leaving. This means the trashcan
784 code will trigger a little early, but that's no big deal.
785
786 Q. Are there any live examples of code in need of all this
787 complexity?
788
789 A. Yes. See SF bug 668433 for code that crashed (when Python was
790 compiled in debug mode) before the trashcan level manipulations
791 were added. For more discussion, see SF patches 581742, 575073
792 and bug 574207.
793 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000794}
795
Jeremy Hylton938ace62002-07-17 16:30:39 +0000796static PyTypeObject *solid_base(PyTypeObject *type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000797
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798/* type test with subclassing support */
799
800int
801PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
802{
803 PyObject *mro;
804
805 mro = a->tp_mro;
806 if (mro != NULL) {
807 /* Deal with multiple inheritance without recursion
808 by walking the MRO tuple */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000809 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810 assert(PyTuple_Check(mro));
811 n = PyTuple_GET_SIZE(mro);
812 for (i = 0; i < n; i++) {
813 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
814 return 1;
815 }
816 return 0;
817 }
818 else {
819 /* a is not completely initilized yet; follow tp_base */
820 do {
821 if (a == b)
822 return 1;
823 a = a->tp_base;
824 } while (a != NULL);
825 return b == &PyBaseObject_Type;
826 }
827}
828
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000829/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000830 without looking in the instance dictionary
831 (so we can't use PyObject_GetAttr) but still binding
832 it to the instance. The arguments are the object,
833 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000834 static variable used to cache the interned Python string.
835
836 Two variants:
837
838 - lookup_maybe() returns NULL without raising an exception
839 when the _PyType_Lookup() call fails;
840
841 - lookup_method() always raises an exception upon errors.
842*/
Guido van Rossum60718732001-08-28 17:47:51 +0000843
844static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000845lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000846{
847 PyObject *res;
848
849 if (*attrobj == NULL) {
850 *attrobj = PyString_InternFromString(attrstr);
851 if (*attrobj == NULL)
852 return NULL;
853 }
854 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000855 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000856 descrgetfunc f;
857 if ((f = res->ob_type->tp_descr_get) == NULL)
858 Py_INCREF(res);
859 else
860 res = f(res, self, (PyObject *)(self->ob_type));
861 }
862 return res;
863}
864
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000865static PyObject *
866lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
867{
868 PyObject *res = lookup_maybe(self, attrstr, attrobj);
869 if (res == NULL && !PyErr_Occurred())
870 PyErr_SetObject(PyExc_AttributeError, *attrobj);
871 return res;
872}
873
Guido van Rossum2730b132001-08-28 18:22:14 +0000874/* A variation of PyObject_CallMethod that uses lookup_method()
875 instead of PyObject_GetAttrString(). This uses the same convention
876 as lookup_method to cache the interned name string object. */
877
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000878static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000879call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
880{
881 va_list va;
882 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000883 va_start(va, format);
884
Guido van Rossumda21c012001-10-03 00:50:18 +0000885 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000886 if (func == NULL) {
887 va_end(va);
888 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000889 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000890 return NULL;
891 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000892
893 if (format && *format)
894 args = Py_VaBuildValue(format, va);
895 else
896 args = PyTuple_New(0);
897
898 va_end(va);
899
900 if (args == NULL)
901 return NULL;
902
903 assert(PyTuple_Check(args));
904 retval = PyObject_Call(func, args, NULL);
905
906 Py_DECREF(args);
907 Py_DECREF(func);
908
909 return retval;
910}
911
912/* Clone of call_method() that returns NotImplemented when the lookup fails. */
913
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000914static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000915call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
916{
917 va_list va;
918 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000919 va_start(va, format);
920
Guido van Rossumda21c012001-10-03 00:50:18 +0000921 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000922 if (func == NULL) {
923 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000924 if (!PyErr_Occurred()) {
925 Py_INCREF(Py_NotImplemented);
926 return Py_NotImplemented;
927 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000928 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000929 }
930
931 if (format && *format)
932 args = Py_VaBuildValue(format, va);
933 else
934 args = PyTuple_New(0);
935
936 va_end(va);
937
Guido van Rossum717ce002001-09-14 16:58:08 +0000938 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000939 return NULL;
940
Guido van Rossum717ce002001-09-14 16:58:08 +0000941 assert(PyTuple_Check(args));
942 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000943
944 Py_DECREF(args);
945 Py_DECREF(func);
946
947 return retval;
948}
949
Tim Petersea7f75d2002-12-07 21:39:16 +0000950/*
Guido van Rossum1f121312002-11-14 19:49:16 +0000951 Method resolution order algorithm C3 described in
952 "A Monotonic Superclass Linearization for Dylan",
953 by Kim Barrett, Bob Cassel, Paul Haahr,
Tim Petersea7f75d2002-12-07 21:39:16 +0000954 David A. Moon, Keith Playford, and P. Tucker Withington.
Guido van Rossum1f121312002-11-14 19:49:16 +0000955 (OOPSLA 1996)
956
Guido van Rossum98f33732002-11-25 21:36:54 +0000957 Some notes about the rules implied by C3:
958
Tim Petersea7f75d2002-12-07 21:39:16 +0000959 No duplicate bases.
Guido van Rossum98f33732002-11-25 21:36:54 +0000960 It isn't legal to repeat a class in a list of base classes.
961
962 The next three properties are the 3 constraints in "C3".
963
Tim Petersea7f75d2002-12-07 21:39:16 +0000964 Local precendece order.
Guido van Rossum98f33732002-11-25 21:36:54 +0000965 If A precedes B in C's MRO, then A will precede B in the MRO of all
966 subclasses of C.
967
968 Monotonicity.
969 The MRO of a class must be an extension without reordering of the
970 MRO of each of its superclasses.
971
972 Extended Precedence Graph (EPG).
973 Linearization is consistent if there is a path in the EPG from
974 each class to all its successors in the linearization. See
975 the paper for definition of EPG.
Guido van Rossum1f121312002-11-14 19:49:16 +0000976 */
977
Tim Petersea7f75d2002-12-07 21:39:16 +0000978static int
Guido van Rossum1f121312002-11-14 19:49:16 +0000979tail_contains(PyObject *list, int whence, PyObject *o) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000980 Py_ssize_t j, size;
Guido van Rossum1f121312002-11-14 19:49:16 +0000981 size = PyList_GET_SIZE(list);
982
983 for (j = whence+1; j < size; j++) {
984 if (PyList_GET_ITEM(list, j) == o)
985 return 1;
986 }
987 return 0;
988}
989
Guido van Rossum98f33732002-11-25 21:36:54 +0000990static PyObject *
991class_name(PyObject *cls)
992{
993 PyObject *name = PyObject_GetAttrString(cls, "__name__");
994 if (name == NULL) {
995 PyErr_Clear();
996 Py_XDECREF(name);
997 name = PyObject_Repr(cls);
998 }
999 if (name == NULL)
1000 return NULL;
1001 if (!PyString_Check(name)) {
1002 Py_DECREF(name);
1003 return NULL;
1004 }
1005 return name;
1006}
1007
1008static int
1009check_duplicates(PyObject *list)
1010{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001011 Py_ssize_t i, j, n;
Guido van Rossum98f33732002-11-25 21:36:54 +00001012 /* Let's use a quadratic time algorithm,
1013 assuming that the bases lists is short.
1014 */
1015 n = PyList_GET_SIZE(list);
1016 for (i = 0; i < n; i++) {
1017 PyObject *o = PyList_GET_ITEM(list, i);
1018 for (j = i + 1; j < n; j++) {
1019 if (PyList_GET_ITEM(list, j) == o) {
1020 o = class_name(o);
1021 PyErr_Format(PyExc_TypeError,
1022 "duplicate base class %s",
1023 o ? PyString_AS_STRING(o) : "?");
1024 Py_XDECREF(o);
1025 return -1;
1026 }
1027 }
1028 }
1029 return 0;
1030}
1031
1032/* Raise a TypeError for an MRO order disagreement.
1033
1034 It's hard to produce a good error message. In the absence of better
1035 insight into error reporting, report the classes that were candidates
1036 to be put next into the MRO. There is some conflict between the
1037 order in which they should be put in the MRO, but it's hard to
1038 diagnose what constraint can't be satisfied.
1039*/
1040
1041static void
1042set_mro_error(PyObject *to_merge, int *remain)
1043{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001044 Py_ssize_t i, n, off, to_merge_size;
Guido van Rossum98f33732002-11-25 21:36:54 +00001045 char buf[1000];
1046 PyObject *k, *v;
1047 PyObject *set = PyDict_New();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001048 if (!set) return;
Guido van Rossum98f33732002-11-25 21:36:54 +00001049
1050 to_merge_size = PyList_GET_SIZE(to_merge);
1051 for (i = 0; i < to_merge_size; i++) {
1052 PyObject *L = PyList_GET_ITEM(to_merge, i);
1053 if (remain[i] < PyList_GET_SIZE(L)) {
1054 PyObject *c = PyList_GET_ITEM(L, remain[i]);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055 if (PyDict_SetItem(set, c, Py_None) < 0) {
1056 Py_DECREF(set);
Guido van Rossum98f33732002-11-25 21:36:54 +00001057 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001058 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001059 }
1060 }
1061 n = PyDict_Size(set);
1062
Raymond Hettingerf394df42003-04-06 19:13:41 +00001063 off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1064consistent method resolution\norder (MRO) for bases");
Guido van Rossum98f33732002-11-25 21:36:54 +00001065 i = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001066 while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001067 PyObject *name = class_name(k);
1068 off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1069 name ? PyString_AS_STRING(name) : "?");
1070 Py_XDECREF(name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001071 if (--n && (size_t)(off+1) < sizeof(buf)) {
Guido van Rossum98f33732002-11-25 21:36:54 +00001072 buf[off++] = ',';
1073 buf[off] = '\0';
1074 }
1075 }
1076 PyErr_SetString(PyExc_TypeError, buf);
1077 Py_DECREF(set);
1078}
1079
Tim Petersea7f75d2002-12-07 21:39:16 +00001080static int
Guido van Rossum1f121312002-11-14 19:49:16 +00001081pmerge(PyObject *acc, PyObject* to_merge) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001082 Py_ssize_t i, j, to_merge_size, empty_cnt;
Guido van Rossum1f121312002-11-14 19:49:16 +00001083 int *remain;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001084 int ok;
Tim Petersea7f75d2002-12-07 21:39:16 +00001085
Guido van Rossum1f121312002-11-14 19:49:16 +00001086 to_merge_size = PyList_GET_SIZE(to_merge);
1087
Guido van Rossum98f33732002-11-25 21:36:54 +00001088 /* remain stores an index into each sublist of to_merge.
1089 remain[i] is the index of the next base in to_merge[i]
1090 that is not included in acc.
1091 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092 remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
Guido van Rossum1f121312002-11-14 19:49:16 +00001093 if (remain == NULL)
1094 return -1;
1095 for (i = 0; i < to_merge_size; i++)
1096 remain[i] = 0;
1097
1098 again:
1099 empty_cnt = 0;
1100 for (i = 0; i < to_merge_size; i++) {
1101 PyObject *candidate;
Tim Petersea7f75d2002-12-07 21:39:16 +00001102
Guido van Rossum1f121312002-11-14 19:49:16 +00001103 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1104
1105 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1106 empty_cnt++;
1107 continue;
1108 }
1109
Guido van Rossum98f33732002-11-25 21:36:54 +00001110 /* Choose next candidate for MRO.
1111
1112 The input sequences alone can determine the choice.
1113 If not, choose the class which appears in the MRO
1114 of the earliest direct superclass of the new class.
1115 */
1116
Guido van Rossum1f121312002-11-14 19:49:16 +00001117 candidate = PyList_GET_ITEM(cur_list, remain[i]);
1118 for (j = 0; j < to_merge_size; j++) {
1119 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum98f33732002-11-25 21:36:54 +00001120 if (tail_contains(j_lst, remain[j], candidate)) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001121 goto skip; /* continue outer loop */
Guido van Rossum98f33732002-11-25 21:36:54 +00001122 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001123 }
1124 ok = PyList_Append(acc, candidate);
1125 if (ok < 0) {
1126 PyMem_Free(remain);
1127 return -1;
1128 }
1129 for (j = 0; j < to_merge_size; j++) {
1130 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
Guido van Rossum768158c2002-12-31 16:33:01 +00001131 if (remain[j] < PyList_GET_SIZE(j_lst) &&
1132 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001133 remain[j]++;
1134 }
1135 }
1136 goto again;
Tim Peters9a6b8d82002-11-14 23:22:33 +00001137 skip: ;
Guido van Rossum1f121312002-11-14 19:49:16 +00001138 }
1139
Guido van Rossum98f33732002-11-25 21:36:54 +00001140 if (empty_cnt == to_merge_size) {
1141 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001142 return 0;
Guido van Rossum98f33732002-11-25 21:36:54 +00001143 }
1144 set_mro_error(to_merge, remain);
1145 PyMem_FREE(remain);
Guido van Rossum1f121312002-11-14 19:49:16 +00001146 return -1;
1147}
1148
Tim Peters6d6c1a32001-08-02 04:15:00 +00001149static PyObject *
1150mro_implementation(PyTypeObject *type)
1151{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001152 Py_ssize_t i, n;
1153 int ok;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001154 PyObject *bases, *result;
Guido van Rossum1f121312002-11-14 19:49:16 +00001155 PyObject *to_merge, *bases_aslist;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001156
Guido van Rossum63517572002-06-18 16:44:57 +00001157 if(type->tp_dict == NULL) {
1158 if(PyType_Ready(type) < 0)
1159 return NULL;
1160 }
1161
Guido van Rossum98f33732002-11-25 21:36:54 +00001162 /* Find a superclass linearization that honors the constraints
1163 of the explicit lists of bases and the constraints implied by
Tim Petersea7f75d2002-12-07 21:39:16 +00001164 each base class.
Guido van Rossum98f33732002-11-25 21:36:54 +00001165
1166 to_merge is a list of lists, where each list is a superclass
1167 linearization implied by a base class. The last element of
1168 to_merge is the declared list of bases.
1169 */
1170
Tim Peters6d6c1a32001-08-02 04:15:00 +00001171 bases = type->tp_bases;
1172 n = PyTuple_GET_SIZE(bases);
Guido van Rossum1f121312002-11-14 19:49:16 +00001173
1174 to_merge = PyList_New(n+1);
1175 if (to_merge == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001176 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001177
Tim Peters6d6c1a32001-08-02 04:15:00 +00001178 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001179 PyObject *base = PyTuple_GET_ITEM(bases, i);
1180 PyObject *parentMRO;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001181 parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001182 if (parentMRO == NULL) {
Guido van Rossum1f121312002-11-14 19:49:16 +00001183 Py_DECREF(to_merge);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001184 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +00001185 }
1186
1187 PyList_SET_ITEM(to_merge, i, parentMRO);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001188 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001189
1190 bases_aslist = PySequence_List(bases);
1191 if (bases_aslist == NULL) {
1192 Py_DECREF(to_merge);
1193 return NULL;
1194 }
Guido van Rossum98f33732002-11-25 21:36:54 +00001195 /* This is just a basic sanity check. */
1196 if (check_duplicates(bases_aslist) < 0) {
1197 Py_DECREF(to_merge);
1198 Py_DECREF(bases_aslist);
1199 return NULL;
1200 }
Guido van Rossum1f121312002-11-14 19:49:16 +00001201 PyList_SET_ITEM(to_merge, n, bases_aslist);
1202
1203 result = Py_BuildValue("[O]", (PyObject *)type);
1204 if (result == NULL) {
1205 Py_DECREF(to_merge);
1206 return NULL;
1207 }
1208
1209 ok = pmerge(result, to_merge);
1210 Py_DECREF(to_merge);
1211 if (ok < 0) {
1212 Py_DECREF(result);
1213 return NULL;
1214 }
1215
Tim Peters6d6c1a32001-08-02 04:15:00 +00001216 return result;
1217}
1218
1219static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001220mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001221{
1222 PyTypeObject *type = (PyTypeObject *)self;
1223
Tim Peters6d6c1a32001-08-02 04:15:00 +00001224 return mro_implementation(type);
1225}
1226
1227static int
1228mro_internal(PyTypeObject *type)
1229{
1230 PyObject *mro, *result, *tuple;
Armin Rigo037d1e02005-12-29 17:07:39 +00001231 int checkit = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001232
1233 if (type->ob_type == &PyType_Type) {
1234 result = mro_implementation(type);
1235 }
1236 else {
Guido van Rossum60718732001-08-28 17:47:51 +00001237 static PyObject *mro_str;
Armin Rigo037d1e02005-12-29 17:07:39 +00001238 checkit = 1;
Guido van Rossum60718732001-08-28 17:47:51 +00001239 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001240 if (mro == NULL)
1241 return -1;
1242 result = PyObject_CallObject(mro, NULL);
1243 Py_DECREF(mro);
1244 }
1245 if (result == NULL)
1246 return -1;
1247 tuple = PySequence_Tuple(result);
1248 Py_DECREF(result);
Armin Rigo037d1e02005-12-29 17:07:39 +00001249 if (tuple == NULL)
1250 return -1;
1251 if (checkit) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001252 Py_ssize_t i, len;
Armin Rigo037d1e02005-12-29 17:07:39 +00001253 PyObject *cls;
1254 PyTypeObject *solid;
1255
1256 solid = solid_base(type);
1257
1258 len = PyTuple_GET_SIZE(tuple);
1259
1260 for (i = 0; i < len; i++) {
1261 PyTypeObject *t;
1262 cls = PyTuple_GET_ITEM(tuple, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001263 if (!PyType_Check(cls)) {
Armin Rigo037d1e02005-12-29 17:07:39 +00001264 PyErr_Format(PyExc_TypeError,
1265 "mro() returned a non-class ('%.500s')",
1266 cls->ob_type->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001267 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001268 return -1;
1269 }
1270 t = (PyTypeObject*)cls;
1271 if (!PyType_IsSubtype(solid, solid_base(t))) {
1272 PyErr_Format(PyExc_TypeError,
1273 "mro() returned base with unsuitable layout ('%.500s')",
1274 t->tp_name);
Neal Norwitz50bf51a2006-01-02 02:46:54 +00001275 Py_DECREF(tuple);
Armin Rigo037d1e02005-12-29 17:07:39 +00001276 return -1;
1277 }
1278 }
1279 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001280 type->tp_mro = tuple;
1281 return 0;
1282}
1283
1284
1285/* Calculate the best base amongst multiple base classes.
1286 This is the first one that's on the path to the "solid base". */
1287
1288static PyTypeObject *
1289best_base(PyObject *bases)
1290{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001291 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001292 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +00001293 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001294
1295 assert(PyTuple_Check(bases));
1296 n = PyTuple_GET_SIZE(bases);
1297 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +00001298 base = NULL;
1299 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001300 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001301 base_proto = PyTuple_GET_ITEM(bases, i);
Tim Petersa91e9642001-11-14 23:32:33 +00001302 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001303 PyErr_SetString(
1304 PyExc_TypeError,
1305 "bases must be types");
1306 return NULL;
1307 }
Tim Petersa91e9642001-11-14 23:32:33 +00001308 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001309 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001310 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001311 return NULL;
1312 }
1313 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +00001314 if (winner == NULL) {
1315 winner = candidate;
1316 base = base_i;
1317 }
1318 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001319 ;
1320 else if (PyType_IsSubtype(candidate, winner)) {
1321 winner = candidate;
1322 base = base_i;
1323 }
1324 else {
1325 PyErr_SetString(
1326 PyExc_TypeError,
1327 "multiple bases have "
1328 "instance lay-out conflict");
1329 return NULL;
1330 }
1331 }
Guido van Rossume54616c2001-12-14 04:19:56 +00001332 if (base == NULL)
1333 PyErr_SetString(PyExc_TypeError,
1334 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001335 return base;
1336}
1337
1338static int
1339extra_ivars(PyTypeObject *type, PyTypeObject *base)
1340{
Neil Schemenauerc806c882001-08-29 23:54:54 +00001341 size_t t_size = type->tp_basicsize;
1342 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001343
Guido van Rossum9676b222001-08-17 20:32:36 +00001344 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001345 if (type->tp_itemsize || base->tp_itemsize) {
1346 /* If itemsize is involved, stricter rules */
1347 return t_size != b_size ||
1348 type->tp_itemsize != base->tp_itemsize;
1349 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001350 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1351 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
1352 t_size -= sizeof(PyObject *);
1353 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1354 type->tp_dictoffset + sizeof(PyObject *) == t_size)
1355 t_size -= sizeof(PyObject *);
1356
1357 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001358}
1359
1360static PyTypeObject *
1361solid_base(PyTypeObject *type)
1362{
1363 PyTypeObject *base;
1364
1365 if (type->tp_base)
1366 base = solid_base(type->tp_base);
1367 else
1368 base = &PyBaseObject_Type;
1369 if (extra_ivars(type, base))
1370 return type;
1371 else
1372 return base;
1373}
1374
Jeremy Hylton938ace62002-07-17 16:30:39 +00001375static void object_dealloc(PyObject *);
1376static int object_init(PyObject *, PyObject *, PyObject *);
1377static int update_slot(PyTypeObject *, PyObject *);
1378static void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001379
1380static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001381subtype_dict(PyObject *obj, void *context)
1382{
1383 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1384 PyObject *dict;
1385
1386 if (dictptr == NULL) {
1387 PyErr_SetString(PyExc_AttributeError,
1388 "This object has no __dict__");
1389 return NULL;
1390 }
1391 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +00001392 if (dict == NULL)
1393 *dictptr = dict = PyDict_New();
1394 Py_XINCREF(dict);
1395 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001396}
1397
Guido van Rossum6661be32001-10-26 04:26:12 +00001398static int
1399subtype_setdict(PyObject *obj, PyObject *value, void *context)
1400{
1401 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1402 PyObject *dict;
1403
1404 if (dictptr == NULL) {
1405 PyErr_SetString(PyExc_AttributeError,
1406 "This object has no __dict__");
1407 return -1;
1408 }
Guido van Rossumd331cb52001-12-05 19:46:42 +00001409 if (value != NULL && !PyDict_Check(value)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001410 PyErr_Format(PyExc_TypeError,
1411 "__dict__ must be set to a dictionary, "
1412 "not a '%.200s'", value->ob_type->tp_name);
Guido van Rossum6661be32001-10-26 04:26:12 +00001413 return -1;
1414 }
1415 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +00001416 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +00001417 *dictptr = value;
1418 Py_XDECREF(dict);
1419 return 0;
1420}
1421
Guido van Rossumad47da02002-08-12 19:05:44 +00001422static PyObject *
1423subtype_getweakref(PyObject *obj, void *context)
1424{
1425 PyObject **weaklistptr;
1426 PyObject *result;
1427
1428 if (obj->ob_type->tp_weaklistoffset == 0) {
1429 PyErr_SetString(PyExc_AttributeError,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001430 "This object has no __weakref__");
Guido van Rossumad47da02002-08-12 19:05:44 +00001431 return NULL;
1432 }
1433 assert(obj->ob_type->tp_weaklistoffset > 0);
1434 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
Guido van Rossum3747a0f2002-08-12 19:25:08 +00001435 (size_t)(obj->ob_type->tp_basicsize));
Guido van Rossumad47da02002-08-12 19:05:44 +00001436 weaklistptr = (PyObject **)
Guido van Rossum3747a0f2002-08-12 19:25:08 +00001437 ((char *)obj + obj->ob_type->tp_weaklistoffset);
Guido van Rossumad47da02002-08-12 19:05:44 +00001438 if (*weaklistptr == NULL)
1439 result = Py_None;
1440 else
1441 result = *weaklistptr;
1442 Py_INCREF(result);
1443 return result;
1444}
1445
Guido van Rossum373c7412003-01-07 13:41:37 +00001446/* Three variants on the subtype_getsets list. */
1447
1448static PyGetSetDef subtype_getsets_full[] = {
Guido van Rossumad47da02002-08-12 19:05:44 +00001449 {"__dict__", subtype_dict, subtype_setdict,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001450 PyDoc_STR("dictionary for instance variables (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001451 {"__weakref__", subtype_getweakref, NULL,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001452 PyDoc_STR("list of weak references to the object (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +00001453 {0}
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001454};
1455
Guido van Rossum373c7412003-01-07 13:41:37 +00001456static PyGetSetDef subtype_getsets_dict_only[] = {
1457 {"__dict__", subtype_dict, subtype_setdict,
1458 PyDoc_STR("dictionary for instance variables (if defined)")},
1459 {0}
1460};
1461
1462static PyGetSetDef subtype_getsets_weakref_only[] = {
1463 {"__weakref__", subtype_getweakref, NULL,
1464 PyDoc_STR("list of weak references to the object (if defined)")},
1465 {0}
1466};
1467
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001468static int
1469valid_identifier(PyObject *s)
1470{
Guido van Rossum03013a02002-07-16 14:30:28 +00001471 unsigned char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001472 Py_ssize_t i, n;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001473
1474 if (!PyString_Check(s)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001475 PyErr_Format(PyExc_TypeError,
1476 "__slots__ items must be strings, not '%.200s'",
1477 s->ob_type->tp_name);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001478 return 0;
1479 }
Guido van Rossum03013a02002-07-16 14:30:28 +00001480 p = (unsigned char *) PyString_AS_STRING(s);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001481 n = PyString_GET_SIZE(s);
1482 /* We must reject an empty name. As a hack, we bump the
1483 length to 1 so that the loop will balk on the trailing \0. */
1484 if (n == 0)
1485 n = 1;
1486 for (i = 0; i < n; i++, p++) {
1487 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1488 PyErr_SetString(PyExc_TypeError,
1489 "__slots__ must be identifiers");
1490 return 0;
1491 }
1492 }
1493 return 1;
1494}
1495
Martin v. Löwisd919a592002-10-14 21:07:28 +00001496#ifdef Py_USING_UNICODE
1497/* Replace Unicode objects in slots. */
1498
1499static PyObject *
Martin v. Löwiseb079f12006-02-16 14:32:27 +00001500_unicode_to_string(PyObject *slots, Py_ssize_t nslots)
Martin v. Löwisd919a592002-10-14 21:07:28 +00001501{
1502 PyObject *tmp = slots;
1503 PyObject *o, *o1;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00001504 Py_ssize_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001505 ssizessizeargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
Martin v. Löwisd919a592002-10-14 21:07:28 +00001506 for (i = 0; i < nslots; i++) {
1507 if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
1508 if (tmp == slots) {
1509 tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
1510 if (tmp == NULL)
1511 return NULL;
1512 }
1513 o1 = _PyUnicode_AsDefaultEncodedString
1514 (o, NULL);
1515 if (o1 == NULL) {
1516 Py_DECREF(tmp);
1517 return 0;
1518 }
1519 Py_INCREF(o1);
1520 Py_DECREF(o);
1521 PyTuple_SET_ITEM(tmp, i, o1);
1522 }
1523 }
1524 return tmp;
1525}
1526#endif
1527
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001528static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001529type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1530{
1531 PyObject *name, *bases, *dict;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001532 static char *kwlist[] = {"name", "bases", "dict", 0};
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001533 PyObject *slots, *tmp, *newslots;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001534 PyTypeObject *type, *base, *tmptype, *winner;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001535 PyHeapTypeObject *et;
Guido van Rossum6f799372001-09-20 20:46:19 +00001536 PyMemberDef *mp;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001537 Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
Guido van Rossumad47da02002-08-12 19:05:44 +00001538 int j, may_add_dict, may_add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001539
Tim Peters3abca122001-10-27 19:37:48 +00001540 assert(args != NULL && PyTuple_Check(args));
1541 assert(kwds == NULL || PyDict_Check(kwds));
1542
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001543 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +00001544 {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001545 const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1546 const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
Tim Peters3abca122001-10-27 19:37:48 +00001547
1548 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1549 PyObject *x = PyTuple_GET_ITEM(args, 0);
1550 Py_INCREF(x->ob_type);
1551 return (PyObject *) x->ob_type;
1552 }
1553
1554 /* SF bug 475327 -- if that didn't trigger, we need 3
1555 arguments. but PyArg_ParseTupleAndKeywords below may give
1556 a msg saying type() needs exactly 3. */
1557 if (nargs + nkwds != 3) {
1558 PyErr_SetString(PyExc_TypeError,
1559 "type() takes 1 or 3 arguments");
1560 return NULL;
1561 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001562 }
1563
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001564 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001565 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1566 &name,
1567 &PyTuple_Type, &bases,
1568 &PyDict_Type, &dict))
1569 return NULL;
1570
1571 /* Determine the proper metatype to deal with this,
1572 and check for metatype conflicts while we're at it.
1573 Note that if some other metatype wins to contract,
1574 it's possible that its instances are not types. */
1575 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001576 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001577 for (i = 0; i < nbases; i++) {
1578 tmp = PyTuple_GET_ITEM(bases, i);
1579 tmptype = tmp->ob_type;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001580 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001581 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001582 if (PyType_IsSubtype(tmptype, winner)) {
1583 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001584 continue;
1585 }
1586 PyErr_SetString(PyExc_TypeError,
Guido van Rossum636688d2003-04-23 12:07:22 +00001587 "metaclass conflict: "
1588 "the metaclass of a derived class "
1589 "must be a (non-strict) subclass "
1590 "of the metaclasses of all its bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001591 return NULL;
1592 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001593 if (winner != metatype) {
1594 if (winner->tp_new != type_new) /* Pass it to the winner */
1595 return winner->tp_new(winner, args, kwds);
1596 metatype = winner;
1597 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001598
1599 /* Adjust for empty tuple bases */
1600 if (nbases == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001601 bases = PyTuple_Pack(1, &PyBaseObject_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001602 if (bases == NULL)
1603 return NULL;
1604 nbases = 1;
1605 }
1606 else
1607 Py_INCREF(bases);
1608
1609 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1610
1611 /* Calculate best base, and check that all bases are type objects */
1612 base = best_base(bases);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001613 if (base == NULL) {
1614 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001615 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001616 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001617 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1618 PyErr_Format(PyExc_TypeError,
1619 "type '%.100s' is not an acceptable base type",
1620 base->tp_name);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001621 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001622 return NULL;
1623 }
1624
Tim Peters6d6c1a32001-08-02 04:15:00 +00001625 /* Check for a __slots__ sequence variable in dict, and count it */
1626 slots = PyDict_GetItemString(dict, "__slots__");
1627 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001628 add_dict = 0;
1629 add_weak = 0;
Guido van Rossumad47da02002-08-12 19:05:44 +00001630 may_add_dict = base->tp_dictoffset == 0;
1631 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1632 if (slots == NULL) {
1633 if (may_add_dict) {
1634 add_dict++;
1635 }
1636 if (may_add_weak) {
1637 add_weak++;
1638 }
1639 }
1640 else {
1641 /* Have slots */
1642
Tim Peters6d6c1a32001-08-02 04:15:00 +00001643 /* Make it into a tuple */
1644 if (PyString_Check(slots))
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001645 slots = PyTuple_Pack(1, slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001646 else
1647 slots = PySequence_Tuple(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001648 if (slots == NULL) {
1649 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001650 return NULL;
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001651 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001652 assert(PyTuple_Check(slots));
1653
1654 /* Are slots allowed? */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001655 nslots = PyTuple_GET_SIZE(slots);
Jeremy Hylton1c7a0ea2003-07-16 16:08:23 +00001656 if (nslots > 0 && base->tp_itemsize != 0) {
Guido van Rossumc4141872001-08-30 04:43:35 +00001657 PyErr_Format(PyExc_TypeError,
1658 "nonempty __slots__ "
1659 "not supported for subtype of '%s'",
1660 base->tp_name);
Guido van Rossumad47da02002-08-12 19:05:44 +00001661 bad_slots:
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001662 Py_DECREF(bases);
Guido van Rossumad47da02002-08-12 19:05:44 +00001663 Py_DECREF(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00001664 return NULL;
1665 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001666
Martin v. Löwisd919a592002-10-14 21:07:28 +00001667#ifdef Py_USING_UNICODE
1668 tmp = _unicode_to_string(slots, nslots);
Martin v. Löwis13b1a5c2002-10-14 21:11:34 +00001669 if (tmp != slots) {
1670 Py_DECREF(slots);
1671 slots = tmp;
1672 }
Martin v. Löwisd919a592002-10-14 21:07:28 +00001673 if (!tmp)
1674 return NULL;
1675#endif
Guido van Rossumad47da02002-08-12 19:05:44 +00001676 /* Check for valid slot names and two special cases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001677 for (i = 0; i < nslots; i++) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001678 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1679 char *s;
1680 if (!valid_identifier(tmp))
1681 goto bad_slots;
1682 assert(PyString_Check(tmp));
1683 s = PyString_AS_STRING(tmp);
1684 if (strcmp(s, "__dict__") == 0) {
1685 if (!may_add_dict || add_dict) {
1686 PyErr_SetString(PyExc_TypeError,
1687 "__dict__ slot disallowed: "
1688 "we already got one");
1689 goto bad_slots;
1690 }
1691 add_dict++;
1692 }
1693 if (strcmp(s, "__weakref__") == 0) {
1694 if (!may_add_weak || add_weak) {
1695 PyErr_SetString(PyExc_TypeError,
1696 "__weakref__ slot disallowed: "
1697 "either we already got one, "
1698 "or __itemsize__ != 0");
1699 goto bad_slots;
1700 }
1701 add_weak++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001702 }
1703 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001704
Guido van Rossumad47da02002-08-12 19:05:44 +00001705 /* Copy slots into yet another tuple, demangling names */
1706 newslots = PyTuple_New(nslots - add_dict - add_weak);
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001707 if (newslots == NULL)
Guido van Rossumad47da02002-08-12 19:05:44 +00001708 goto bad_slots;
1709 for (i = j = 0; i < nslots; i++) {
1710 char *s;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001711 tmp = PyTuple_GET_ITEM(slots, i);
Guido van Rossumad47da02002-08-12 19:05:44 +00001712 s = PyString_AS_STRING(tmp);
1713 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1714 (add_weak && strcmp(s, "__weakref__") == 0))
1715 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 tmp =_Py_Mangle(name, tmp);
1717 if (!tmp)
1718 goto bad_slots;
Guido van Rossumad47da02002-08-12 19:05:44 +00001719 PyTuple_SET_ITEM(newslots, j, tmp);
1720 j++;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001721 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001722 assert(j == nslots - add_dict - add_weak);
1723 nslots = j;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001724 Py_DECREF(slots);
1725 slots = newslots;
1726
Guido van Rossumad47da02002-08-12 19:05:44 +00001727 /* Secondary bases may provide weakrefs or dict */
1728 if (nbases > 1 &&
1729 ((may_add_dict && !add_dict) ||
1730 (may_add_weak && !add_weak))) {
1731 for (i = 0; i < nbases; i++) {
1732 tmp = PyTuple_GET_ITEM(bases, i);
1733 if (tmp == (PyObject *)base)
1734 continue; /* Skip primary base */
Guido van Rossumad47da02002-08-12 19:05:44 +00001735 assert(PyType_Check(tmp));
1736 tmptype = (PyTypeObject *)tmp;
1737 if (may_add_dict && !add_dict &&
1738 tmptype->tp_dictoffset != 0)
1739 add_dict++;
1740 if (may_add_weak && !add_weak &&
1741 tmptype->tp_weaklistoffset != 0)
1742 add_weak++;
1743 if (may_add_dict && !add_dict)
1744 continue;
1745 if (may_add_weak && !add_weak)
1746 continue;
1747 /* Nothing more to check */
1748 break;
1749 }
1750 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001751 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001752
1753 /* XXX From here until type is safely allocated,
1754 "return NULL" may leak slots! */
1755
1756 /* Allocate the type object */
1757 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
Guido van Rossumad47da02002-08-12 19:05:44 +00001758 if (type == NULL) {
1759 Py_XDECREF(slots);
Michael W. Hudsona6a277d2003-08-08 13:57:22 +00001760 Py_DECREF(bases);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001761 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00001762 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001763
1764 /* Keep name and slots alive in the extended type object */
Guido van Rossume5c691a2003-03-07 15:13:17 +00001765 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001766 Py_INCREF(name);
Georg Brandlc255c7b2006-02-20 22:27:28 +00001767 et->ht_name = name;
1768 et->ht_slots = slots;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001769
Guido van Rossumdc91b992001-08-08 22:26:22 +00001770 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001771 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1772 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001773 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1774 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001775
Guido van Rossumdc91b992001-08-08 22:26:22 +00001776 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001777 type->tp_as_number = &et->as_number;
1778 type->tp_as_sequence = &et->as_sequence;
1779 type->tp_as_mapping = &et->as_mapping;
1780 type->tp_as_buffer = &et->as_buffer;
1781 type->tp_name = PyString_AS_STRING(name);
1782
1783 /* Set tp_base and tp_bases */
1784 type->tp_bases = bases;
1785 Py_INCREF(base);
1786 type->tp_base = base;
1787
Guido van Rossum687ae002001-10-15 22:03:32 +00001788 /* Initialize tp_dict from passed-in dict */
1789 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001790 if (dict == NULL) {
1791 Py_DECREF(type);
1792 return NULL;
1793 }
1794
Guido van Rossumc3542212001-08-16 09:18:56 +00001795 /* Set __module__ in the dict */
1796 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1797 tmp = PyEval_GetGlobals();
1798 if (tmp != NULL) {
1799 tmp = PyDict_GetItemString(tmp, "__name__");
1800 if (tmp != NULL) {
1801 if (PyDict_SetItemString(dict, "__module__",
1802 tmp) < 0)
1803 return NULL;
1804 }
1805 }
1806 }
1807
Tim Peters2f93e282001-10-04 05:27:00 +00001808 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00001809 and is a string. The __doc__ accessor will first look for tp_doc;
1810 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00001811 */
1812 {
1813 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1814 if (doc != NULL && PyString_Check(doc)) {
1815 const size_t n = (size_t)PyString_GET_SIZE(doc);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001816 char *tp_doc = (char *)PyObject_MALLOC(n+1);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001817 if (tp_doc == NULL) {
Tim Peters2f93e282001-10-04 05:27:00 +00001818 Py_DECREF(type);
1819 return NULL;
1820 }
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001821 memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
1822 type->tp_doc = tp_doc;
Tim Peters2f93e282001-10-04 05:27:00 +00001823 }
1824 }
1825
Tim Peters6d6c1a32001-08-02 04:15:00 +00001826 /* Special-case __new__: if it's a plain function,
1827 make it a static function */
1828 tmp = PyDict_GetItemString(dict, "__new__");
1829 if (tmp != NULL && PyFunction_Check(tmp)) {
1830 tmp = PyStaticMethod_New(tmp);
1831 if (tmp == NULL) {
1832 Py_DECREF(type);
1833 return NULL;
1834 }
1835 PyDict_SetItemString(dict, "__new__", tmp);
1836 Py_DECREF(tmp);
1837 }
1838
1839 /* Add descriptors for custom slots from __slots__, or for __dict__ */
Guido van Rossume5c691a2003-03-07 15:13:17 +00001840 mp = PyHeapType_GET_MEMBERS(et);
Neil Schemenauerc806c882001-08-29 23:54:54 +00001841 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001842 if (slots != NULL) {
1843 for (i = 0; i < nslots; i++, mp++) {
1844 mp->name = PyString_AS_STRING(
1845 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001846 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001847 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001848 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001849 strcmp(mp->name, "__weakref__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001850 add_weak++;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001851 mp->type = T_OBJECT;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001852 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001853 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001854 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001855 slotoffset += sizeof(PyObject *);
1856 }
1857 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001858 if (add_dict) {
1859 if (base->tp_itemsize)
1860 type->tp_dictoffset = -(long)sizeof(PyObject *);
1861 else
1862 type->tp_dictoffset = slotoffset;
1863 slotoffset += sizeof(PyObject *);
1864 }
1865 if (add_weak) {
1866 assert(!base->tp_itemsize);
1867 type->tp_weaklistoffset = slotoffset;
1868 slotoffset += sizeof(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001869 }
1870 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001871 type->tp_itemsize = base->tp_itemsize;
Guido van Rossume5c691a2003-03-07 15:13:17 +00001872 type->tp_members = PyHeapType_GET_MEMBERS(et);
Guido van Rossum373c7412003-01-07 13:41:37 +00001873
1874 if (type->tp_weaklistoffset && type->tp_dictoffset)
1875 type->tp_getset = subtype_getsets_full;
1876 else if (type->tp_weaklistoffset && !type->tp_dictoffset)
1877 type->tp_getset = subtype_getsets_weakref_only;
1878 else if (!type->tp_weaklistoffset && type->tp_dictoffset)
1879 type->tp_getset = subtype_getsets_dict_only;
1880 else
1881 type->tp_getset = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001882
1883 /* Special case some slots */
1884 if (type->tp_dictoffset != 0 || nslots > 0) {
1885 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1886 type->tp_getattro = PyObject_GenericGetAttr;
1887 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1888 type->tp_setattro = PyObject_GenericSetAttr;
1889 }
1890 type->tp_dealloc = subtype_dealloc;
1891
Guido van Rossum9475a232001-10-05 20:51:39 +00001892 /* Enable GC unless there are really no instance variables possible */
1893 if (!(type->tp_basicsize == sizeof(PyObject) &&
1894 type->tp_itemsize == 0))
1895 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1896
Tim Peters6d6c1a32001-08-02 04:15:00 +00001897 /* Always override allocation strategy to use regular heap */
1898 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001899 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001900 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001901 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001902 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00001903 }
1904 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001905 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001906
1907 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001908 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001909 Py_DECREF(type);
1910 return NULL;
1911 }
1912
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001913 /* Put the proper slots in place */
1914 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001915
Tim Peters6d6c1a32001-08-02 04:15:00 +00001916 return (PyObject *)type;
1917}
1918
1919/* Internal API to look for a name through the MRO.
1920 This returns a borrowed reference, and doesn't set an exception! */
1921PyObject *
1922_PyType_Lookup(PyTypeObject *type, PyObject *name)
1923{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001924 Py_ssize_t i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001925 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001926
Guido van Rossum687ae002001-10-15 22:03:32 +00001927 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001928 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00001929
1930 /* If mro is NULL, the type is either not yet initialized
1931 by PyType_Ready(), or already cleared by type_clear().
1932 Either way the safest thing to do is to return NULL. */
1933 if (mro == NULL)
1934 return NULL;
1935
Tim Peters6d6c1a32001-08-02 04:15:00 +00001936 assert(PyTuple_Check(mro));
1937 n = PyTuple_GET_SIZE(mro);
1938 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001939 base = PyTuple_GET_ITEM(mro, i);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001940 assert(PyType_Check(base));
1941 dict = ((PyTypeObject *)base)->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001942 assert(dict && PyDict_Check(dict));
1943 res = PyDict_GetItem(dict, name);
1944 if (res != NULL)
1945 return res;
1946 }
1947 return NULL;
1948}
1949
1950/* This is similar to PyObject_GenericGetAttr(),
1951 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1952static PyObject *
1953type_getattro(PyTypeObject *type, PyObject *name)
1954{
1955 PyTypeObject *metatype = type->ob_type;
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001956 PyObject *meta_attribute, *attribute;
1957 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001958
1959 /* Initialize this type (we'll assume the metatype is initialized) */
1960 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001961 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001962 return NULL;
1963 }
1964
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001965 /* No readable descriptor found yet */
1966 meta_get = NULL;
Tim Peters34592512002-07-11 06:23:50 +00001967
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001968 /* Look for the attribute in the metatype */
1969 meta_attribute = _PyType_Lookup(metatype, name);
1970
1971 if (meta_attribute != NULL) {
1972 meta_get = meta_attribute->ob_type->tp_descr_get;
Tim Peters34592512002-07-11 06:23:50 +00001973
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001974 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
1975 /* Data descriptors implement tp_descr_set to intercept
1976 * writes. Assume the attribute is not overridden in
1977 * type's tp_dict (and bases): call the descriptor now.
1978 */
1979 return meta_get(meta_attribute, (PyObject *)type,
1980 (PyObject *)metatype);
1981 }
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001982 Py_INCREF(meta_attribute);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001983 }
1984
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001985 /* No data descriptor found on metatype. Look in tp_dict of this
1986 * type and its bases */
1987 attribute = _PyType_Lookup(type, name);
1988 if (attribute != NULL) {
1989 /* Implement descriptor functionality, if any */
1990 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001991
1992 Py_XDECREF(meta_attribute);
1993
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001994 if (local_get != NULL) {
1995 /* NULL 2nd argument indicates the descriptor was
1996 * found on the target object itself (or a base) */
1997 return local_get(attribute, (PyObject *)NULL,
1998 (PyObject *)type);
1999 }
Tim Peters34592512002-07-11 06:23:50 +00002000
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002001 Py_INCREF(attribute);
2002 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002003 }
2004
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002005 /* No attribute found in local __dict__ (or bases): use the
2006 * descriptor from the metatype, if any */
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00002007 if (meta_get != NULL) {
2008 PyObject *res;
2009 res = meta_get(meta_attribute, (PyObject *)type,
2010 (PyObject *)metatype);
2011 Py_DECREF(meta_attribute);
2012 return res;
2013 }
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002014
2015 /* If an ordinary attribute was found on the metatype, return it now */
2016 if (meta_attribute != NULL) {
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002017 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002018 }
2019
2020 /* Give up */
2021 PyErr_Format(PyExc_AttributeError,
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00002022 "type object '%.50s' has no attribute '%.400s'",
2023 type->tp_name, PyString_AS_STRING(name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002024 return NULL;
2025}
2026
2027static int
2028type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2029{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002030 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2031 PyErr_Format(
2032 PyExc_TypeError,
2033 "can't set attributes of built-in/extension type '%s'",
2034 type->tp_name);
2035 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002036 }
Guido van Rossum8d24ee92003-03-24 23:49:49 +00002037 /* XXX Example of how I expect this to be used...
2038 if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
2039 return -1;
2040 */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002041 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2042 return -1;
2043 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002044}
2045
2046static void
2047type_dealloc(PyTypeObject *type)
2048{
Guido van Rossume5c691a2003-03-07 15:13:17 +00002049 PyHeapTypeObject *et;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002050
2051 /* Assert this is a heap-allocated type object */
2052 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002053 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00002054 PyObject_ClearWeakRefs((PyObject *)type);
Guido van Rossume5c691a2003-03-07 15:13:17 +00002055 et = (PyHeapTypeObject *)type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002056 Py_XDECREF(type->tp_base);
2057 Py_XDECREF(type->tp_dict);
2058 Py_XDECREF(type->tp_bases);
2059 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00002060 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00002061 Py_XDECREF(type->tp_subclasses);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002062 /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2063 * of most other objects. It's okay to cast it to char *.
2064 */
2065 PyObject_Free((char *)type->tp_doc);
Georg Brandlc255c7b2006-02-20 22:27:28 +00002066 Py_XDECREF(et->ht_name);
2067 Py_XDECREF(et->ht_slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002068 type->ob_type->tp_free((PyObject *)type);
2069}
2070
Guido van Rossum1c450732001-10-08 15:18:27 +00002071static PyObject *
2072type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2073{
2074 PyObject *list, *raw, *ref;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002075 Py_ssize_t i, n;
Guido van Rossum1c450732001-10-08 15:18:27 +00002076
2077 list = PyList_New(0);
2078 if (list == NULL)
2079 return NULL;
2080 raw = type->tp_subclasses;
2081 if (raw == NULL)
2082 return list;
2083 assert(PyList_Check(raw));
2084 n = PyList_GET_SIZE(raw);
2085 for (i = 0; i < n; i++) {
2086 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00002087 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00002088 ref = PyWeakref_GET_OBJECT(ref);
2089 if (ref != Py_None) {
2090 if (PyList_Append(list, ref) < 0) {
2091 Py_DECREF(list);
2092 return NULL;
2093 }
2094 }
2095 }
2096 return list;
2097}
2098
Tim Peters6d6c1a32001-08-02 04:15:00 +00002099static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002100 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002101 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
Guido van Rossum1c450732001-10-08 15:18:27 +00002102 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002103 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002104 {0}
2105};
2106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002107PyDoc_STRVAR(type_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002108"type(object) -> the object's type\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002109"type(name, bases, dict) -> a new type");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002110
Guido van Rossum048eb752001-10-02 21:24:57 +00002111static int
2112type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2113{
Guido van Rossuma3862092002-06-10 15:24:42 +00002114 /* Because of type_is_gc(), the collector only calls this
2115 for heaptypes. */
2116 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002117
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002118 Py_VISIT(type->tp_dict);
2119 Py_VISIT(type->tp_cache);
2120 Py_VISIT(type->tp_mro);
2121 Py_VISIT(type->tp_bases);
2122 Py_VISIT(type->tp_base);
Guido van Rossuma3862092002-06-10 15:24:42 +00002123
2124 /* There's no need to visit type->tp_subclasses or
Georg Brandlc255c7b2006-02-20 22:27:28 +00002125 ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
Guido van Rossuma3862092002-06-10 15:24:42 +00002126 in cycles; tp_subclasses is a list of weak references,
2127 and slots is a tuple of strings. */
Guido van Rossum048eb752001-10-02 21:24:57 +00002128
Guido van Rossum048eb752001-10-02 21:24:57 +00002129 return 0;
2130}
2131
2132static int
2133type_clear(PyTypeObject *type)
2134{
Guido van Rossuma3862092002-06-10 15:24:42 +00002135 /* Because of type_is_gc(), the collector only calls this
2136 for heaptypes. */
2137 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00002138
Guido van Rossuma3862092002-06-10 15:24:42 +00002139 /* The only field we need to clear is tp_mro, which is part of a
2140 hard cycle (its first element is the class itself) that won't
2141 be broken otherwise (it's a tuple and tuples don't have a
2142 tp_clear handler). None of the other fields need to be
2143 cleared, and here's why:
Guido van Rossum048eb752001-10-02 21:24:57 +00002144
Guido van Rossuma3862092002-06-10 15:24:42 +00002145 tp_dict:
2146 It is a dict, so the collector will call its tp_clear.
2147
2148 tp_cache:
2149 Not used; if it were, it would be a dict.
2150
2151 tp_bases, tp_base:
2152 If these are involved in a cycle, there must be at least
2153 one other, mutable object in the cycle, e.g. a base
2154 class's dict; the cycle will be broken that way.
2155
2156 tp_subclasses:
2157 A list of weak references can't be part of a cycle; and
2158 lists have their own tp_clear.
2159
Guido van Rossume5c691a2003-03-07 15:13:17 +00002160 slots (in PyHeapTypeObject):
Guido van Rossuma3862092002-06-10 15:24:42 +00002161 A tuple of strings can't be part of a cycle.
2162 */
2163
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002164 Py_CLEAR(type->tp_mro);
Guido van Rossum048eb752001-10-02 21:24:57 +00002165
2166 return 0;
2167}
2168
2169static int
2170type_is_gc(PyTypeObject *type)
2171{
2172 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2173}
2174
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002175PyTypeObject PyType_Type = {
2176 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002177 0, /* ob_size */
2178 "type", /* tp_name */
Guido van Rossume5c691a2003-03-07 15:13:17 +00002179 sizeof(PyHeapTypeObject), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00002180 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002181 (destructor)type_dealloc, /* tp_dealloc */
2182 0, /* tp_print */
2183 0, /* tp_getattr */
2184 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002185 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002186 (reprfunc)type_repr, /* tp_repr */
2187 0, /* tp_as_number */
2188 0, /* tp_as_sequence */
2189 0, /* tp_as_mapping */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002190 0, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002191 (ternaryfunc)type_call, /* tp_call */
2192 0, /* tp_str */
2193 (getattrofunc)type_getattro, /* tp_getattro */
2194 (setattrofunc)type_setattro, /* tp_setattro */
2195 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00002196 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2197 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002198 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00002199 (traverseproc)type_traverse, /* tp_traverse */
2200 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002201 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00002202 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002203 0, /* tp_iter */
2204 0, /* tp_iternext */
2205 type_methods, /* tp_methods */
2206 type_members, /* tp_members */
2207 type_getsets, /* tp_getset */
2208 0, /* tp_base */
2209 0, /* tp_dict */
2210 0, /* tp_descr_get */
2211 0, /* tp_descr_set */
2212 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
2213 0, /* tp_init */
2214 0, /* tp_alloc */
2215 type_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002216 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00002217 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002218};
Tim Peters6d6c1a32001-08-02 04:15:00 +00002219
2220
2221/* The base type of all types (eventually)... except itself. */
2222
2223static int
2224object_init(PyObject *self, PyObject *args, PyObject *kwds)
2225{
2226 return 0;
2227}
2228
Guido van Rossum298e4212003-02-13 16:30:16 +00002229/* If we don't have a tp_new for a new-style class, new will use this one.
2230 Therefore this should take no arguments/keywords. However, this new may
2231 also be inherited by objects that define a tp_init but no tp_new. These
2232 objects WILL pass argumets to tp_new, because it gets the same args as
2233 tp_init. So only allow arguments if we aren't using the default init, in
2234 which case we expect init to handle argument parsing. */
2235static PyObject *
2236object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2237{
2238 if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
2239 (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
2240 PyErr_SetString(PyExc_TypeError,
2241 "default __new__ takes no parameters");
2242 return NULL;
2243 }
2244 return type->tp_alloc(type, 0);
2245}
2246
Tim Peters6d6c1a32001-08-02 04:15:00 +00002247static void
2248object_dealloc(PyObject *self)
2249{
2250 self->ob_type->tp_free(self);
2251}
2252
Guido van Rossum8e248182001-08-12 05:17:56 +00002253static PyObject *
2254object_repr(PyObject *self)
2255{
Guido van Rossum76e69632001-08-16 18:52:43 +00002256 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00002257 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002258
Guido van Rossum76e69632001-08-16 18:52:43 +00002259 type = self->ob_type;
2260 mod = type_module(type, NULL);
2261 if (mod == NULL)
2262 PyErr_Clear();
2263 else if (!PyString_Check(mod)) {
2264 Py_DECREF(mod);
2265 mod = NULL;
2266 }
2267 name = type_name(type, NULL);
2268 if (name == NULL)
2269 return NULL;
2270 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00002271 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002272 PyString_AS_STRING(mod),
2273 PyString_AS_STRING(name),
2274 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002275 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00002276 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002277 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00002278 Py_XDECREF(mod);
2279 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00002280 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00002281}
2282
Guido van Rossumb8f63662001-08-15 23:57:02 +00002283static PyObject *
2284object_str(PyObject *self)
2285{
2286 unaryfunc f;
2287
2288 f = self->ob_type->tp_repr;
2289 if (f == NULL)
2290 f = object_repr;
2291 return f(self);
2292}
2293
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002294static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002295object_richcompare(PyObject *self, PyObject *other, int op)
2296{
2297 PyObject *res;
2298
2299 switch (op) {
2300
2301 case Py_EQ:
2302 res = (self == other) ? Py_True : Py_False;
2303 break;
2304
2305 case Py_NE:
2306 res = (self != other) ? Py_True : Py_False;
2307 break;
2308
2309 default:
2310 res = Py_NotImplemented;
2311 break;
2312 }
2313
2314 Py_INCREF(res);
2315 return res;
2316}
2317
2318static PyObject *
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002319object_get_class(PyObject *self, void *closure)
2320{
2321 Py_INCREF(self->ob_type);
2322 return (PyObject *)(self->ob_type);
2323}
2324
2325static int
2326equiv_structs(PyTypeObject *a, PyTypeObject *b)
2327{
2328 return a == b ||
2329 (a != NULL &&
2330 b != NULL &&
2331 a->tp_basicsize == b->tp_basicsize &&
2332 a->tp_itemsize == b->tp_itemsize &&
2333 a->tp_dictoffset == b->tp_dictoffset &&
2334 a->tp_weaklistoffset == b->tp_weaklistoffset &&
2335 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
2336 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
2337}
2338
2339static int
2340same_slots_added(PyTypeObject *a, PyTypeObject *b)
2341{
2342 PyTypeObject *base = a->tp_base;
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002343 Py_ssize_t size;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002344
2345 if (base != b->tp_base)
2346 return 0;
2347 if (equiv_structs(a, base) && equiv_structs(b, base))
2348 return 1;
2349 size = base->tp_basicsize;
2350 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
2351 size += sizeof(PyObject *);
2352 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
2353 size += sizeof(PyObject *);
2354 return size == a->tp_basicsize && size == b->tp_basicsize;
2355}
2356
2357static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002358compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002359{
2360 PyTypeObject *newbase, *oldbase;
2361
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002362 if (newto->tp_dealloc != oldto->tp_dealloc ||
2363 newto->tp_free != oldto->tp_free)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002364 {
2365 PyErr_Format(PyExc_TypeError,
2366 "%s assignment: "
2367 "'%s' deallocator differs from '%s'",
2368 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002369 newto->tp_name,
2370 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002371 return 0;
2372 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002373 newbase = newto;
2374 oldbase = oldto;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002375 while (equiv_structs(newbase, newbase->tp_base))
2376 newbase = newbase->tp_base;
2377 while (equiv_structs(oldbase, oldbase->tp_base))
2378 oldbase = oldbase->tp_base;
2379 if (newbase != oldbase &&
2380 (newbase->tp_base != oldbase->tp_base ||
2381 !same_slots_added(newbase, oldbase))) {
2382 PyErr_Format(PyExc_TypeError,
2383 "%s assignment: "
2384 "'%s' object layout differs from '%s'",
2385 attr,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002386 newto->tp_name,
2387 oldto->tp_name);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002388 return 0;
2389 }
Tim Petersea7f75d2002-12-07 21:39:16 +00002390
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002391 return 1;
2392}
2393
2394static int
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002395object_set_class(PyObject *self, PyObject *value, void *closure)
2396{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002397 PyTypeObject *oldto = self->ob_type;
2398 PyTypeObject *newto;
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002399
Guido van Rossumb6b89422002-04-15 01:03:30 +00002400 if (value == NULL) {
2401 PyErr_SetString(PyExc_TypeError,
2402 "can't delete __class__ attribute");
2403 return -1;
2404 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002405 if (!PyType_Check(value)) {
2406 PyErr_Format(PyExc_TypeError,
2407 "__class__ must be set to new-style class, not '%s' object",
2408 value->ob_type->tp_name);
2409 return -1;
2410 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002411 newto = (PyTypeObject *)value;
2412 if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
2413 !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
Guido van Rossum40af8892002-08-10 05:42:07 +00002414 {
2415 PyErr_Format(PyExc_TypeError,
2416 "__class__ assignment: only for heap types");
2417 return -1;
2418 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002419 if (compatible_for_assignment(newto, oldto, "__class__")) {
2420 Py_INCREF(newto);
2421 self->ob_type = newto;
2422 Py_DECREF(oldto);
Michael W. Hudson98bbc492002-11-26 14:47:27 +00002423 return 0;
2424 }
2425 else {
Guido van Rossum9ee4b942002-05-24 18:47:47 +00002426 return -1;
2427 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002428}
2429
2430static PyGetSetDef object_getsets[] = {
2431 {"__class__", object_get_class, object_set_class,
Neal Norwitz858e34f2002-08-13 17:18:45 +00002432 PyDoc_STR("the object's class")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002433 {0}
2434};
2435
Guido van Rossumc53f0092003-02-18 22:05:12 +00002436
Guido van Rossum036f9992003-02-21 22:02:54 +00002437/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
2438 We fall back to helpers in copy_reg for:
2439 - pickle protocols < 2
2440 - calculating the list of slot names (done only once per class)
2441 - the __newobj__ function (which is used as a token but never called)
2442*/
2443
2444static PyObject *
2445import_copy_reg(void)
2446{
2447 static PyObject *copy_reg_str;
Guido van Rossum3926a632001-09-25 16:25:58 +00002448
2449 if (!copy_reg_str) {
2450 copy_reg_str = PyString_InternFromString("copy_reg");
2451 if (copy_reg_str == NULL)
2452 return NULL;
2453 }
Guido van Rossum036f9992003-02-21 22:02:54 +00002454
2455 return PyImport_Import(copy_reg_str);
2456}
2457
2458static PyObject *
2459slotnames(PyObject *cls)
2460{
2461 PyObject *clsdict;
2462 PyObject *copy_reg;
2463 PyObject *slotnames;
2464
2465 if (!PyType_Check(cls)) {
2466 Py_INCREF(Py_None);
2467 return Py_None;
2468 }
2469
2470 clsdict = ((PyTypeObject *)cls)->tp_dict;
2471 slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
Armin Rigoec862b92005-09-24 22:58:41 +00002472 if (slotnames != NULL && PyList_Check(slotnames)) {
Guido van Rossum036f9992003-02-21 22:02:54 +00002473 Py_INCREF(slotnames);
2474 return slotnames;
2475 }
2476
2477 copy_reg = import_copy_reg();
2478 if (copy_reg == NULL)
2479 return NULL;
2480
2481 slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
2482 Py_DECREF(copy_reg);
2483 if (slotnames != NULL &&
2484 slotnames != Py_None &&
2485 !PyList_Check(slotnames))
2486 {
2487 PyErr_SetString(PyExc_TypeError,
2488 "copy_reg._slotnames didn't return a list or None");
2489 Py_DECREF(slotnames);
2490 slotnames = NULL;
2491 }
2492
2493 return slotnames;
2494}
2495
2496static PyObject *
2497reduce_2(PyObject *obj)
2498{
2499 PyObject *cls, *getnewargs;
2500 PyObject *args = NULL, *args2 = NULL;
2501 PyObject *getstate = NULL, *state = NULL, *names = NULL;
2502 PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
2503 PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002504 Py_ssize_t i, n;
Guido van Rossum036f9992003-02-21 22:02:54 +00002505
2506 cls = PyObject_GetAttrString(obj, "__class__");
2507 if (cls == NULL)
2508 return NULL;
2509
2510 getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
2511 if (getnewargs != NULL) {
2512 args = PyObject_CallObject(getnewargs, NULL);
2513 Py_DECREF(getnewargs);
2514 if (args != NULL && !PyTuple_Check(args)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002515 PyErr_Format(PyExc_TypeError,
2516 "__getnewargs__ should return a tuple, "
2517 "not '%.200s'", args->ob_type->tp_name);
Guido van Rossum036f9992003-02-21 22:02:54 +00002518 goto end;
2519 }
2520 }
2521 else {
2522 PyErr_Clear();
2523 args = PyTuple_New(0);
2524 }
2525 if (args == NULL)
2526 goto end;
2527
2528 getstate = PyObject_GetAttrString(obj, "__getstate__");
2529 if (getstate != NULL) {
2530 state = PyObject_CallObject(getstate, NULL);
2531 Py_DECREF(getstate);
Neal Norwitze2fdc612003-06-08 13:19:58 +00002532 if (state == NULL)
2533 goto end;
Guido van Rossum036f9992003-02-21 22:02:54 +00002534 }
2535 else {
Jim Fulton8a1a5942004-02-08 04:21:26 +00002536 PyErr_Clear();
Guido van Rossum036f9992003-02-21 22:02:54 +00002537 state = PyObject_GetAttrString(obj, "__dict__");
2538 if (state == NULL) {
2539 PyErr_Clear();
2540 state = Py_None;
2541 Py_INCREF(state);
2542 }
2543 names = slotnames(cls);
2544 if (names == NULL)
2545 goto end;
2546 if (names != Py_None) {
2547 assert(PyList_Check(names));
2548 slots = PyDict_New();
2549 if (slots == NULL)
2550 goto end;
2551 n = 0;
2552 /* Can't pre-compute the list size; the list
2553 is stored on the class so accessible to other
2554 threads, which may be run by DECREF */
2555 for (i = 0; i < PyList_GET_SIZE(names); i++) {
2556 PyObject *name, *value;
2557 name = PyList_GET_ITEM(names, i);
2558 value = PyObject_GetAttr(obj, name);
2559 if (value == NULL)
2560 PyErr_Clear();
2561 else {
2562 int err = PyDict_SetItem(slots, name,
2563 value);
2564 Py_DECREF(value);
2565 if (err)
2566 goto end;
2567 n++;
2568 }
2569 }
2570 if (n) {
2571 state = Py_BuildValue("(NO)", state, slots);
2572 if (state == NULL)
2573 goto end;
2574 }
2575 }
2576 }
2577
2578 if (!PyList_Check(obj)) {
2579 listitems = Py_None;
2580 Py_INCREF(listitems);
2581 }
2582 else {
2583 listitems = PyObject_GetIter(obj);
2584 if (listitems == NULL)
2585 goto end;
2586 }
2587
2588 if (!PyDict_Check(obj)) {
2589 dictitems = Py_None;
2590 Py_INCREF(dictitems);
2591 }
2592 else {
2593 dictitems = PyObject_CallMethod(obj, "iteritems", "");
2594 if (dictitems == NULL)
2595 goto end;
2596 }
2597
2598 copy_reg = import_copy_reg();
2599 if (copy_reg == NULL)
2600 goto end;
2601 newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
2602 if (newobj == NULL)
2603 goto end;
2604
2605 n = PyTuple_GET_SIZE(args);
2606 args2 = PyTuple_New(n+1);
2607 if (args2 == NULL)
2608 goto end;
2609 PyTuple_SET_ITEM(args2, 0, cls);
2610 cls = NULL;
2611 for (i = 0; i < n; i++) {
2612 PyObject *v = PyTuple_GET_ITEM(args, i);
2613 Py_INCREF(v);
2614 PyTuple_SET_ITEM(args2, i+1, v);
2615 }
2616
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002617 res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
Guido van Rossum036f9992003-02-21 22:02:54 +00002618
2619 end:
2620 Py_XDECREF(cls);
2621 Py_XDECREF(args);
2622 Py_XDECREF(args2);
Jeremy Hyltond06483c2003-04-09 21:01:42 +00002623 Py_XDECREF(slots);
Guido van Rossum036f9992003-02-21 22:02:54 +00002624 Py_XDECREF(state);
2625 Py_XDECREF(names);
2626 Py_XDECREF(listitems);
2627 Py_XDECREF(dictitems);
2628 Py_XDECREF(copy_reg);
2629 Py_XDECREF(newobj);
2630 return res;
2631}
2632
2633static PyObject *
2634object_reduce_ex(PyObject *self, PyObject *args)
2635{
2636 /* Call copy_reg._reduce_ex(self, proto) */
2637 PyObject *reduce, *copy_reg, *res;
2638 int proto = 0;
2639
2640 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
2641 return NULL;
2642
2643 reduce = PyObject_GetAttrString(self, "__reduce__");
2644 if (reduce == NULL)
2645 PyErr_Clear();
2646 else {
2647 PyObject *cls, *clsreduce, *objreduce;
2648 int override;
2649 cls = PyObject_GetAttrString(self, "__class__");
2650 if (cls == NULL) {
2651 Py_DECREF(reduce);
2652 return NULL;
2653 }
2654 clsreduce = PyObject_GetAttrString(cls, "__reduce__");
2655 Py_DECREF(cls);
2656 if (clsreduce == NULL) {
2657 Py_DECREF(reduce);
2658 return NULL;
2659 }
2660 objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
2661 "__reduce__");
2662 override = (clsreduce != objreduce);
2663 Py_DECREF(clsreduce);
2664 if (override) {
2665 res = PyObject_CallObject(reduce, NULL);
2666 Py_DECREF(reduce);
2667 return res;
2668 }
2669 else
2670 Py_DECREF(reduce);
2671 }
2672
2673 if (proto >= 2)
2674 return reduce_2(self);
2675
2676 copy_reg = import_copy_reg();
Guido van Rossum3926a632001-09-25 16:25:58 +00002677 if (!copy_reg)
2678 return NULL;
Guido van Rossum036f9992003-02-21 22:02:54 +00002679
Guido van Rossumc53f0092003-02-18 22:05:12 +00002680 res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
Guido van Rossum3926a632001-09-25 16:25:58 +00002681 Py_DECREF(copy_reg);
Guido van Rossum036f9992003-02-21 22:02:54 +00002682
Guido van Rossum3926a632001-09-25 16:25:58 +00002683 return res;
2684}
2685
2686static PyMethodDef object_methods[] = {
Guido van Rossumc53f0092003-02-18 22:05:12 +00002687 {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
2688 PyDoc_STR("helper for pickle")},
2689 {"__reduce__", object_reduce_ex, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002690 PyDoc_STR("helper for pickle")},
Guido van Rossum3926a632001-09-25 16:25:58 +00002691 {0}
2692};
2693
Guido van Rossum036f9992003-02-21 22:02:54 +00002694
Tim Peters6d6c1a32001-08-02 04:15:00 +00002695PyTypeObject PyBaseObject_Type = {
2696 PyObject_HEAD_INIT(&PyType_Type)
2697 0, /* ob_size */
2698 "object", /* tp_name */
2699 sizeof(PyObject), /* tp_basicsize */
2700 0, /* tp_itemsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002701 object_dealloc, /* tp_dealloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002702 0, /* tp_print */
2703 0, /* tp_getattr */
2704 0, /* tp_setattr */
2705 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002706 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002707 0, /* tp_as_number */
2708 0, /* tp_as_sequence */
2709 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002710 (hashfunc)_Py_HashPointer, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002711 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002712 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002713 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00002714 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002715 0, /* tp_as_buffer */
2716 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002717 PyDoc_STR("The most base type"), /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002718 0, /* tp_traverse */
2719 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002720 object_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002721 0, /* tp_weaklistoffset */
2722 0, /* tp_iter */
2723 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00002724 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002725 0, /* tp_members */
2726 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002727 0, /* tp_base */
2728 0, /* tp_dict */
2729 0, /* tp_descr_get */
2730 0, /* tp_descr_set */
2731 0, /* tp_dictoffset */
2732 object_init, /* tp_init */
2733 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossum298e4212003-02-13 16:30:16 +00002734 object_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002735 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002736};
2737
2738
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002739/* Add the methods from tp_methods to the __dict__ in a type object */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002740
2741static int
2742add_methods(PyTypeObject *type, PyMethodDef *meth)
2743{
Guido van Rossum687ae002001-10-15 22:03:32 +00002744 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002745
2746 for (; meth->ml_name != NULL; meth++) {
2747 PyObject *descr;
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002748 if (PyDict_GetItemString(dict, meth->ml_name) &&
2749 !(meth->ml_flags & METH_COEXIST))
2750 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00002751 if (meth->ml_flags & METH_CLASS) {
2752 if (meth->ml_flags & METH_STATIC) {
2753 PyErr_SetString(PyExc_ValueError,
2754 "method cannot be both class and static");
2755 return -1;
2756 }
Tim Petersbca1cbc2002-12-09 22:56:13 +00002757 descr = PyDescr_NewClassMethod(type, meth);
Fred Drake7bf97152002-03-28 05:33:33 +00002758 }
2759 else if (meth->ml_flags & METH_STATIC) {
Guido van Rossum9af48ff2003-02-11 17:12:46 +00002760 PyObject *cfunc = PyCFunction_New(meth, NULL);
2761 if (cfunc == NULL)
2762 return -1;
2763 descr = PyStaticMethod_New(cfunc);
2764 Py_DECREF(cfunc);
Fred Drake7bf97152002-03-28 05:33:33 +00002765 }
2766 else {
2767 descr = PyDescr_NewMethod(type, meth);
2768 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002769 if (descr == NULL)
2770 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00002771 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002772 return -1;
2773 Py_DECREF(descr);
2774 }
2775 return 0;
2776}
2777
2778static int
Guido van Rossum6f799372001-09-20 20:46:19 +00002779add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002780{
Guido van Rossum687ae002001-10-15 22:03:32 +00002781 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002782
2783 for (; memb->name != NULL; memb++) {
2784 PyObject *descr;
2785 if (PyDict_GetItemString(dict, memb->name))
2786 continue;
2787 descr = PyDescr_NewMember(type, memb);
2788 if (descr == NULL)
2789 return -1;
2790 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2791 return -1;
2792 Py_DECREF(descr);
2793 }
2794 return 0;
2795}
2796
2797static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00002798add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002799{
Guido van Rossum687ae002001-10-15 22:03:32 +00002800 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002801
2802 for (; gsp->name != NULL; gsp++) {
2803 PyObject *descr;
2804 if (PyDict_GetItemString(dict, gsp->name))
2805 continue;
2806 descr = PyDescr_NewGetSet(type, gsp);
2807
2808 if (descr == NULL)
2809 return -1;
2810 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2811 return -1;
2812 Py_DECREF(descr);
2813 }
2814 return 0;
2815}
2816
Guido van Rossum13d52f02001-08-10 21:24:08 +00002817static void
2818inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002819{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00002820 Py_ssize_t oldsize, newsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002821
Guido van Rossum13d52f02001-08-10 21:24:08 +00002822 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00002823 oldsize = base->tp_basicsize;
2824 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2825 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2826 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00002827 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00002828 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00002829 if (type->tp_traverse == NULL)
2830 type->tp_traverse = base->tp_traverse;
2831 if (type->tp_clear == NULL)
2832 type->tp_clear = base->tp_clear;
2833 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002834 {
Guido van Rossumf884b742001-12-17 17:14:22 +00002835 /* The condition below could use some explanation.
2836 It appears that tp_new is not inherited for static types
2837 whose base class is 'object'; this seems to be a precaution
2838 so that old extension types don't suddenly become
2839 callable (object.__new__ wouldn't insure the invariants
2840 that the extension type's own factory function ensures).
2841 Heap types, of course, are under our control, so they do
2842 inherit tp_new; static extension types that specify some
2843 other built-in type as the default are considered
2844 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00002845 if (base != &PyBaseObject_Type ||
2846 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2847 if (type->tp_new == NULL)
2848 type->tp_new = base->tp_new;
2849 }
2850 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00002851 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00002852
2853 /* Copy other non-function slots */
2854
2855#undef COPYVAL
2856#define COPYVAL(SLOT) \
2857 if (type->SLOT == 0) type->SLOT = base->SLOT
2858
2859 COPYVAL(tp_itemsize);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002860 COPYVAL(tp_weaklistoffset);
2861 COPYVAL(tp_dictoffset);
Guido van Rossum13d52f02001-08-10 21:24:08 +00002862}
2863
Guido van Rossum38938152006-08-21 23:36:26 +00002864/* Map rich comparison operators to their __xx__ namesakes */
2865static char *name_op[] = {
2866 "__lt__",
2867 "__le__",
2868 "__eq__",
2869 "__ne__",
2870 "__gt__",
2871 "__ge__",
2872 /* These are only for overrides_cmp_or_hash(): */
2873 "__cmp__",
2874 "__hash__",
2875};
2876
2877static int
2878overrides_cmp_or_hash(PyTypeObject *type)
2879{
2880 int i;
2881 PyObject *dict = type->tp_dict;
2882
2883 assert(dict != NULL);
2884 for (i = 0; i < 8; i++) {
2885 if (PyDict_GetItemString(dict, name_op[i]) != NULL)
2886 return 1;
2887 }
2888 return 0;
2889}
2890
Guido van Rossum13d52f02001-08-10 21:24:08 +00002891static void
2892inherit_slots(PyTypeObject *type, PyTypeObject *base)
2893{
2894 PyTypeObject *basebase;
2895
2896#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00002897#undef COPYSLOT
2898#undef COPYNUM
2899#undef COPYSEQ
2900#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00002901#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00002902
2903#define SLOTDEFINED(SLOT) \
2904 (base->SLOT != 0 && \
2905 (basebase == NULL || base->SLOT != basebase->SLOT))
2906
Tim Peters6d6c1a32001-08-02 04:15:00 +00002907#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00002908 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00002909
2910#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
2911#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
2912#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00002913#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002914
Guido van Rossum13d52f02001-08-10 21:24:08 +00002915 /* This won't inherit indirect slots (from tp_as_number etc.)
2916 if type doesn't provide the space. */
2917
2918 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
2919 basebase = base->tp_base;
2920 if (basebase->tp_as_number == NULL)
2921 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002922 COPYNUM(nb_add);
2923 COPYNUM(nb_subtract);
2924 COPYNUM(nb_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002925 COPYNUM(nb_remainder);
2926 COPYNUM(nb_divmod);
2927 COPYNUM(nb_power);
2928 COPYNUM(nb_negative);
2929 COPYNUM(nb_positive);
2930 COPYNUM(nb_absolute);
Jack Diederich4dafcc42006-11-28 19:15:13 +00002931 COPYNUM(nb_bool);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002932 COPYNUM(nb_invert);
2933 COPYNUM(nb_lshift);
2934 COPYNUM(nb_rshift);
2935 COPYNUM(nb_and);
2936 COPYNUM(nb_xor);
2937 COPYNUM(nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002938 COPYNUM(nb_int);
2939 COPYNUM(nb_long);
2940 COPYNUM(nb_float);
2941 COPYNUM(nb_oct);
2942 COPYNUM(nb_hex);
2943 COPYNUM(nb_inplace_add);
2944 COPYNUM(nb_inplace_subtract);
2945 COPYNUM(nb_inplace_multiply);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002946 COPYNUM(nb_inplace_remainder);
2947 COPYNUM(nb_inplace_power);
2948 COPYNUM(nb_inplace_lshift);
2949 COPYNUM(nb_inplace_rshift);
2950 COPYNUM(nb_inplace_and);
2951 COPYNUM(nb_inplace_xor);
2952 COPYNUM(nb_inplace_or);
Neal Norwitzbcc0db82006-03-24 08:14:36 +00002953 COPYNUM(nb_true_divide);
2954 COPYNUM(nb_floor_divide);
2955 COPYNUM(nb_inplace_true_divide);
2956 COPYNUM(nb_inplace_floor_divide);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002957 COPYNUM(nb_index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002958 }
2959
Guido van Rossum13d52f02001-08-10 21:24:08 +00002960 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
2961 basebase = base->tp_base;
2962 if (basebase->tp_as_sequence == NULL)
2963 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002964 COPYSEQ(sq_length);
2965 COPYSEQ(sq_concat);
2966 COPYSEQ(sq_repeat);
2967 COPYSEQ(sq_item);
2968 COPYSEQ(sq_slice);
2969 COPYSEQ(sq_ass_item);
2970 COPYSEQ(sq_ass_slice);
2971 COPYSEQ(sq_contains);
2972 COPYSEQ(sq_inplace_concat);
2973 COPYSEQ(sq_inplace_repeat);
2974 }
2975
Guido van Rossum13d52f02001-08-10 21:24:08 +00002976 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
2977 basebase = base->tp_base;
2978 if (basebase->tp_as_mapping == NULL)
2979 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002980 COPYMAP(mp_length);
2981 COPYMAP(mp_subscript);
2982 COPYMAP(mp_ass_subscript);
2983 }
2984
Tim Petersfc57ccb2001-10-12 02:38:24 +00002985 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
2986 basebase = base->tp_base;
2987 if (basebase->tp_as_buffer == NULL)
2988 basebase = NULL;
2989 COPYBUF(bf_getreadbuffer);
2990 COPYBUF(bf_getwritebuffer);
2991 COPYBUF(bf_getsegcount);
2992 COPYBUF(bf_getcharbuffer);
2993 }
2994
Guido van Rossum13d52f02001-08-10 21:24:08 +00002995 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002996
Tim Peters6d6c1a32001-08-02 04:15:00 +00002997 COPYSLOT(tp_dealloc);
2998 COPYSLOT(tp_print);
2999 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3000 type->tp_getattr = base->tp_getattr;
3001 type->tp_getattro = base->tp_getattro;
3002 }
3003 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3004 type->tp_setattr = base->tp_setattr;
3005 type->tp_setattro = base->tp_setattro;
3006 }
3007 /* tp_compare see tp_richcompare */
3008 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003009 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003010 COPYSLOT(tp_call);
3011 COPYSLOT(tp_str);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003012 {
Guido van Rossum38938152006-08-21 23:36:26 +00003013 /* Copy comparison-related slots only when
3014 not overriding them anywhere */
Guido van Rossumb8f63662001-08-15 23:57:02 +00003015 if (type->tp_compare == NULL &&
3016 type->tp_richcompare == NULL &&
Guido van Rossum38938152006-08-21 23:36:26 +00003017 type->tp_hash == NULL &&
3018 !overrides_cmp_or_hash(type))
Guido van Rossumb8f63662001-08-15 23:57:02 +00003019 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003020 type->tp_compare = base->tp_compare;
3021 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003022 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003023 }
3024 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003025 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003026 COPYSLOT(tp_iter);
3027 COPYSLOT(tp_iternext);
3028 }
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003029 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003030 COPYSLOT(tp_descr_get);
3031 COPYSLOT(tp_descr_set);
3032 COPYSLOT(tp_dictoffset);
3033 COPYSLOT(tp_init);
3034 COPYSLOT(tp_alloc);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00003035 COPYSLOT(tp_is_gc);
Tim Peters3cfe7542003-05-21 21:29:48 +00003036 if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3037 (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3038 /* They agree about gc. */
3039 COPYSLOT(tp_free);
3040 }
3041 else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3042 type->tp_free == NULL &&
3043 base->tp_free == _PyObject_Del) {
3044 /* A bit of magic to plug in the correct default
3045 * tp_free function when a derived class adds gc,
3046 * didn't define tp_free, and the base uses the
3047 * default non-gc tp_free.
3048 */
3049 type->tp_free = PyObject_GC_Del;
3050 }
3051 /* else they didn't agree about gc, and there isn't something
3052 * obvious to be done -- the type is on its own.
3053 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003054 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003055}
3056
Jeremy Hylton938ace62002-07-17 16:30:39 +00003057static int add_operators(PyTypeObject *);
Guido van Rossum13d52f02001-08-10 21:24:08 +00003058
Tim Peters6d6c1a32001-08-02 04:15:00 +00003059int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003060PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003061{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003062 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003063 PyTypeObject *base;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003064 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003065
Guido van Rossumcab05802002-06-10 15:29:03 +00003066 if (type->tp_flags & Py_TPFLAGS_READY) {
3067 assert(type->tp_dict != NULL);
Guido van Rossumd614f972001-08-10 17:39:49 +00003068 return 0;
Guido van Rossumcab05802002-06-10 15:29:03 +00003069 }
Guido van Rossumd614f972001-08-10 17:39:49 +00003070 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00003071
3072 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003073
Tim Peters36eb4df2003-03-23 03:33:13 +00003074#ifdef Py_TRACE_REFS
3075 /* PyType_Ready is the closest thing we have to a choke point
3076 * for type objects, so is the best place I can think of to try
3077 * to get type objects into the doubly-linked list of all objects.
3078 * Still, not all type objects go thru PyType_Ready.
3079 */
Tim Peters7571a0f2003-03-23 17:52:28 +00003080 _Py_AddToAllObjects((PyObject *)type, 0);
Tim Peters36eb4df2003-03-23 03:33:13 +00003081#endif
3082
Tim Peters6d6c1a32001-08-02 04:15:00 +00003083 /* Initialize tp_base (defaults to BaseObject unless that's us) */
3084 base = type->tp_base;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003085 if (base == NULL && type != &PyBaseObject_Type) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00003086 base = type->tp_base = &PyBaseObject_Type;
Martin v. Löwisbf608752004-08-18 13:16:54 +00003087 Py_INCREF(base);
3088 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003089
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003090 /* Now the only way base can still be NULL is if type is
3091 * &PyBaseObject_Type.
3092 */
3093
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003094 /* Initialize the base class */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003095 if (base != NULL && base->tp_dict == NULL) {
Guido van Rossum323a9cf2002-08-14 17:26:30 +00003096 if (PyType_Ready(base) < 0)
3097 goto error;
3098 }
3099
Guido van Rossum0986d822002-04-08 01:38:42 +00003100 /* Initialize ob_type if NULL. This means extensions that want to be
3101 compilable separately on Windows can call PyType_Ready() instead of
3102 initializing the ob_type field of their type objects. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003103 /* The test for base != NULL is really unnecessary, since base is only
3104 NULL when type is &PyBaseObject_Type, and we know its ob_type is
3105 not NULL (it's initialized to &PyType_Type). But coverity doesn't
3106 know that. */
3107 if (type->ob_type == NULL && base != NULL)
Guido van Rossum0986d822002-04-08 01:38:42 +00003108 type->ob_type = base->ob_type;
3109
Tim Peters6d6c1a32001-08-02 04:15:00 +00003110 /* Initialize tp_bases */
3111 bases = type->tp_bases;
3112 if (bases == NULL) {
3113 if (base == NULL)
3114 bases = PyTuple_New(0);
3115 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003116 bases = PyTuple_Pack(1, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003117 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003118 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003119 type->tp_bases = bases;
3120 }
3121
Guido van Rossum687ae002001-10-15 22:03:32 +00003122 /* Initialize tp_dict */
3123 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003124 if (dict == NULL) {
3125 dict = PyDict_New();
3126 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00003127 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00003128 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003129 }
3130
Guido van Rossum687ae002001-10-15 22:03:32 +00003131 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003132 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003133 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003134 if (type->tp_methods != NULL) {
3135 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003136 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003137 }
3138 if (type->tp_members != NULL) {
3139 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003140 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003141 }
3142 if (type->tp_getset != NULL) {
3143 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00003144 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003145 }
3146
Tim Peters6d6c1a32001-08-02 04:15:00 +00003147 /* Calculate method resolution order */
3148 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00003149 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003150 }
3151
Guido van Rossum13d52f02001-08-10 21:24:08 +00003152 /* Inherit special flags from dominant base */
3153 if (type->tp_base != NULL)
3154 inherit_special(type, type->tp_base);
3155
Tim Peters6d6c1a32001-08-02 04:15:00 +00003156 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00003157 bases = type->tp_mro;
3158 assert(bases != NULL);
3159 assert(PyTuple_Check(bases));
3160 n = PyTuple_GET_SIZE(bases);
3161 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003162 PyObject *b = PyTuple_GET_ITEM(bases, i);
3163 if (PyType_Check(b))
3164 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003165 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003166
Tim Peters3cfe7542003-05-21 21:29:48 +00003167 /* Sanity check for tp_free. */
3168 if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
3169 (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
3170 /* This base class needs to call tp_free, but doesn't have
3171 * one, or its tp_free is for non-gc'ed objects.
3172 */
3173 PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
3174 "gc and is a base type but has inappropriate "
3175 "tp_free slot",
3176 type->tp_name);
3177 goto error;
3178 }
3179
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003180 /* if the type dictionary doesn't contain a __doc__, set it from
3181 the tp_doc slot.
3182 */
3183 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
3184 if (type->tp_doc != NULL) {
3185 PyObject *doc = PyString_FromString(type->tp_doc);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003186 if (doc == NULL)
3187 goto error;
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003188 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
3189 Py_DECREF(doc);
3190 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00003191 PyDict_SetItemString(type->tp_dict,
3192 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00003193 }
3194 }
3195
Guido van Rossum38938152006-08-21 23:36:26 +00003196 /* Hack for tp_hash and __hash__.
3197 If after all that, tp_hash is still NULL, and __hash__ is not in
3198 tp_dict, set tp_dict['__hash__'] equal to None.
3199 This signals that __hash__ is not inherited.
3200 */
3201 if (type->tp_hash == NULL) {
3202 if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
3203 if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
3204 goto error;
3205 }
3206 }
3207
Guido van Rossum13d52f02001-08-10 21:24:08 +00003208 /* Some more special stuff */
3209 base = type->tp_base;
3210 if (base != NULL) {
3211 if (type->tp_as_number == NULL)
3212 type->tp_as_number = base->tp_as_number;
3213 if (type->tp_as_sequence == NULL)
3214 type->tp_as_sequence = base->tp_as_sequence;
3215 if (type->tp_as_mapping == NULL)
3216 type->tp_as_mapping = base->tp_as_mapping;
Guido van Rossumeea47182003-02-11 20:39:59 +00003217 if (type->tp_as_buffer == NULL)
3218 type->tp_as_buffer = base->tp_as_buffer;
Guido van Rossum13d52f02001-08-10 21:24:08 +00003219 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003220
Guido van Rossum1c450732001-10-08 15:18:27 +00003221 /* Link into each base class's list of subclasses */
3222 bases = type->tp_bases;
3223 n = PyTuple_GET_SIZE(bases);
3224 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003225 PyObject *b = PyTuple_GET_ITEM(bases, i);
3226 if (PyType_Check(b) &&
3227 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00003228 goto error;
3229 }
3230
Guido van Rossum13d52f02001-08-10 21:24:08 +00003231 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00003232 assert(type->tp_dict != NULL);
3233 type->tp_flags =
3234 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003235 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00003236
3237 error:
3238 type->tp_flags &= ~Py_TPFLAGS_READYING;
3239 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003240}
3241
Guido van Rossum1c450732001-10-08 15:18:27 +00003242static int
3243add_subclass(PyTypeObject *base, PyTypeObject *type)
3244{
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003245 Py_ssize_t i;
3246 int result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003247 PyObject *list, *ref, *newobj;
Guido van Rossum1c450732001-10-08 15:18:27 +00003248
3249 list = base->tp_subclasses;
3250 if (list == NULL) {
3251 base->tp_subclasses = list = PyList_New(0);
3252 if (list == NULL)
3253 return -1;
3254 }
3255 assert(PyList_Check(list));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003256 newobj = PyWeakref_NewRef((PyObject *)type, NULL);
Guido van Rossum1c450732001-10-08 15:18:27 +00003257 i = PyList_GET_SIZE(list);
3258 while (--i >= 0) {
3259 ref = PyList_GET_ITEM(list, i);
3260 assert(PyWeakref_CheckRef(ref));
Guido van Rossum3930bc32002-10-18 13:51:49 +00003261 if (PyWeakref_GET_OBJECT(ref) == Py_None)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003262 return PyList_SetItem(list, i, newobj);
Guido van Rossum1c450732001-10-08 15:18:27 +00003263 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003264 result = PyList_Append(list, newobj);
3265 Py_DECREF(newobj);
Martin v. Löwiseb079f12006-02-16 14:32:27 +00003266 return result;
Guido van Rossum1c450732001-10-08 15:18:27 +00003267}
3268
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003269static void
3270remove_subclass(PyTypeObject *base, PyTypeObject *type)
3271{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003272 Py_ssize_t i;
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003273 PyObject *list, *ref;
3274
3275 list = base->tp_subclasses;
3276 if (list == NULL) {
3277 return;
3278 }
3279 assert(PyList_Check(list));
3280 i = PyList_GET_SIZE(list);
3281 while (--i >= 0) {
3282 ref = PyList_GET_ITEM(list, i);
3283 assert(PyWeakref_CheckRef(ref));
3284 if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
3285 /* this can't fail, right? */
3286 PySequence_DelItem(list, i);
3287 return;
3288 }
3289 }
3290}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003291
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003292static int
3293check_num_args(PyObject *ob, int n)
3294{
3295 if (!PyTuple_CheckExact(ob)) {
3296 PyErr_SetString(PyExc_SystemError,
3297 "PyArg_UnpackTuple() argument list is not a tuple");
3298 return 0;
3299 }
3300 if (n == PyTuple_GET_SIZE(ob))
3301 return 1;
3302 PyErr_Format(
3303 PyExc_TypeError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00003304 "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003305 return 0;
3306}
3307
Tim Peters6d6c1a32001-08-02 04:15:00 +00003308/* Generic wrappers for overloadable 'operators' such as __getitem__ */
3309
3310/* There's a wrapper *function* for each distinct function typedef used
3311 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
3312 wrapper *table* for each distinct operation (e.g. __len__, __add__).
3313 Most tables have only one entry; the tables for binary operators have two
3314 entries, one regular and one with reversed arguments. */
3315
3316static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003317wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003318{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003319 lenfunc func = (lenfunc)wrapped;
3320 Py_ssize_t res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003321
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003322 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003323 return NULL;
3324 res = (*func)(self);
3325 if (res == -1 && PyErr_Occurred())
3326 return NULL;
3327 return PyInt_FromLong((long)res);
3328}
3329
Tim Peters6d6c1a32001-08-02 04:15:00 +00003330static PyObject *
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003331wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
3332{
3333 inquiry func = (inquiry)wrapped;
3334 int res;
3335
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003336 if (!check_num_args(args, 0))
Raymond Hettingerf34f2642003-10-11 17:29:04 +00003337 return NULL;
3338 res = (*func)(self);
3339 if (res == -1 && PyErr_Occurred())
3340 return NULL;
3341 return PyBool_FromLong((long)res);
3342}
3343
3344static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003345wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
3346{
3347 binaryfunc func = (binaryfunc)wrapped;
3348 PyObject *other;
3349
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003350 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003351 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003352 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003353 return (*func)(self, other);
3354}
3355
3356static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003357wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
3358{
3359 binaryfunc func = (binaryfunc)wrapped;
3360 PyObject *other;
3361
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003362 if (!check_num_args(args, 1))
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003363 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003364 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003365 return (*func)(self, other);
3366}
3367
3368static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003369wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
3370{
3371 binaryfunc func = (binaryfunc)wrapped;
3372 PyObject *other;
3373
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003374 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003375 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003376 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00003377 if (!PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00003378 Py_INCREF(Py_NotImplemented);
3379 return Py_NotImplemented;
3380 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003381 return (*func)(other, self);
3382}
3383
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003384static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003385wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
3386{
3387 ternaryfunc func = (ternaryfunc)wrapped;
3388 PyObject *other;
3389 PyObject *third = Py_None;
3390
3391 /* Note: This wrapper only works for __pow__() */
3392
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003393 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003394 return NULL;
3395 return (*func)(self, other, third);
3396}
3397
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00003398static PyObject *
3399wrap_ternaryfunc_r(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
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003407 if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00003408 return NULL;
3409 return (*func)(other, self, third);
3410}
3411
Tim Peters6d6c1a32001-08-02 04:15:00 +00003412static PyObject *
3413wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
3414{
3415 unaryfunc func = (unaryfunc)wrapped;
3416
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003417 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003418 return NULL;
3419 return (*func)(self);
3420}
3421
Tim Peters6d6c1a32001-08-02 04:15:00 +00003422static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003423wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003424{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003425 ssizeargfunc func = (ssizeargfunc)wrapped;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003426 PyObject* o;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003427 Py_ssize_t i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003428
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003429 if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
3430 return NULL;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003431 i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003432 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00003433 return NULL;
3434 return (*func)(self, i);
3435}
3436
Martin v. Löwis18e16552006-02-15 17:27:45 +00003437static Py_ssize_t
Guido van Rossum5d815f32001-08-17 21:57:47 +00003438getindex(PyObject *self, PyObject *arg)
3439{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003440 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003441
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003442 i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003443 if (i == -1 && PyErr_Occurred())
3444 return -1;
3445 if (i < 0) {
3446 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
3447 if (sq && sq->sq_length) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00003448 Py_ssize_t n = (*sq->sq_length)(self);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003449 if (n < 0)
3450 return -1;
3451 i += n;
3452 }
3453 }
3454 return i;
3455}
3456
3457static PyObject *
3458wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
3459{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003460 ssizeargfunc func = (ssizeargfunc)wrapped;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003461 PyObject *arg;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003462 Py_ssize_t i;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003463
Guido van Rossumf4593e02001-10-03 12:09:30 +00003464 if (PyTuple_GET_SIZE(args) == 1) {
3465 arg = PyTuple_GET_ITEM(args, 0);
3466 i = getindex(self, arg);
3467 if (i == -1 && PyErr_Occurred())
3468 return NULL;
3469 return (*func)(self, i);
3470 }
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003471 check_num_args(args, 1);
Guido van Rossumf4593e02001-10-03 12:09:30 +00003472 assert(PyErr_Occurred());
3473 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003474}
3475
Tim Peters6d6c1a32001-08-02 04:15:00 +00003476static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003477wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003478{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003479 ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
3480 Py_ssize_t i, j;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003481
Martin v. Löwis18e16552006-02-15 17:27:45 +00003482 if (!PyArg_ParseTuple(args, "nn", &i, &j))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003483 return NULL;
3484 return (*func)(self, i, j);
3485}
3486
Tim Peters6d6c1a32001-08-02 04:15:00 +00003487static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00003488wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003489{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003490 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3491 Py_ssize_t i;
3492 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003493 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003494
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003495 if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
Guido van Rossum5d815f32001-08-17 21:57:47 +00003496 return NULL;
3497 i = getindex(self, arg);
3498 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00003499 return NULL;
3500 res = (*func)(self, i, value);
3501 if (res == -1 && PyErr_Occurred())
3502 return NULL;
3503 Py_INCREF(Py_None);
3504 return Py_None;
3505}
3506
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003507static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00003508wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003509{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003510 ssizeobjargproc func = (ssizeobjargproc)wrapped;
3511 Py_ssize_t i;
3512 int res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00003513 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003514
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003515 if (!check_num_args(args, 1))
Guido van Rossum5d815f32001-08-17 21:57:47 +00003516 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003517 arg = PyTuple_GET_ITEM(args, 0);
Guido van Rossum5d815f32001-08-17 21:57:47 +00003518 i = getindex(self, arg);
3519 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003520 return NULL;
3521 res = (*func)(self, i, NULL);
3522 if (res == -1 && PyErr_Occurred())
3523 return NULL;
3524 Py_INCREF(Py_None);
3525 return Py_None;
3526}
3527
Tim Peters6d6c1a32001-08-02 04:15:00 +00003528static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00003529wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003530{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003531 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3532 Py_ssize_t i, j;
3533 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003534 PyObject *value;
3535
Martin v. Löwis18e16552006-02-15 17:27:45 +00003536 if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003537 return NULL;
3538 res = (*func)(self, i, j, value);
3539 if (res == -1 && PyErr_Occurred())
3540 return NULL;
3541 Py_INCREF(Py_None);
3542 return Py_None;
3543}
3544
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003545static PyObject *
3546wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
3547{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003548 ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
3549 Py_ssize_t i, j;
3550 int res;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003551
Martin v. Löwis18e16552006-02-15 17:27:45 +00003552 if (!PyArg_ParseTuple(args, "nn", &i, &j))
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003553 return NULL;
3554 res = (*func)(self, i, j, NULL);
3555 if (res == -1 && PyErr_Occurred())
3556 return NULL;
3557 Py_INCREF(Py_None);
3558 return Py_None;
3559}
3560
Tim Peters6d6c1a32001-08-02 04:15:00 +00003561/* XXX objobjproc is a misnomer; should be objargpred */
3562static PyObject *
3563wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
3564{
3565 objobjproc func = (objobjproc)wrapped;
3566 int res;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00003567 PyObject *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003568
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003569 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003570 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003571 value = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003572 res = (*func)(self, value);
3573 if (res == -1 && PyErr_Occurred())
3574 return NULL;
Guido van Rossum22c3dda2003-10-09 03:46:35 +00003575 else
3576 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003577}
3578
Tim Peters6d6c1a32001-08-02 04:15:00 +00003579static PyObject *
3580wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
3581{
3582 objobjargproc func = (objobjargproc)wrapped;
3583 int res;
3584 PyObject *key, *value;
3585
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003586 if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003587 return NULL;
3588 res = (*func)(self, key, value);
3589 if (res == -1 && PyErr_Occurred())
3590 return NULL;
3591 Py_INCREF(Py_None);
3592 return Py_None;
3593}
3594
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003595static PyObject *
3596wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
3597{
3598 objobjargproc func = (objobjargproc)wrapped;
3599 int res;
3600 PyObject *key;
3601
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003602 if (!check_num_args(args, 1))
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003603 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003604 key = PyTuple_GET_ITEM(args, 0);
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00003605 res = (*func)(self, key, NULL);
3606 if (res == -1 && PyErr_Occurred())
3607 return NULL;
3608 Py_INCREF(Py_None);
3609 return Py_None;
3610}
3611
Tim Peters6d6c1a32001-08-02 04:15:00 +00003612static PyObject *
3613wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
3614{
3615 cmpfunc func = (cmpfunc)wrapped;
3616 int res;
3617 PyObject *other;
3618
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003619 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003620 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003621 other = PyTuple_GET_ITEM(args, 0);
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00003622 if (other->ob_type->tp_compare != func &&
3623 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00003624 PyErr_Format(
3625 PyExc_TypeError,
3626 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
3627 self->ob_type->tp_name,
3628 self->ob_type->tp_name,
3629 other->ob_type->tp_name);
3630 return NULL;
3631 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003632 res = (*func)(self, other);
3633 if (PyErr_Occurred())
3634 return NULL;
3635 return PyInt_FromLong((long)res);
3636}
3637
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003638/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
Guido van Rossum52b27052003-04-15 20:05:10 +00003639 This is called the Carlo Verre hack after its discoverer. */
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003640static int
3641hackcheck(PyObject *self, setattrofunc func, char *what)
3642{
3643 PyTypeObject *type = self->ob_type;
3644 while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
3645 type = type->tp_base;
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003646 /* If type is NULL now, this is a really weird type.
Thomas Wouters89f507f2006-12-13 04:49:30 +00003647 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003648 if (type && type->tp_setattro != func) {
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003649 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
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003665 if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003666 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
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003683 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003684 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003685 name = PyTuple_GET_ITEM(args, 0);
Guido van Rossum4dcdb782003-04-14 21:46:03 +00003686 if (!hackcheck(self, func, "__delattr__"))
3687 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003688 res = (*func)(self, name, NULL);
3689 if (res < 0)
3690 return NULL;
3691 Py_INCREF(Py_None);
3692 return Py_None;
3693}
3694
Tim Peters6d6c1a32001-08-02 04:15:00 +00003695static PyObject *
3696wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
3697{
3698 hashfunc func = (hashfunc)wrapped;
3699 long res;
3700
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003701 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003702 return NULL;
3703 res = (*func)(self);
3704 if (res == -1 && PyErr_Occurred())
3705 return NULL;
3706 return PyInt_FromLong(res);
3707}
3708
Tim Peters6d6c1a32001-08-02 04:15:00 +00003709static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00003710wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003711{
3712 ternaryfunc func = (ternaryfunc)wrapped;
3713
Guido van Rossumc8e56452001-10-22 00:43:43 +00003714 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003715}
3716
Tim Peters6d6c1a32001-08-02 04:15:00 +00003717static PyObject *
3718wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
3719{
3720 richcmpfunc func = (richcmpfunc)wrapped;
3721 PyObject *other;
3722
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003723 if (!check_num_args(args, 1))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003724 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003725 other = PyTuple_GET_ITEM(args, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003726 return (*func)(self, other, op);
3727}
3728
3729#undef RICHCMP_WRAPPER
3730#define RICHCMP_WRAPPER(NAME, OP) \
3731static PyObject * \
3732richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
3733{ \
3734 return wrap_richcmpfunc(self, args, wrapped, OP); \
3735}
3736
Jack Jansen8e938b42001-08-08 15:29:49 +00003737RICHCMP_WRAPPER(lt, Py_LT)
3738RICHCMP_WRAPPER(le, Py_LE)
3739RICHCMP_WRAPPER(eq, Py_EQ)
3740RICHCMP_WRAPPER(ne, Py_NE)
3741RICHCMP_WRAPPER(gt, Py_GT)
3742RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003743
Tim Peters6d6c1a32001-08-02 04:15:00 +00003744static PyObject *
3745wrap_next(PyObject *self, PyObject *args, void *wrapped)
3746{
3747 unaryfunc func = (unaryfunc)wrapped;
3748 PyObject *res;
3749
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003750 if (!check_num_args(args, 0))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003751 return NULL;
3752 res = (*func)(self);
3753 if (res == NULL && !PyErr_Occurred())
3754 PyErr_SetNone(PyExc_StopIteration);
3755 return res;
3756}
3757
Tim Peters6d6c1a32001-08-02 04:15:00 +00003758static PyObject *
3759wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
3760{
3761 descrgetfunc func = (descrgetfunc)wrapped;
3762 PyObject *obj;
3763 PyObject *type = NULL;
3764
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003765 if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003766 return NULL;
Guido van Rossum82ed25c2003-02-11 16:25:43 +00003767 if (obj == Py_None)
3768 obj = NULL;
3769 if (type == Py_None)
3770 type = NULL;
3771 if (type == NULL &&obj == NULL) {
3772 PyErr_SetString(PyExc_TypeError,
3773 "__get__(None, None) is invalid");
3774 return NULL;
3775 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003776 return (*func)(self, obj, type);
3777}
3778
Tim Peters6d6c1a32001-08-02 04:15:00 +00003779static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003780wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003781{
3782 descrsetfunc func = (descrsetfunc)wrapped;
3783 PyObject *obj, *value;
3784 int ret;
3785
Raymond Hettinger56bb16f2003-10-11 19:32:18 +00003786 if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003787 return NULL;
3788 ret = (*func)(self, obj, value);
3789 if (ret < 0)
3790 return NULL;
3791 Py_INCREF(Py_None);
3792 return Py_None;
3793}
Guido van Rossum22b13872002-08-06 21:41:44 +00003794
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00003795static PyObject *
3796wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
3797{
3798 descrsetfunc func = (descrsetfunc)wrapped;
3799 PyObject *obj;
3800 int ret;
3801
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003802 if (!check_num_args(args, 1))
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00003803 return NULL;
Raymond Hettinger6a8bbdb2003-12-13 15:21:55 +00003804 obj = PyTuple_GET_ITEM(args, 0);
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00003805 ret = (*func)(self, obj, NULL);
3806 if (ret < 0)
3807 return NULL;
3808 Py_INCREF(Py_None);
3809 return Py_None;
3810}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003811
Tim Peters6d6c1a32001-08-02 04:15:00 +00003812static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00003813wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003814{
3815 initproc func = (initproc)wrapped;
3816
Guido van Rossumc8e56452001-10-22 00:43:43 +00003817 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003818 return NULL;
3819 Py_INCREF(Py_None);
3820 return Py_None;
3821}
3822
Tim Peters6d6c1a32001-08-02 04:15:00 +00003823static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003824tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003825{
Barry Warsaw60f01882001-08-22 19:24:42 +00003826 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003827 PyObject *arg0, *res;
3828
3829 if (self == NULL || !PyType_Check(self))
3830 Py_FatalError("__new__() called with non-type 'self'");
3831 type = (PyTypeObject *)self;
3832 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003833 PyErr_Format(PyExc_TypeError,
3834 "%s.__new__(): not enough arguments",
3835 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003836 return NULL;
3837 }
3838 arg0 = PyTuple_GET_ITEM(args, 0);
3839 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003840 PyErr_Format(PyExc_TypeError,
3841 "%s.__new__(X): X is not a type object (%s)",
3842 type->tp_name,
3843 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003844 return NULL;
3845 }
3846 subtype = (PyTypeObject *)arg0;
3847 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003848 PyErr_Format(PyExc_TypeError,
3849 "%s.__new__(%s): %s is not a subtype of %s",
3850 type->tp_name,
3851 subtype->tp_name,
3852 subtype->tp_name,
3853 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003854 return NULL;
3855 }
Barry Warsaw60f01882001-08-22 19:24:42 +00003856
3857 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00003858 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00003859 most derived base that's not a heap type is this type. */
3860 staticbase = subtype;
3861 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
3862 staticbase = staticbase->tp_base;
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003863 /* If staticbase is NULL now, it is a really weird type.
Thomas Wouters89f507f2006-12-13 04:49:30 +00003864 In the spirit of backwards compatibility (?), just shut up. */
Guido van Rossum692cdbc2006-03-10 02:04:28 +00003865 if (staticbase && staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003866 PyErr_Format(PyExc_TypeError,
3867 "%s.__new__(%s) is not safe, use %s.__new__()",
3868 type->tp_name,
3869 subtype->tp_name,
3870 staticbase == NULL ? "?" : staticbase->tp_name);
3871 return NULL;
3872 }
3873
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003874 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3875 if (args == NULL)
3876 return NULL;
3877 res = type->tp_new(subtype, args, kwds);
3878 Py_DECREF(args);
3879 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003880}
3881
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003882static struct PyMethodDef tp_new_methoddef[] = {
3883 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003884 PyDoc_STR("T.__new__(S, ...) -> "
3885 "a new object with type S, a subtype of T")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00003886 {0}
3887};
3888
3889static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003890add_tp_new_wrapper(PyTypeObject *type)
3891{
Guido van Rossumf040ede2001-08-07 16:40:56 +00003892 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003893
Guido van Rossum687ae002001-10-15 22:03:32 +00003894 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00003895 return 0;
3896 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003897 if (func == NULL)
3898 return -1;
Raymond Hettinger8d726ee2004-06-25 22:24:35 +00003899 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
Raymond Hettingerd56cbe52004-06-25 22:17:39 +00003900 Py_DECREF(func);
3901 return -1;
3902 }
3903 Py_DECREF(func);
3904 return 0;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003905}
3906
Guido van Rossumf040ede2001-08-07 16:40:56 +00003907/* Slot wrappers that call the corresponding __foo__ slot. See comments
3908 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003909
Guido van Rossumdc91b992001-08-08 22:26:22 +00003910#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003911static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003912FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003913{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00003914 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003915 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003916}
3917
Guido van Rossumdc91b992001-08-08 22:26:22 +00003918#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003919static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003920FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003921{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003922 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003923 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003924}
3925
Guido van Rossumcd118802003-01-06 22:57:47 +00003926/* Boolean helper for SLOT1BINFULL().
3927 right.__class__ is a nontrivial subclass of left.__class__. */
3928static int
3929method_is_overloaded(PyObject *left, PyObject *right, char *name)
3930{
3931 PyObject *a, *b;
3932 int ok;
3933
3934 b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
3935 if (b == NULL) {
3936 PyErr_Clear();
3937 /* If right doesn't have it, it's not overloaded */
3938 return 0;
3939 }
3940
3941 a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
3942 if (a == NULL) {
3943 PyErr_Clear();
3944 Py_DECREF(b);
3945 /* If right has it but left doesn't, it's overloaded */
3946 return 1;
3947 }
3948
3949 ok = PyObject_RichCompareBool(a, b, Py_NE);
3950 Py_DECREF(a);
3951 Py_DECREF(b);
3952 if (ok < 0) {
3953 PyErr_Clear();
3954 return 0;
3955 }
3956
3957 return ok;
3958}
3959
Guido van Rossumdc91b992001-08-08 22:26:22 +00003960
3961#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003962static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003963FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003964{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003965 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00003966 int do_other = self->ob_type != other->ob_type && \
3967 other->ob_type->tp_as_number != NULL && \
3968 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003969 if (self->ob_type->tp_as_number != NULL && \
3970 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
3971 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00003972 if (do_other && \
Guido van Rossumcd118802003-01-06 22:57:47 +00003973 PyType_IsSubtype(other->ob_type, self->ob_type) && \
3974 method_is_overloaded(self, other, ROPSTR)) { \
Guido van Rossum55f20992001-10-01 17:18:22 +00003975 r = call_maybe( \
3976 other, ROPSTR, &rcache_str, "(O)", self); \
3977 if (r != Py_NotImplemented) \
3978 return r; \
3979 Py_DECREF(r); \
3980 do_other = 0; \
3981 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00003982 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00003983 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003984 if (r != Py_NotImplemented || \
3985 other->ob_type == self->ob_type) \
3986 return r; \
3987 Py_DECREF(r); \
3988 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00003989 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00003990 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00003991 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003992 } \
3993 Py_INCREF(Py_NotImplemented); \
3994 return Py_NotImplemented; \
3995}
3996
3997#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
3998 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
3999
4000#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
4001static PyObject * \
4002FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
4003{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00004004 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00004005 return call_method(self, OPSTR, &cache_str, \
4006 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00004007}
4008
Martin v. Löwis18e16552006-02-15 17:27:45 +00004009static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +00004010slot_sq_length(PyObject *self)
4011{
Guido van Rossum2730b132001-08-28 18:22:14 +00004012 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004013 PyObject *res = call_method(self, "__len__", &len_str, "()");
Martin v. Löwis18e16552006-02-15 17:27:45 +00004014 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004015
4016 if (res == NULL)
4017 return -1;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004018 len = PyInt_AsSsize_t(res);
Guido van Rossum26111622001-10-01 16:42:49 +00004019 Py_DECREF(res);
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004020 if (len < 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004021 if (!PyErr_Occurred())
4022 PyErr_SetString(PyExc_ValueError,
4023 "__len__() should return >= 0");
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00004024 return -1;
4025 }
Guido van Rossum26111622001-10-01 16:42:49 +00004026 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004027}
4028
Guido van Rossumf4593e02001-10-03 12:09:30 +00004029/* Super-optimized version of slot_sq_item.
4030 Other slots could do the same... */
4031static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00004032slot_sq_item(PyObject *self, Py_ssize_t i)
Guido van Rossumf4593e02001-10-03 12:09:30 +00004033{
4034 static PyObject *getitem_str;
4035 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
4036 descrgetfunc f;
4037
4038 if (getitem_str == NULL) {
4039 getitem_str = PyString_InternFromString("__getitem__");
4040 if (getitem_str == NULL)
4041 return NULL;
4042 }
4043 func = _PyType_Lookup(self->ob_type, getitem_str);
4044 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00004045 if ((f = func->ob_type->tp_descr_get) == NULL)
4046 Py_INCREF(func);
Neal Norwitz673cd822002-10-18 16:33:13 +00004047 else {
Guido van Rossumf4593e02001-10-03 12:09:30 +00004048 func = f(func, self, (PyObject *)(self->ob_type));
Neal Norwitz673cd822002-10-18 16:33:13 +00004049 if (func == NULL) {
4050 return NULL;
4051 }
4052 }
Martin v. Löwiseb079f12006-02-16 14:32:27 +00004053 ival = PyInt_FromSsize_t(i);
Guido van Rossumf4593e02001-10-03 12:09:30 +00004054 if (ival != NULL) {
4055 args = PyTuple_New(1);
4056 if (args != NULL) {
4057 PyTuple_SET_ITEM(args, 0, ival);
4058 retval = PyObject_Call(func, args, NULL);
4059 Py_XDECREF(args);
4060 Py_XDECREF(func);
4061 return retval;
4062 }
4063 }
4064 }
4065 else {
4066 PyErr_SetObject(PyExc_AttributeError, getitem_str);
4067 }
4068 Py_XDECREF(args);
4069 Py_XDECREF(ival);
4070 Py_XDECREF(func);
4071 return NULL;
4072}
4073
Martin v. Löwis18e16552006-02-15 17:27:45 +00004074SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004075
4076static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004077slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004078{
4079 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004080 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004081
4082 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004083 res = call_method(self, "__delitem__", &delitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004084 "(n)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004085 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004086 res = call_method(self, "__setitem__", &setitem_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004087 "(nO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004088 if (res == NULL)
4089 return -1;
4090 Py_DECREF(res);
4091 return 0;
4092}
4093
4094static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004095slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004096{
4097 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004098 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004099
4100 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004101 res = call_method(self, "__delslice__", &delslice_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004102 "(nn)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004103 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004104 res = call_method(self, "__setslice__", &setslice_str,
Thomas Wouters3fc2ca32006-04-21 11:28:17 +00004105 "(nnO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004106 if (res == NULL)
4107 return -1;
4108 Py_DECREF(res);
4109 return 0;
4110}
4111
4112static int
4113slot_sq_contains(PyObject *self, PyObject *value)
4114{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004115 PyObject *func, *res, *args;
Tim Petersbf9b2442003-03-23 05:35:36 +00004116 int result = -1;
4117
Guido van Rossum60718732001-08-28 17:47:51 +00004118 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004119
Guido van Rossum55f20992001-10-01 17:18:22 +00004120 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004121 if (func != NULL) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004122 args = PyTuple_Pack(1, value);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004123 if (args == NULL)
4124 res = NULL;
4125 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004126 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004127 Py_DECREF(args);
4128 }
4129 Py_DECREF(func);
Tim Petersbf9b2442003-03-23 05:35:36 +00004130 if (res != NULL) {
4131 result = PyObject_IsTrue(res);
4132 Py_DECREF(res);
4133 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004134 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004135 else if (! PyErr_Occurred()) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00004136 /* Possible results: -1 and 1 */
4137 result = (int)_PySequence_IterSearch(self, value,
Tim Petersbf9b2442003-03-23 05:35:36 +00004138 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004139 }
Tim Petersbf9b2442003-03-23 05:35:36 +00004140 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004141}
4142
Tim Peters6d6c1a32001-08-02 04:15:00 +00004143#define slot_mp_length slot_sq_length
4144
Guido van Rossumdc91b992001-08-08 22:26:22 +00004145SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004146
4147static int
4148slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
4149{
4150 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004151 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004152
4153 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004154 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004155 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004156 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004157 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004158 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004159 if (res == NULL)
4160 return -1;
4161 Py_DECREF(res);
4162 return 0;
4163}
4164
Guido van Rossumdc91b992001-08-08 22:26:22 +00004165SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
4166SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
4167SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004168SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
4169SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
4170
Jeremy Hylton938ace62002-07-17 16:30:39 +00004171static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
Guido van Rossumdc91b992001-08-08 22:26:22 +00004172
4173SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
4174 nb_power, "__pow__", "__rpow__")
4175
4176static PyObject *
4177slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
4178{
Guido van Rossum2730b132001-08-28 18:22:14 +00004179 static PyObject *pow_str;
4180
Guido van Rossumdc91b992001-08-08 22:26:22 +00004181 if (modulus == Py_None)
4182 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00004183 /* Three-arg power doesn't use __rpow__. But ternary_op
4184 can call this when the second argument's type uses
4185 slot_nb_power, so check before calling self.__pow__. */
4186 if (self->ob_type->tp_as_number != NULL &&
4187 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
4188 return call_method(self, "__pow__", &pow_str,
4189 "(OO)", other, modulus);
4190 }
4191 Py_INCREF(Py_NotImplemented);
4192 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00004193}
4194
4195SLOT0(slot_nb_negative, "__neg__")
4196SLOT0(slot_nb_positive, "__pos__")
4197SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004198
4199static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00004200slot_nb_bool(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004201{
Tim Petersea7f75d2002-12-07 21:39:16 +00004202 PyObject *func, *args;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004203 static PyObject *bool_str, *len_str;
Tim Petersea7f75d2002-12-07 21:39:16 +00004204 int result = -1;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004205 int from_len = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004206
Jack Diederich4dafcc42006-11-28 19:15:13 +00004207 func = lookup_maybe(self, "__bool__", &bool_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004208 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00004209 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00004210 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00004211 func = lookup_maybe(self, "__len__", &len_str);
Tim Petersea7f75d2002-12-07 21:39:16 +00004212 if (func == NULL)
4213 return PyErr_Occurred() ? -1 : 1;
Jack Diederich4dafcc42006-11-28 19:15:13 +00004214 from_len = 1;
Tim Petersea7f75d2002-12-07 21:39:16 +00004215 }
4216 args = PyTuple_New(0);
4217 if (args != NULL) {
4218 PyObject *temp = PyObject_Call(func, args, NULL);
4219 Py_DECREF(args);
4220 if (temp != NULL) {
Jack Diederich4dafcc42006-11-28 19:15:13 +00004221 if (from_len) {
4222 /* enforced by slot_nb_len */
Jeremy Hylton090a3492003-06-27 16:46:45 +00004223 result = PyObject_IsTrue(temp);
Jack Diederich4dafcc42006-11-28 19:15:13 +00004224 }
4225 else if (PyBool_Check(temp)) {
4226 result = PyObject_IsTrue(temp);
4227 }
Jeremy Hylton090a3492003-06-27 16:46:45 +00004228 else {
4229 PyErr_Format(PyExc_TypeError,
Jack Diederich4dafcc42006-11-28 19:15:13 +00004230 "__bool__ should return "
4231 "bool, returned %s",
4232 temp->ob_type->tp_name);
Jeremy Hylton3e3159c2003-06-27 17:38:27 +00004233 result = -1;
Jeremy Hylton090a3492003-06-27 16:46:45 +00004234 }
Tim Petersea7f75d2002-12-07 21:39:16 +00004235 Py_DECREF(temp);
Guido van Rossum55f20992001-10-01 17:18:22 +00004236 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00004237 }
Guido van Rossum55f20992001-10-01 17:18:22 +00004238 Py_DECREF(func);
Tim Petersea7f75d2002-12-07 21:39:16 +00004239 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004240}
4241
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004242
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004243static PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004244slot_nb_index(PyObject *self)
4245{
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004246 static PyObject *index_str;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004247 return call_method(self, "__index__", &index_str, "()");
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004248}
4249
4250
Guido van Rossumdc91b992001-08-08 22:26:22 +00004251SLOT0(slot_nb_invert, "__invert__")
4252SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
4253SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
4254SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
4255SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
4256SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00004257
Guido van Rossumdc91b992001-08-08 22:26:22 +00004258SLOT0(slot_nb_int, "__int__")
4259SLOT0(slot_nb_long, "__long__")
4260SLOT0(slot_nb_float, "__float__")
4261SLOT0(slot_nb_oct, "__oct__")
4262SLOT0(slot_nb_hex, "__hex__")
4263SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
4264SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
4265SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004266SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
Guido van Rossum6e5680f2002-10-15 01:01:53 +00004267SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00004268SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
4269SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
4270SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
4271SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
4272SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
4273SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
4274 "__floordiv__", "__rfloordiv__")
4275SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
4276SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
4277SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00004278
4279static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00004280half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004281{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004282 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004283 static PyObject *cmp_str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004284 Py_ssize_t c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004285
Guido van Rossum60718732001-08-28 17:47:51 +00004286 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004287 if (func == NULL) {
4288 PyErr_Clear();
4289 }
4290 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004291 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004292 if (args == NULL)
4293 res = NULL;
4294 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004295 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004296 Py_DECREF(args);
4297 }
Raymond Hettingerab5dae32002-06-24 13:08:16 +00004298 Py_DECREF(func);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004299 if (res != Py_NotImplemented) {
4300 if (res == NULL)
4301 return -2;
4302 c = PyInt_AsLong(res);
4303 Py_DECREF(res);
4304 if (c == -1 && PyErr_Occurred())
4305 return -2;
4306 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
4307 }
4308 Py_DECREF(res);
4309 }
4310 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004311}
4312
Guido van Rossumab3b0342001-09-18 20:38:53 +00004313/* This slot is published for the benefit of try_3way_compare in object.c */
4314int
4315_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00004316{
4317 int c;
4318
Guido van Rossumab3b0342001-09-18 20:38:53 +00004319 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004320 c = half_compare(self, other);
4321 if (c <= 1)
4322 return c;
4323 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00004324 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004325 c = half_compare(other, self);
4326 if (c < -1)
4327 return -2;
4328 if (c <= 1)
4329 return -c;
4330 }
4331 return (void *)self < (void *)other ? -1 :
4332 (void *)self > (void *)other ? 1 : 0;
4333}
4334
4335static PyObject *
4336slot_tp_repr(PyObject *self)
4337{
4338 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004339 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004340
Guido van Rossum60718732001-08-28 17:47:51 +00004341 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004342 if (func != NULL) {
4343 res = PyEval_CallObject(func, NULL);
4344 Py_DECREF(func);
4345 return res;
4346 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00004347 PyErr_Clear();
4348 return PyString_FromFormat("<%s object at %p>",
4349 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004350}
4351
4352static PyObject *
4353slot_tp_str(PyObject *self)
4354{
4355 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004356 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004357
Guido van Rossum60718732001-08-28 17:47:51 +00004358 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004359 if (func != NULL) {
4360 res = PyEval_CallObject(func, NULL);
4361 Py_DECREF(func);
4362 return res;
4363 }
4364 else {
4365 PyErr_Clear();
4366 return slot_tp_repr(self);
4367 }
4368}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004369
4370static long
4371slot_tp_hash(PyObject *self)
4372{
Guido van Rossum4011a242006-08-17 23:09:57 +00004373 PyObject *func, *res;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004374 static PyObject *hash_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004375 long h;
4376
Guido van Rossum60718732001-08-28 17:47:51 +00004377 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004378
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004379 if (func == Py_None) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00004380 Py_DECREF(func);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004381 func = NULL;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004382 }
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004383
4384 if (func == NULL) {
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004385 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
4386 self->ob_type->tp_name);
4387 return -1;
4388 }
4389
Guido van Rossum4011a242006-08-17 23:09:57 +00004390 res = PyEval_CallObject(func, NULL);
Guido van Rossum50e9fb92006-08-17 05:42:55 +00004391 Py_DECREF(func);
4392 if (res == NULL)
4393 return -1;
4394 if (PyLong_Check(res))
4395 h = PyLong_Type.tp_hash(res);
4396 else
4397 h = PyInt_AsLong(res);
4398 Py_DECREF(res);
4399 if (h == -1 && !PyErr_Occurred())
4400 h = -2;
4401 return h;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004402}
4403
4404static PyObject *
4405slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
4406{
Guido van Rossum60718732001-08-28 17:47:51 +00004407 static PyObject *call_str;
4408 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004409 PyObject *res;
4410
4411 if (meth == NULL)
4412 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004413
4414 /* PyObject_Call() will end up calling slot_tp_call() again if
4415 the object returned for __call__ has __call__ itself defined
4416 upon it. This can be an infinite recursion if you set
4417 __call__ in a class to an instance of it. */
4418 if (Py_EnterRecursiveCall(" in __call__")) {
4419 Py_DECREF(meth);
4420 return NULL;
4421 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004422 res = PyObject_Call(meth, args, kwds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004423 Py_LeaveRecursiveCall();
4424
Tim Peters6d6c1a32001-08-02 04:15:00 +00004425 Py_DECREF(meth);
4426 return res;
4427}
4428
Guido van Rossum14a6f832001-10-17 13:59:09 +00004429/* There are two slot dispatch functions for tp_getattro.
4430
4431 - slot_tp_getattro() is used when __getattribute__ is overridden
4432 but no __getattr__ hook is present;
4433
4434 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
4435
Guido van Rossumc334df52002-04-04 23:44:47 +00004436 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
4437 detects the absence of __getattr__ and then installs the simpler slot if
4438 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00004439
Tim Peters6d6c1a32001-08-02 04:15:00 +00004440static PyObject *
4441slot_tp_getattro(PyObject *self, PyObject *name)
4442{
Guido van Rossum14a6f832001-10-17 13:59:09 +00004443 static PyObject *getattribute_str = NULL;
4444 return call_method(self, "__getattribute__", &getattribute_str,
4445 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004446}
4447
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004448static PyObject *
4449slot_tp_getattr_hook(PyObject *self, PyObject *name)
4450{
4451 PyTypeObject *tp = self->ob_type;
4452 PyObject *getattr, *getattribute, *res;
4453 static PyObject *getattribute_str = NULL;
4454 static PyObject *getattr_str = NULL;
4455
4456 if (getattr_str == NULL) {
4457 getattr_str = PyString_InternFromString("__getattr__");
4458 if (getattr_str == NULL)
4459 return NULL;
4460 }
4461 if (getattribute_str == NULL) {
4462 getattribute_str =
4463 PyString_InternFromString("__getattribute__");
4464 if (getattribute_str == NULL)
4465 return NULL;
4466 }
4467 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004468 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004469 /* No __getattr__ hook: use a simpler dispatcher */
4470 tp->tp_getattro = slot_tp_getattro;
4471 return slot_tp_getattro(self, name);
4472 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004473 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004474 if (getattribute == NULL ||
4475 (getattribute->ob_type == &PyWrapperDescr_Type &&
4476 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
4477 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004478 res = PyObject_GenericGetAttr(self, name);
4479 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00004480 res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL);
Guido van Rossum14a6f832001-10-17 13:59:09 +00004481 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004482 PyErr_Clear();
Thomas Wouters477c8d52006-05-27 19:21:47 +00004483 res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00004484 }
4485 return res;
4486}
4487
Tim Peters6d6c1a32001-08-02 04:15:00 +00004488static int
4489slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
4490{
4491 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004492 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004493
4494 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00004495 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004496 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004497 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004498 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004499 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004500 if (res == NULL)
4501 return -1;
4502 Py_DECREF(res);
4503 return 0;
4504}
4505
Tim Peters6d6c1a32001-08-02 04:15:00 +00004506static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00004507half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004508{
Guido van Rossumb8f63662001-08-15 23:57:02 +00004509 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004510 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00004511
Guido van Rossum60718732001-08-28 17:47:51 +00004512 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004513 if (func == NULL) {
4514 PyErr_Clear();
4515 Py_INCREF(Py_NotImplemented);
4516 return Py_NotImplemented;
4517 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00004518 args = PyTuple_Pack(1, other);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004519 if (args == NULL)
4520 res = NULL;
4521 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00004522 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004523 Py_DECREF(args);
4524 }
4525 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004526 return res;
4527}
4528
Guido van Rossumb8f63662001-08-15 23:57:02 +00004529static PyObject *
4530slot_tp_richcompare(PyObject *self, PyObject *other, int op)
4531{
4532 PyObject *res;
4533
4534 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
4535 res = half_richcompare(self, other, op);
4536 if (res != Py_NotImplemented)
4537 return res;
4538 Py_DECREF(res);
4539 }
4540 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
Tim Petersf4aca752004-09-23 02:39:37 +00004541 res = half_richcompare(other, self, _Py_SwappedOp[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004542 if (res != Py_NotImplemented) {
4543 return res;
4544 }
4545 Py_DECREF(res);
4546 }
4547 Py_INCREF(Py_NotImplemented);
4548 return Py_NotImplemented;
4549}
4550
4551static PyObject *
4552slot_tp_iter(PyObject *self)
4553{
4554 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00004555 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004556
Guido van Rossum60718732001-08-28 17:47:51 +00004557 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004558 if (func != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +00004559 PyObject *args;
4560 args = res = PyTuple_New(0);
4561 if (args != NULL) {
4562 res = PyObject_Call(func, args, NULL);
4563 Py_DECREF(args);
4564 }
4565 Py_DECREF(func);
4566 return res;
Guido van Rossumb8f63662001-08-15 23:57:02 +00004567 }
4568 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00004569 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004570 if (func == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004571 PyErr_Format(PyExc_TypeError,
4572 "'%.200s' object is not iterable",
4573 self->ob_type->tp_name);
Guido van Rossumb8f63662001-08-15 23:57:02 +00004574 return NULL;
4575 }
4576 Py_DECREF(func);
4577 return PySeqIter_New(self);
4578}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004579
4580static PyObject *
4581slot_tp_iternext(PyObject *self)
4582{
Guido van Rossum2730b132001-08-28 18:22:14 +00004583 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00004584 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00004585}
4586
Guido van Rossum1a493502001-08-17 16:47:50 +00004587static PyObject *
4588slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4589{
4590 PyTypeObject *tp = self->ob_type;
4591 PyObject *get;
4592 static PyObject *get_str = NULL;
4593
4594 if (get_str == NULL) {
4595 get_str = PyString_InternFromString("__get__");
4596 if (get_str == NULL)
4597 return NULL;
4598 }
4599 get = _PyType_Lookup(tp, get_str);
4600 if (get == NULL) {
4601 /* Avoid further slowdowns */
4602 if (tp->tp_descr_get == slot_tp_descr_get)
4603 tp->tp_descr_get = NULL;
4604 Py_INCREF(self);
4605 return self;
4606 }
Guido van Rossum2c252392001-08-24 10:13:31 +00004607 if (obj == NULL)
4608 obj = Py_None;
4609 if (type == NULL)
4610 type = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00004611 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
Guido van Rossum1a493502001-08-17 16:47:50 +00004612}
Tim Peters6d6c1a32001-08-02 04:15:00 +00004613
4614static int
4615slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
4616{
Guido van Rossum2c252392001-08-24 10:13:31 +00004617 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00004618 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00004619
4620 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00004621 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004622 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00004623 else
Guido van Rossum2730b132001-08-28 18:22:14 +00004624 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00004625 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004626 if (res == NULL)
4627 return -1;
4628 Py_DECREF(res);
4629 return 0;
4630}
4631
4632static int
4633slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
4634{
Guido van Rossum60718732001-08-28 17:47:51 +00004635 static PyObject *init_str;
4636 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004637 PyObject *res;
4638
4639 if (meth == NULL)
4640 return -1;
4641 res = PyObject_Call(meth, args, kwds);
4642 Py_DECREF(meth);
4643 if (res == NULL)
4644 return -1;
Raymond Hettingerb67cc802005-03-03 16:45:19 +00004645 if (res != Py_None) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004646 PyErr_Format(PyExc_TypeError,
4647 "__init__() should return None, not '%.200s'",
4648 res->ob_type->tp_name);
Raymond Hettingerb67cc802005-03-03 16:45:19 +00004649 Py_DECREF(res);
4650 return -1;
4651 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004652 Py_DECREF(res);
4653 return 0;
4654}
4655
4656static PyObject *
4657slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4658{
Guido van Rossum7bed2132002-08-08 21:57:53 +00004659 static PyObject *new_str;
4660 PyObject *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004661 PyObject *newargs, *x;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004662 Py_ssize_t i, n;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004663
Guido van Rossum7bed2132002-08-08 21:57:53 +00004664 if (new_str == NULL) {
4665 new_str = PyString_InternFromString("__new__");
4666 if (new_str == NULL)
4667 return NULL;
4668 }
4669 func = PyObject_GetAttr((PyObject *)type, new_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004670 if (func == NULL)
4671 return NULL;
4672 assert(PyTuple_Check(args));
4673 n = PyTuple_GET_SIZE(args);
4674 newargs = PyTuple_New(n+1);
4675 if (newargs == NULL)
4676 return NULL;
4677 Py_INCREF(type);
4678 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
4679 for (i = 0; i < n; i++) {
4680 x = PyTuple_GET_ITEM(args, i);
4681 Py_INCREF(x);
4682 PyTuple_SET_ITEM(newargs, i+1, x);
4683 }
4684 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00004685 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004686 Py_DECREF(func);
4687 return x;
4688}
4689
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004690static void
4691slot_tp_del(PyObject *self)
4692{
4693 static PyObject *del_str = NULL;
4694 PyObject *del, *res;
4695 PyObject *error_type, *error_value, *error_traceback;
4696
4697 /* Temporarily resurrect the object. */
4698 assert(self->ob_refcnt == 0);
4699 self->ob_refcnt = 1;
4700
4701 /* Save the current exception, if any. */
4702 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4703
4704 /* Execute __del__ method, if any. */
4705 del = lookup_maybe(self, "__del__", &del_str);
4706 if (del != NULL) {
4707 res = PyEval_CallObject(del, NULL);
4708 if (res == NULL)
4709 PyErr_WriteUnraisable(del);
4710 else
4711 Py_DECREF(res);
4712 Py_DECREF(del);
4713 }
4714
4715 /* Restore the saved exception. */
4716 PyErr_Restore(error_type, error_value, error_traceback);
4717
4718 /* Undo the temporary resurrection; can't use DECREF here, it would
4719 * cause a recursive call.
4720 */
4721 assert(self->ob_refcnt > 0);
4722 if (--self->ob_refcnt == 0)
4723 return; /* this is the normal path out */
4724
4725 /* __del__ resurrected it! Make it look like the original Py_DECREF
4726 * never happened.
4727 */
4728 {
Martin v. Löwis725507b2006-03-07 12:08:51 +00004729 Py_ssize_t refcnt = self->ob_refcnt;
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004730 _Py_NewReference(self);
4731 self->ob_refcnt = refcnt;
4732 }
4733 assert(!PyType_IS_GC(self->ob_type) ||
4734 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
Michael W. Hudson3f3b6682004-08-03 10:21:03 +00004735 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
4736 * we need to undo that. */
4737 _Py_DEC_REFTOTAL;
4738 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
4739 * chain, so no more to do there.
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004740 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
4741 * _Py_NewReference bumped tp_allocs: both of those need to be
4742 * undone.
4743 */
4744#ifdef COUNT_ALLOCS
4745 --self->ob_type->tp_frees;
4746 --self->ob_type->tp_allocs;
4747#endif
4748}
4749
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004750
4751/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
Tim Petersbf9b2442003-03-23 05:35:36 +00004752 functions. The offsets here are relative to the 'PyHeapTypeObject'
Guido van Rossume5c691a2003-03-07 15:13:17 +00004753 structure, which incorporates the additional structures used for numbers,
4754 sequences and mappings.
4755 Note that multiple names may map to the same slot (e.g. __eq__,
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004756 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00004757 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
4758 terminated with an all-zero entry. (This table is further initialized and
4759 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004760
Guido van Rossum6d204072001-10-21 00:44:31 +00004761typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004762
4763#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00004764#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004765#undef ETSLOT
4766#undef SQSLOT
4767#undef MPSLOT
4768#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00004769#undef UNSLOT
4770#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004771#undef BINSLOT
4772#undef RBINSLOT
4773
Guido van Rossum6d204072001-10-21 00:44:31 +00004774#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004775 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
4776 PyDoc_STR(DOC)}
Guido van Rossumc8e56452001-10-22 00:43:43 +00004777#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
4778 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004779 PyDoc_STR(DOC), FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00004780#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Guido van Rossume5c691a2003-03-07 15:13:17 +00004781 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00004782 PyDoc_STR(DOC)}
Guido van Rossum6d204072001-10-21 00:44:31 +00004783#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4784 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
4785#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4786 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
4787#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4788 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
4789#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4790 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4791 "x." NAME "() <==> " DOC)
4792#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
4793 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
4794 "x." NAME "(y) <==> x" DOC "y")
4795#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
4796 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4797 "x." NAME "(y) <==> x" DOC "y")
4798#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
4799 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4800 "x." NAME "(y) <==> y" DOC "x")
Anthony Baxter56616992005-06-03 14:12:21 +00004801#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4802 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
4803 "x." NAME "(y) <==> " DOC)
4804#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
4805 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
4806 "x." NAME "(y) <==> " DOC)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004807
4808static slotdef slotdefs[] = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00004809 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00004810 "x.__len__() <==> len(x)"),
Armin Rigofd163f92005-12-29 15:59:19 +00004811 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
4812 The logic in abstract.c always falls back to nb_add/nb_multiply in
4813 this case. Defining both the nb_* and the sq_* slots to call the
4814 user-defined methods has unexpected side-effects, as shown by
4815 test_descr.notimplemented() */
4816 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
4817 "x.__add__(y) <==> x+y"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004818 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
Armin Rigofd163f92005-12-29 15:59:19 +00004819 "x.__mul__(n) <==> x*n"),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004820 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
Armin Rigofd163f92005-12-29 15:59:19 +00004821 "x.__rmul__(n) <==> n*x"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004822 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
4823 "x.__getitem__(y) <==> x[y]"),
Martin v. Löwis18e16552006-02-15 17:27:45 +00004824 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
Brett Cannon154da9b2003-05-20 02:30:04 +00004825 "x.__getslice__(i, j) <==> x[i:j]\n\
4826 \n\
4827 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004828 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00004829 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004830 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
Brett Cannonbe67d872003-05-20 02:40:12 +00004831 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004832 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Martin v. Löwis18e16552006-02-15 17:27:45 +00004833 wrap_ssizessizeobjargproc,
Brett Cannonbe67d872003-05-20 02:40:12 +00004834 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
4835 \n\
4836 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004837 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
Brett Cannonbe67d872003-05-20 02:40:12 +00004838 "x.__delslice__(i, j) <==> del x[i:j]\n\
4839 \n\
4840 Use of negative indices is not supported."),
Guido van Rossum6d204072001-10-21 00:44:31 +00004841 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
4842 "x.__contains__(y) <==> y in x"),
Armin Rigofd163f92005-12-29 15:59:19 +00004843 SQSLOT("__iadd__", sq_inplace_concat, NULL,
4844 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
4845 SQSLOT("__imul__", sq_inplace_repeat, NULL,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004846 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004847
Martin v. Löwis18e16552006-02-15 17:27:45 +00004848 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
Guido van Rossum6d204072001-10-21 00:44:31 +00004849 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00004850 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004851 wrap_binaryfunc,
4852 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004853 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004854 wrap_objobjargproc,
4855 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004856 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004857 wrap_delitem,
4858 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004859
Guido van Rossum6d204072001-10-21 00:44:31 +00004860 BINSLOT("__add__", nb_add, slot_nb_add,
4861 "+"),
4862 RBINSLOT("__radd__", nb_add, slot_nb_add,
4863 "+"),
4864 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
4865 "-"),
4866 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
4867 "-"),
4868 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
4869 "*"),
4870 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
4871 "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004872 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
4873 "%"),
4874 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
4875 "%"),
Anthony Baxter56616992005-06-03 14:12:21 +00004876 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00004877 "divmod(x, y)"),
Anthony Baxter56616992005-06-03 14:12:21 +00004878 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
Guido van Rossum6d204072001-10-21 00:44:31 +00004879 "divmod(y, x)"),
4880 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
4881 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
4882 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
4883 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
4884 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
4885 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
4886 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
4887 "abs(x)"),
Jack Diederich4dafcc42006-11-28 19:15:13 +00004888 UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
Guido van Rossum6d204072001-10-21 00:44:31 +00004889 "x != 0"),
4890 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
4891 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
4892 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
4893 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
4894 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
4895 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
4896 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
4897 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
4898 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
4899 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
4900 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004901 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
4902 "int(x)"),
4903 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
4904 "long(x)"),
4905 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
4906 "float(x)"),
4907 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
4908 "oct(x)"),
4909 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
4910 "hex(x)"),
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004911 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004912 "x[y:z] <==> x[y.__index__():z.__index__()]"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004913 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
4914 wrap_binaryfunc, "+"),
4915 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
4916 wrap_binaryfunc, "-"),
4917 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
4918 wrap_binaryfunc, "*"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004919 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
4920 wrap_binaryfunc, "%"),
4921 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
Guido van Rossum6e5680f2002-10-15 01:01:53 +00004922 wrap_binaryfunc, "**"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004923 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
4924 wrap_binaryfunc, "<<"),
4925 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
4926 wrap_binaryfunc, ">>"),
4927 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
4928 wrap_binaryfunc, "&"),
4929 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
4930 wrap_binaryfunc, "^"),
4931 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
4932 wrap_binaryfunc, "|"),
4933 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
4934 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
4935 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
4936 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
4937 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
4938 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
4939 IBSLOT("__itruediv__", nb_inplace_true_divide,
4940 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004941
Guido van Rossum6d204072001-10-21 00:44:31 +00004942 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
4943 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00004944 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00004945 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
4946 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00004947 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00004948 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
4949 "x.__cmp__(y) <==> cmp(x,y)"),
4950 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
4951 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00004952 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
4953 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004954 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00004955 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
4956 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
4957 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
4958 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
4959 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
4960 "x.__setattr__('name', value) <==> x.name = value"),
4961 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
4962 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
4963 "x.__delattr__('name') <==> del x.name"),
4964 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
4965 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
4966 "x.__lt__(y) <==> x<y"),
4967 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
4968 "x.__le__(y) <==> x<=y"),
4969 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
4970 "x.__eq__(y) <==> x==y"),
4971 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
4972 "x.__ne__(y) <==> x!=y"),
4973 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
4974 "x.__gt__(y) <==> x>y"),
4975 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
4976 "x.__ge__(y) <==> x>=y"),
4977 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
4978 "x.__iter__() <==> iter(x)"),
4979 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
4980 "x.next() -> the next value, or raise StopIteration"),
4981 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
4982 "descr.__get__(obj[, type]) -> value"),
4983 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
4984 "descr.__set__(obj, value)"),
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004985 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
4986 wrap_descr_delete, "descr.__delete__(obj)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00004987 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00004988 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00004989 "see x.__class__.__doc__ for signature",
4990 PyWrapperFlag_KEYWORDS),
4991 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004992 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004993 {NULL}
4994};
4995
Guido van Rossumc334df52002-04-04 23:44:47 +00004996/* Given a type pointer and an offset gotten from a slotdef entry, return a
4997 pointer to the actual slot. This is not quite the same as simply adding
4998 the offset to the type pointer, since it takes care to indirect through the
4999 proper indirection pointer (as_buffer, etc.); it returns NULL if the
5000 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005001static void **
Martin v. Löwis18e16552006-02-15 17:27:45 +00005002slotptr(PyTypeObject *type, int ioffset)
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005003{
5004 char *ptr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005005 long offset = ioffset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005006
Guido van Rossume5c691a2003-03-07 15:13:17 +00005007 /* Note: this depends on the order of the members of PyHeapTypeObject! */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005008 assert(offset >= 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005009 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
5010 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
5011 ptr = (char *)type->tp_as_sequence;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005012 offset -= offsetof(PyHeapTypeObject, as_sequence);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005013 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005014 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
5015 ptr = (char *)type->tp_as_mapping;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005016 offset -= offsetof(PyHeapTypeObject, as_mapping);
Guido van Rossum09638c12002-06-13 19:17:46 +00005017 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005018 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
5019 ptr = (char *)type->tp_as_number;
Guido van Rossume5c691a2003-03-07 15:13:17 +00005020 offset -= offsetof(PyHeapTypeObject, as_number);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005021 }
5022 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005023 ptr = (char *)type;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005024 }
5025 if (ptr != NULL)
5026 ptr += offset;
5027 return (void **)ptr;
5028}
Guido van Rossumf040ede2001-08-07 16:40:56 +00005029
Guido van Rossumc334df52002-04-04 23:44:47 +00005030/* Length of array of slotdef pointers used to store slots with the
5031 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
5032 the same __name__, for any __name__. Since that's a static property, it is
5033 appropriate to declare fixed-size arrays for this. */
5034#define MAX_EQUIV 10
5035
5036/* Return a slot pointer for a given name, but ONLY if the attribute has
5037 exactly one slot function. The name must be an interned string. */
5038static void **
5039resolve_slotdups(PyTypeObject *type, PyObject *name)
5040{
5041 /* XXX Maybe this could be optimized more -- but is it worth it? */
5042
5043 /* pname and ptrs act as a little cache */
5044 static PyObject *pname;
5045 static slotdef *ptrs[MAX_EQUIV];
5046 slotdef *p, **pp;
5047 void **res, **ptr;
5048
5049 if (pname != name) {
5050 /* Collect all slotdefs that match name into ptrs. */
5051 pname = name;
5052 pp = ptrs;
5053 for (p = slotdefs; p->name_strobj; p++) {
5054 if (p->name_strobj == name)
5055 *pp++ = p;
5056 }
5057 *pp = NULL;
5058 }
5059
5060 /* Look in all matching slots of the type; if exactly one of these has
5061 a filled-in slot, return its value. Otherwise return NULL. */
5062 res = NULL;
5063 for (pp = ptrs; *pp; pp++) {
5064 ptr = slotptr(type, (*pp)->offset);
5065 if (ptr == NULL || *ptr == NULL)
5066 continue;
5067 if (res != NULL)
5068 return NULL;
5069 res = ptr;
5070 }
5071 return res;
5072}
5073
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005074/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
Guido van Rossumc334df52002-04-04 23:44:47 +00005075 does some incredibly complex thinking and then sticks something into the
5076 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
5077 interests, and then stores a generic wrapper or a specific function into
5078 the slot.) Return a pointer to the next slotdef with a different offset,
5079 because that's convenient for fixup_slot_dispatchers(). */
5080static slotdef *
5081update_one_slot(PyTypeObject *type, slotdef *p)
5082{
5083 PyObject *descr;
5084 PyWrapperDescrObject *d;
5085 void *generic = NULL, *specific = NULL;
5086 int use_generic = 0;
5087 int offset = p->offset;
5088 void **ptr = slotptr(type, offset);
5089
5090 if (ptr == NULL) {
5091 do {
5092 ++p;
5093 } while (p->offset == offset);
5094 return p;
5095 }
5096 do {
5097 descr = _PyType_Lookup(type, p->name_strobj);
5098 if (descr == NULL)
5099 continue;
5100 if (descr->ob_type == &PyWrapperDescr_Type) {
5101 void **tptr = resolve_slotdups(type, p->name_strobj);
5102 if (tptr == NULL || tptr == ptr)
5103 generic = p->function;
5104 d = (PyWrapperDescrObject *)descr;
5105 if (d->d_base->wrapper == p->wrapper &&
5106 PyType_IsSubtype(type, d->d_type))
5107 {
5108 if (specific == NULL ||
5109 specific == d->d_wrapped)
5110 specific = d->d_wrapped;
5111 else
5112 use_generic = 1;
5113 }
5114 }
Guido van Rossum721f62e2002-08-09 02:14:34 +00005115 else if (descr->ob_type == &PyCFunction_Type &&
5116 PyCFunction_GET_FUNCTION(descr) ==
5117 (PyCFunction)tp_new_wrapper &&
5118 strcmp(p->name, "__new__") == 0)
5119 {
5120 /* The __new__ wrapper is not a wrapper descriptor,
5121 so must be special-cased differently.
5122 If we don't do this, creating an instance will
5123 always use slot_tp_new which will look up
5124 __new__ in the MRO which will call tp_new_wrapper
5125 which will look through the base classes looking
5126 for a static base and call its tp_new (usually
5127 PyType_GenericNew), after performing various
5128 sanity checks and constructing a new argument
5129 list. Cut all that nonsense short -- this speeds
5130 up instance creation tremendously. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005131 specific = (void *)type->tp_new;
Guido van Rossum721f62e2002-08-09 02:14:34 +00005132 /* XXX I'm not 100% sure that there isn't a hole
5133 in this reasoning that requires additional
5134 sanity checks. I'll buy the first person to
5135 point out a bug in this reasoning a beer. */
5136 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005137 else {
5138 use_generic = 1;
5139 generic = p->function;
5140 }
5141 } while ((++p)->offset == offset);
5142 if (specific && !use_generic)
5143 *ptr = specific;
5144 else
5145 *ptr = generic;
5146 return p;
5147}
5148
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005149/* In the type, update the slots whose slotdefs are gathered in the pp array.
5150 This is a callback for update_subclasses(). */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005151static int
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005152update_slots_callback(PyTypeObject *type, void *data)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005153{
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005154 slotdef **pp = (slotdef **)data;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005155
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005156 for (; *pp; pp++)
Guido van Rossumc334df52002-04-04 23:44:47 +00005157 update_one_slot(type, *pp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005158 return 0;
5159}
5160
Guido van Rossumc334df52002-04-04 23:44:47 +00005161/* Comparison function for qsort() to compare slotdefs by their offset, and
5162 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005163static int
5164slotdef_cmp(const void *aa, const void *bb)
5165{
5166 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
5167 int c = a->offset - b->offset;
5168 if (c != 0)
5169 return c;
5170 else
Martin v. Löwis18e16552006-02-15 17:27:45 +00005171 /* Cannot use a-b, as this gives off_t,
5172 which may lose precision when converted to int. */
5173 return (a > b) ? 1 : (a < b) ? -1 : 0;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005174}
5175
Guido van Rossumc334df52002-04-04 23:44:47 +00005176/* Initialize the slotdefs table by adding interned string objects for the
5177 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005178static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005179init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005180{
5181 slotdef *p;
5182 static int initialized = 0;
5183
5184 if (initialized)
5185 return;
5186 for (p = slotdefs; p->name; p++) {
5187 p->name_strobj = PyString_InternFromString(p->name);
5188 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00005189 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005190 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005191 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
5192 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005193 initialized = 1;
5194}
5195
Guido van Rossumc334df52002-04-04 23:44:47 +00005196/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005197static int
5198update_slot(PyTypeObject *type, PyObject *name)
5199{
Guido van Rossumc334df52002-04-04 23:44:47 +00005200 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005201 slotdef *p;
5202 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005203 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005204
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005205 init_slotdefs();
5206 pp = ptrs;
5207 for (p = slotdefs; p->name; p++) {
5208 /* XXX assume name is interned! */
5209 if (p->name_strobj == name)
5210 *pp++ = p;
5211 }
5212 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005213 for (pp = ptrs; *pp; pp++) {
5214 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005215 offset = p->offset;
5216 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005217 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00005218 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005219 }
Guido van Rossumc334df52002-04-04 23:44:47 +00005220 if (ptrs[0] == NULL)
5221 return 0; /* Not an attribute that affects any slots */
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005222 return update_subclasses(type, name,
5223 update_slots_callback, (void *)ptrs);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00005224}
5225
Guido van Rossumc334df52002-04-04 23:44:47 +00005226/* Store the proper functions in the slot dispatches at class (type)
5227 definition time, based upon which operations the class overrides in its
5228 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00005229static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005230fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00005231{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00005232 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005233
Guido van Rossumd396b9c2001-10-13 20:02:41 +00005234 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00005235 for (p = slotdefs; p->name; )
5236 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00005237}
Guido van Rossum705f0f52001-08-24 16:47:00 +00005238
Michael W. Hudson98bbc492002-11-26 14:47:27 +00005239static void
5240update_all_slots(PyTypeObject* type)
5241{
5242 slotdef *p;
5243
5244 init_slotdefs();
5245 for (p = slotdefs; p->name; p++) {
5246 /* update_slot returns int but can't actually fail */
5247 update_slot(type, p->name_strobj);
5248 }
5249}
5250
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005251/* recurse_down_subclasses() and update_subclasses() are mutually
5252 recursive functions to call a callback for all subclasses,
5253 but refraining from recursing into subclasses that define 'name'. */
5254
5255static int
5256update_subclasses(PyTypeObject *type, PyObject *name,
5257 update_callback callback, void *data)
5258{
5259 if (callback(type, data) < 0)
5260 return -1;
5261 return recurse_down_subclasses(type, name, callback, data);
5262}
5263
5264static int
5265recurse_down_subclasses(PyTypeObject *type, PyObject *name,
5266 update_callback callback, void *data)
5267{
5268 PyTypeObject *subclass;
5269 PyObject *ref, *subclasses, *dict;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005270 Py_ssize_t i, n;
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005271
5272 subclasses = type->tp_subclasses;
5273 if (subclasses == NULL)
5274 return 0;
5275 assert(PyList_Check(subclasses));
5276 n = PyList_GET_SIZE(subclasses);
5277 for (i = 0; i < n; i++) {
5278 ref = PyList_GET_ITEM(subclasses, i);
5279 assert(PyWeakref_CheckRef(ref));
5280 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
5281 assert(subclass != NULL);
5282 if ((PyObject *)subclass == Py_None)
5283 continue;
5284 assert(PyType_Check(subclass));
5285 /* Avoid recursing down into unaffected classes */
5286 dict = subclass->tp_dict;
5287 if (dict != NULL && PyDict_Check(dict) &&
5288 PyDict_GetItem(dict, name) != NULL)
5289 continue;
5290 if (update_subclasses(subclass, name, callback, data) < 0)
5291 return -1;
5292 }
5293 return 0;
5294}
5295
Guido van Rossum6d204072001-10-21 00:44:31 +00005296/* This function is called by PyType_Ready() to populate the type's
5297 dictionary with method descriptors for function slots. For each
Guido van Rossum09638c12002-06-13 19:17:46 +00005298 function slot (like tp_repr) that's defined in the type, one or more
5299 corresponding descriptors are added in the type's tp_dict dictionary
5300 under the appropriate name (like __repr__). Some function slots
5301 cause more than one descriptor to be added (for example, the nb_add
5302 slot adds both __add__ and __radd__ descriptors) and some function
5303 slots compete for the same descriptor (for example both sq_item and
5304 mp_subscript generate a __getitem__ descriptor).
5305
5306 In the latter case, the first slotdef entry encoutered wins. Since
Tim Petersbf9b2442003-03-23 05:35:36 +00005307 slotdef entries are sorted by the offset of the slot in the
Guido van Rossume5c691a2003-03-07 15:13:17 +00005308 PyHeapTypeObject, this gives us some control over disambiguating
Guido van Rossum8d24ee92003-03-24 23:49:49 +00005309 between competing slots: the members of PyHeapTypeObject are listed
5310 from most general to least general, so the most general slot is
5311 preferred. In particular, because as_mapping comes before as_sequence,
5312 for a type that defines both mp_subscript and sq_item, mp_subscript
5313 wins.
Guido van Rossum09638c12002-06-13 19:17:46 +00005314
5315 This only adds new descriptors and doesn't overwrite entries in
5316 tp_dict that were previously defined. The descriptors contain a
5317 reference to the C function they must call, so that it's safe if they
5318 are copied into a subtype's __dict__ and the subtype has a different
5319 C function in its slot -- calling the method defined by the
5320 descriptor will call the C function that was used to create it,
5321 rather than the C function present in the slot when it is called.
5322 (This is important because a subtype may have a C function in the
5323 slot that calls the method from the dictionary, and we want to avoid
5324 infinite recursion here.) */
Guido van Rossum6d204072001-10-21 00:44:31 +00005325
5326static int
5327add_operators(PyTypeObject *type)
5328{
5329 PyObject *dict = type->tp_dict;
5330 slotdef *p;
5331 PyObject *descr;
5332 void **ptr;
5333
5334 init_slotdefs();
5335 for (p = slotdefs; p->name; p++) {
5336 if (p->wrapper == NULL)
5337 continue;
5338 ptr = slotptr(type, p->offset);
5339 if (!ptr || !*ptr)
5340 continue;
5341 if (PyDict_GetItem(dict, p->name_strobj))
5342 continue;
5343 descr = PyDescr_NewWrapper(type, p, *ptr);
5344 if (descr == NULL)
5345 return -1;
5346 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
5347 return -1;
5348 Py_DECREF(descr);
5349 }
5350 if (type->tp_new != NULL) {
5351 if (add_tp_new_wrapper(type) < 0)
5352 return -1;
5353 }
5354 return 0;
5355}
5356
Guido van Rossum705f0f52001-08-24 16:47:00 +00005357
5358/* Cooperative 'super' */
5359
5360typedef struct {
5361 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00005362 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005363 PyObject *obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005364 PyTypeObject *obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005365} superobject;
5366
Guido van Rossum6f799372001-09-20 20:46:19 +00005367static PyMemberDef super_members[] = {
5368 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
5369 "the class invoking super()"},
5370 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
5371 "the instance invoking super(); may be None"},
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005372 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00005373 "the type of the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005374 {0}
5375};
5376
Guido van Rossum705f0f52001-08-24 16:47:00 +00005377static void
5378super_dealloc(PyObject *self)
5379{
5380 superobject *su = (superobject *)self;
5381
Guido van Rossum048eb752001-10-02 21:24:57 +00005382 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005383 Py_XDECREF(su->obj);
5384 Py_XDECREF(su->type);
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005385 Py_XDECREF(su->obj_type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005386 self->ob_type->tp_free(self);
5387}
5388
5389static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005390super_repr(PyObject *self)
5391{
5392 superobject *su = (superobject *)self;
5393
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005394 if (su->obj_type)
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005395 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005396 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005397 su->type ? su->type->tp_name : "NULL",
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005398 su->obj_type->tp_name);
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005399 else
5400 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00005401 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005402 su->type ? su->type->tp_name : "NULL");
5403}
5404
5405static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00005406super_getattro(PyObject *self, PyObject *name)
5407{
5408 superobject *su = (superobject *)self;
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005409 int skip = su->obj_type == NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005410
Guido van Rossum76ba09f2003-04-16 19:40:58 +00005411 if (!skip) {
5412 /* We want __class__ to return the class of the super object
5413 (i.e. super, or a subclass), not the class of su->obj. */
5414 skip = (PyString_Check(name) &&
5415 PyString_GET_SIZE(name) == 9 &&
5416 strcmp(PyString_AS_STRING(name), "__class__") == 0);
5417 }
5418
5419 if (!skip) {
Tim Petersa91e9642001-11-14 23:32:33 +00005420 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005421 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005422 descrgetfunc f;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005423 Py_ssize_t i, n;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005424
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005425 starttype = su->obj_type;
Guido van Rossum155db9a2002-04-02 17:53:47 +00005426 mro = starttype->tp_mro;
5427
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005428 if (mro == NULL)
5429 n = 0;
5430 else {
5431 assert(PyTuple_Check(mro));
5432 n = PyTuple_GET_SIZE(mro);
5433 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005434 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00005435 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00005436 break;
5437 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005438 i++;
5439 res = NULL;
5440 for (; i < n; i++) {
5441 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00005442 if (PyType_Check(tmp))
5443 dict = ((PyTypeObject *)tmp)->tp_dict;
Tim Petersa91e9642001-11-14 23:32:33 +00005444 else
5445 continue;
5446 res = PyDict_GetItem(dict, name);
Guido van Rossum6cc5bb62003-04-16 20:01:36 +00005447 if (res != NULL) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00005448 Py_INCREF(res);
5449 f = res->ob_type->tp_descr_get;
5450 if (f != NULL) {
Phillip J. Eby91a968a2004-03-25 02:19:34 +00005451 tmp = f(res,
5452 /* Only pass 'obj' param if
5453 this is instance-mode super
5454 (See SF ID #743627)
5455 */
Hye-Shik Changff365c92004-03-25 16:37:03 +00005456 (su->obj == (PyObject *)
5457 su->obj_type
Phillip J. Eby91a968a2004-03-25 02:19:34 +00005458 ? (PyObject *)NULL
5459 : su->obj),
Guido van Rossumd4641072002-04-03 02:13:37 +00005460 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005461 Py_DECREF(res);
5462 res = tmp;
5463 }
5464 return res;
5465 }
5466 }
5467 }
5468 return PyObject_GenericGetAttr(self, name);
5469}
5470
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005471static PyTypeObject *
Guido van Rossum5b443c62001-12-03 15:38:28 +00005472supercheck(PyTypeObject *type, PyObject *obj)
5473{
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005474 /* Check that a super() call makes sense. Return a type object.
5475
5476 obj can be a new-style class, or an instance of one:
5477
5478 - If it is a class, it must be a subclass of 'type'. This case is
5479 used for class methods; the return value is obj.
5480
5481 - If it is an instance, it must be an instance of 'type'. This is
5482 the normal case; the return value is obj.__class__.
5483
5484 But... when obj is an instance, we want to allow for the case where
5485 obj->ob_type is not a subclass of type, but obj.__class__ is!
5486 This will allow using super() with a proxy for obj.
5487 */
5488
Guido van Rossum8e80a722003-02-18 19:22:22 +00005489 /* Check for first bullet above (special case) */
5490 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
5491 Py_INCREF(obj);
5492 return (PyTypeObject *)obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005493 }
Guido van Rossum8e80a722003-02-18 19:22:22 +00005494
5495 /* Normal case */
5496 if (PyType_IsSubtype(obj->ob_type, type)) {
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005497 Py_INCREF(obj->ob_type);
5498 return obj->ob_type;
5499 }
5500 else {
5501 /* Try the slow way */
5502 static PyObject *class_str = NULL;
5503 PyObject *class_attr;
5504
5505 if (class_str == NULL) {
5506 class_str = PyString_FromString("__class__");
5507 if (class_str == NULL)
5508 return NULL;
5509 }
5510
5511 class_attr = PyObject_GetAttr(obj, class_str);
5512
5513 if (class_attr != NULL &&
5514 PyType_Check(class_attr) &&
5515 (PyTypeObject *)class_attr != obj->ob_type)
5516 {
5517 int ok = PyType_IsSubtype(
5518 (PyTypeObject *)class_attr, type);
5519 if (ok)
5520 return (PyTypeObject *)class_attr;
5521 }
5522
5523 if (class_attr == NULL)
5524 PyErr_Clear();
5525 else
5526 Py_DECREF(class_attr);
5527 }
5528
Tim Peters97e5ff52003-02-18 19:32:50 +00005529 PyErr_SetString(PyExc_TypeError,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005530 "super(type, obj): "
5531 "obj must be an instance or subtype of type");
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005532 return NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00005533}
5534
Guido van Rossum705f0f52001-08-24 16:47:00 +00005535static PyObject *
5536super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5537{
5538 superobject *su = (superobject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005539 superobject *newobj;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005540
5541 if (obj == NULL || obj == Py_None || su->obj != NULL) {
5542 /* Not binding to an object, or already bound */
5543 Py_INCREF(self);
5544 return self;
5545 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00005546 if (su->ob_type != &PySuper_Type)
Armin Rigo7726dc02005-05-15 15:32:08 +00005547 /* If su is an instance of a (strict) subclass of super,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005548 call its type */
Thomas Wouters477c8d52006-05-27 19:21:47 +00005549 return PyObject_CallFunctionObjArgs((PyObject *)su->ob_type,
5550 su->type, obj, NULL);
Guido van Rossum5b443c62001-12-03 15:38:28 +00005551 else {
5552 /* Inline the common case */
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005553 PyTypeObject *obj_type = supercheck(su->type, obj);
5554 if (obj_type == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00005555 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005556 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
Guido van Rossum5b443c62001-12-03 15:38:28 +00005557 NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005558 if (newobj == NULL)
Guido van Rossum5b443c62001-12-03 15:38:28 +00005559 return NULL;
5560 Py_INCREF(su->type);
5561 Py_INCREF(obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005562 newobj->type = su->type;
5563 newobj->obj = obj;
5564 newobj->obj_type = obj_type;
5565 return (PyObject *)newobj;
Guido van Rossum5b443c62001-12-03 15:38:28 +00005566 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005567}
5568
5569static int
5570super_init(PyObject *self, PyObject *args, PyObject *kwds)
5571{
5572 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00005573 PyTypeObject *type;
5574 PyObject *obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005575 PyTypeObject *obj_type = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005576
Thomas Wouters89f507f2006-12-13 04:49:30 +00005577 if (!_PyArg_NoKeywords("super", kwds))
5578 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005579 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
5580 return -1;
5581 if (obj == Py_None)
5582 obj = NULL;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005583 if (obj != NULL) {
5584 obj_type = supercheck(type, obj);
5585 if (obj_type == NULL)
5586 return -1;
5587 Py_INCREF(obj);
5588 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00005589 Py_INCREF(type);
Guido van Rossum705f0f52001-08-24 16:47:00 +00005590 su->type = type;
5591 su->obj = obj;
Guido van Rossuma89d10e2003-02-12 03:58:38 +00005592 su->obj_type = obj_type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00005593 return 0;
5594}
5595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005596PyDoc_STRVAR(super_doc,
Guido van Rossum705f0f52001-08-24 16:47:00 +00005597"super(type) -> unbound super object\n"
5598"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00005599"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00005600"Typical use to call a cooperative superclass method:\n"
5601"class C(B):\n"
5602" def meth(self, arg):\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005603" super(C, self).meth(arg)");
Guido van Rossum705f0f52001-08-24 16:47:00 +00005604
Guido van Rossum048eb752001-10-02 21:24:57 +00005605static int
5606super_traverse(PyObject *self, visitproc visit, void *arg)
5607{
5608 superobject *su = (superobject *)self;
Guido van Rossum048eb752001-10-02 21:24:57 +00005609
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005610 Py_VISIT(su->obj);
5611 Py_VISIT(su->type);
5612 Py_VISIT(su->obj_type);
Guido van Rossum048eb752001-10-02 21:24:57 +00005613
5614 return 0;
5615}
5616
Guido van Rossum705f0f52001-08-24 16:47:00 +00005617PyTypeObject PySuper_Type = {
5618 PyObject_HEAD_INIT(&PyType_Type)
5619 0, /* ob_size */
5620 "super", /* tp_name */
5621 sizeof(superobject), /* tp_basicsize */
5622 0, /* tp_itemsize */
5623 /* methods */
5624 super_dealloc, /* tp_dealloc */
5625 0, /* tp_print */
5626 0, /* tp_getattr */
5627 0, /* tp_setattr */
5628 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005629 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005630 0, /* tp_as_number */
5631 0, /* tp_as_sequence */
5632 0, /* tp_as_mapping */
5633 0, /* tp_hash */
5634 0, /* tp_call */
5635 0, /* tp_str */
5636 super_getattro, /* tp_getattro */
5637 0, /* tp_setattro */
5638 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00005639 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
5640 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005641 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00005642 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005643 0, /* tp_clear */
5644 0, /* tp_richcompare */
5645 0, /* tp_weaklistoffset */
5646 0, /* tp_iter */
5647 0, /* tp_iternext */
5648 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00005649 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005650 0, /* tp_getset */
5651 0, /* tp_base */
5652 0, /* tp_dict */
5653 super_descr_get, /* tp_descr_get */
5654 0, /* tp_descr_set */
5655 0, /* tp_dictoffset */
5656 super_init, /* tp_init */
5657 PyType_GenericAlloc, /* tp_alloc */
5658 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00005659 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00005660};