annotate Objects/typeobject.c @ 20354:fdc1914406e2 legacy-trunk

Updated, and added a very terse description of PyMac_SetConsoleHandler().
author Jack Jansen <jack.jansen@cwi.nl>
date Tue, 09 Oct 2001 23:14:06 +0000
parents 67a7a99f2d5a
children be08734b0afd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Type object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
4 #include "Python.h"
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
5 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
7 static PyMemberDef type_members[] = {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
8 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
9 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
10 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
11 {"__doc__", T_STRING, offsetof(PyTypeObject, tp_doc), READONLY},
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
12 {"__weakrefoffset__", T_LONG,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
13 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
15 {"__dictoffset__", T_LONG,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
17 {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
18 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
19 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
20 };
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
22 static PyObject *
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
23 type_name(PyTypeObject *type, void *context)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
24 {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
25 char *s;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
26
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
27 s = strrchr(type->tp_name, '.');
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
28 if (s == NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
29 s = type->tp_name;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
30 else
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
31 s++;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
32 return PyString_FromString(s);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
33 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
34
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
35 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
36 type_module(PyTypeObject *type, void *context)
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
37 {
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
38 PyObject *mod;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
39 char *s;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
40
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
41 s = strrchr(type->tp_name, '.');
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
42 if (s != NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
43 return PyString_FromStringAndSize(type->tp_name,
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
44 (int)(s - type->tp_name));
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
45 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
46 return PyString_FromString("__builtin__");
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
47 mod = PyDict_GetItemString(type->tp_defined, "__module__");
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
48 if (mod != NULL && PyString_Check(mod)) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
49 Py_INCREF(mod);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
50 return mod;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
51 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
52 PyErr_SetString(PyExc_AttributeError, "__module__");
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
53 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
54 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
55
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
56 static int
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
57 type_set_module(PyTypeObject *type, PyObject *value, void *context)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
58 {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
59 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) ||
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
60 strrchr(type->tp_name, '.')) {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
61 PyErr_Format(PyExc_TypeError,
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
62 "can't set %s.__module__", type->tp_name);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
63 return -1;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
64 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
65 if (!value) {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
66 PyErr_Format(PyExc_TypeError,
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
67 "can't delete %s.__module__", type->tp_name);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
68 return -1;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
69 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
70 return PyDict_SetItemString(type->tp_dict, "__module__", value);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
71 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
72
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
73 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
74 type_dict(PyTypeObject *type, void *context)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
75 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
76 if (type->tp_dict == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
77 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
78 return Py_None;
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
79 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
80 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
81 Py_INCREF(type->tp_dict);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
82 return type->tp_dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
83 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
84 return PyDictProxy_New(type->tp_dict);
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
85 }
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
86
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
87 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
88 type_defined(PyTypeObject *type, void *context)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
89 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
90 if (type->tp_defined == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
91 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
92 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
93 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
94 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
95 Py_INCREF(type->tp_defined);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
96 return type->tp_defined;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
97 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
98 return PyDictProxy_New(type->tp_defined);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
99 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
100
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
101 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
102 type_dynamic(PyTypeObject *type, void *context)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
103 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
104 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
105
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
106 res = (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) ? Py_True : Py_False;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
107 Py_INCREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
108 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
109 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
110
20015
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
111 PyGetSetDef type_getsets[] = {
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
112 {"__name__", (getter)type_name, NULL, NULL},
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
113 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
114 {"__dict__", (getter)type_dict, NULL, NULL},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
115 {"__defined__", (getter)type_defined, NULL, NULL},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
116 {"__dynamic__", (getter)type_dynamic, NULL, NULL},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
117 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
118 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
119
18063
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
120 static int
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
121 type_compare(PyObject *v, PyObject *w)
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
122 {
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
123 /* This is called with type objects only. So we
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
124 can just compare the addresses. */
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
125 Py_uintptr_t vv = (Py_uintptr_t)v;
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
126 Py_uintptr_t ww = (Py_uintptr_t)w;
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
127 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
128 }
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
129
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
130 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
131 type_repr(PyTypeObject *type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 {
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
133 PyObject *mod, *name, *rtn;
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
134 char *kind;
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
135
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
136 mod = type_module(type, NULL);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
137 if (mod == NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
138 PyErr_Clear();
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
139 else if (!PyString_Check(mod)) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
140 Py_DECREF(mod);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
141 mod = NULL;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
142 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
143 name = type_name(type, NULL);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
144 if (name == NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
145 return NULL;
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
146
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
147 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
148 kind = "class";
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
149 else
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
150 kind = "type";
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
151
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
152 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
153 rtn = PyString_FromFormat("<%s '%s.%s'>",
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
154 kind,
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
155 PyString_AS_STRING(mod),
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
156 PyString_AS_STRING(name));
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
157 }
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
158 else
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
159 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
160
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
161 Py_XDECREF(mod);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
162 Py_DECREF(name);
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
163 return rtn;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
166 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
167 type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
168 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
169 PyObject *obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
170
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
171 if (type->tp_new == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
172 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
173 "cannot create '%.100s' instances",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
174 type->tp_name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
175 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
176 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
177
19898
fc40d0b54043 type_call(): Change in policy. The keyword args (if any) are now passed
Tim Peters <tim.peters@gmail.com>
parents: 19808
diff changeset
178 obj = type->tp_new(type, args, kwds);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
179 if (obj != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
180 type = obj->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
181 if (type->tp_init != NULL &&
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
182 type->tp_init(obj, args, kwds) < 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
183 Py_DECREF(obj);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
184 obj = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
185 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
186 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
187 return obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
188 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
189
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
190 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
191 PyType_GenericAlloc(PyTypeObject *type, int nitems)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
192 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
193 PyObject *obj;
20296
6cc0b48049ec Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents: 20294
diff changeset
194 const size_t size = _PyObject_VAR_SIZE(type, nitems);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
195
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
196 if (PyType_IS_GC(type))
20294
426b379a138b _PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents: 20293
diff changeset
197 obj = _PyObject_GC_Malloc(type, nitems);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
198 else
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
199 obj = PyObject_MALLOC(size);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
200
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
201 if (obj == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
202 return PyErr_NoMemory();
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
203
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
204 memset(obj, '\0', size);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
205
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
206 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
207 Py_INCREF(type);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
208
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
209 if (type->tp_itemsize == 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
210 PyObject_INIT(obj, type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
211 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
212 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
20293
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
213
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
214 if (PyType_IS_GC(type))
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
215 _PyObject_GC_TRACK(obj);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
216 return obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
217 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
218
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
219 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
220 PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
221 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
222 return type->tp_alloc(type, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
223 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
224
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
225 /* Helpers for subtyping */
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
226
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
227 static int
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
228 subtype_traverse(PyObject *self, visitproc visit, void *arg)
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
229 {
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
230 PyTypeObject *type, *base;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
231 traverseproc f;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
232 int err;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
233
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
234 /* Find the nearest base with a different tp_traverse */
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
235 type = self->ob_type;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
236 base = type->tp_base;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
237 while ((f = base->tp_traverse) == subtype_traverse) {
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
238 base = base->tp_base;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
239 assert(base);
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
240 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
241
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
242 if (type->tp_dictoffset != base->tp_dictoffset) {
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
243 PyObject **dictptr = _PyObject_GetDictPtr(self);
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
244 if (dictptr && *dictptr) {
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
245 err = visit(*dictptr, arg);
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
246 if (err)
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
247 return err;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
248 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
249 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
250
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
251 if (f)
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
252 return f(self, visit, arg);
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
253 return 0;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
254 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
255
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
256 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
257 subtype_dealloc(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
258 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
259 PyTypeObject *type, *base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
260 destructor f;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
261
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
262 /* This exists so we can DECREF self->ob_type */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
263
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
264 /* Find the nearest base with a different tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
265 type = self->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
266 base = type->tp_base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
267 while ((f = base->tp_dealloc) == subtype_dealloc) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
268 base = base->tp_base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
269 assert(base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
270 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
271
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
272 /* If we added a dict, DECREF it */
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
273 if (type->tp_dictoffset && !base->tp_dictoffset) {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
274 PyObject **dictptr = _PyObject_GetDictPtr(self);
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
275 if (dictptr != NULL) {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
276 PyObject *dict = *dictptr;
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
277 if (dict != NULL) {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
278 Py_DECREF(dict);
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
279 *dictptr = NULL;
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
280 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
281 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
282 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
283
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
284 /* If we added weaklist, we clear it */
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
285 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
286 PyObject_ClearWeakRefs(self);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
287
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
288 /* Finalize GC if the base doesn't do GC and we do */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
289 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
290 _PyObject_GC_UNTRACK(self);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
291
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
292 /* Call the base tp_dealloc() */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
293 assert(f);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
294 f(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
295
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
296 /* Can't reference self beyond this point */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
297 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
298 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
299 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
300 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
301
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
302 staticforward PyTypeObject *solid_base(PyTypeObject *type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
303
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
304 typedef struct {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
305 PyTypeObject type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
306 PyNumberMethods as_number;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
307 PySequenceMethods as_sequence;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
308 PyMappingMethods as_mapping;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
309 PyBufferProcs as_buffer;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
310 PyObject *name, *slots;
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
311 PyMemberDef members[1];
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
312 } etype;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
313
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
314 /* type test with subclassing support */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
315
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
316 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
317 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
318 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
319 PyObject *mro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
320
19803
32cfce4d83dc PyType_IsSubtype(): test tp_flags for HAVE_CLASS bit before accessing
Guido van Rossum <guido@python.org>
parents: 19630
diff changeset
321 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
32cfce4d83dc PyType_IsSubtype(): test tp_flags for HAVE_CLASS bit before accessing
Guido van Rossum <guido@python.org>
parents: 19630
diff changeset
322 return b == a || b == &PyBaseObject_Type;
32cfce4d83dc PyType_IsSubtype(): test tp_flags for HAVE_CLASS bit before accessing
Guido van Rossum <guido@python.org>
parents: 19630
diff changeset
323
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
324 mro = a->tp_mro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
325 if (mro != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
326 /* Deal with multiple inheritance without recursion
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
327 by walking the MRO tuple */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
328 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
329 assert(PyTuple_Check(mro));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
330 n = PyTuple_GET_SIZE(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
331 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
332 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
333 return 1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
334 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
335 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
336 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
337 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
338 /* a is not completely initilized yet; follow tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
339 do {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
340 if (a == b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
341 return 1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
342 a = a->tp_base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
343 } while (a != NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
344 return b == &PyBaseObject_Type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
345 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
346 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
347
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
348 /* Internal routines to do a method lookup in the type
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
349 without looking in the instance dictionary
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
350 (so we can't use PyObject_GetAttr) but still binding
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
351 it to the instance. The arguments are the object,
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
352 the method name as a C string, and the address of a
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
353 static variable used to cache the interned Python string.
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
354
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
355 Two variants:
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
356
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
357 - lookup_maybe() returns NULL without raising an exception
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
358 when the _PyType_Lookup() call fails;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
359
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
360 - lookup_method() always raises an exception upon errors.
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
361 */
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
362
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
363 static PyObject *
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
364 lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
365 {
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
366 PyObject *res;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
367
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
368 if (*attrobj == NULL) {
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
369 *attrobj = PyString_InternFromString(attrstr);
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
370 if (*attrobj == NULL)
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
371 return NULL;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
372 }
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
373 res = _PyType_Lookup(self->ob_type, *attrobj);
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
374 if (res != NULL) {
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
375 descrgetfunc f;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
376 if ((f = res->ob_type->tp_descr_get) == NULL)
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
377 Py_INCREF(res);
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
378 else
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
379 res = f(res, self, (PyObject *)(self->ob_type));
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
380 }
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
381 return res;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
382 }
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
383
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
384 static PyObject *
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
385 lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
386 {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
387 PyObject *res = lookup_maybe(self, attrstr, attrobj);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
388 if (res == NULL && !PyErr_Occurred())
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
389 PyErr_SetObject(PyExc_AttributeError, *attrobj);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
390 return res;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
391 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
392
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
393 /* A variation of PyObject_CallMethod that uses lookup_method()
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
394 instead of PyObject_GetAttrString(). This uses the same convention
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
395 as lookup_method to cache the interned name string object. */
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
396
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
397 PyObject *
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
398 call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
399 {
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
400 va_list va;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
401 PyObject *args, *func = 0, *retval;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
402 va_start(va, format);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
403
20225
aa18c0ecc4fb call_method(), call_maybe(): fix a performance bug: the argument
Guido van Rossum <guido@python.org>
parents: 20220
diff changeset
404 func = lookup_maybe(o, name, nameobj);
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
405 if (func == NULL) {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
406 va_end(va);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
407 if (!PyErr_Occurred())
20225
aa18c0ecc4fb call_method(), call_maybe(): fix a performance bug: the argument
Guido van Rossum <guido@python.org>
parents: 20220
diff changeset
408 PyErr_SetObject(PyExc_AttributeError, *nameobj);
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
409 return NULL;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
410 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
411
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
412 if (format && *format)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
413 args = Py_VaBuildValue(format, va);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
414 else
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
415 args = PyTuple_New(0);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
416
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
417 va_end(va);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
418
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
419 if (args == NULL)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
420 return NULL;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
421
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
422 assert(PyTuple_Check(args));
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
423 retval = PyObject_Call(func, args, NULL);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
424
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
425 Py_DECREF(args);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
426 Py_DECREF(func);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
427
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
428 return retval;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
429 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
430
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
431 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
432
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
433 PyObject *
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
434 call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
435 {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
436 va_list va;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
437 PyObject *args, *func = 0, *retval;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
438 va_start(va, format);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
439
20225
aa18c0ecc4fb call_method(), call_maybe(): fix a performance bug: the argument
Guido van Rossum <guido@python.org>
parents: 20220
diff changeset
440 func = lookup_maybe(o, name, nameobj);
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
441 if (func == NULL) {
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
442 va_end(va);
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
443 if (!PyErr_Occurred()) {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
444 Py_INCREF(Py_NotImplemented);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
445 return Py_NotImplemented;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
446 }
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
447 return NULL;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
448 }
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
449
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
450 if (format && *format)
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
451 args = Py_VaBuildValue(format, va);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
452 else
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
453 args = PyTuple_New(0);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
454
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
455 va_end(va);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
456
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
457 if (args == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
458 return NULL;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
459
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
460 assert(PyTuple_Check(args));
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
461 retval = PyObject_Call(func, args, NULL);
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
462
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
463 Py_DECREF(args);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
464 Py_DECREF(func);
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
465
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
466 return retval;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
467 }
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
468
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
469 /* Method resolution order algorithm from "Putting Metaclasses to Work"
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
470 by Forman and Danforth (Addison-Wesley 1999). */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
471
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
472 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
473 conservative_merge(PyObject *left, PyObject *right)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
474 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
475 int left_size;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
476 int right_size;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
477 int i, j, r, ok;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
478 PyObject *temp, *rr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
479
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
480 assert(PyList_Check(left));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
481 assert(PyList_Check(right));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
482
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
483 again:
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
484 left_size = PyList_GET_SIZE(left);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
485 right_size = PyList_GET_SIZE(right);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
486 for (i = 0; i < left_size; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
487 for (j = 0; j < right_size; j++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
488 if (PyList_GET_ITEM(left, i) ==
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
489 PyList_GET_ITEM(right, j)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
490 /* found a merge point */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
491 temp = PyList_New(0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
492 if (temp == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
493 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
494 for (r = 0; r < j; r++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
495 rr = PyList_GET_ITEM(right, r);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
496 ok = PySequence_Contains(left, rr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
497 if (ok < 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
498 Py_DECREF(temp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
499 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
500 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
501 if (!ok) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
502 ok = PyList_Append(temp, rr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
503 if (ok < 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
504 Py_DECREF(temp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
505 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
506 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
507 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
508 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
509 ok = PyList_SetSlice(left, i, i, temp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
510 Py_DECREF(temp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
511 if (ok < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
512 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
513 ok = PyList_SetSlice(right, 0, j+1, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
514 if (ok < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
515 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
516 goto again;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
517 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
518 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
519 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
520 return PyList_SetSlice(left, left_size, left_size, right);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
521 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
522
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
523 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
524 serious_order_disagreements(PyObject *left, PyObject *right)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
525 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
526 return 0; /* XXX later -- for now, we cheat: "don't do that" */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
527 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
528
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
529 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
530 mro_implementation(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
531 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
532 int i, n, ok;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
533 PyObject *bases, *result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
534
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
535 bases = type->tp_bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
536 n = PyTuple_GET_SIZE(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
537 result = Py_BuildValue("[O]", (PyObject *)type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
538 if (result == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
539 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
540 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
541 PyTypeObject *base =
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
542 (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
543 PyObject *parentMRO = PySequence_List(base->tp_mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
544 if (parentMRO == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
545 Py_DECREF(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
546 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
547 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
548 if (serious_order_disagreements(result, parentMRO)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
549 Py_DECREF(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
550 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
551 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
552 ok = conservative_merge(result, parentMRO);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
553 Py_DECREF(parentMRO);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
554 if (ok < 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
555 Py_DECREF(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
556 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
557 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
558 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
559 return result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
560 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
561
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
562 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19300
diff changeset
563 mro_external(PyObject *self)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
564 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
565 PyTypeObject *type = (PyTypeObject *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
566
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
567 return mro_implementation(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
568 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
569
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
570 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
571 mro_internal(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
572 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
573 PyObject *mro, *result, *tuple;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
574
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
575 if (type->ob_type == &PyType_Type) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
576 result = mro_implementation(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
577 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
578 else {
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
579 static PyObject *mro_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
580 mro = lookup_method((PyObject *)type, "mro", &mro_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
581 if (mro == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
582 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
583 result = PyObject_CallObject(mro, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
584 Py_DECREF(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
585 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
586 if (result == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
587 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
588 tuple = PySequence_Tuple(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
589 Py_DECREF(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
590 type->tp_mro = tuple;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
591 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
592 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
593
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
594
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
595 /* Calculate the best base amongst multiple base classes.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
596 This is the first one that's on the path to the "solid base". */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
597
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
598 static PyTypeObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
599 best_base(PyObject *bases)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
600 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
601 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
602 PyTypeObject *base, *winner, *candidate, *base_i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
603
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
604 assert(PyTuple_Check(bases));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
605 n = PyTuple_GET_SIZE(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
606 assert(n > 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
607 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
608 winner = &PyBaseObject_Type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
609 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
610 base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
611 if (!PyType_Check((PyObject *)base_i)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
612 PyErr_SetString(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
613 PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
614 "bases must be types");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
615 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
616 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
617 if (base_i->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
618 if (PyType_Ready(base_i) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
619 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
620 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
621 candidate = solid_base(base_i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
622 if (PyType_IsSubtype(winner, candidate))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
623 ;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
624 else if (PyType_IsSubtype(candidate, winner)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
625 winner = candidate;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
626 base = base_i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
627 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
628 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
629 PyErr_SetString(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
630 PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
631 "multiple bases have "
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
632 "instance lay-out conflict");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
633 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
634 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
635 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
636 assert(base != NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
637 return base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
638 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
639
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
640 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
641 extra_ivars(PyTypeObject *type, PyTypeObject *base)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
642 {
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
643 size_t t_size = type->tp_basicsize;
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
644 size_t b_size = base->tp_basicsize;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
645
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
646 assert(t_size >= b_size); /* Else type smaller than base! */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
647 if (type->tp_itemsize || base->tp_itemsize) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
648 /* If itemsize is involved, stricter rules */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
649 return t_size != b_size ||
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
650 type->tp_itemsize != base->tp_itemsize;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
651 }
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
652 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
653 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
654 t_size -= sizeof(PyObject *);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
655 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
656 type->tp_dictoffset + sizeof(PyObject *) == t_size)
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
657 t_size -= sizeof(PyObject *);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
658
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
659 return t_size != b_size;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
660 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
661
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
662 static PyTypeObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
663 solid_base(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
664 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
665 PyTypeObject *base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
666
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
667 if (type->tp_base)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
668 base = solid_base(type->tp_base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
669 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
670 base = &PyBaseObject_Type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
671 if (extra_ivars(type, base))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
672 return type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
673 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
674 return base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
675 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
676
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
677 staticforward void object_dealloc(PyObject *);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
678 staticforward int object_init(PyObject *, PyObject *, PyObject *);
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
679 staticforward int update_slot(PyTypeObject *, PyObject *, PyObject *);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
680 staticforward void fixup_slot_dispatchers(PyTypeObject *);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
681
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
682 static PyObject *
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
683 subtype_dict(PyObject *obj, void *context)
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
684 {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
685 PyObject **dictptr = _PyObject_GetDictPtr(obj);
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
686 PyObject *dict;
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
687
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
688 if (dictptr == NULL) {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
689 PyErr_SetString(PyExc_AttributeError,
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
690 "This object has no __dict__");
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
691 return NULL;
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
692 }
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
693 dict = *dictptr;
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
694 if (dict == NULL)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
695 *dictptr = dict = PyDict_New();
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
696 Py_XINCREF(dict);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
697 return dict;
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
698 }
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
699
20015
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
700 PyGetSetDef subtype_getsets[] = {
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
701 {"__dict__", subtype_dict, NULL, NULL},
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
702 {0},
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
703 };
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
704
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
705 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
706 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
707 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
708 PyObject *name, *bases, *dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
709 static char *kwlist[] = {"name", "bases", "dict", 0};
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
710 PyObject *slots, *tmp;
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
711 PyTypeObject *type, *base, *tmptype, *winner;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
712 etype *et;
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
713 PyMemberDef *mp;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
714 int i, nbases, nslots, slotoffset, dynamic, add_dict, add_weak;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
715
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
716 /* Special case: type(x) should return x->ob_type */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
717 if (metatype == &PyType_Type &&
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
718 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
719 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
720 PyObject *x = PyTuple_GET_ITEM(args, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
721 Py_INCREF(x->ob_type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
722 return (PyObject *) x->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
723 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
724
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
725 /* Check arguments: (name, bases, dict) */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
726 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
727 &name,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
728 &PyTuple_Type, &bases,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
729 &PyDict_Type, &dict))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
730 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
731
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
732 /* Determine the proper metatype to deal with this,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
733 and check for metatype conflicts while we're at it.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
734 Note that if some other metatype wins to contract,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
735 it's possible that its instances are not types. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
736 nbases = PyTuple_GET_SIZE(bases);
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
737 winner = metatype;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
738 for (i = 0; i < nbases; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
739 tmp = PyTuple_GET_ITEM(bases, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
740 tmptype = tmp->ob_type;
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
741 if (PyType_IsSubtype(winner, tmptype))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
742 continue;
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
743 if (PyType_IsSubtype(tmptype, winner)) {
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
744 winner = tmptype;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
745 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
746 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
747 PyErr_SetString(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
748 "metatype conflict among bases");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
749 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
750 }
19329
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
751 if (winner != metatype) {
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
752 if (winner->tp_new != type_new) /* Pass it to the winner */
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
753 return winner->tp_new(winner, args, kwds);
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
754 metatype = winner;
f93c16c10a55 type_new(): only defer to the winning metatype if it's different from
Guido van Rossum <guido@python.org>
parents: 19316
diff changeset
755 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
756
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
757 /* Adjust for empty tuple bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
758 if (nbases == 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
759 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
760 if (bases == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
761 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
762 nbases = 1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
763 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
764 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
765 Py_INCREF(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
766
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
767 /* XXX From here until type is allocated, "return NULL" leaks bases! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
768
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
769 /* Calculate best base, and check that all bases are type objects */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
770 base = best_base(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
771 if (base == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
772 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
773 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
774 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
775 "type '%.100s' is not an acceptable base type",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
776 base->tp_name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
777 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
778 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
779
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
780 /* Should this be a dynamic class (i.e. modifiable __dict__)?
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
781 Look in two places for a variable named __dynamic__:
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
782 1) in the class dict
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
783 2) in the module dict (globals)
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
784 The first variable that is an int >= 0 is used.
20259
6152f82ea823 Make new classes dynamic by default.
Guido van Rossum <guido@python.org>
parents: 20241
diff changeset
785 Otherwise, the default is dynamic. */
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
786 dynamic = -1; /* Not yet determined */
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
787 /* Look in the class */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
788 tmp = PyDict_GetItemString(dict, "__dynamic__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
789 if (tmp != NULL) {
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
790 dynamic = PyInt_AsLong(tmp);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
791 if (dynamic < 0)
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
792 PyErr_Clear();
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
793 }
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
794 if (dynamic < 0) {
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
795 /* Look in the module globals */
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
796 tmp = PyEval_GetGlobals();
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
797 if (tmp != NULL) {
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
798 tmp = PyDict_GetItemString(tmp, "__dynamic__");
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
799 if (tmp != NULL) {
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
800 dynamic = PyInt_AsLong(tmp);
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
801 if (dynamic < 0)
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
802 PyErr_Clear();
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
803 }
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
804 }
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
805 }
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
806 if (dynamic < 0) {
20259
6152f82ea823 Make new classes dynamic by default.
Guido van Rossum <guido@python.org>
parents: 20241
diff changeset
807 /* Default to dynamic */
6152f82ea823 Make new classes dynamic by default.
Guido van Rossum <guido@python.org>
parents: 20241
diff changeset
808 dynamic = 1;
6152f82ea823 Make new classes dynamic by default.
Guido van Rossum <guido@python.org>
parents: 20241
diff changeset
809
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
810 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
811
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
812 /* Check for a __slots__ sequence variable in dict, and count it */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
813 slots = PyDict_GetItemString(dict, "__slots__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
814 nslots = 0;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
815 add_dict = 0;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
816 add_weak = 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
817 if (slots != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
818 /* Make it into a tuple */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
819 if (PyString_Check(slots))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
820 slots = Py_BuildValue("(O)", slots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
821 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
822 slots = PySequence_Tuple(slots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
823 if (slots == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
824 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
825 nslots = PyTuple_GET_SIZE(slots);
19595
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
826 if (nslots > 0 && base->tp_itemsize != 0) {
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
827 PyErr_Format(PyExc_TypeError,
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
828 "nonempty __slots__ "
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
829 "not supported for subtype of '%s'",
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
830 base->tp_name);
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
831 return NULL;
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
832 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
833 for (i = 0; i < nslots; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
834 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
835 PyErr_SetString(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
836 "__slots__ must be a sequence of strings");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
837 Py_DECREF(slots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
838 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
839 }
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
840 /* XXX Check against null bytes in name */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
841 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
842 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
843 if (slots == NULL && base->tp_dictoffset == 0 &&
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
844 (base->tp_setattro == PyObject_GenericSetAttr ||
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
845 base->tp_setattro == NULL)) {
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
846 add_dict++;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
847 }
19595
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
848 if (slots == NULL && base->tp_weaklistoffset == 0 &&
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
849 base->tp_itemsize == 0) {
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
850 nslots++;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
851 add_weak++;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
852 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
853
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
854 /* XXX From here until type is safely allocated,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
855 "return NULL" may leak slots! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
856
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
857 /* Allocate the type object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
858 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
859 if (type == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
860 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
861
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
862 /* Keep name and slots alive in the extended type object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
863 et = (etype *)type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
864 Py_INCREF(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
865 et->name = name;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
866 et->slots = slots;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
867
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
868 /* Initialize tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
869 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
870 Py_TPFLAGS_BASETYPE;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
871 if (dynamic)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
872 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
873 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
874 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
875
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
876 /* It's a new-style number unless it specifically inherits any
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
877 old-style numeric behavior */
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
878 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
879 (base->tp_as_number == NULL))
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
880 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
881
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
882 /* Initialize essential fields */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
883 type->tp_as_number = &et->as_number;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
884 type->tp_as_sequence = &et->as_sequence;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
885 type->tp_as_mapping = &et->as_mapping;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
886 type->tp_as_buffer = &et->as_buffer;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
887 type->tp_name = PyString_AS_STRING(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
888
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
889 /* Set tp_base and tp_bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
890 type->tp_bases = bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
891 Py_INCREF(base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
892 type->tp_base = base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
893
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
894 /* Initialize tp_defined from passed-in dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
895 type->tp_defined = dict = PyDict_Copy(dict);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
896 if (dict == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
897 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
898 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
899 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
900
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
901 /* Set __module__ in the dict */
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
902 if (PyDict_GetItemString(dict, "__module__") == NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
903 tmp = PyEval_GetGlobals();
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
904 if (tmp != NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
905 tmp = PyDict_GetItemString(tmp, "__name__");
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
906 if (tmp != NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
907 if (PyDict_SetItemString(dict, "__module__",
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
908 tmp) < 0)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
909 return NULL;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
910 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
911 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
912 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
913
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
914 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
915 and is a string (tp_doc is a char* -- can't copy a general object
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
916 into it).
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
917 XXX What if it's a Unicode string? Don't know -- this ignores it.
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
918 */
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
919 {
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
920 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
921 if (doc != NULL && PyString_Check(doc)) {
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
922 const size_t n = (size_t)PyString_GET_SIZE(doc);
20241
aff6fcd26c15 type_new(): cast PyObject_MALLOC's result to char*, for clarity.
Tim Peters <tim.peters@gmail.com>
parents: 20239
diff changeset
923 type->tp_doc = (char *)PyObject_MALLOC(n+1);
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
924 if (type->tp_doc == NULL) {
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
925 Py_DECREF(type);
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
926 return NULL;
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
927 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
928 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
929 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
930 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
931
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
932 /* Special-case __new__: if it's a plain function,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
933 make it a static function */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
934 tmp = PyDict_GetItemString(dict, "__new__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
935 if (tmp != NULL && PyFunction_Check(tmp)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
936 tmp = PyStaticMethod_New(tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
937 if (tmp == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
938 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
939 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
940 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
941 PyDict_SetItemString(dict, "__new__", tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
942 Py_DECREF(tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
943 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
944
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
945 /* Add descriptors for custom slots from __slots__, or for __dict__ */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
946 mp = et->members;
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
947 slotoffset = base->tp_basicsize;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
948 if (slots != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
949 for (i = 0; i < nslots; i++, mp++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
950 mp->name = PyString_AS_STRING(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
951 PyTuple_GET_ITEM(slots, i));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
952 mp->type = T_OBJECT;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
953 mp->offset = slotoffset;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
954 if (base->tp_weaklistoffset == 0 &&
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
955 strcmp(mp->name, "__weakref__") == 0)
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
956 type->tp_weaklistoffset = slotoffset;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
957 slotoffset += sizeof(PyObject *);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
958 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
959 }
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
960 else {
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
961 if (add_dict) {
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
962 if (base->tp_itemsize)
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
963 type->tp_dictoffset =
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
964 -(long)sizeof(PyObject *);
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
965 else
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
966 type->tp_dictoffset = slotoffset;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
967 slotoffset += sizeof(PyObject *);
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
968 type->tp_getset = subtype_getsets;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
969 }
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
970 if (add_weak) {
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
971 assert(!base->tp_itemsize);
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
972 type->tp_weaklistoffset = slotoffset;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
973 mp->name = "__weakref__";
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
974 mp->type = T_OBJECT;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
975 mp->offset = slotoffset;
19950
76133a465e03 type_new(): Didn't compile anymore, due to change in struct memberlist
Tim Peters <tim.peters@gmail.com>
parents: 19914
diff changeset
976 mp->flags = READONLY;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
977 mp++;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
978 slotoffset += sizeof(PyObject *);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
979 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
980 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
981 type->tp_basicsize = slotoffset;
19616
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
982 type->tp_itemsize = base->tp_itemsize;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
983 type->tp_members = et->members;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
984
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
985 /* Special case some slots */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
986 if (type->tp_dictoffset != 0 || nslots > 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
987 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
988 type->tp_getattro = PyObject_GenericGetAttr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
989 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
990 type->tp_setattro = PyObject_GenericSetAttr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
991 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
992 type->tp_dealloc = subtype_dealloc;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
993
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
994 /* Enable GC unless there are really no instance variables possible */
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
995 if (!(type->tp_basicsize == sizeof(PyObject) &&
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
996 type->tp_itemsize == 0))
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
997 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
998
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
999 /* Always override allocation strategy to use regular heap */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1000 type->tp_alloc = PyType_GenericAlloc;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1001 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1002 type->tp_free = _PyObject_GC_Del;
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
1003 type->tp_traverse = subtype_traverse;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1004 type->tp_clear = base->tp_clear;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1005 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1006 else
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1007 type->tp_free = _PyObject_Del;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1008
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1009 /* Initialize the rest */
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
1010 if (PyType_Ready(type) < 0) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1011 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1012 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1013 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1014
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1015 /* Put the proper slots in place */
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1016 fixup_slot_dispatchers(type);
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
1017
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1018 return (PyObject *)type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1019 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1020
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1021 /* Internal API to look for a name through the MRO.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1022 This returns a borrowed reference, and doesn't set an exception! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1023 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1024 _PyType_Lookup(PyTypeObject *type, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1025 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1026 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1027 PyObject *mro, *res, *dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1028
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1029 /* For static types, look in tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1030 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1031 dict = type->tp_dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1032 assert(dict && PyDict_Check(dict));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1033 return PyDict_GetItem(dict, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1034 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1035
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1036 /* For dynamic types, look in tp_defined of types in MRO */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1037 mro = type->tp_mro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1038 assert(PyTuple_Check(mro));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1039 n = PyTuple_GET_SIZE(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1040 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1041 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1042 assert(PyType_Check(type));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1043 dict = type->tp_defined;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1044 assert(dict && PyDict_Check(dict));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1045 res = PyDict_GetItem(dict, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1046 if (res != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1047 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1048 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1049 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1050 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1051
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1052 /* This is similar to PyObject_GenericGetAttr(),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1053 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1054 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1055 type_getattro(PyTypeObject *type, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1056 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1057 PyTypeObject *metatype = type->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1058 PyObject *descr, *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1059 descrgetfunc f;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1060
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1061 /* Initialize this type (we'll assume the metatype is initialized) */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1062 if (type->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
1063 if (PyType_Ready(type) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1064 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1065 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1066
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1067 /* Get a descriptor from the metatype */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1068 descr = _PyType_Lookup(metatype, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1069 f = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1070 if (descr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1071 f = descr->ob_type->tp_descr_get;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1072 if (f != NULL && PyDescr_IsData(descr))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1073 return f(descr,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1074 (PyObject *)type, (PyObject *)metatype);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1075 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1076
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1077 /* Look in tp_defined of this type and its bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1078 res = _PyType_Lookup(type, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1079 if (res != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1080 f = res->ob_type->tp_descr_get;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1081 if (f != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1082 return f(res, (PyObject *)NULL, (PyObject *)type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1083 Py_INCREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1084 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1085 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1086
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1087 /* Use the descriptor from the metatype */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1088 if (f != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1089 res = f(descr, (PyObject *)type, (PyObject *)metatype);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1090 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1091 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1092 if (descr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1093 Py_INCREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1094 return descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1095 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1096
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1097 /* Give up */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1098 PyErr_Format(PyExc_AttributeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1099 "type object '%.50s' has no attribute '%.400s'",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1100 type->tp_name, PyString_AS_STRING(name));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1101 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1102 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1103
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1104 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1105 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1106 {
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1107 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1108 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1109 return -1;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1110 return update_slot(type, name, value);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1111 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1112 PyErr_SetString(PyExc_TypeError, "can't set static type attributes");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1113 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1114 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1115
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1116 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1117 type_dealloc(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1118 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1119 etype *et;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1120
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1121 /* Assert this is a heap-allocated type object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1122 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1123 _PyObject_GC_UNTRACK(type);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1124 PyObject_ClearWeakRefs((PyObject *)type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1125 et = (etype *)type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1126 Py_XDECREF(type->tp_base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1127 Py_XDECREF(type->tp_dict);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1128 Py_XDECREF(type->tp_bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1129 Py_XDECREF(type->tp_mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1130 Py_XDECREF(type->tp_defined);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1131 Py_XDECREF(type->tp_subclasses);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1132 Py_XDECREF(et->name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1133 Py_XDECREF(et->slots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1134 type->ob_type->tp_free((PyObject *)type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1135 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1136
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1137 static PyObject *
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1138 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1139 {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1140 PyObject *list, *raw, *ref;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1141 int i, n;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1142
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1143 list = PyList_New(0);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1144 if (list == NULL)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1145 return NULL;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1146 raw = type->tp_subclasses;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1147 if (raw == NULL)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1148 return list;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1149 assert(PyList_Check(raw));
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1150 n = PyList_GET_SIZE(raw);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1151 for (i = 0; i < n; i++) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1152 ref = PyList_GET_ITEM(raw, i);
20320
f29b8ea0b695 type_subclasses(): debug build was broken due to typo in new assert().
Tim Peters <tim.peters@gmail.com>
parents: 20315
diff changeset
1153 assert(PyWeakref_CheckRef(ref));
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1154 ref = PyWeakref_GET_OBJECT(ref);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1155 if (ref != Py_None) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1156 if (PyList_Append(list, ref) < 0) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1157 Py_DECREF(list);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1158 return NULL;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1159 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1160 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1161 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1162 return list;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1163 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1164
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1165 static PyMethodDef type_methods[] = {
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19300
diff changeset
1166 {"mro", (PyCFunction)mro_external, METH_NOARGS,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1167 "mro() -> list\nreturn a type's method resolution order"},
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1168 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1169 "__subclasses__() -> list of immediate subclasses"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1170 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1171 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1172
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1173 static char type_doc[] =
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1174 "type(object) -> the object's type\n"
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1175 "type(name, bases, dict) -> a new type";
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1176
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1177 static int
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1178 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1179 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1180 etype *et;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1181 int err;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1182
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1183 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1184 return 0;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1185
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1186 et = (etype *)type;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1187
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1188 #define VISIT(SLOT) \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1189 if (SLOT) { \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1190 err = visit((PyObject *)(SLOT), arg); \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1191 if (err) \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1192 return err; \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1193 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1194
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1195 VISIT(type->tp_dict);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1196 VISIT(type->tp_defined);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1197 VISIT(type->tp_mro);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1198 VISIT(type->tp_bases);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1199 VISIT(type->tp_base);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1200 VISIT(type->tp_subclasses);
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1201 VISIT(et->slots);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1202
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1203 #undef VISIT
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1204
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1205 return 0;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1206 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1207
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1208 static int
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1209 type_clear(PyTypeObject *type)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1210 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1211 etype *et;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1212 PyObject *tmp;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1213
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1214 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1215 return 0;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1216
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1217 et = (etype *)type;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1218
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1219 #define CLEAR(SLOT) \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1220 if (SLOT) { \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1221 tmp = (PyObject *)(SLOT); \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1222 SLOT = NULL; \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1223 Py_DECREF(tmp); \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1224 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1225
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1226 CLEAR(type->tp_dict);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1227 CLEAR(type->tp_defined);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1228 CLEAR(type->tp_mro);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1229 CLEAR(type->tp_bases);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1230 CLEAR(type->tp_base);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1231 CLEAR(type->tp_subclasses);
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1232 CLEAR(et->slots);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1233
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1234 if (type->tp_doc != NULL) {
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1235 PyObject_FREE(type->tp_doc);
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1236 type->tp_doc = NULL;
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1237 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1238
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1239 #undef CLEAR
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1240
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1241 return 0;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1242 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1243
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1244 static int
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1245 type_is_gc(PyTypeObject *type)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1246 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1247 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1248 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1249
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1250 PyTypeObject PyType_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1251 PyObject_HEAD_INIT(&PyType_Type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1252 0, /* ob_size */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1253 "type", /* tp_name */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1254 sizeof(etype), /* tp_basicsize */
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
1255 sizeof(PyMemberDef), /* tp_itemsize */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1256 (destructor)type_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1257 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1258 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1259 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1260 type_compare, /* tp_compare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1261 (reprfunc)type_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1262 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1263 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1264 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1265 (hashfunc)_Py_HashPointer, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1266 (ternaryfunc)type_call, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1267 0, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1268 (getattrofunc)type_getattro, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1269 (setattrofunc)type_setattro, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1270 0, /* tp_as_buffer */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1271 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1272 Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1273 type_doc, /* tp_doc */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1274 (traverseproc)type_traverse, /* tp_traverse */
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1275 (inquiry)type_clear, /* tp_clear */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1276 0, /* tp_richcompare */
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1277 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1278 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1279 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1280 type_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1281 type_members, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1282 type_getsets, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1283 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1284 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1285 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1286 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1287 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1288 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1289 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1290 type_new, /* tp_new */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1291 _PyObject_GC_Del, /* tp_free */
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1292 (inquiry)type_is_gc, /* tp_is_gc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1293 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1294
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1295
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1296 /* The base type of all types (eventually)... except itself. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1297
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1298 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1299 object_init(PyObject *self, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1300 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1301 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1302 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1303
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1304 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1305 object_dealloc(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1306 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1307 self->ob_type->tp_free(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1308 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1309
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1310 static PyObject *
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1311 object_repr(PyObject *self)
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1312 {
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1313 PyTypeObject *type;
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
1314 PyObject *mod, *name, *rtn;
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1315
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1316 type = self->ob_type;
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1317 mod = type_module(type, NULL);
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1318 if (mod == NULL)
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1319 PyErr_Clear();
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1320 else if (!PyString_Check(mod)) {
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1321 Py_DECREF(mod);
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1322 mod = NULL;
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1323 }
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1324 name = type_name(type, NULL);
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1325 if (name == NULL)
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1326 return NULL;
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1327 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
20056
33edc99861b9 Fix the baffler that Tim reported: sometimes the repr() of an object
Guido van Rossum <guido@python.org>
parents: 20028
diff changeset
1328 rtn = PyString_FromFormat("<%s.%s object at %p>",
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
1329 PyString_AS_STRING(mod),
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
1330 PyString_AS_STRING(name),
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
1331 self);
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1332 else
20056
33edc99861b9 Fix the baffler that Tim reported: sometimes the repr() of an object
Guido van Rossum <guido@python.org>
parents: 20028
diff changeset
1333 rtn = PyString_FromFormat("<%s object at %p>",
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
1334 type->tp_name, self);
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1335 Py_XDECREF(mod);
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1336 Py_DECREF(name);
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
1337 return rtn;
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1338 }
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1339
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1340 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1341 object_str(PyObject *self)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1342 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1343 unaryfunc f;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1344
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1345 f = self->ob_type->tp_repr;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1346 if (f == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1347 f = object_repr;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1348 return f(self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1349 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1350
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1351 static long
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1352 object_hash(PyObject *self)
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1353 {
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1354 return _Py_HashPointer(self);
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1355 }
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1356
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1357 static PyObject *
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1358 object_get_class(PyObject *self, void *closure)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1359 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1360 Py_INCREF(self->ob_type);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1361 return (PyObject *)(self->ob_type);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1362 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1363
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1364 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1365 equiv_structs(PyTypeObject *a, PyTypeObject *b)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1366 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1367 return a == b ||
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1368 (a != NULL &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1369 b != NULL &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1370 a->tp_basicsize == b->tp_basicsize &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1371 a->tp_itemsize == b->tp_itemsize &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1372 a->tp_dictoffset == b->tp_dictoffset &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1373 a->tp_weaklistoffset == b->tp_weaklistoffset &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1374 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1375 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1376 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1377
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1378 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1379 same_slots_added(PyTypeObject *a, PyTypeObject *b)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1380 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1381 PyTypeObject *base = a->tp_base;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1382 int size;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1383
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1384 if (base != b->tp_base)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1385 return 0;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1386 if (equiv_structs(a, base) && equiv_structs(b, base))
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1387 return 1;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1388 size = base->tp_basicsize;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1389 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1390 size += sizeof(PyObject *);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1391 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1392 size += sizeof(PyObject *);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1393 return size == a->tp_basicsize && size == b->tp_basicsize;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1394 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1395
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1396 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1397 object_set_class(PyObject *self, PyObject *value, void *closure)
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1398 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1399 PyTypeObject *old = self->ob_type;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1400 PyTypeObject *new, *newbase, *oldbase;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1401
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1402 if (!PyType_Check(value)) {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1403 PyErr_Format(PyExc_TypeError,
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1404 "__class__ must be set to new-style class, not '%s' object",
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1405 value->ob_type->tp_name);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1406 return -1;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1407 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1408 new = (PyTypeObject *)value;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1409 newbase = new;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1410 oldbase = old;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1411 while (equiv_structs(newbase, newbase->tp_base))
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1412 newbase = newbase->tp_base;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1413 while (equiv_structs(oldbase, oldbase->tp_base))
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1414 oldbase = oldbase->tp_base;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1415 if (newbase != oldbase &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1416 (newbase->tp_base != oldbase->tp_base ||
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1417 !same_slots_added(newbase, oldbase))) {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1418 PyErr_Format(PyExc_TypeError,
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1419 "__class__ assignment: "
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1420 "'%s' object layout differs from '%s'",
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1421 new->tp_name,
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1422 old->tp_name);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1423 return -1;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1424 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1425 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1426 Py_INCREF(new);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1427 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1428 self->ob_type = new;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1429 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1430 Py_DECREF(old);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1431 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1432 return 0;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1433 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1434
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1435 static PyGetSetDef object_getsets[] = {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1436 {"__class__", object_get_class, object_set_class,
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1437 "the object's class"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1438 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1439 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1440
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1441 static PyObject *
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1442 object_reduce(PyObject *self, PyObject *args)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1443 {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1444 /* Call copy_reg._reduce(self) */
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1445 static PyObject *copy_reg_str;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1446 PyObject *copy_reg, *res;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1447
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1448 if (!copy_reg_str) {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1449 copy_reg_str = PyString_InternFromString("copy_reg");
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1450 if (copy_reg_str == NULL)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1451 return NULL;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1452 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1453 copy_reg = PyImport_Import(copy_reg_str);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1454 if (!copy_reg)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1455 return NULL;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1456 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1457 Py_DECREF(copy_reg);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1458 return res;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1459 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1460
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1461 static PyMethodDef object_methods[] = {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1462 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1463 {0}
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1464 };
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1465
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1466 PyTypeObject PyBaseObject_Type = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1467 PyObject_HEAD_INIT(&PyType_Type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1468 0, /* ob_size */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1469 "object", /* tp_name */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1470 sizeof(PyObject), /* tp_basicsize */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1471 0, /* tp_itemsize */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1472 (destructor)object_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1473 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1474 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1475 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1476 0, /* tp_compare */
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1477 object_repr, /* tp_repr */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1478 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1479 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1480 0, /* tp_as_mapping */
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1481 object_hash, /* tp_hash */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1482 0, /* tp_call */
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1483 object_str, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1484 PyObject_GenericGetAttr, /* tp_getattro */
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1485 PyObject_GenericSetAttr, /* tp_setattro */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1486 0, /* tp_as_buffer */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1487 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1488 "The most base type", /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1489 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1490 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1491 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1492 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1493 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1494 0, /* tp_iternext */
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1495 object_methods, /* tp_methods */
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1496 0, /* tp_members */
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1497 object_getsets, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1498 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1499 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1500 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1501 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1502 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1503 object_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1504 PyType_GenericAlloc, /* tp_alloc */
19108
b7984c9bbf2c Thinking back to the 2.22 revision, I didn't like what I did there one
Guido van Rossum <guido@python.org>
parents: 19082
diff changeset
1505 PyType_GenericNew, /* tp_new */
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
1506 _PyObject_Del, /* tp_free */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1507 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1508
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1509
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1510 /* Initialize the __dict__ in a type object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1511
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1512 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1513 add_methods(PyTypeObject *type, PyMethodDef *meth)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1514 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1515 PyObject *dict = type->tp_defined;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1516
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1517 for (; meth->ml_name != NULL; meth++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1518 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1519 if (PyDict_GetItemString(dict, meth->ml_name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1520 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1521 descr = PyDescr_NewMethod(type, meth);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1522 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1523 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1524 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1525 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1526 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1527 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1528 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1529 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1530
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1531 static int
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
1532 add_members(PyTypeObject *type, PyMemberDef *memb)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1533 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1534 PyObject *dict = type->tp_defined;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1535
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1536 for (; memb->name != NULL; memb++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1537 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1538 if (PyDict_GetItemString(dict, memb->name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1539 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1540 descr = PyDescr_NewMember(type, memb);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1541 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1542 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1543 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1544 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1545 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1546 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1547 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1548 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1549
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1550 static int
20015
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
1551 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1552 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1553 PyObject *dict = type->tp_defined;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1554
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1555 for (; gsp->name != NULL; gsp++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1556 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1557 if (PyDict_GetItemString(dict, gsp->name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1558 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1559 descr = PyDescr_NewGetSet(type, gsp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1560
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1561 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1562 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1563 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1564 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1565 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1566 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1567 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1568 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1569
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1570 static void
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1571 inherit_special(PyTypeObject *type, PyTypeObject *base)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1572 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1573 int oldsize, newsize;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1574
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1575 /* Special flag magic */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1576 if (!type->tp_as_buffer && base->tp_as_buffer) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1577 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1578 type->tp_flags |=
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1579 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1580 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1581 if (!type->tp_as_sequence && base->tp_as_sequence) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1582 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1583 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1584 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1585 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1586 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1587 if ((!type->tp_as_number && base->tp_as_number) ||
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1588 (!type->tp_as_sequence && base->tp_as_sequence)) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1589 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1590 if (!type->tp_as_number && !type->tp_as_sequence) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1591 type->tp_flags |= base->tp_flags &
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1592 Py_TPFLAGS_HAVE_INPLACEOPS;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1593 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1594 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1595 /* Wow */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1596 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1597 if (!type->tp_as_number && base->tp_as_number) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1598 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1599 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1600 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1601
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1602 /* Copying basicsize is connected to the GC flags */
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1603 oldsize = base->tp_basicsize;
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1604 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1605 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1606 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1607 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1608 (!type->tp_traverse && !type->tp_clear)) {
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1609 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1610 if (type->tp_traverse == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1611 type->tp_traverse = base->tp_traverse;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1612 if (type->tp_clear == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1613 type->tp_clear = base->tp_clear;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1614 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1615 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1616 if (base != &PyBaseObject_Type ||
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1617 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1618 if (type->tp_new == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1619 type->tp_new = base->tp_new;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1620 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1621 }
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1622 type->tp_basicsize = newsize;
19253
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1623
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1624 /* Copy other non-function slots */
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1625
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1626 #undef COPYVAL
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1627 #define COPYVAL(SLOT) \
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1628 if (type->SLOT == 0) type->SLOT = base->SLOT
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1629
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1630 COPYVAL(tp_itemsize);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1631 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1632 COPYVAL(tp_weaklistoffset);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1633 }
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1634 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1635 COPYVAL(tp_dictoffset);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1636 }
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1637 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1638
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1639 static void
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1640 inherit_slots(PyTypeObject *type, PyTypeObject *base)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1641 {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1642 PyTypeObject *basebase;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1643
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1644 #undef SLOTDEFINED
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1645 #undef COPYSLOT
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1646 #undef COPYNUM
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1647 #undef COPYSEQ
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1648 #undef COPYMAP
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1649
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1650 #define SLOTDEFINED(SLOT) \
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1651 (base->SLOT != 0 && \
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1652 (basebase == NULL || base->SLOT != basebase->SLOT))
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1653
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1654 #define COPYSLOT(SLOT) \
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1655 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1656
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1657 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1658 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1659 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1660
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1661 /* This won't inherit indirect slots (from tp_as_number etc.)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1662 if type doesn't provide the space. */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1663
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1664 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1665 basebase = base->tp_base;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1666 if (basebase->tp_as_number == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1667 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1668 COPYNUM(nb_add);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1669 COPYNUM(nb_subtract);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1670 COPYNUM(nb_multiply);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1671 COPYNUM(nb_divide);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1672 COPYNUM(nb_remainder);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1673 COPYNUM(nb_divmod);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1674 COPYNUM(nb_power);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1675 COPYNUM(nb_negative);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1676 COPYNUM(nb_positive);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1677 COPYNUM(nb_absolute);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1678 COPYNUM(nb_nonzero);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1679 COPYNUM(nb_invert);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1680 COPYNUM(nb_lshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1681 COPYNUM(nb_rshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1682 COPYNUM(nb_and);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1683 COPYNUM(nb_xor);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1684 COPYNUM(nb_or);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1685 COPYNUM(nb_coerce);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1686 COPYNUM(nb_int);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1687 COPYNUM(nb_long);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1688 COPYNUM(nb_float);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1689 COPYNUM(nb_oct);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1690 COPYNUM(nb_hex);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1691 COPYNUM(nb_inplace_add);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1692 COPYNUM(nb_inplace_subtract);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1693 COPYNUM(nb_inplace_multiply);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1694 COPYNUM(nb_inplace_divide);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1695 COPYNUM(nb_inplace_remainder);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1696 COPYNUM(nb_inplace_power);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1697 COPYNUM(nb_inplace_lshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1698 COPYNUM(nb_inplace_rshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1699 COPYNUM(nb_inplace_and);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1700 COPYNUM(nb_inplace_xor);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1701 COPYNUM(nb_inplace_or);
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1702 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1703 COPYNUM(nb_true_divide);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1704 COPYNUM(nb_floor_divide);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1705 COPYNUM(nb_inplace_true_divide);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1706 COPYNUM(nb_inplace_floor_divide);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1707 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1708 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1709
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1710 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1711 basebase = base->tp_base;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1712 if (basebase->tp_as_sequence == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1713 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1714 COPYSEQ(sq_length);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1715 COPYSEQ(sq_concat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1716 COPYSEQ(sq_repeat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1717 COPYSEQ(sq_item);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1718 COPYSEQ(sq_slice);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1719 COPYSEQ(sq_ass_item);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1720 COPYSEQ(sq_ass_slice);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1721 COPYSEQ(sq_contains);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1722 COPYSEQ(sq_inplace_concat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1723 COPYSEQ(sq_inplace_repeat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1724 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1725
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1726 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1727 basebase = base->tp_base;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1728 if (basebase->tp_as_mapping == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1729 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1730 COPYMAP(mp_length);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1731 COPYMAP(mp_subscript);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1732 COPYMAP(mp_ass_subscript);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1733 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1734
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1735 basebase = base->tp_base;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1736
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1737 COPYSLOT(tp_dealloc);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1738 COPYSLOT(tp_print);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1739 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1740 type->tp_getattr = base->tp_getattr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1741 type->tp_getattro = base->tp_getattro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1742 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1743 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1744 type->tp_setattr = base->tp_setattr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1745 type->tp_setattro = base->tp_setattro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1746 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1747 /* tp_compare see tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1748 COPYSLOT(tp_repr);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1749 /* tp_hash see tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1750 COPYSLOT(tp_call);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1751 COPYSLOT(tp_str);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1752 COPYSLOT(tp_as_buffer);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1753 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1754 if (type->tp_compare == NULL &&
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1755 type->tp_richcompare == NULL &&
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1756 type->tp_hash == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1757 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1758 type->tp_compare = base->tp_compare;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1759 type->tp_richcompare = base->tp_richcompare;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1760 type->tp_hash = base->tp_hash;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1761 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1762 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1763 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1764 COPYSLOT(tp_compare);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1765 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1766 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1767 COPYSLOT(tp_iter);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1768 COPYSLOT(tp_iternext);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1769 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1770 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1771 COPYSLOT(tp_descr_get);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1772 COPYSLOT(tp_descr_set);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1773 COPYSLOT(tp_dictoffset);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1774 COPYSLOT(tp_init);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1775 COPYSLOT(tp_alloc);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1776 COPYSLOT(tp_free);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1777 }
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1778 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1779
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1780 staticforward int add_operators(PyTypeObject *);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1781 staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1782
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1783 int
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
1784 PyType_Ready(PyTypeObject *type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1785 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1786 PyObject *dict, *bases, *x;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1787 PyTypeObject *base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1788 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1789
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1790 if (type->tp_flags & Py_TPFLAGS_READY) {
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1791 assert(type->tp_dict != NULL);
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1792 return 0;
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1793 }
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1794 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1795 assert(type->tp_dict == NULL);
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1796
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1797 type->tp_flags |= Py_TPFLAGS_READYING;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1798
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1799 /* Initialize tp_base (defaults to BaseObject unless that's us) */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1800 base = type->tp_base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1801 if (base == NULL && type != &PyBaseObject_Type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1802 base = type->tp_base = &PyBaseObject_Type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1803
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1804 /* Initialize tp_bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1805 bases = type->tp_bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1806 if (bases == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1807 if (base == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1808 bases = PyTuple_New(0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1809 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1810 bases = Py_BuildValue("(O)", base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1811 if (bases == NULL)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1812 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1813 type->tp_bases = bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1814 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1815
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1816 /* Initialize the base class */
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
1817 if (base && base->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
1818 if (PyType_Ready(base) < 0)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1819 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1820 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1821
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1822 /* Initialize tp_defined */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1823 dict = type->tp_defined;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1824 if (dict == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1825 dict = PyDict_New();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1826 if (dict == NULL)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1827 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1828 type->tp_defined = dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1829 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1830
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1831 /* Add type-specific descriptors to tp_defined */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1832 if (add_operators(type) < 0)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1833 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1834 if (type->tp_methods != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1835 if (add_methods(type, type->tp_methods) < 0)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1836 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1837 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1838 if (type->tp_members != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1839 if (add_members(type, type->tp_members) < 0)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1840 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1841 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1842 if (type->tp_getset != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1843 if (add_getset(type, type->tp_getset) < 0)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1844 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1845 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1846
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1847 /* Temporarily make tp_dict the same object as tp_defined.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1848 (This is needed to call mro(), and can stay this way for
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1849 dynamic types). */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1850 Py_INCREF(type->tp_defined);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1851 type->tp_dict = type->tp_defined;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1852
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1853 /* Calculate method resolution order */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1854 if (mro_internal(type) < 0) {
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1855 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1856 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1857
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1858 /* Inherit special flags from dominant base */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1859 if (type->tp_base != NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1860 inherit_special(type, type->tp_base);
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1861
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1862 /* Initialize tp_dict properly */
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1863 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1864 /* For a static type, tp_dict is the consolidation
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1865 of the tp_defined of its bases in MRO. */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1866 Py_DECREF(type->tp_dict);
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1867 type->tp_dict = PyDict_Copy(type->tp_defined);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1868 if (type->tp_dict == NULL)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1869 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1870 bases = type->tp_mro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1871 assert(bases != NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1872 assert(PyTuple_Check(bases));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1873 n = PyTuple_GET_SIZE(bases);
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1874 for (i = 1; i < n; i++) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1875 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1876 assert(PyType_Check(base));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1877 x = base->tp_defined;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1878 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1879 goto error;
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1880 inherit_slots(type, base);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1881 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1882 }
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1883 else {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1884 /* For a dynamic type, we simply inherit the base slots. */
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1885 bases = type->tp_mro;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1886 assert(bases != NULL);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1887 assert(PyTuple_Check(bases));
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1888 n = PyTuple_GET_SIZE(bases);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1889 for (i = 1; i < n; i++) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1890 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1891 assert(PyType_Check(base));
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1892 inherit_slots(type, base);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1893 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1894 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1895
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1896 /* Some more special stuff */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1897 base = type->tp_base;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1898 if (base != NULL) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1899 if (type->tp_as_number == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1900 type->tp_as_number = base->tp_as_number;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1901 if (type->tp_as_sequence == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1902 type->tp_as_sequence = base->tp_as_sequence;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1903 if (type->tp_as_mapping == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1904 type->tp_as_mapping = base->tp_as_mapping;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1905 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1906
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1907 /* Link into each base class's list of subclasses */
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1908 bases = type->tp_bases;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1909 n = PyTuple_GET_SIZE(bases);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1910 for (i = 0; i < n; i++) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1911 base = (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1912 if (add_subclass((PyTypeObject *)base, type) < 0)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1913 goto error;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1914 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1915
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1916 /* All done -- set the ready flag */
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1917 assert(type->tp_dict != NULL);
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1918 type->tp_flags =
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1919 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1920 return 0;
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1921
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1922 error:
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1923 type->tp_flags &= ~Py_TPFLAGS_READYING;
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
1924 return -1;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1925 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1926
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1927 static int
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1928 add_subclass(PyTypeObject *base, PyTypeObject *type)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1929 {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1930 int i;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1931 PyObject *list, *ref, *new;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1932
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1933 list = base->tp_subclasses;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1934 if (list == NULL) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1935 base->tp_subclasses = list = PyList_New(0);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1936 if (list == NULL)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1937 return -1;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1938 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1939 assert(PyList_Check(list));
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1940 new = PyWeakref_NewRef((PyObject *)type, NULL);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1941 i = PyList_GET_SIZE(list);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1942 while (--i >= 0) {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1943 ref = PyList_GET_ITEM(list, i);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1944 assert(PyWeakref_CheckRef(ref));
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1945 if (PyWeakref_GET_OBJECT(ref) == Py_None)
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1946 return PyList_SetItem(list, i, new);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1947 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1948 i = PyList_Append(list, new);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1949 Py_DECREF(new);
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1950 return i;
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1951 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1952
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1953
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1954 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1955
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1956 /* There's a wrapper *function* for each distinct function typedef used
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1957 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1958 wrapper *table* for each distinct operation (e.g. __len__, __add__).
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1959 Most tables have only one entry; the tables for binary operators have two
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1960 entries, one regular and one with reversed arguments. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1961
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1962 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1963 wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1964 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1965 inquiry func = (inquiry)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1966 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1967
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1968 if (!PyArg_ParseTuple(args, ""))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1969 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1970 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1971 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1972 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1973 return PyInt_FromLong((long)res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1974 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1975
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1976 static struct wrapperbase tab_len[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1977 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1978 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1979 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1980
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1981 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1982 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1983 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1984 binaryfunc func = (binaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1985 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1986
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1987 if (!PyArg_ParseTuple(args, "O", &other))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1988 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1989 return (*func)(self, other);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1990 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1991
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1992 static PyObject *
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
1993 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
1994 {
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
1995 binaryfunc func = (binaryfunc)wrapped;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
1996 PyObject *other;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
1997
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
1998 if (!PyArg_ParseTuple(args, "O", &other))
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
1999 return NULL;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2000 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2001 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2002 Py_INCREF(Py_NotImplemented);
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2003 return Py_NotImplemented;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2004 }
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2005 return (*func)(self, other);
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2006 }
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2007
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2008 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2009 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2010 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2011 binaryfunc func = (binaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2012 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2013
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2014 if (!PyArg_ParseTuple(args, "O", &other))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2015 return NULL;
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2016 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2017 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2018 Py_INCREF(Py_NotImplemented);
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2019 return Py_NotImplemented;
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2020 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2021 return (*func)(other, self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2022 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2023
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2024 #undef BINARY
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2025 #define BINARY(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2026 static struct wrapperbase tab_##NAME[] = { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2027 {"__" #NAME "__", \
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2028 (wrapperfunc)wrap_binaryfunc_l, \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2029 "x.__" #NAME "__(y) <==> " #OP}, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2030 {"__r" #NAME "__", \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2031 (wrapperfunc)wrap_binaryfunc_r, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2032 "y.__r" #NAME "__(x) <==> " #OP}, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2033 {0} \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2034 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2035
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2036 BINARY(add, "x+y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2037 BINARY(sub, "x-y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2038 BINARY(mul, "x*y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2039 BINARY(div, "x/y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2040 BINARY(mod, "x%y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2041 BINARY(divmod, "divmod(x,y)");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2042 BINARY(lshift, "x<<y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2043 BINARY(rshift, "x>>y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2044 BINARY(and, "x&y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2045 BINARY(xor, "x^y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2046 BINARY(or, "x|y");
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2047
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2048 static PyObject *
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2049 wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2050 {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2051 coercion func = (coercion)wrapped;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2052 PyObject *other, *res;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2053 int ok;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2054
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2055 if (!PyArg_ParseTuple(args, "O", &other))
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2056 return NULL;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2057 ok = func(&self, &other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2058 if (ok < 0)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2059 return NULL;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2060 if (ok > 0) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2061 Py_INCREF(Py_NotImplemented);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2062 return Py_NotImplemented;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2063 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2064 res = PyTuple_New(2);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2065 if (res == NULL) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2066 Py_DECREF(self);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2067 Py_DECREF(other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2068 return NULL;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2069 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2070 PyTuple_SET_ITEM(res, 0, self);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2071 PyTuple_SET_ITEM(res, 1, other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2072 return res;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2073 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2074
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2075 static struct wrapperbase tab_coerce[] = {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2076 {"__coerce__", (wrapperfunc)wrap_coercefunc,
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2077 "x.__coerce__(y) <==> coerce(x, y)"},
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2078 {0}
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2079 };
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2080
20095
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2081 BINARY(floordiv, "x//y");
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2082 BINARY(truediv, "x/y # true division");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2083
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2084 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2085 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2086 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2087 ternaryfunc func = (ternaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2088 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2089 PyObject *third = Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2090
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2091 /* Note: This wrapper only works for __pow__() */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2092
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2093 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2094 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2095 return (*func)(self, other, third);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2096 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2097
20168
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2098 static PyObject *
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2099 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2100 {
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2101 ternaryfunc func = (ternaryfunc)wrapped;
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2102 PyObject *other;
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2103 PyObject *third = Py_None;
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2104
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2105 /* Note: This wrapper only works for __pow__() */
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2106
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2107 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2108 return NULL;
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2109 return (*func)(other, self, third);
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2110 }
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2111
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2112 #undef TERNARY
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2113 #define TERNARY(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2114 static struct wrapperbase tab_##NAME[] = { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2115 {"__" #NAME "__", \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2116 (wrapperfunc)wrap_ternaryfunc, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2117 "x.__" #NAME "__(y, z) <==> " #OP}, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2118 {"__r" #NAME "__", \
20168
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2119 (wrapperfunc)wrap_ternaryfunc_r, \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2120 "y.__r" #NAME "__(x, z) <==> " #OP}, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2121 {0} \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2122 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2123
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2124 TERNARY(pow, "(x**y) % z");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2125
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2126 #undef UNARY
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2127 #define UNARY(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2128 static struct wrapperbase tab_##NAME[] = { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2129 {"__" #NAME "__", \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2130 (wrapperfunc)wrap_unaryfunc, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2131 "x.__" #NAME "__() <==> " #OP}, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2132 {0} \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2133 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2134
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2135 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2136 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2137 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2138 unaryfunc func = (unaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2139
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2140 if (!PyArg_ParseTuple(args, ""))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2141 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2142 return (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2143 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2144
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2145 UNARY(neg, "-x");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2146 UNARY(pos, "+x");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2147 UNARY(abs, "abs(x)");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2148 UNARY(nonzero, "x != 0");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2149 UNARY(invert, "~x");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2150 UNARY(int, "int(x)");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2151 UNARY(long, "long(x)");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2152 UNARY(float, "float(x)");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2153 UNARY(oct, "oct(x)");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2154 UNARY(hex, "hex(x)");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2155
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2156 #undef IBINARY
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2157 #define IBINARY(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2158 static struct wrapperbase tab_##NAME[] = { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2159 {"__" #NAME "__", \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2160 (wrapperfunc)wrap_binaryfunc, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2161 "x.__" #NAME "__(y) <==> " #OP}, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2162 {0} \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2163 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2164
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2165 IBINARY(iadd, "x+=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2166 IBINARY(isub, "x-=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2167 IBINARY(imul, "x*=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2168 IBINARY(idiv, "x/=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2169 IBINARY(imod, "x%=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2170 IBINARY(ilshift, "x<<=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2171 IBINARY(irshift, "x>>=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2172 IBINARY(iand, "x&=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2173 IBINARY(ixor, "x^=y");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2174 IBINARY(ior, "x|=y");
20095
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2175 IBINARY(ifloordiv, "x//=y");
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2176 IBINARY(itruediv, "x/=y # true division");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2177
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2178 #undef ITERNARY
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2179 #define ITERNARY(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2180 static struct wrapperbase tab_##NAME[] = { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2181 {"__" #NAME "__", \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2182 (wrapperfunc)wrap_ternaryfunc, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2183 "x.__" #NAME "__(y) <==> " #OP}, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2184 {0} \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2185 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2186
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2187 ITERNARY(ipow, "x = (x**y) % z");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2188
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2189 static struct wrapperbase tab_getitem[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2190 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2191 "x.__getitem__(y) <==> x[y]"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2192 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2193 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2194
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2195 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2196 wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2197 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2198 intargfunc func = (intargfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2199 int i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2200
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2201 if (!PyArg_ParseTuple(args, "i", &i))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2202 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2203 return (*func)(self, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2204 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2205
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2206 static struct wrapperbase tab_mul_int[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2207 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2208 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2209 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2210 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2211
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2212 static struct wrapperbase tab_concat[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2213 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2214 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2215 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2216
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2217 static struct wrapperbase tab_imul_int[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2218 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2219 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2220 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2221
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2222 static int
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2223 getindex(PyObject *self, PyObject *arg)
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2224 {
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2225 int i;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2226
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2227 i = PyInt_AsLong(arg);
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2228 if (i == -1 && PyErr_Occurred())
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2229 return -1;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2230 if (i < 0) {
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2231 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2232 if (sq && sq->sq_length) {
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2233 int n = (*sq->sq_length)(self);
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2234 if (n < 0)
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2235 return -1;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2236 i += n;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2237 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2238 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2239 return i;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2240 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2241
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2242 static PyObject *
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2243 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2244 {
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2245 intargfunc func = (intargfunc)wrapped;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2246 PyObject *arg;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2247 int i;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2248
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2249 if (PyTuple_GET_SIZE(args) == 1) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2250 arg = PyTuple_GET_ITEM(args, 0);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2251 i = getindex(self, arg);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2252 if (i == -1 && PyErr_Occurred())
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2253 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2254 return (*func)(self, i);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2255 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2256 PyArg_ParseTuple(args, "O", &arg);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2257 assert(PyErr_Occurred());
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2258 return NULL;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2259 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2260
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2261 static struct wrapperbase tab_getitem_int[] = {
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2262 {"__getitem__", (wrapperfunc)wrap_sq_item,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2263 "x.__getitem__(i) <==> x[i]"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2264 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2265 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2266
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2267 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2268 wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2269 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2270 intintargfunc func = (intintargfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2271 int i, j;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2272
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2273 if (!PyArg_ParseTuple(args, "ii", &i, &j))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2274 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2275 return (*func)(self, i, j);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2276 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2277
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2278 static struct wrapperbase tab_getslice[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2279 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2280 "x.__getslice__(i, j) <==> x[i:j]"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2281 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2282 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2283
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2284 static PyObject *
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2285 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2286 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2287 intobjargproc func = (intobjargproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2288 int i, res;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2289 PyObject *arg, *value;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2290
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2291 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2292 return NULL;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2293 i = getindex(self, arg);
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2294 if (i == -1 && PyErr_Occurred())
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2295 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2296 res = (*func)(self, i, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2297 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2298 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2299 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2300 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2301 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2302
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2303 static PyObject *
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2304 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2305 {
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2306 intobjargproc func = (intobjargproc)wrapped;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2307 int i, res;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2308 PyObject *arg;
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2309
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2310 if (!PyArg_ParseTuple(args, "O", &arg))
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2311 return NULL;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2312 i = getindex(self, arg);
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2313 if (i == -1 && PyErr_Occurred())
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2314 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2315 res = (*func)(self, i, NULL);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2316 if (res == -1 && PyErr_Occurred())
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2317 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2318 Py_INCREF(Py_None);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2319 return Py_None;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2320 }
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2321
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2322 static struct wrapperbase tab_setitem_int[] = {
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2323 {"__setitem__", (wrapperfunc)wrap_sq_setitem,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2324 "x.__setitem__(i, y) <==> x[i]=y"},
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2325 {"__delitem__", (wrapperfunc)wrap_sq_delitem,
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2326 "x.__delitem__(y) <==> del x[y]"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2327 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2328 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2329
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2330 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2331 wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2332 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2333 intintobjargproc func = (intintobjargproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2334 int i, j, res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2335 PyObject *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2336
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2337 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2338 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2339 res = (*func)(self, i, j, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2340 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2341 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2342 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2343 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2344 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2345
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2346 static PyObject *
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2347 wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2348 {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2349 intintobjargproc func = (intintobjargproc)wrapped;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2350 int i, j, res;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2351
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2352 if (!PyArg_ParseTuple(args, "ii", &i, &j))
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2353 return NULL;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2354 res = (*func)(self, i, j, NULL);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2355 if (res == -1 && PyErr_Occurred())
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2356 return NULL;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2357 Py_INCREF(Py_None);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2358 return Py_None;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2359 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2360
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2361 static struct wrapperbase tab_setslice[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2362 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2363 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2364 {"__delslice__", (wrapperfunc)wrap_delslice,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2365 "x.__delslice__(i, j) <==> del x[i:j]"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2366 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2367 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2368
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2369 /* XXX objobjproc is a misnomer; should be objargpred */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2370 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2371 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2372 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2373 objobjproc func = (objobjproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2374 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2375 PyObject *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2376
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2377 if (!PyArg_ParseTuple(args, "O", &value))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2378 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2379 res = (*func)(self, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2380 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2381 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2382 return PyInt_FromLong((long)res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2383 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2384
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2385 static struct wrapperbase tab_contains[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2386 {"__contains__", (wrapperfunc)wrap_objobjproc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2387 "x.__contains__(y) <==> y in x"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2388 {0}
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2389 };
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2390
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2391 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2392 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2393 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2394 objobjargproc func = (objobjargproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2395 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2396 PyObject *key, *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2397
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2398 if (!PyArg_ParseTuple(args, "OO", &key, &value))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2399 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2400 res = (*func)(self, key, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2401 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2402 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2403 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2404 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2405 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2406
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2407 static PyObject *
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2408 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2409 {
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2410 objobjargproc func = (objobjargproc)wrapped;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2411 int res;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2412 PyObject *key;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2413
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2414 if (!PyArg_ParseTuple(args, "O", &key))
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2415 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2416 res = (*func)(self, key, NULL);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2417 if (res == -1 && PyErr_Occurred())
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2418 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2419 Py_INCREF(Py_None);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2420 return Py_None;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2421 }
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2422
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2423 static struct wrapperbase tab_setitem[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2424 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2425 "x.__setitem__(y, z) <==> x[y]=z"},
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2426 {"__delitem__", (wrapperfunc)wrap_delitem,
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2427 "x.__delitem__(y) <==> del x[y]"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2428 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2429 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2430
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2431 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2432 wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2433 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2434 cmpfunc func = (cmpfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2435 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2436 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2437
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2438 if (!PyArg_ParseTuple(args, "O", &other))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2439 return NULL;
20063
9a599b1d9d1c Another comparison patch-up: comparing a type with a dynamic metatype
Guido van Rossum <guido@python.org>
parents: 20056
diff changeset
2440 if (other->ob_type->tp_compare != func &&
9a599b1d9d1c Another comparison patch-up: comparing a type with a dynamic metatype
Guido van Rossum <guido@python.org>
parents: 20056
diff changeset
2441 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
19964
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
2442 PyErr_Format(
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
2443 PyExc_TypeError,
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
2444 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
2445 self->ob_type->tp_name,
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
2446 self->ob_type->tp_name,
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
2447 other->ob_type->tp_name);
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
2448 return NULL;
43e5fd54f8c9 wrap_cmpfunc(): added a safety check for the __cmp__ wrapper. We can
Guido van Rossum <guido@python.org>
parents: 19950
diff changeset
2449 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2450 res = (*func)(self, other);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2451 if (PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2452 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2453 return PyInt_FromLong((long)res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2454 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2455
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2456 static struct wrapperbase tab_cmp[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2457 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2458 "x.__cmp__(y) <==> cmp(x,y)"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2459 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2460 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2461
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2462 static struct wrapperbase tab_repr[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2463 {"__repr__", (wrapperfunc)wrap_unaryfunc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2464 "x.__repr__() <==> repr(x)"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2465 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2466 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2467
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2468 static struct wrapperbase tab_getattr[] = {
20021
40cc6621eddc Change the name of the __getattr__ special method for new-style
Guido van Rossum <guido@python.org>
parents: 20015
diff changeset
2469 {"__getattribute__", (wrapperfunc)wrap_binaryfunc,
40cc6621eddc Change the name of the __getattr__ special method for new-style
Guido van Rossum <guido@python.org>
parents: 20015
diff changeset
2470 "x.__getattribute__('name') <==> x.name"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2471 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2472 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2473
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2474 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2475 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2476 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2477 setattrofunc func = (setattrofunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2478 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2479 PyObject *name, *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2480
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2481 if (!PyArg_ParseTuple(args, "OO", &name, &value))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2482 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2483 res = (*func)(self, name, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2484 if (res < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2485 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2486 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2487 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2488 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2489
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2490 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2491 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2492 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2493 setattrofunc func = (setattrofunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2494 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2495 PyObject *name;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2496
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2497 if (!PyArg_ParseTuple(args, "O", &name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2498 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2499 res = (*func)(self, name, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2500 if (res < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2501 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2502 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2503 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2504 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2505
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2506 static struct wrapperbase tab_setattr[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2507 {"__setattr__", (wrapperfunc)wrap_setattr,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2508 "x.__setattr__('name', value) <==> x.name = value"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2509 {"__delattr__", (wrapperfunc)wrap_delattr,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2510 "x.__delattr__('name') <==> del x.name"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2511 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2512 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2513
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2514 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2515 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2516 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2517 hashfunc func = (hashfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2518 long res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2519
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2520 if (!PyArg_ParseTuple(args, ""))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2521 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2522 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2523 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2524 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2525 return PyInt_FromLong(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2526 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2527
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2528 static struct wrapperbase tab_hash[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2529 {"__hash__", (wrapperfunc)wrap_hashfunc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2530 "x.__hash__() <==> hash(x)"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2531 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2532 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2533
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2534 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2535 wrap_call(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2536 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2537 ternaryfunc func = (ternaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2538
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2539 /* XXX What about keyword arguments? */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2540 return (*func)(self, args, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2541 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2542
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2543 static struct wrapperbase tab_call[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2544 {"__call__", (wrapperfunc)wrap_call,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2545 "x.__call__(...) <==> x(...)"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2546 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2547 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2548
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2549 static struct wrapperbase tab_str[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2550 {"__str__", (wrapperfunc)wrap_unaryfunc,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2551 "x.__str__() <==> str(x)"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2552 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2553 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2554
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2555 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2556 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2557 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2558 richcmpfunc func = (richcmpfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2559 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2560
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2561 if (!PyArg_ParseTuple(args, "O", &other))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2562 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2563 return (*func)(self, other, op);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2564 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2565
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2566 #undef RICHCMP_WRAPPER
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2567 #define RICHCMP_WRAPPER(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2568 static PyObject * \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2569 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2570 { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2571 return wrap_richcmpfunc(self, args, wrapped, OP); \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2572 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2573
19070
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
2574 RICHCMP_WRAPPER(lt, Py_LT)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
2575 RICHCMP_WRAPPER(le, Py_LE)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
2576 RICHCMP_WRAPPER(eq, Py_EQ)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
2577 RICHCMP_WRAPPER(ne, Py_NE)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
2578 RICHCMP_WRAPPER(gt, Py_GT)
39a73c6773f1 Removed extraneous semicolons that caused a gazzilion "empty declaration" warnings in the MetroWerks compiler.
Jack Jansen <jack.jansen@cwi.nl>
parents: 19040
diff changeset
2579 RICHCMP_WRAPPER(ge, Py_GE)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2580
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2581 #undef RICHCMP_ENTRY
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2582 #define RICHCMP_ENTRY(NAME, EXPR) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2583 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2584 "x.__" #NAME "__(y) <==> " EXPR}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2585
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2586 static struct wrapperbase tab_richcmp[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2587 RICHCMP_ENTRY(lt, "x<y"),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2588 RICHCMP_ENTRY(le, "x<=y"),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2589 RICHCMP_ENTRY(eq, "x==y"),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2590 RICHCMP_ENTRY(ne, "x!=y"),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2591 RICHCMP_ENTRY(gt, "x>y"),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2592 RICHCMP_ENTRY(ge, "x>=y"),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2593 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2594 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2595
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2596 static struct wrapperbase tab_iter[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2597 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2598 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2599 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2600
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2601 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2602 wrap_next(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2603 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2604 unaryfunc func = (unaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2605 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2606
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2607 if (!PyArg_ParseTuple(args, ""))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2608 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2609 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2610 if (res == NULL && !PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2611 PyErr_SetNone(PyExc_StopIteration);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2612 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2613 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2614
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2615 static struct wrapperbase tab_next[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2616 {"next", (wrapperfunc)wrap_next,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2617 "x.next() -> the next value, or raise StopIteration"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2618 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2619 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2620
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2621 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2622 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2623 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2624 descrgetfunc func = (descrgetfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2625 PyObject *obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2626 PyObject *type = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2627
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2628 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2629 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2630 return (*func)(self, obj, type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2631 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2632
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2633 static struct wrapperbase tab_descr_get[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2634 {"__get__", (wrapperfunc)wrap_descr_get,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2635 "descr.__get__(obj, type) -> value"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2636 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2637 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2638
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2639 static PyObject *
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2640 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2641 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2642 descrsetfunc func = (descrsetfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2643 PyObject *obj, *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2644 int ret;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2645
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2646 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2647 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2648 ret = (*func)(self, obj, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2649 if (ret < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2650 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2651 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2652 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2653 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2654
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2655 static struct wrapperbase tab_descr_set[] = {
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2656 {"__set__", (wrapperfunc)wrap_descr_set,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2657 "descr.__set__(obj, value)"},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2658 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2659 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2660
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2661 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2662 wrap_init(PyObject *self, PyObject *args, void *wrapped)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2663 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2664 initproc func = (initproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2665
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2666 /* XXX What about keyword arguments? */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2667 if (func(self, args, NULL) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2668 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2669 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2670 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2671 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2672
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2673 static struct wrapperbase tab_init[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2674 {"__init__", (wrapperfunc)wrap_init,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2675 "x.__init__(...) initializes x; "
20190
5642d37f1b16 Fix typo found by doerwalter.
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2676 "see x.__class__.__doc__ for signature"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2677 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2678 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2679
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2680 static PyObject *
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2681 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2682 {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2683 PyTypeObject *type, *subtype, *staticbase;
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2684 PyObject *arg0, *res;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2685
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2686 if (self == NULL || !PyType_Check(self))
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2687 Py_FatalError("__new__() called with non-type 'self'");
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2688 type = (PyTypeObject *)self;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2689 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2690 PyErr_Format(PyExc_TypeError,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2691 "%s.__new__(): not enough arguments",
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2692 type->tp_name);
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2693 return NULL;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2694 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2695 arg0 = PyTuple_GET_ITEM(args, 0);
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2696 if (!PyType_Check(arg0)) {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2697 PyErr_Format(PyExc_TypeError,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2698 "%s.__new__(X): X is not a type object (%s)",
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2699 type->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2700 arg0->ob_type->tp_name);
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2701 return NULL;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2702 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2703 subtype = (PyTypeObject *)arg0;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2704 if (!PyType_IsSubtype(subtype, type)) {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2705 PyErr_Format(PyExc_TypeError,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2706 "%s.__new__(%s): %s is not a subtype of %s",
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2707 type->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2708 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2709 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2710 type->tp_name);
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2711 return NULL;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2712 }
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2713
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2714 /* Check that the use doesn't do something silly and unsafe like
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2715 object.__new__(dictionary). To do this, we check that the
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2716 most derived base that's not a heap type is this type. */
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2717 staticbase = subtype;
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2718 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2719 staticbase = staticbase->tp_base;
19914
2027dc124edd tp_new_wrapper(): A subtle change in the check for safe use.
Guido van Rossum <guido@python.org>
parents: 19913
diff changeset
2720 if (staticbase->tp_new != type->tp_new) {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2721 PyErr_Format(PyExc_TypeError,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2722 "%s.__new__(%s) is not safe, use %s.__new__()",
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2723 type->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2724 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2725 staticbase == NULL ? "?" : staticbase->tp_name);
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2726 return NULL;
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2727 }
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2728
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2729 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2730 if (args == NULL)
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2731 return NULL;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2732 res = type->tp_new(subtype, args, kwds);
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2733 Py_DECREF(args);
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2734 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2735 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2736
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2737 static struct PyMethodDef tp_new_methoddef[] = {
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2738 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2739 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2740 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2741 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2742
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2743 static int
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2744 add_tp_new_wrapper(PyTypeObject *type)
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2745 {
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2746 PyObject *func;
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2747
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2748 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2749 return 0;
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2750 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2751 if (func == NULL)
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2752 return -1;
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2753 return PyDict_SetItemString(type->tp_defined, "__new__", func);
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2754 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2755
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2756 static int
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2757 add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2758 {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2759 PyObject *dict = type->tp_defined;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2760
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2761 for (; wraps->name != NULL; wraps++) {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2762 PyObject *descr;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2763 if (PyDict_GetItemString(dict, wraps->name))
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2764 continue;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2765 descr = PyDescr_NewWrapper(type, wraps, wrapped);
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2766 if (descr == NULL)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2767 return -1;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2768 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2769 return -1;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2770 Py_DECREF(descr);
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2771 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2772 return 0;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2773 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2774
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
2775 /* This function is called by PyType_Ready() to populate the type's
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2776 dictionary with method descriptors for function slots. For each
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2777 function slot (like tp_repr) that's defined in the type, one or
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2778 more corresponding descriptors are added in the type's tp_defined
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2779 dictionary under the appropriate name (like __repr__). Some
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2780 function slots cause more than one descriptor to be added (for
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2781 example, the nb_add slot adds both __add__ and __radd__
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2782 descriptors) and some function slots compete for the same
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2783 descriptor (for example both sq_item and mp_subscript generate a
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2784 __getitem__ descriptor). This only adds new descriptors and
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2785 doesn't overwrite entries in tp_defined that were previously
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2786 defined. The descriptors contain a reference to the C function
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2787 they must call, so that it's safe if they are copied into a
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2788 subtype's __dict__ and the subtype has a different C function in
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2789 its slot -- calling the method defined by the descriptor will call
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2790 the C function that was used to create it, rather than the C
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2791 function present in the slot when it is called. (This is important
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2792 because a subtype may have a C function in the slot that calls the
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2793 method from the dictionary, and we want to avoid infinite recursion
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2794 here.) */
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2795
19011
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2796 static int
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2797 add_operators(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2798 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2799 PySequenceMethods *sq;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2800 PyMappingMethods *mp;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2801 PyNumberMethods *nb;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2802
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2803 #undef ADD
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2804 #define ADD(SLOT, TABLE) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2805 if (SLOT) { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2806 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2807 return -1; \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2808 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2809
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2810 if ((sq = type->tp_as_sequence) != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2811 ADD(sq->sq_length, tab_len);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2812 ADD(sq->sq_concat, tab_concat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2813 ADD(sq->sq_repeat, tab_mul_int);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2814 ADD(sq->sq_item, tab_getitem_int);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2815 ADD(sq->sq_slice, tab_getslice);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2816 ADD(sq->sq_ass_item, tab_setitem_int);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2817 ADD(sq->sq_ass_slice, tab_setslice);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2818 ADD(sq->sq_contains, tab_contains);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2819 ADD(sq->sq_inplace_concat, tab_iadd);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2820 ADD(sq->sq_inplace_repeat, tab_imul_int);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2821 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2822
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2823 if ((mp = type->tp_as_mapping) != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2824 if (sq->sq_length == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2825 ADD(mp->mp_length, tab_len);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2826 ADD(mp->mp_subscript, tab_getitem);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2827 ADD(mp->mp_ass_subscript, tab_setitem);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2828 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2829
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2830 if ((nb = type->tp_as_number) != NULL) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2831 ADD(nb->nb_add, tab_add);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2832 ADD(nb->nb_subtract, tab_sub);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2833 ADD(nb->nb_multiply, tab_mul);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2834 ADD(nb->nb_divide, tab_div);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2835 ADD(nb->nb_remainder, tab_mod);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2836 ADD(nb->nb_divmod, tab_divmod);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2837 ADD(nb->nb_power, tab_pow);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2838 ADD(nb->nb_negative, tab_neg);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2839 ADD(nb->nb_positive, tab_pos);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2840 ADD(nb->nb_absolute, tab_abs);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2841 ADD(nb->nb_nonzero, tab_nonzero);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2842 ADD(nb->nb_invert, tab_invert);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2843 ADD(nb->nb_lshift, tab_lshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2844 ADD(nb->nb_rshift, tab_rshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2845 ADD(nb->nb_and, tab_and);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2846 ADD(nb->nb_xor, tab_xor);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2847 ADD(nb->nb_or, tab_or);
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2848 ADD(nb->nb_coerce, tab_coerce);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2849 ADD(nb->nb_int, tab_int);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2850 ADD(nb->nb_long, tab_long);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2851 ADD(nb->nb_float, tab_float);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2852 ADD(nb->nb_oct, tab_oct);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2853 ADD(nb->nb_hex, tab_hex);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2854 ADD(nb->nb_inplace_add, tab_iadd);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2855 ADD(nb->nb_inplace_subtract, tab_isub);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2856 ADD(nb->nb_inplace_multiply, tab_imul);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2857 ADD(nb->nb_inplace_divide, tab_idiv);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2858 ADD(nb->nb_inplace_remainder, tab_imod);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2859 ADD(nb->nb_inplace_power, tab_ipow);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2860 ADD(nb->nb_inplace_lshift, tab_ilshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2861 ADD(nb->nb_inplace_rshift, tab_irshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2862 ADD(nb->nb_inplace_and, tab_iand);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2863 ADD(nb->nb_inplace_xor, tab_ixor);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2864 ADD(nb->nb_inplace_or, tab_ior);
20328
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2865 if (type->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
20095
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2866 ADD(nb->nb_floor_divide, tab_floordiv);
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2867 ADD(nb->nb_true_divide, tab_truediv);
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2868 ADD(nb->nb_inplace_floor_divide, tab_ifloordiv);
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2869 ADD(nb->nb_inplace_true_divide, tab_itruediv);
289c1a700286 add_operators(): the __floordiv__ and __truediv__ descriptors (and
Guido van Rossum <guido@python.org>
parents: 20084
diff changeset
2870 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2871 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2872
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2873 ADD(type->tp_getattro, tab_getattr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2874 ADD(type->tp_setattro, tab_setattr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2875 ADD(type->tp_compare, tab_cmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2876 ADD(type->tp_repr, tab_repr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2877 ADD(type->tp_hash, tab_hash);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2878 ADD(type->tp_call, tab_call);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2879 ADD(type->tp_str, tab_str);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2880 ADD(type->tp_richcompare, tab_richcmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2881 ADD(type->tp_iter, tab_iter);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2882 ADD(type->tp_iternext, tab_next);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2883 ADD(type->tp_descr_get, tab_descr_get);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2884 ADD(type->tp_descr_set, tab_descr_set);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2885 ADD(type->tp_init, tab_init);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2886
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2887 if (type->tp_new != NULL) {
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2888 if (add_tp_new_wrapper(type) < 0)
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2889 return -1;
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2890 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2891
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2892 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2893 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2894
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2895 /* Slot wrappers that call the corresponding __foo__ slot. See comments
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2896 below at override_slots() for more explanation. */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2897
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2898 #define SLOT0(FUNCNAME, OPSTR) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2899 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2900 FUNCNAME(PyObject *self) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2901 { \
19540
e141b392b61d Fix a typo in SLOT0 macro for the declaration of cache_str.
Guido van Rossum <guido@python.org>
parents: 19538
diff changeset
2902 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2903 return call_method(self, OPSTR, &cache_str, "()"); \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2904 }
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2905
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2906 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2907 static PyObject * \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2908 FUNCNAME(PyObject *self, ARG1TYPE arg1) \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2909 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2910 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2911 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2912 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2913
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2914
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2915 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2916 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2917 FUNCNAME(PyObject *self, PyObject *other) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2918 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2919 static PyObject *cache_str, *rcache_str; \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2920 int do_other = self->ob_type != other->ob_type && \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2921 other->ob_type->tp_as_number != NULL && \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2922 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2923 if (self->ob_type->tp_as_number != NULL && \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2924 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2925 PyObject *r; \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2926 if (do_other && \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2927 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2928 r = call_maybe( \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2929 other, ROPSTR, &rcache_str, "(O)", self); \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2930 if (r != Py_NotImplemented) \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2931 return r; \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2932 Py_DECREF(r); \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2933 do_other = 0; \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2934 } \
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
2935 r = call_maybe( \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2936 self, OPSTR, &cache_str, "(O)", other); \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2937 if (r != Py_NotImplemented || \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2938 other->ob_type == self->ob_type) \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2939 return r; \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2940 Py_DECREF(r); \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2941 } \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2942 if (do_other) { \
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
2943 return call_maybe( \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2944 other, ROPSTR, &rcache_str, "(O)", self); \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2945 } \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2946 Py_INCREF(Py_NotImplemented); \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2947 return Py_NotImplemented; \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2948 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2949
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2950 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2951 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2952
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2953 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2954 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2955 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2956 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2957 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2958 return call_method(self, OPSTR, &cache_str, \
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2959 "(" ARGCODES ")", arg1, arg2); \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2960 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2961
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2962 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2963 slot_sq_length(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2964 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2965 static PyObject *len_str;
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2966 PyObject *res = call_method(self, "__len__", &len_str, "()");
20197
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
2967 int len;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2968
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2969 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2970 return -1;
20197
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
2971 len = (int)PyInt_AsLong(res);
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
2972 Py_DECREF(res);
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
2973 return len;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2974 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2975
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2976 SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2977 SLOT1(slot_sq_repeat, "__mul__", int, "i")
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2978
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2979 /* Super-optimized version of slot_sq_item.
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2980 Other slots could do the same... */
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2981 static PyObject *
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2982 slot_sq_item(PyObject *self, int i)
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2983 {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2984 static PyObject *getitem_str;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2985 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2986 descrgetfunc f;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2987
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2988 if (getitem_str == NULL) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2989 getitem_str = PyString_InternFromString("__getitem__");
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2990 if (getitem_str == NULL)
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2991 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2992 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2993 func = _PyType_Lookup(self->ob_type, getitem_str);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2994 if (func != NULL) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2995 if (func->ob_type == &PyWrapperDescr_Type) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2996 PyWrapperDescrObject *wrapper =
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2997 (PyWrapperDescrObject *)func;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2998 if (wrapper->d_base->wrapper == wrap_sq_item) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2999 intargfunc f;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3000 f = (intargfunc)(wrapper->d_wrapped);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3001 return f(self, i);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3002 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3003 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3004 if ((f = func->ob_type->tp_descr_get) == NULL)
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3005 Py_INCREF(func);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3006 else
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3007 func = f(func, self, (PyObject *)(self->ob_type));
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3008 ival = PyInt_FromLong(i);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3009 if (ival != NULL) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3010 args = PyTuple_New(1);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3011 if (args != NULL) {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3012 PyTuple_SET_ITEM(args, 0, ival);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3013 retval = PyObject_Call(func, args, NULL);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3014 Py_XDECREF(args);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3015 Py_XDECREF(func);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3016 return retval;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3017 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3018 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3019 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3020 else {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3021 PyErr_SetObject(PyExc_AttributeError, getitem_str);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3022 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3023 Py_XDECREF(args);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3024 Py_XDECREF(ival);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3025 Py_XDECREF(func);
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3026 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3027 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
3028
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3029 SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3030
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3031 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3032 slot_sq_ass_item(PyObject *self, int index, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3033 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3034 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3035 static PyObject *delitem_str, *setitem_str;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3036
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3037 if (value == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3038 res = call_method(self, "__delitem__", &delitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3039 "(i)", index);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3040 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3041 res = call_method(self, "__setitem__", &setitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3042 "(iO)", index, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3043 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3044 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3045 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3046 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3047 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3048
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3049 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3050 slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3051 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3052 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3053 static PyObject *delslice_str, *setslice_str;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3054
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3055 if (value == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3056 res = call_method(self, "__delslice__", &delslice_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3057 "(ii)", i, j);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3058 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3059 res = call_method(self, "__setslice__", &setslice_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3060 "(iiO)", i, j, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3061 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3062 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3063 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3064 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3065 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3066
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3067 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3068 slot_sq_contains(PyObject *self, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3069 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3070 PyObject *func, *res, *args;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3071 static PyObject *contains_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3072
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3073 func = lookup_maybe(self, "__contains__", &contains_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3074
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3075 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3076 args = Py_BuildValue("(O)", value);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3077 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3078 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3079 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3080 res = PyObject_Call(func, args, NULL);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3081 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3082 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3083 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3084 if (res == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3085 return -1;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3086 return PyObject_IsTrue(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3087 }
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3088 else if (PyErr_Occurred())
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3089 return -1;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3090 else {
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19803
diff changeset
3091 return _PySequence_IterSearch(self, value,
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19803
diff changeset
3092 PY_ITERSEARCH_CONTAINS);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3093 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3094 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3095
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3096 SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3097 SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3098
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3099 #define slot_mp_length slot_sq_length
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3100
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3101 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3102
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3103 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3104 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3105 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3106 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3107 static PyObject *delitem_str, *setitem_str;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3108
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3109 if (value == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3110 res = call_method(self, "__delitem__", &delitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3111 "(O)", key);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3112 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3113 res = call_method(self, "__setitem__", &setitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3114 "(OO)", key, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3115 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3116 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3117 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3118 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3119 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3120
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3121 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3122 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3123 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3124 SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3125 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3126 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3127
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3128 staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3129
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3130 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3131 nb_power, "__pow__", "__rpow__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3132
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3133 static PyObject *
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3134 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3135 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3136 static PyObject *pow_str;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3137
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3138 if (modulus == Py_None)
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3139 return slot_nb_power_binary(self, other);
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3140 /* Three-arg power doesn't use __rpow__ */
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3141 return call_method(self, "__pow__", &pow_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3142 "(OO)", other, modulus);
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3143 }
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3144
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3145 SLOT0(slot_nb_negative, "__neg__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3146 SLOT0(slot_nb_positive, "__pos__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3147 SLOT0(slot_nb_absolute, "__abs__")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3148
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3149 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3150 slot_nb_nonzero(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3151 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3152 PyObject *func, *res;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3153 static PyObject *nonzero_str, *len_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3154
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3155 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3156 if (func == NULL) {
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3157 if (PyErr_Occurred())
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3158 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3159 func = lookup_maybe(self, "__len__", &len_str);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3160 if (func == NULL) {
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3161 if (PyErr_Occurred())
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3162 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3163 else
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3164 return 1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3165 }
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3166 }
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3167 res = PyObject_CallObject(func, NULL);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3168 Py_DECREF(func);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3169 if (res == NULL)
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3170 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3171 return PyObject_IsTrue(res);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3172 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3173
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3174 SLOT0(slot_nb_invert, "__invert__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3175 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3176 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3177 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3178 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3179 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3180
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3181 static int
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3182 slot_nb_coerce(PyObject **a, PyObject **b)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3183 {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3184 static PyObject *coerce_str;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3185 PyObject *self = *a, *other = *b;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3186
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3187 if (self->ob_type->tp_as_number != NULL &&
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3188 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3189 PyObject *r;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3190 r = call_maybe(
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3191 self, "__coerce__", &coerce_str, "(O)", other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3192 if (r == NULL)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3193 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3194 if (r == Py_NotImplemented) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3195 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3196 }
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3197 else {
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3198 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3199 PyErr_SetString(PyExc_TypeError,
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3200 "__coerce__ didn't return a 2-tuple");
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3201 Py_DECREF(r);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3202 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3203 }
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3204 *a = PyTuple_GET_ITEM(r, 0);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3205 Py_INCREF(*a);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3206 *b = PyTuple_GET_ITEM(r, 1);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3207 Py_INCREF(*b);
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3208 Py_DECREF(r);
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3209 return 0;
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3210 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3211 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3212 if (other->ob_type->tp_as_number != NULL &&
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3213 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3214 PyObject *r;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3215 r = call_maybe(
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3216 other, "__coerce__", &coerce_str, "(O)", self);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3217 if (r == NULL)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3218 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3219 if (r == Py_NotImplemented) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3220 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3221 return 1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3222 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3223 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3224 PyErr_SetString(PyExc_TypeError,
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3225 "__coerce__ didn't return a 2-tuple");
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3226 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3227 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3228 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3229 *a = PyTuple_GET_ITEM(r, 1);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3230 Py_INCREF(*a);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3231 *b = PyTuple_GET_ITEM(r, 0);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3232 Py_INCREF(*b);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3233 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3234 return 0;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3235 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3236 return 1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3237 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3238
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3239 SLOT0(slot_nb_int, "__int__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3240 SLOT0(slot_nb_long, "__long__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3241 SLOT0(slot_nb_float, "__float__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3242 SLOT0(slot_nb_oct, "__oct__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3243 SLOT0(slot_nb_hex, "__hex__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3244 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3245 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3246 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3247 SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3248 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3249 SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3250 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3251 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3252 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3253 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3254 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3255 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3256 "__floordiv__", "__rfloordiv__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3257 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3258 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3259 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3260
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3261 static int
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3262 half_compare(PyObject *self, PyObject *other)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3263 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3264 PyObject *func, *args, *res;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3265 static PyObject *cmp_str;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3266 int c;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3267
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3268 func = lookup_method(self, "__cmp__", &cmp_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3269 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3270 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3271 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3272 else {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3273 args = Py_BuildValue("(O)", other);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3274 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3275 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3276 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3277 res = PyObject_Call(func, args, NULL);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3278 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3279 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3280 if (res != Py_NotImplemented) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3281 if (res == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3282 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3283 c = PyInt_AsLong(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3284 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3285 if (c == -1 && PyErr_Occurred())
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3286 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3287 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3288 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3289 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3290 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3291 return 2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3292 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3293
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3294 /* This slot is published for the benefit of try_3way_compare in object.c */
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3295 int
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3296 _PyObject_SlotCompare(PyObject *self, PyObject *other)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3297 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3298 int c;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3299
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3300 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3301 c = half_compare(self, other);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3302 if (c <= 1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3303 return c;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3304 }
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3305 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3306 c = half_compare(other, self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3307 if (c < -1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3308 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3309 if (c <= 1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3310 return -c;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3311 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3312 return (void *)self < (void *)other ? -1 :
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3313 (void *)self > (void *)other ? 1 : 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3314 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3315
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3316 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3317 slot_tp_repr(PyObject *self)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3318 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3319 PyObject *func, *res;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3320 static PyObject *repr_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3321
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3322 func = lookup_method(self, "__repr__", &repr_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3323 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3324 res = PyEval_CallObject(func, NULL);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3325 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3326 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3327 }
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
3328 PyErr_Clear();
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
3329 return PyString_FromFormat("<%s object at %p>",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
3330 self->ob_type->tp_name, self);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3331 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3332
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3333 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3334 slot_tp_str(PyObject *self)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3335 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3336 PyObject *func, *res;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3337 static PyObject *str_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3338
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3339 func = lookup_method(self, "__str__", &str_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3340 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3341 res = PyEval_CallObject(func, NULL);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3342 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3343 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3344 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3345 else {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3346 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3347 return slot_tp_repr(self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3348 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3349 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3350
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3351 static long
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3352 slot_tp_hash(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3353 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3354 PyObject *func, *res;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3355 static PyObject *hash_str, *eq_str, *cmp_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3356
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3357 long h;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3358
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3359 func = lookup_method(self, "__hash__", &hash_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3360
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3361 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3362 res = PyEval_CallObject(func, NULL);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3363 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3364 if (res == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3365 return -1;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3366 h = PyInt_AsLong(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3367 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3368 else {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3369 PyErr_Clear();
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3370 func = lookup_method(self, "__eq__", &eq_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3371 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3372 PyErr_Clear();
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3373 func = lookup_method(self, "__cmp__", &cmp_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3374 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3375 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3376 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3377 PyErr_SetString(PyExc_TypeError, "unhashable type");
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3378 return -1;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3379 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3380 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3381 h = _Py_HashPointer((void *)self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3382 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3383 if (h == -1 && !PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3384 h = -2;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3385 return h;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3386 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3387
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3388 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3389 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3390 {
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3391 static PyObject *call_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3392 PyObject *meth = lookup_method(self, "__call__", &call_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3393 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3394
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3395 if (meth == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3396 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3397 res = PyObject_Call(meth, args, kwds);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3398 Py_DECREF(meth);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3399 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3400 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3401
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3402 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3403 slot_tp_getattro(PyObject *self, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3404 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3405 PyTypeObject *tp = self->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3406 PyObject *getattr;
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
3407 static PyObject *getattr_str = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3408
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
3409 if (getattr_str == NULL) {
20021
40cc6621eddc Change the name of the __getattr__ special method for new-style
Guido van Rossum <guido@python.org>
parents: 20015
diff changeset
3410 getattr_str = PyString_InternFromString("__getattribute__");
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
3411 if (getattr_str == NULL)
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
3412 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3413 }
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
3414 getattr = _PyType_Lookup(tp, getattr_str);
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
3415 if (getattr == NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
3416 /* Avoid further slowdowns */
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
3417 if (tp->tp_getattro == slot_tp_getattro)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
3418 tp->tp_getattro = PyObject_GenericGetAttr;
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
3419 return PyObject_GenericGetAttr(self, name);
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
3420 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3421 return PyObject_CallFunction(getattr, "OO", self, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3422 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3423
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3424 static PyObject *
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3425 slot_tp_getattr_hook(PyObject *self, PyObject *name)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3426 {
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3427 PyTypeObject *tp = self->ob_type;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3428 PyObject *getattr, *getattribute, *res;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3429 static PyObject *getattribute_str = NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3430 static PyObject *getattr_str = NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3431
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3432 if (getattr_str == NULL) {
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3433 getattr_str = PyString_InternFromString("__getattr__");
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3434 if (getattr_str == NULL)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3435 return NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3436 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3437 if (getattribute_str == NULL) {
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3438 getattribute_str =
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3439 PyString_InternFromString("__getattribute__");
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3440 if (getattribute_str == NULL)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3441 return NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3442 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3443 getattr = _PyType_Lookup(tp, getattr_str);
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3444 getattribute = _PyType_Lookup(tp, getattribute_str);
20231
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3445 if (getattribute != NULL &&
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3446 getattribute->ob_type == &PyWrapperDescr_Type &&
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3447 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3448 PyObject_GenericGetAttr)
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3449 getattribute = NULL;
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3450 if (getattr == NULL && getattribute == NULL) {
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3451 /* Avoid further slowdowns */
20231
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3452 /* XXX This is questionable: it means that a class that
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3453 isn't born with __getattr__ or __getattribute__ cannot
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3454 acquire them in later life. But it's a relatively big
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3455 speedup, so I'm keeping it in for now. If this is
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3456 removed, you can also remove the "def __getattr__" from
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3457 class C (marked with another XXX comment) in dynamics()
464f9a09a910 typeobject.c, slot_tp_gettattr_hook(): fix the speedup hack -- the
Guido van Rossum <guido@python.org>
parents: 20229
diff changeset
3458 in Lib/test/test_descr.py. */
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3459 if (tp->tp_getattro == slot_tp_getattr_hook)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3460 tp->tp_getattro = PyObject_GenericGetAttr;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3461 return PyObject_GenericGetAttr(self, name);
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3462 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3463 if (getattribute == NULL)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3464 res = PyObject_GenericGetAttr(self, name);
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3465 else
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3466 res = PyObject_CallFunction(getattribute, "OO", self, name);
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3467 if (getattr != NULL &&
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
3468 res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3469 PyErr_Clear();
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3470 res = PyObject_CallFunction(getattr, "OO", self, name);
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3471 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3472 return res;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3473 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3474
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3475 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3476 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3477 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3478 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3479 static PyObject *delattr_str, *setattr_str;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3480
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3481 if (value == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3482 res = call_method(self, "__delattr__", &delattr_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3483 "(O)", name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3484 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3485 res = call_method(self, "__setattr__", &setattr_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3486 "(OO)", name, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3487 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3488 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3489 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3490 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3491 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3492
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3493 /* Map rich comparison operators to their __xx__ namesakes */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3494 static char *name_op[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3495 "__lt__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3496 "__le__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3497 "__eq__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3498 "__ne__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3499 "__gt__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3500 "__ge__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3501 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3502
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3503 static PyObject *
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3504 half_richcompare(PyObject *self, PyObject *other, int op)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3505 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3506 PyObject *func, *args, *res;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3507 static PyObject *op_str[6];
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3508
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3509 func = lookup_method(self, name_op[op], &op_str[op]);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3510 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3511 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3512 Py_INCREF(Py_NotImplemented);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3513 return Py_NotImplemented;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3514 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3515 args = Py_BuildValue("(O)", other);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3516 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3517 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3518 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3519 res = PyObject_Call(func, args, NULL);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3520 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3521 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3522 Py_DECREF(func);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3523 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3524 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3525
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3526 /* Map rich comparison operators to their swapped version, e.g. LT --> GT */
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3527 static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3528
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3529 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3530 slot_tp_richcompare(PyObject *self, PyObject *other, int op)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3531 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3532 PyObject *res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3533
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3534 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3535 res = half_richcompare(self, other, op);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3536 if (res != Py_NotImplemented)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3537 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3538 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3539 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3540 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3541 res = half_richcompare(other, self, swapped_op[op]);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3542 if (res != Py_NotImplemented) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3543 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3544 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3545 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3546 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3547 Py_INCREF(Py_NotImplemented);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3548 return Py_NotImplemented;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3549 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3550
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3551 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3552 slot_tp_iter(PyObject *self)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3553 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3554 PyObject *func, *res;
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3555 static PyObject *iter_str, *getitem_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3556
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3557 func = lookup_method(self, "__iter__", &iter_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3558 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3559 res = PyObject_CallObject(func, NULL);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3560 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3561 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3562 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3563 PyErr_Clear();
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3564 func = lookup_method(self, "__getitem__", &getitem_str);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3565 if (func == NULL) {
20183
c6b6189a8397 SF bug [#466173] unpack TypeError unclear
Tim Peters <tim.peters@gmail.com>
parents: 20168
diff changeset
3566 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3567 return NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3568 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3569 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3570 return PySeqIter_New(self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3571 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3572
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3573 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3574 slot_tp_iternext(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3575 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3576 static PyObject *next_str;
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3577 return call_method(self, "next", &next_str, "()");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3578 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3579
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3580 static PyObject *
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3581 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3582 {
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3583 PyTypeObject *tp = self->ob_type;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3584 PyObject *get;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3585 static PyObject *get_str = NULL;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3586
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3587 if (get_str == NULL) {
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3588 get_str = PyString_InternFromString("__get__");
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3589 if (get_str == NULL)
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3590 return NULL;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3591 }
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3592 get = _PyType_Lookup(tp, get_str);
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3593 if (get == NULL) {
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3594 /* Avoid further slowdowns */
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3595 if (tp->tp_descr_get == slot_tp_descr_get)
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3596 tp->tp_descr_get = NULL;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3597 Py_INCREF(self);
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3598 return self;
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3599 }
19464
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3600 if (obj == NULL)
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3601 obj = Py_None;
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3602 if (type == NULL)
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3603 type = Py_None;
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3604 return PyObject_CallFunction(get, "OOO", self, obj, type);
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3605 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3606
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3607 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3608 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3609 {
19464
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3610 PyObject *res;
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3611 static PyObject *del_str, *set_str;
19464
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3612
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3613 if (value == NULL)
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3614 res = call_method(self, "__del__", &del_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3615 "(O)", target);
19464
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3616 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3617 res = call_method(self, "__set__", &set_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3618 "(OO)", target, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3619 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3620 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3621 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3622 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3623 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3624
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3625 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3626 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3627 {
19535
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3628 static PyObject *init_str;
9753608e2d57 Change in policy: when a slot_tp_xxx function looks for the __xxx__ method,
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
3629 PyObject *meth = lookup_method(self, "__init__", &init_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3630 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3631
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3632 if (meth == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3633 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3634 res = PyObject_Call(meth, args, kwds);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3635 Py_DECREF(meth);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3636 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3637 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3638 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3639 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3640 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3641
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3642 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3643 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3644 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3645 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3646 PyObject *newargs, *x;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3647 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3648
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3649 if (func == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3650 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3651 assert(PyTuple_Check(args));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3652 n = PyTuple_GET_SIZE(args);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3653 newargs = PyTuple_New(n+1);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3654 if (newargs == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3655 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3656 Py_INCREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3657 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3658 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3659 x = PyTuple_GET_ITEM(args, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3660 Py_INCREF(x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3661 PyTuple_SET_ITEM(newargs, i+1, x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3662 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3663 x = PyObject_Call(func, newargs, kwds);
20195
3408d929bb94 slot_tp_new(): newargs was leaking.
Guido van Rossum <guido@python.org>
parents: 20190
diff changeset
3664 Py_DECREF(newargs);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3665 Py_DECREF(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3666 return x;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3667 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3668
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3669
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3670 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3671 functions. The offsets here are relative to the 'etype' structure, which
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3672 incorporates the additional structures used for numbers, sequences and
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3673 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3674 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3675 slots (e.g. __str__ affects tp_str as well as tp_repr). */
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3676
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3677 typedef struct {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3678 char *name;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3679 int offset;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3680 void *function;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3681 wrapperfunc wrapper;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3682 } slotdef;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3683
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3684 #undef TPSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3685 #undef ETSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3686 #undef SQSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3687 #undef MPSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3688 #undef NBSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3689 #undef BINSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3690 #undef RBINSLOT
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3691
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3692 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER) \
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3693 {NAME, offsetof(PyTypeObject, SLOT), FUNCTION, WRAPPER}
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3694 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER) \
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3695 {NAME, offsetof(etype, SLOT), FUNCTION, WRAPPER}
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3696 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER) \
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3697 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3698 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER) \
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3699 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3700 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER) \
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3701 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3702 #define BINSLOT(NAME, SLOT, FUNCTION) \
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3703 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3704 #define RBINSLOT(NAME, SLOT, FUNCTION) \
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3705 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3706
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3707 static slotdef slotdefs[] = {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3708 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3709 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3710 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3711 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3712 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3713 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3714 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3715 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3716 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3717 wrap_intintobjargproc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3718 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3719 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3720 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3721 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3722 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3723 wrap_intargfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3724
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3725 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry),
20345
67a7a99f2d5a The slot definition table entry for mp_getitem had a bogus wrapper
Guido van Rossum <guido@python.org>
parents: 20343
diff changeset
3726 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
67a7a99f2d5a The slot definition table entry for mp_getitem had a bogus wrapper
Guido van Rossum <guido@python.org>
parents: 20343
diff changeset
3727 wrap_binaryfunc),
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3728 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3729 wrap_objobjargproc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3730 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3731 wrap_delitem),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3732
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3733 BINSLOT("__add__", nb_add, slot_nb_add),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3734 RBINSLOT("__radd__", nb_add, slot_nb_add),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3735 BINSLOT("__sub__", nb_subtract, slot_nb_subtract),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3736 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3737 BINSLOT("__mul__", nb_multiply, slot_nb_multiply),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3738 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3739 BINSLOT("__div__", nb_divide, slot_nb_divide),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3740 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3741 BINSLOT("__mod__", nb_remainder, slot_nb_remainder),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3742 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3743 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3744 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3745 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3746 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3747 NBSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3748 NBSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3749 NBSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3750 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3751 NBSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3752 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3753 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3754 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3755 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3756 BINSLOT("__and__", nb_and, slot_nb_and),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3757 RBINSLOT("__rand__", nb_and, slot_nb_and),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3758 BINSLOT("__xor__", nb_xor, slot_nb_xor),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3759 RBINSLOT("__rxor__", nb_xor, slot_nb_xor),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3760 BINSLOT("__or__", nb_or, slot_nb_or),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3761 RBINSLOT("__ror__", nb_or, slot_nb_or),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3762 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3763 NBSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3764 NBSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3765 NBSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3766 NBSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3767 NBSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3768 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3769 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3770 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3771 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3772 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3773 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3774 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3775 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3776 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3777 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3778 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3779 wrap_ternaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3780 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3781 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3782 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3783 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3784 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3785 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3786 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3787 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3788 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3789 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3790 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3791 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3792 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3793 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3794 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3795 slot_nb_inplace_floor_divide, wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3796 NBSLOT("__itruediv__", nb_inplace_true_divide,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3797 slot_nb_inplace_true_divide, wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3798
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3799 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3800 TPSLOT("__str__", tp_print, NULL, NULL),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3801 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3802 TPSLOT("__repr__", tp_print, NULL, NULL),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3803 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3804 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3805 TPSLOT("__call__", tp_call, slot_tp_call, wrap_call),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3806 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattro,
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3807 wrap_binaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3808 TPSLOT("__getattribute__", tp_getattr, NULL, NULL),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3809 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3810 TPSLOT("__getattr__", tp_getattr, NULL, NULL),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3811 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3812 TPSLOT("__setattr__", tp_setattr, NULL, NULL),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3813 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3814 TPSLOT("__delattr__", tp_setattr, NULL, NULL),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3815 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3816 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3817 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3818 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3819 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3820 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3821 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3822 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3823 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3824 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3825 TPSLOT("__init__", tp_init, slot_tp_init, wrap_init),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3826 TPSLOT("__new__", tp_new, slot_tp_new, NULL),
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3827 {NULL}
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3828 };
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3829
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3830 static int
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3831 update_slot(PyTypeObject *type, PyObject *name, PyObject *value)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3832 {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3833 char *s;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3834 int n;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3835 slotdef *p;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3836 void **ptr;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3837
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3838 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3839 if (value == NULL)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3840 return 0; /* Can't unset a slot */
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3841 s = PyString_AsString(name);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3842 n = PyString_Size(name);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3843 if (s == NULL || n < 0) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3844 /* Shouldn't happen, but can't be bothered */
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3845 PyErr_Clear();
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3846 return 0;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3847 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3848 if (!(s[0] == '_' && s[1] == '_' && s[n-1] == '_' && s[n-2] == '_'))
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3849 return 0;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3850 for (p = slotdefs; p->name; p++) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3851 if (!strcmp(p->name, s)) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3852 ptr = (void **) ((char *)type + p->offset);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3853 *ptr = p->function;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3854 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3855 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3856 return 0;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3857 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3858
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3859 static void **
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3860 slotptr(PyTypeObject *type, int offset)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3861 {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3862 char *ptr;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3863
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3864 assert(offset >= 0);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3865 assert(offset < offsetof(etype, as_buffer));
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3866 if (offset >= offsetof(etype, as_mapping)) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3867 ptr = (void *)type->tp_as_mapping;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3868 offset -= offsetof(etype, as_mapping);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3869 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3870 else if (offset >= offsetof(etype, as_sequence)) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3871 ptr = (void *)type->tp_as_sequence;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3872 offset -= offsetof(etype, as_sequence);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3873 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3874 else if (offset >= offsetof(etype, as_number)) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3875 ptr = (void *)type->tp_as_number;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3876 offset -= offsetof(etype, as_number);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3877 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3878 else {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3879 ptr = (void *)type;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3880 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3881 if (ptr != NULL)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3882 ptr += offset;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3883 return (void **)ptr;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3884 }
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
3885
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3886 static void
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3887 fixup_slot_dispatchers(PyTypeObject *type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3888 {
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3889 slotdef *p;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3890 PyObject *mro, *descr;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3891 PyTypeObject *base;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3892 PyWrapperDescrObject *d;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3893 int i, n;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3894 void **ptr;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3895
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3896 for (p = slotdefs; p->name; p++) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3897 ptr = slotptr(type, p->offset);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3898 if (ptr)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3899 *ptr = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3900 }
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3901 mro = type->tp_mro;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3902 assert(PyTuple_Check(mro));
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3903 n = PyTuple_GET_SIZE(mro);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3904 for (p = slotdefs; p->name; p++) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3905 for (i = 0; i < n; i++) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3906 base = (PyTypeObject *)PyTuple_GET_ITEM(mro, i);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3907 assert(PyType_Check(base));
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3908 descr = PyDict_GetItemString(
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3909 base->tp_defined, p->name);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3910 if (descr == NULL)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3911 continue;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3912 ptr = slotptr(type, p->offset);
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3913 if (ptr == NULL)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3914 continue;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3915 if (descr->ob_type == &PyWrapperDescr_Type) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3916 d = (PyWrapperDescrObject *)descr;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3917 if (d->d_base->wrapper == p->wrapper) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3918 if (*ptr == NULL) {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3919 *ptr = d->d_wrapped;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3920 continue;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3921 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3922 if (p->wrapper == wrap_binaryfunc_r)
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3923 continue;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3924 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3925 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3926 *ptr = p->function;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3927 break;
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3928 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3929 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3930 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3931
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3932
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3933 /* Cooperative 'super' */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3934
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3935 typedef struct {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3936 PyObject_HEAD
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
3937 PyTypeObject *type;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3938 PyObject *obj;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3939 } superobject;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3940
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
3941 static PyMemberDef super_members[] = {
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
3942 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
3943 "the class invoking super()"},
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
3944 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
3945 "the instance invoking super(); may be None"},
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3946 {0}
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3947 };
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3948
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3949 static void
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3950 super_dealloc(PyObject *self)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3951 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3952 superobject *su = (superobject *)self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3953
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
3954 _PyObject_GC_UNTRACK(self);
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3955 Py_XDECREF(su->obj);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3956 Py_XDECREF(su->type);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3957 self->ob_type->tp_free(self);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3958 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3959
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3960 static PyObject *
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3961 super_repr(PyObject *self)
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3962 {
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3963 superobject *su = (superobject *)self;
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3964
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3965 if (su->obj)
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3966 return PyString_FromFormat(
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
3967 "<super: <class '%s'>, <%s object>>",
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3968 su->type ? su->type->tp_name : "NULL",
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3969 su->obj->ob_type->tp_name);
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3970 else
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3971 return PyString_FromFormat(
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
3972 "<super: <class '%s'>, NULL>",
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3973 su->type ? su->type->tp_name : "NULL");
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3974 }
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3975
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3976 static PyObject *
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3977 super_getattro(PyObject *self, PyObject *name)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3978 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3979 superobject *su = (superobject *)self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3980
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3981 if (su->obj != NULL) {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3982 PyObject *mro, *res, *tmp;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3983 descrgetfunc f;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3984 int i, n;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3985
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
3986 mro = su->obj->ob_type->tp_mro;
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3987 if (mro == NULL)
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3988 n = 0;
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3989 else {
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3990 assert(PyTuple_Check(mro));
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3991 n = PyTuple_GET_SIZE(mro);
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3992 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3993 for (i = 0; i < n; i++) {
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
3994 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3995 break;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
3996 }
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
3997 if (i >= n && PyType_Check(su->obj)) {
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
3998 mro = ((PyTypeObject *)(su->obj))->tp_mro;
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
3999 if (mro == NULL)
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4000 n = 0;
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4001 else {
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4002 assert(PyTuple_Check(mro));
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4003 n = PyTuple_GET_SIZE(mro);
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4004 }
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4005 for (i = 0; i < n; i++) {
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4006 if ((PyObject *)(su->type) ==
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4007 PyTuple_GET_ITEM(mro, i))
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4008 break;
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4009 }
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4010 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4011 i++;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4012 res = NULL;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4013 for (; i < n; i++) {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4014 tmp = PyTuple_GET_ITEM(mro, i);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4015 assert(PyType_Check(tmp));
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4016 res = PyDict_GetItem(
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4017 ((PyTypeObject *)tmp)->tp_defined, name);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4018 if (res != NULL) {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4019 Py_INCREF(res);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4020 f = res->ob_type->tp_descr_get;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4021 if (f != NULL) {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4022 tmp = f(res, su->obj, res);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4023 Py_DECREF(res);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4024 res = tmp;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4025 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4026 return res;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4027 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4028 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4029 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4030 return PyObject_GenericGetAttr(self, name);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4031 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4032
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4033 static PyObject *
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4034 super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4035 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4036 superobject *su = (superobject *)self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4037 superobject *new;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4038
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4039 if (obj == NULL || obj == Py_None || su->obj != NULL) {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4040 /* Not binding to an object, or already bound */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4041 Py_INCREF(self);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4042 return self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4043 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4044 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4045 if (new == NULL)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4046 return NULL;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4047 Py_INCREF(su->type);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4048 Py_INCREF(obj);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4049 new->type = su->type;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4050 new->obj = obj;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4051 return (PyObject *)new;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4052 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4053
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4054 static int
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4055 super_init(PyObject *self, PyObject *args, PyObject *kwds)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4056 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4057 superobject *su = (superobject *)self;
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4058 PyTypeObject *type;
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4059 PyObject *obj = NULL;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4060
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4061 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4062 return -1;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4063 if (obj == Py_None)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4064 obj = NULL;
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4065 if (obj != NULL &&
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4066 !PyType_IsSubtype(obj->ob_type, type) &&
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4067 !(PyType_Check(obj) &&
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4068 PyType_IsSubtype((PyTypeObject *)obj, type))) {
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4069 PyErr_SetString(PyExc_TypeError,
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4070 "super(type, obj): "
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4071 "obj must be an instance or subtype of type");
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4072 return -1;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4073 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4074 Py_INCREF(type);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4075 Py_XINCREF(obj);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4076 su->type = type;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4077 su->obj = obj;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4078 return 0;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4079 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4080
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4081 static char super_doc[] =
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4082 "super(type) -> unbound super object\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4083 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
19551
01a6901cd305 Fix super() so that it is usable for static methods (like __new__) as well.
Guido van Rossum <guido@python.org>
parents: 19540
diff changeset
4084 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4085 "Typical use to call a cooperative superclass method:\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4086 "class C(B):\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4087 " def meth(self, arg):\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4088 " super(C, self).meth(arg)";
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4089
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4090 static int
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4091 super_traverse(PyObject *self, visitproc visit, void *arg)
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4092 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4093 superobject *su = (superobject *)self;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4094 int err;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4095
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4096 #define VISIT(SLOT) \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4097 if (SLOT) { \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4098 err = visit((PyObject *)(SLOT), arg); \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4099 if (err) \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4100 return err; \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4101 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4102
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4103 VISIT(su->obj);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4104 VISIT(su->type);
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4105
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4106 #undef VISIT
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4107
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4108 return 0;
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4109 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4110
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4111 PyTypeObject PySuper_Type = {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4112 PyObject_HEAD_INIT(&PyType_Type)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4113 0, /* ob_size */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4114 "super", /* tp_name */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4115 sizeof(superobject), /* tp_basicsize */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4116 0, /* tp_itemsize */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4117 /* methods */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4118 super_dealloc, /* tp_dealloc */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4119 0, /* tp_print */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4120 0, /* tp_getattr */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4121 0, /* tp_setattr */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4122 0, /* tp_compare */
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4123 super_repr, /* tp_repr */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4124 0, /* tp_as_number */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4125 0, /* tp_as_sequence */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4126 0, /* tp_as_mapping */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4127 0, /* tp_hash */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4128 0, /* tp_call */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4129 0, /* tp_str */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4130 super_getattro, /* tp_getattro */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4131 0, /* tp_setattro */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4132 0, /* tp_as_buffer */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4133 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4134 Py_TPFLAGS_BASETYPE, /* tp_flags */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4135 super_doc, /* tp_doc */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4136 super_traverse, /* tp_traverse */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4137 0, /* tp_clear */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4138 0, /* tp_richcompare */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4139 0, /* tp_weaklistoffset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4140 0, /* tp_iter */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4141 0, /* tp_iternext */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4142 0, /* tp_methods */
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4143 super_members, /* tp_members */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4144 0, /* tp_getset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4145 0, /* tp_base */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4146 0, /* tp_dict */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4147 super_descr_get, /* tp_descr_get */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4148 0, /* tp_descr_set */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4149 0, /* tp_dictoffset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4150 super_init, /* tp_init */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4151 PyType_GenericAlloc, /* tp_alloc */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4152 PyType_GenericNew, /* tp_new */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4153 _PyObject_GC_Del, /* tp_free */
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4154 };