annotate Objects/typeobject.c @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents e374ea7ac4ce
children eea40c417205
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
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
7 #include <ctype.h>
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
8
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
9 /* The *real* layout of a type object when allocated on the heap */
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
10 /* XXX Should we publish this in a header file? */
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
11 typedef struct {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
12 PyTypeObject type;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
13 PyNumberMethods as_number;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
14 PySequenceMethods as_sequence;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
15 PyMappingMethods as_mapping;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
16 PyBufferProcs as_buffer;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
17 PyObject *name, *slots;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
18 PyMemberDef members[1];
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
19 } etype;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
20
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
21 static PyMemberDef type_members[] = {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
22 {"__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
23 {"__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
24 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
25 {"__weakrefoffset__", T_LONG,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
26 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
27 {"__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
28 {"__dictoffset__", T_LONG,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
29 offsetof(PyTypeObject, tp_dictoffset), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
30 {"__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
31 {"__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
32 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
33 };
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
35 static PyObject *
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
36 type_name(PyTypeObject *type, void *context)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
37 {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
38 char *s;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
39
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
40 s = strrchr(type->tp_name, '.');
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
41 if (s == NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
42 s = type->tp_name;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
43 else
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
44 s++;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
45 return PyString_FromString(s);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
46 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
47
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
48 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
49 type_module(PyTypeObject *type, void *context)
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
50 {
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
51 PyObject *mod;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
52 char *s;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
53
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
54 s = strrchr(type->tp_name, '.');
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
55 if (s != NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
56 return PyString_FromStringAndSize(type->tp_name,
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
57 (int)(s - type->tp_name));
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
58 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
59 return PyString_FromString("__builtin__");
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
60 mod = PyDict_GetItemString(type->tp_dict, "__module__");
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
61 if (mod != NULL && PyString_Check(mod)) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
62 Py_INCREF(mod);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
63 return mod;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
64 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
65 PyErr_SetString(PyExc_AttributeError, "__module__");
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
66 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
67 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
68
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
69 static int
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
70 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
71 {
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
72 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
73 strrchr(type->tp_name, '.')) {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
74 PyErr_Format(PyExc_TypeError,
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
75 "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
76 return -1;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
77 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
78 if (!value) {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
79 PyErr_Format(PyExc_TypeError,
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
80 "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
81 return -1;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
82 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
83 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
84 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
85
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
86 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
87 type_dict(PyTypeObject *type, void *context)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
88 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
89 if (type->tp_dict == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
90 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
91 return Py_None;
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
92 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
93 return PyDictProxy_New(type->tp_dict);
2080
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
94 }
c21cc7b3ce28 added getattr(), supporting __doc__ and _name__
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
95
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
96 static PyObject *
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
97 type_get_doc(PyTypeObject *type, void *context)
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
98 {
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
99 PyObject *result;
23120
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
100 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
101 return PyString_FromString(type->tp_doc);
23120
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
102 result = PyDict_GetItemString(type->tp_dict, "__doc__");
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
103 if (result == NULL) {
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
104 result = Py_None;
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
105 Py_INCREF(result);
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
106 }
23120
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
107 else if (result->ob_type->tp_descr_get) {
23131
90adbcf62013 Darn. Of course the warning that Tim killed on the trunk must also be
Guido van Rossum <guido@python.org>
parents: 23120
diff changeset
108 result = result->ob_type->tp_descr_get(result, NULL,
90adbcf62013 Darn. Of course the warning that Tim killed on the trunk must also be
Guido van Rossum <guido@python.org>
parents: 23120
diff changeset
109 (PyObject *)type);
23120
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
110 }
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
111 else {
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
112 Py_INCREF(result);
737f4458e5de Backport rev 2.143 (note: some earlier bugfix candidates still TBD).
Guido van Rossum <guido@python.org>
parents: 22872
diff changeset
113 }
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
114 return result;
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
115 }
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
116
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
117 static PyGetSetDef type_getsets[] = {
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
118 {"__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
119 {"__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
120 {"__dict__", (getter)type_dict, NULL, NULL},
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
121 {"__doc__", (getter)type_get_doc, NULL, NULL},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
122 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
123 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
124
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
125 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
126 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
127 {
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 /* 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
129 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
130 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
131 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
132 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
133 }
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
134
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
135 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
136 type_repr(PyTypeObject *type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 {
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
138 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
139 char *kind;
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
140
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
141 mod = type_module(type, NULL);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
142 if (mod == NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
143 PyErr_Clear();
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
144 else if (!PyString_Check(mod)) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
145 Py_DECREF(mod);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
146 mod = NULL;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
147 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
148 name = type_name(type, NULL);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
149 if (name == NULL)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
150 return NULL;
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
151
20076
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
152 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
153 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
154 else
d9250d5b79a7 Change repr() of a new-style class to say <class 'ClassName'> rather
Guido van Rossum <guido@python.org>
parents: 20075
diff changeset
155 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
156
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
157 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
158 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
159 kind,
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
160 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
161 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
162 }
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
163 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
164 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
165
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
166 Py_XDECREF(mod);
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
167 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
168 return rtn;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
171 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
172 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
173 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
174 PyObject *obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
175
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
176 if (type->tp_new == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
177 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
178 "cannot create '%.100s' instances",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
179 type->tp_name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
180 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
181 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
182
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
183 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
184 if (obj != NULL) {
20516
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
185 /* Ugly exception: when the call was type(something),
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
186 don't call tp_init on the result. */
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
187 if (type == &PyType_Type &&
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
188 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
189 (kwds == NULL ||
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
190 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
d21160a92179 Fix SF bug #472234: type(obj) calls type->tp_init (Roeland Rengelink)
Guido van Rossum <guido@python.org>
parents: 20499
diff changeset
191 return obj;
25657
0690ea6d1153 Backport, at the reqest of Kevin Jacobs:
Guido van Rossum <guido@python.org>
parents: 24826
diff changeset
192 /* If the returned object is not an instance of type,
0690ea6d1153 Backport, at the reqest of Kevin Jacobs:
Guido van Rossum <guido@python.org>
parents: 24826
diff changeset
193 it won't be initialized. */
0690ea6d1153 Backport, at the reqest of Kevin Jacobs:
Guido van Rossum <guido@python.org>
parents: 24826
diff changeset
194 if (!PyType_IsSubtype(obj->ob_type, type))
0690ea6d1153 Backport, at the reqest of Kevin Jacobs:
Guido van Rossum <guido@python.org>
parents: 24826
diff changeset
195 return obj;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
196 type = obj->ob_type;
24251
7d0982056f72 The object returned by tp_new() may not have a tp_init.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24186
diff changeset
197 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
7d0982056f72 The object returned by tp_new() may not have a tp_init.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24186
diff changeset
198 type->tp_init != NULL &&
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
199 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
200 Py_DECREF(obj);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
201 obj = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
202 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
203 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
204 return obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
205 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
206
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
207 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
208 PyType_GenericAlloc(PyTypeObject *type, int nitems)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
209 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
210 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
211 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
212
b285decf0421 Repaired the debug Windows deaths in test_descr, by allocating enough
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
213 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
214 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
215 else
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
216 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
217
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
218 if (obj == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
219 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
220
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
221 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
222
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
223 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
224 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
225
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
226 if (type->tp_itemsize == 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
227 PyObject_INIT(obj, type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
228 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
229 (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
230
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
231 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
232 _PyObject_GC_TRACK(obj);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
233 return obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
234 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
235
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
236 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
237 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
238 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
239 return type->tp_alloc(type, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
240 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
241
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
242 /* 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
243
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
244 static int
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
245 traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
246 {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
247 int i, n;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
248 PyMemberDef *mp;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
249
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
250 n = type->ob_size;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
251 mp = ((etype *)type)->members;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
252 for (i = 0; i < n; i++, mp++) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
253 if (mp->type == T_OBJECT_EX) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
254 char *addr = (char *)self + mp->offset;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
255 PyObject *obj = *(PyObject **)addr;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
256 if (obj != NULL) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
257 int err = visit(obj, arg);
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
258 if (err)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
259 return err;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
260 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
261 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
262 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
263 return 0;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
264 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
265
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
266 static int
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
267 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
268 {
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
269 PyTypeObject *type, *base;
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
270 traverseproc basetraverse;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
271
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
272 /* Find the nearest base with a different tp_traverse,
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
273 and traverse slots while we're at it */
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
274 type = self->ob_type;
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
275 base = type;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
276 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
277 if (base->ob_size) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
278 int err = traverse_slots(base, self, visit, arg);
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
279 if (err)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
280 return err;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
281 }
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
282 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
283 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
284 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
285
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
286 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
287 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
288 if (dictptr && *dictptr) {
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
289 int err = visit(*dictptr, arg);
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
290 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
291 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
292 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
293 }
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
294
23758
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
295 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
296 /* For a heaptype, the instances count as references
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
297 to the type. Traverse the type so the collector
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
298 can find cycles involving this link. */
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
299 int err = visit((PyObject *)type, arg);
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
300 if (err)
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
301 return err;
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
302 }
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
303
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
304 if (basetraverse)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
305 return basetraverse(self, visit, arg);
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
306 return 0;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
307 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
308
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
309 static void
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
310 clear_slots(PyTypeObject *type, PyObject *self)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
311 {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
312 int i, n;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
313 PyMemberDef *mp;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
314
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
315 n = type->ob_size;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
316 mp = ((etype *)type)->members;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
317 for (i = 0; i < n; i++, mp++) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
318 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
319 char *addr = (char *)self + mp->offset;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
320 PyObject *obj = *(PyObject **)addr;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
321 if (obj != NULL) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
322 Py_DECREF(obj);
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
323 *(PyObject **)addr = NULL;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
324 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
325 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
326 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
327 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
328
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
329 static int
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
330 subtype_clear(PyObject *self)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
331 {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
332 PyTypeObject *type, *base;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
333 inquiry baseclear;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
334
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
335 /* Find the nearest base with a different tp_clear
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
336 and clear slots while we're at it */
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
337 type = self->ob_type;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
338 base = type;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
339 while ((baseclear = base->tp_clear) == subtype_clear) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
340 if (base->ob_size)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
341 clear_slots(base, self);
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
342 base = base->tp_base;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
343 assert(base);
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
344 }
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
345
23758
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
346 /* There's no need to clear the instance dict (if any);
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
347 the collector will call its tp_clear handler. */
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
348
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
349 if (baseclear)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
350 return baseclear(self);
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
351 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
352 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
353
20753
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
354 staticforward PyObject *lookup_maybe(PyObject *, char *, PyObject **);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
355
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
356 static int
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
357 call_finalizer(PyObject *self)
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
358 {
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
359 static PyObject *del_str = NULL;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
360 PyObject *del, *res;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
361 PyObject *error_type, *error_value, *error_traceback;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
362
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
363 /* Temporarily resurrect the object. */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
364 #ifdef Py_TRACE_REFS
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
365 #ifndef Py_REF_DEBUG
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
366 # error "Py_TRACE_REFS defined but Py_REF_DEBUG not."
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
367 #endif
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
368 /* much too complicated if Py_TRACE_REFS defined */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
369 _Py_NewReference((PyObject *)self);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
370 #ifdef COUNT_ALLOCS
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
371 /* compensate for boost in _Py_NewReference; note that
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
372 * _Py_RefTotal was also boosted; we'll knock that down later.
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
373 */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
374 self->ob_type->tp_allocs--;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
375 #endif
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
376 #else /* !Py_TRACE_REFS */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
377 /* Py_INCREF boosts _Py_RefTotal if Py_REF_DEBUG is defined */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
378 Py_INCREF(self);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
379 #endif /* !Py_TRACE_REFS */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
380
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
381 /* Save the current exception, if any. */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
382 PyErr_Fetch(&error_type, &error_value, &error_traceback);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
383
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
384 /* Execute __del__ method, if any. */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
385 del = lookup_maybe(self, "__del__", &del_str);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
386 if (del != NULL) {
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
387 res = PyEval_CallObject(del, NULL);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
388 if (res == NULL)
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
389 PyErr_WriteUnraisable(del);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
390 else
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
391 Py_DECREF(res);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
392 Py_DECREF(del);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
393 }
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
394
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
395 /* Restore the saved exception. */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
396 PyErr_Restore(error_type, error_value, error_traceback);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
397
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
398 /* Undo the temporary resurrection; can't use DECREF here, it would
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
399 * cause a recursive call.
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
400 */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
401 #ifdef Py_REF_DEBUG
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
402 /* _Py_RefTotal was boosted either by _Py_NewReference or
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
403 * Py_INCREF above.
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
404 */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
405 _Py_RefTotal--;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
406 #endif
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
407 if (--self->ob_refcnt > 0) {
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
408 #ifdef COUNT_ALLOCS
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
409 self->ob_type->tp_frees--;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
410 #endif
24186
bec5b6610622 Attempting to resurrect a dying instance of a new-style class in a
Tim Peters <tim.peters@gmail.com>
parents: 23968
diff changeset
411 /* This should still be a tracked gc'ed object. */
bec5b6610622 Attempting to resurrect a dying instance of a new-style class in a
Tim Peters <tim.peters@gmail.com>
parents: 23968
diff changeset
412 assert(((PyGC_Head *)(self)-1)->gc.gc_next != NULL);
20753
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
413 return -1; /* __del__ added a reference; don't delete now */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
414 }
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
415 #ifdef Py_TRACE_REFS
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
416 _Py_ForgetReference((PyObject *)self);
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
417 #ifdef COUNT_ALLOCS
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
418 /* compensate for increment in _Py_ForgetReference */
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
419 self->ob_type->tp_frees--;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
420 #endif
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
421 #endif
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
422
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
423 return 0;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
424 }
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
425
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
426 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
427 subtype_dealloc(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
428 {
21246
cb921d483e00 The previous checkin to clear __slots__ variables did a little bit of
Guido van Rossum <guido@python.org>
parents: 21242
diff changeset
429 PyTypeObject *type, *base;
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
430 destructor basedealloc;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
431
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
432 /* 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
433
20753
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
434 if (call_finalizer(self) < 0)
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
435 return;
ecd64931482c Add __del__ callbacks. They are too useful to leave out.
Guido van Rossum <guido@python.org>
parents: 20737
diff changeset
436
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
437 /* Find the nearest base with a different tp_dealloc
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
438 and clear slots while we're at it */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
439 type = self->ob_type;
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
440 base = type;
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
441 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
442 if (base->ob_size)
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
443 clear_slots(base, self);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
444 base = base->tp_base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
445 assert(base);
21246
cb921d483e00 The previous checkin to clear __slots__ variables did a little bit of
Guido van Rossum <guido@python.org>
parents: 21242
diff changeset
446 }
cb921d483e00 The previous checkin to clear __slots__ variables did a little bit of
Guido van Rossum <guido@python.org>
parents: 21242
diff changeset
447
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
448 /* 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
449 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
450 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
451 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
452 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
453 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
454 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
455 *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
456 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
457 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
458 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
459
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
460 /* 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
461 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
462 PyObject_ClearWeakRefs(self);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
463
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
464 /* 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
465 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
466 _PyObject_GC_UNTRACK(self);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
467
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
468 /* Call the base tp_dealloc() */
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
469 assert(basedealloc);
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
470 basedealloc(self);
18933
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 /* 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
473 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
474 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
475 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
476 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
477
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
478 staticforward PyTypeObject *solid_base(PyTypeObject *type);
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 /* type test with subclassing support */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
481
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
482 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
483 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
484 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
485 PyObject *mro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
486
19803
32cfce4d83dc PyType_IsSubtype(): test tp_flags for HAVE_CLASS bit before accessing
Guido van Rossum <guido@python.org>
parents: 19630
diff changeset
487 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
488 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
489
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
490 mro = a->tp_mro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
491 if (mro != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
492 /* Deal with multiple inheritance without recursion
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
493 by walking the MRO tuple */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
494 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
495 assert(PyTuple_Check(mro));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
496 n = PyTuple_GET_SIZE(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
497 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
498 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
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 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
502 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
503 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
504 /* 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
505 do {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
506 if (a == b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
507 return 1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
508 a = a->tp_base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
509 } while (a != NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
510 return b == &PyBaseObject_Type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
511 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
512 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
513
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
514 /* 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
515 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
516 (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
517 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
518 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
519 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
520
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
521 Two variants:
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
522
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
523 - 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
524 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
525
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
526 - 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
527 */
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
528
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
529 static PyObject *
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
530 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
531 {
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
532 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
533
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
534 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
535 *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
536 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
537 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
538 }
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
539 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
540 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
541 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
542 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
543 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
544 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
545 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
546 }
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
547 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
548 }
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
549
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
550 static PyObject *
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
551 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
552 {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
553 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
554 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
555 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
556 return res;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
557 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
558
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
559 /* 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
560 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
561 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
562
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
563 static PyObject *
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
564 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
565 {
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
566 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
567 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
568 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
569
20225
aa18c0ecc4fb call_method(), call_maybe(): fix a performance bug: the argument
Guido van Rossum <guido@python.org>
parents: 20220
diff changeset
570 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
571 if (func == NULL) {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
572 va_end(va);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
573 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
574 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
575 return NULL;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
576 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
577
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
578 if (format && *format)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
579 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
580 else
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
581 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
582
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
583 va_end(va);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
584
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
585 if (args == NULL)
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
586 return NULL;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
587
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
588 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
589 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
590
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
591 Py_DECREF(args);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
592 Py_DECREF(func);
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
593
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
594 return retval;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
595 }
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
596
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
597 /* 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
598
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
599 static PyObject *
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
600 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
601 {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
602 va_list va;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
603 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
604 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
605
20225
aa18c0ecc4fb call_method(), call_maybe(): fix a performance bug: the argument
Guido van Rossum <guido@python.org>
parents: 20220
diff changeset
606 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
607 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
608 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
609 if (!PyErr_Occurred()) {
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
610 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
611 return Py_NotImplemented;
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
612 }
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
613 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
614 }
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
615
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
616 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
617 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
618 else
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
619 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
620
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
621 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
622
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
623 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
624 return NULL;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
625
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
626 assert(PyTuple_Check(args));
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
627 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
628
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
629 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
630 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
631
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
632 return retval;
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
633 }
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
634
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
635 /* 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
636 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
637
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
638 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
639 conservative_merge(PyObject *left, PyObject *right)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
640 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
641 int left_size;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
642 int right_size;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
643 int i, j, r, ok;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
644 PyObject *temp, *rr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
645
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
646 assert(PyList_Check(left));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
647 assert(PyList_Check(right));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
648
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
649 again:
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
650 left_size = PyList_GET_SIZE(left);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
651 right_size = PyList_GET_SIZE(right);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
652 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
653 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
654 if (PyList_GET_ITEM(left, i) ==
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
655 PyList_GET_ITEM(right, j)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
656 /* found a merge point */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
657 temp = PyList_New(0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
658 if (temp == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
659 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
660 for (r = 0; r < j; r++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
661 rr = PyList_GET_ITEM(right, r);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
662 ok = PySequence_Contains(left, rr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
663 if (ok < 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
664 Py_DECREF(temp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
665 return -1;
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 (!ok) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
668 ok = PyList_Append(temp, rr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
669 if (ok < 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
670 Py_DECREF(temp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
671 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
672 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
673 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
674 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
675 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
676 Py_DECREF(temp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
677 if (ok < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
678 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
679 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
680 if (ok < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
681 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
682 goto again;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
683 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
684 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
685 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
686 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
687 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
688
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
689 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
690 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
691 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
692 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
693 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
694
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
695 static int
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
696 fill_classic_mro(PyObject *mro, PyObject *cls)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
697 {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
698 PyObject *bases, *base;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
699 int i, n;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
700
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
701 assert(PyList_Check(mro));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
702 assert(PyClass_Check(cls));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
703 i = PySequence_Contains(mro, cls);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
704 if (i < 0)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
705 return -1;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
706 if (!i) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
707 if (PyList_Append(mro, cls) < 0)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
708 return -1;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
709 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
710 bases = ((PyClassObject *)cls)->cl_bases;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
711 assert(bases && PyTuple_Check(bases));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
712 n = PyTuple_GET_SIZE(bases);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
713 for (i = 0; i < n; i++) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
714 base = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
715 if (fill_classic_mro(mro, base) < 0)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
716 return -1;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
717 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
718 return 0;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
719 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
720
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
721 static PyObject *
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
722 classic_mro(PyObject *cls)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
723 {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
724 PyObject *mro;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
725
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
726 assert(PyClass_Check(cls));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
727 mro = PyList_New(0);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
728 if (mro != NULL) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
729 if (fill_classic_mro(mro, cls) == 0)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
730 return mro;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
731 Py_DECREF(mro);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
732 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
733 return NULL;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
734 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
735
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
736 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
737 mro_implementation(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
738 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
739 int i, n, ok;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
740 PyObject *bases, *result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
741
23904
4f0606ceeef8 Backport:
Guido van Rossum <guido@python.org>
parents: 23857
diff changeset
742 if(type->tp_dict == NULL) {
4f0606ceeef8 Backport:
Guido van Rossum <guido@python.org>
parents: 23857
diff changeset
743 if(PyType_Ready(type) < 0)
4f0606ceeef8 Backport:
Guido van Rossum <guido@python.org>
parents: 23857
diff changeset
744 return NULL;
4f0606ceeef8 Backport:
Guido van Rossum <guido@python.org>
parents: 23857
diff changeset
745 }
4f0606ceeef8 Backport:
Guido van Rossum <guido@python.org>
parents: 23857
diff changeset
746
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
747 bases = type->tp_bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
748 n = PyTuple_GET_SIZE(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
749 result = Py_BuildValue("[O]", (PyObject *)type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
750 if (result == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
751 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
752 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
753 PyObject *base = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
754 PyObject *parentMRO;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
755 if (PyType_Check(base))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
756 parentMRO = PySequence_List(
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
757 ((PyTypeObject*)base)->tp_mro);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
758 else
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
759 parentMRO = classic_mro(base);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
760 if (parentMRO == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
761 Py_DECREF(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
762 return NULL;
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 if (serious_order_disagreements(result, parentMRO)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
765 Py_DECREF(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
766 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
767 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
768 ok = conservative_merge(result, parentMRO);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
769 Py_DECREF(parentMRO);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
770 if (ok < 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
771 Py_DECREF(result);
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 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
774 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
775 return result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
776 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
777
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
778 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
779 mro_external(PyObject *self)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
780 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
781 PyTypeObject *type = (PyTypeObject *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
782
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
783 return mro_implementation(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
784 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
785
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
786 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
787 mro_internal(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
788 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
789 PyObject *mro, *result, *tuple;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
790
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
791 if (type->ob_type == &PyType_Type) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
792 result = mro_implementation(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
793 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
794 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
795 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
796 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
797 if (mro == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
798 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
799 result = PyObject_CallObject(mro, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
800 Py_DECREF(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
801 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
802 if (result == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
803 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
804 tuple = PySequence_Tuple(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
805 Py_DECREF(result);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
806 type->tp_mro = tuple;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
807 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
808 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
809
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 /* 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
812 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
813
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
814 static PyTypeObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
815 best_base(PyObject *bases)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
816 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
817 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
818 PyTypeObject *base, *winner, *candidate, *base_i;
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
819 PyObject *base_proto;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
820
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
821 assert(PyTuple_Check(bases));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
822 n = PyTuple_GET_SIZE(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
823 assert(n > 0);
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
824 base = NULL;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
825 winner = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
826 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
827 base_proto = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
828 if (PyClass_Check(base_proto))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
829 continue;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
830 if (!PyType_Check(base_proto)) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
831 PyErr_SetString(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
832 PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
833 "bases must be types");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
834 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
835 }
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
836 base_i = (PyTypeObject *)base_proto;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
837 if (base_i->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
838 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
839 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
840 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
841 candidate = solid_base(base_i);
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
842 if (winner == NULL) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
843 winner = candidate;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
844 base = base_i;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
845 }
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
846 else if (PyType_IsSubtype(winner, candidate))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
847 ;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
848 else if (PyType_IsSubtype(candidate, winner)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
849 winner = candidate;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
850 base = base_i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
851 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
852 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
853 PyErr_SetString(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
854 PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
855 "multiple bases have "
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
856 "instance lay-out conflict");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
857 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
858 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
859 }
21404
064311149cb4 (Merge into trunk.)
Guido van Rossum <guido@python.org>
parents: 21246
diff changeset
860 if (base == NULL)
064311149cb4 (Merge into trunk.)
Guido van Rossum <guido@python.org>
parents: 21246
diff changeset
861 PyErr_SetString(PyExc_TypeError,
064311149cb4 (Merge into trunk.)
Guido van Rossum <guido@python.org>
parents: 21246
diff changeset
862 "a new-style class can't have only classic bases");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
863 return base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
864 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
865
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
866 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
867 extra_ivars(PyTypeObject *type, PyTypeObject *base)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
868 {
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
869 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
870 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
871
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
872 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
873 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
874 /* If itemsize is involved, stricter rules */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
875 return t_size != b_size ||
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
876 type->tp_itemsize != base->tp_itemsize;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
877 }
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
878 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
879 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
880 t_size -= sizeof(PyObject *);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
881 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
882 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
883 t_size -= sizeof(PyObject *);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
884
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
885 return t_size != b_size;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
886 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
887
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
888 static PyTypeObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
889 solid_base(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
890 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
891 PyTypeObject *base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
892
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
893 if (type->tp_base)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
894 base = solid_base(type->tp_base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
895 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
896 base = &PyBaseObject_Type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
897 if (extra_ivars(type, base))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
898 return type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
899 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
900 return base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
901 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
902
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
903 staticforward void object_dealloc(PyObject *);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
904 staticforward int object_init(PyObject *, PyObject *, PyObject *);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
905 staticforward int update_slot(PyTypeObject *, 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
906 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
907
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
908 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
909 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
910 {
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
911 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
912 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
913
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
914 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
915 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
916 "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
917 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
918 }
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
919 dict = *dictptr;
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
920 if (dict == NULL)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
921 *dictptr = dict = PyDict_New();
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
922 Py_XINCREF(dict);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
923 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
924 }
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
925
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
926 static int
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
927 subtype_setdict(PyObject *obj, PyObject *value, void *context)
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
928 {
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
929 PyObject **dictptr = _PyObject_GetDictPtr(obj);
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
930 PyObject *dict;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
931
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
932 if (dictptr == NULL) {
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
933 PyErr_SetString(PyExc_AttributeError,
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
934 "This object has no __dict__");
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
935 return -1;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
936 }
21235
b782490da7f0 At the PythonLabs meeting someone mentioned it would make Jim really
Guido van Rossum <guido@python.org>
parents: 21192
diff changeset
937 if (value != NULL && !PyDict_Check(value)) {
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
938 PyErr_SetString(PyExc_TypeError,
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
939 "__dict__ must be set to a dictionary");
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
940 return -1;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
941 }
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
942 dict = *dictptr;
21235
b782490da7f0 At the PythonLabs meeting someone mentioned it would make Jim really
Guido van Rossum <guido@python.org>
parents: 21192
diff changeset
943 Py_XINCREF(value);
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
944 *dictptr = value;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
945 Py_XDECREF(dict);
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
946 return 0;
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
947 }
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
948
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20586
diff changeset
949 static PyGetSetDef subtype_getsets[] = {
20681
a0f1c2d2ac9e Allow assignment to newinstance.__dict__.
Guido van Rossum <guido@python.org>
parents: 20599
diff changeset
950 {"__dict__", subtype_dict, subtype_setdict, NULL},
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
951 {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
952 };
337b53679c73 Pytype_GenericAlloc(): round up size so we zap all four bytes of the
Guido van Rossum <guido@python.org>
parents: 19595
diff changeset
953
22331
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
954 /* bozo: __getstate__ that raises TypeError */
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
955
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
956 static PyObject *
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
957 bozo_func(PyObject *self, PyObject *args)
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
958 {
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
959 PyErr_SetString(PyExc_TypeError,
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
960 "a class that defines __slots__ without "
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
961 "defining __getstate__ cannot be pickled");
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
962 return NULL;
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
963 }
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
964
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
965 static PyMethodDef bozo_ml = {"__getstate__", bozo_func};
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
966
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
967 static PyObject *bozo_obj = NULL;
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
968
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
969 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
970 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
971 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
972 PyObject *name, *bases, *dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
973 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
974 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
975 PyTypeObject *type, *base, *tmptype, *winner;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
976 etype *et;
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
977 PyMemberDef *mp;
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
978 int i, nbases, nslots, slotoffset, add_dict, add_weak;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
979
20725
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
980 assert(args != NULL && PyTuple_Check(args));
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
981 assert(kwds == NULL || PyDict_Check(kwds));
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
982
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
983 /* Special case: type(x) should return x->ob_type */
20725
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
984 {
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
985 const int nargs = PyTuple_GET_SIZE(args);
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
986 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
987
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
988 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
989 PyObject *x = PyTuple_GET_ITEM(args, 0);
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
990 Py_INCREF(x->ob_type);
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
991 return (PyObject *) x->ob_type;
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
992 }
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
993
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
994 /* SF bug 475327 -- if that didn't trigger, we need 3
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
995 arguments. but PyArg_ParseTupleAndKeywords below may give
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
996 a msg saying type() needs exactly 3. */
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
997 if (nargs + nkwds != 3) {
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
998 PyErr_SetString(PyExc_TypeError,
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
999 "type() takes 1 or 3 arguments");
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
1000 return NULL;
7130750a6222 SF bug #475327: type() produces incorrect error msg
Tim Peters <tim.peters@gmail.com>
parents: 20681
diff changeset
1001 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1002 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1003
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
1004 /* Check arguments: (name, bases, dict) */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1005 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
1006 &name,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1007 &PyTuple_Type, &bases,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1008 &PyDict_Type, &dict))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1009 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1010
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1011 /* 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
1012 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
1013 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
1014 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
1015 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
1016 winner = metatype;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1017 for (i = 0; i < nbases; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1018 tmp = PyTuple_GET_ITEM(bases, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1019 tmptype = tmp->ob_type;
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1020 if (tmptype == &PyClass_Type)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1021 continue; /* Special case classic classes */
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
1022 if (PyType_IsSubtype(winner, tmptype))
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1023 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
1024 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
1025 winner = tmptype;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1026 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1027 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1028 PyErr_SetString(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1029 "metatype conflict among bases");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1030 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1031 }
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
1032 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
1033 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
1034 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
1035 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
1036 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1037
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1038 /* Adjust for empty tuple bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1039 if (nbases == 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1040 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1041 if (bases == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1042 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1043 nbases = 1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1044 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1045 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1046 Py_INCREF(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1047
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1048 /* 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
1049
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1050 /* 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
1051 base = best_base(bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1052 if (base == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1053 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1054 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
1055 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1056 "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
1057 base->tp_name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1058 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1059 }
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 /* 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
1062 slots = PyDict_GetItemString(dict, "__slots__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1063 nslots = 0;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1064 add_dict = 0;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1065 add_weak = 0;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1066 if (slots != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1067 /* Make it into a tuple */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1068 if (PyString_Check(slots))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1069 slots = Py_BuildValue("(O)", slots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1070 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1071 slots = PySequence_Tuple(slots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1072 if (slots == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1073 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1074 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
1075 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
1076 PyErr_Format(PyExc_TypeError,
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
1077 "nonempty __slots__ "
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
1078 "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
1079 base->tp_name);
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
1080 return NULL;
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
1081 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1082 for (i = 0; i < nslots; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1083 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
1084 PyErr_SetString(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1085 "__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
1086 Py_DECREF(slots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1087 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1088 }
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1089 /* 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
1090 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1091 }
22331
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1092 if (slots != NULL) {
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1093 /* See if *this* class defines __getstate__ */
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1094 PyObject *getstate = PyDict_GetItemString(dict,
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1095 "__getstate__");
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1096 if (getstate == NULL) {
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1097 /* If not, provide a bozo that raises TypeError */
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1098 if (bozo_obj == NULL) {
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1099 bozo_obj = PyCFunction_New(&bozo_ml, NULL);
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1100 if (bozo_obj == NULL) {
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1101 /* XXX decref various things */
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1102 return NULL;
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1103 }
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1104 }
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1105 if (PyDict_SetItemString(dict,
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1106 "__getstate__",
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1107 bozo_obj) < 0) {
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1108 /* XXX decref various things */
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1109 return NULL;
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1110 }
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1111 }
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1112 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1113 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
1114 (base->tp_setattro == PyObject_GenericSetAttr ||
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1115 base->tp_setattro == NULL)) {
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1116 add_dict++;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1117 }
19595
db36e396f043 Safety measures now that str and tuple are subclassable:
Guido van Rossum <guido@python.org>
parents: 19594
diff changeset
1118 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
1119 base->tp_itemsize == 0) {
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1120 nslots++;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1121 add_weak++;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1122 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1123
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1124 /* 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
1125 "return NULL" may leak slots! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1126
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1127 /* Allocate the type object */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1128 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
1129 if (type == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1130 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1131
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1132 /* 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
1133 et = (etype *)type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1134 Py_INCREF(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1135 et->name = name;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1136 et->slots = slots;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1137
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1138 /* Initialize tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1139 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
1140 Py_TPFLAGS_BASETYPE;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1141 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
1142 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
1143
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1144 /* 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
1145 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
1146 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
1147 (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
1148 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
1149
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
1150 /* Initialize essential fields */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1151 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
1152 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
1153 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
1154 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
1155 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
1156
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1157 /* Set tp_base and tp_bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1158 type->tp_bases = bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1159 Py_INCREF(base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1160 type->tp_base = base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1161
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1162 /* Initialize tp_dict from passed-in dict */
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1163 type->tp_dict = dict = PyDict_Copy(dict);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1164 if (dict == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1165 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1166 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1167 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1168
19300
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1169 /* Set __module__ in the dict */
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1170 if (PyDict_GetItemString(dict, "__module__") == NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1171 tmp = PyEval_GetGlobals();
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1172 if (tmp != NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1173 tmp = PyDict_GetItemString(tmp, "__name__");
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1174 if (tmp != NULL) {
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1175 if (PyDict_SetItemString(dict, "__module__",
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1176 tmp) < 0)
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1177 return NULL;
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1178 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1179 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1180 }
81440dd326bb Fix SF bug #442501: calculate __module__ properly.
Guido van Rossum <guido@python.org>
parents: 19297
diff changeset
1181
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1182 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
22348
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
1183 and is a string. The __doc__ accessor will first look for tp_doc;
51aa9eb24d81 SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents: 22331
diff changeset
1184 if that fails, it will still look into __dict__.
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1185 */
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1186 {
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1187 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1188 if (doc != NULL && PyString_Check(doc)) {
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1189 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
1190 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
1191 if (type->tp_doc == NULL) {
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1192 Py_DECREF(type);
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1193 return NULL;
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1194 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1195 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
1196 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1197 }
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1198
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1199 /* 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
1200 make it a static function */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1201 tmp = PyDict_GetItemString(dict, "__new__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1202 if (tmp != NULL && PyFunction_Check(tmp)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1203 tmp = PyStaticMethod_New(tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1204 if (tmp == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1205 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1206 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1207 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1208 PyDict_SetItemString(dict, "__new__", tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1209 Py_DECREF(tmp);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1210 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1211
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1212 /* 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
1213 mp = et->members;
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1214 slotoffset = base->tp_basicsize;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1215 if (slots != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1216 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
1217 mp->name = PyString_AS_STRING(
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1218 PyTuple_GET_ITEM(slots, i));
21192
c18ad52dece3 Fix SF bug #486144: Uninitialized __slot__ vrbl is None.
Guido van Rossum <guido@python.org>
parents: 21151
diff changeset
1219 mp->type = T_OBJECT_EX;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1220 mp->offset = slotoffset;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1221 if (base->tp_weaklistoffset == 0 &&
21192
c18ad52dece3 Fix SF bug #486144: Uninitialized __slot__ vrbl is None.
Guido van Rossum <guido@python.org>
parents: 21151
diff changeset
1222 strcmp(mp->name, "__weakref__") == 0) {
c18ad52dece3 Fix SF bug #486144: Uninitialized __slot__ vrbl is None.
Guido van Rossum <guido@python.org>
parents: 21151
diff changeset
1223 mp->type = T_OBJECT;
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
1224 mp->flags = READONLY;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1225 type->tp_weaklistoffset = slotoffset;
21192
c18ad52dece3 Fix SF bug #486144: Uninitialized __slot__ vrbl is None.
Guido van Rossum <guido@python.org>
parents: 21151
diff changeset
1226 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1227 slotoffset += sizeof(PyObject *);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1228 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1229 }
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1230 else {
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1231 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
1232 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
1233 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
1234 -(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
1235 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
1236 type->tp_dictoffset = slotoffset;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1237 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
1238 type->tp_getset = subtype_getsets;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1239 }
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1240 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
1241 assert(!base->tp_itemsize);
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1242 type->tp_weaklistoffset = slotoffset;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1243 mp->name = "__weakref__";
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1244 mp->type = T_OBJECT;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1245 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
1246 mp->flags = READONLY;
19349
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1247 mp++;
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1248 slotoffset += sizeof(PyObject *);
9b5a2c1a02f3 Weak reference support, closing SF bug #451773.
Guido van Rossum <guido@python.org>
parents: 19339
diff changeset
1249 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1250 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1251 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
1252 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
1253 type->tp_members = et->members;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1254
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1255 /* Special case some slots */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1256 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
1257 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
1258 type->tp_getattro = PyObject_GenericGetAttr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1259 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
1260 type->tp_setattro = PyObject_GenericSetAttr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1261 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1262 type->tp_dealloc = subtype_dealloc;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1263
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20259
diff changeset
1264 /* 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
1265 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
1266 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
1267 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
1268
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1269 /* 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
1270 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
1271 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
1272 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
1273 type->tp_traverse = subtype_traverse;
23687
c4d2d305fd98 Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23650
diff changeset
1274 type->tp_clear = subtype_clear;
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1275 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1276 else
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1277 type->tp_free = _PyObject_Del;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1278
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1279 /* Initialize the rest */
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
1280 if (PyType_Ready(type) < 0) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1281 Py_DECREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1282 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1283 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1284
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1285 /* 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
1286 fixup_slot_dispatchers(type);
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
1287
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1288 return (PyObject *)type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1289 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1290
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1291 /* 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
1292 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
1293 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1294 _PyType_Lookup(PyTypeObject *type, PyObject *name)
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 int i, n;
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1297 PyObject *mro, *res, *base, *dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1298
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1299 /* Look in tp_dict of types in MRO */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1300 mro = type->tp_mro;
23754
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
1301
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
1302 /* If mro is NULL, the type is either not yet initialized
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
1303 by PyType_Ready(), or already cleared by type_clear().
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
1304 Either way the safest thing to do is to return NULL. */
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
1305 if (mro == NULL)
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
1306 return NULL;
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
1307
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1308 assert(PyTuple_Check(mro));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1309 n = PyTuple_GET_SIZE(mro);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1310 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1311 base = PyTuple_GET_ITEM(mro, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1312 if (PyClass_Check(base))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1313 dict = ((PyClassObject *)base)->cl_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1314 else {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1315 assert(PyType_Check(base));
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1316 dict = ((PyTypeObject *)base)->tp_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
1317 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1318 assert(dict && PyDict_Check(dict));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1319 res = PyDict_GetItem(dict, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1320 if (res != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1321 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1322 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1323 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1324 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1325
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1326 /* This is similar to PyObject_GenericGetAttr(),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1327 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
1328 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1329 type_getattro(PyTypeObject *type, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1330 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1331 PyTypeObject *metatype = type->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1332 PyObject *descr, *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1333 descrgetfunc f;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1334
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1335 /* 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
1336 if (type->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
1337 if (PyType_Ready(type) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1338 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1339 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1340
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1341 /* Get a descriptor from the metatype */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1342 descr = _PyType_Lookup(metatype, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1343 f = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1344 if (descr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1345 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
1346 if (f != NULL && PyDescr_IsData(descr))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1347 return f(descr,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1348 (PyObject *)type, (PyObject *)metatype);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1349 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1350
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1351 /* Look in tp_dict of this type and its bases */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1352 res = _PyType_Lookup(type, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1353 if (res != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1354 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
1355 if (f != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1356 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
1357 Py_INCREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1358 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1359 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1360
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1361 /* Use the descriptor from the metatype */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1362 if (f != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1363 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
1364 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1365 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1366 if (descr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1367 Py_INCREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1368 return descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1369 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1370
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1371 /* Give up */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1372 PyErr_Format(PyExc_AttributeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1373 "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
1374 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
1375 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1376 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1377
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1378 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1379 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
1380 {
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1381 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1382 PyErr_Format(
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1383 PyExc_TypeError,
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1384 "can't set attributes of built-in/extension type '%s'",
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1385 type->tp_name);
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1386 return -1;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
1387 }
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1388 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1389 return -1;
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
1390 return update_slot(type, name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1391 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1392
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1393 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1394 type_dealloc(PyTypeObject *type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1395 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1396 etype *et;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1397
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1398 /* 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
1399 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
1400 _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
1401 PyObject_ClearWeakRefs((PyObject *)type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1402 et = (etype *)type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1403 Py_XDECREF(type->tp_base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1404 Py_XDECREF(type->tp_dict);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1405 Py_XDECREF(type->tp_bases);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1406 Py_XDECREF(type->tp_mro);
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1407 Py_XDECREF(type->tp_cache);
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1408 Py_XDECREF(type->tp_subclasses);
24428
15ee445af07f SF patch #587889, fix memory leak of tp_doc
Neal Norwitz <nnorwitz@gmail.com>
parents: 24251
diff changeset
1409 PyObject_Free(type->tp_doc);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1410 Py_XDECREF(et->name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1411 Py_XDECREF(et->slots);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1412 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
1413 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1414
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1415 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
1416 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
1417 {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1418 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
1419 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
1420
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1421 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
1422 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
1423 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
1424 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
1425 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
1426 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
1427 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
1428 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
1429 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
1430 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
1431 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
1432 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
1433 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
1434 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
1435 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
1436 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
1437 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1438 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1439 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1440 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
1441 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
1442
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1443 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
1444 {"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
1445 "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
1446 {"__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
1447 "__subclasses__() -> list of immediate subclasses"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1448 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1449 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1450
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1451 static char type_doc[] =
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1452 "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
1453 "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
1454
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1455 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
1456 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
1457 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1458 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
1459
23758
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1460 /* Because of type_is_gc(), the collector only calls this
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1461 for heaptypes. */
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1462 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
1463
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1464 #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
1465 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
1466 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
1467 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
1468 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
1469 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1470
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1471 VISIT(type->tp_dict);
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1472 VISIT(type->tp_cache);
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1473 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
1474 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
1475 VISIT(type->tp_base);
23758
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1476
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1477 /* There's no need to visit type->tp_subclasses or
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1478 ((etype *)type)->slots, because they can't be involved
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1479 in cycles; tp_subclasses is a list of weak references,
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1480 and slots is a tuple of strings. */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1481
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1482 #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
1483
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1484 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
1485 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1486
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1487 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
1488 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
1489 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1490 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
1491
23758
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1492 /* Because of type_is_gc(), the collector only calls this
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1493 for heaptypes. */
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1494 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
1495
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1496 #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
1497 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
1498 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
1499 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
1500 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
1501 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1502
23758
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1503 /* The only field we need to clear is tp_mro, which is part of a
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1504 hard cycle (its first element is the class itself) that won't
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1505 be broken otherwise (it's a tuple and tuples don't have a
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1506 tp_clear handler). None of the other fields need to be
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1507 cleared, and here's why:
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1508
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1509 tp_dict:
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1510 It is a dict, so the collector will call its tp_clear.
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1511
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1512 tp_cache:
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1513 Not used; if it were, it would be a dict.
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1514
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1515 tp_bases, tp_base:
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1516 If these are involved in a cycle, there must be at least
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1517 one other, mutable object in the cycle, e.g. a base
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1518 class's dict; the cycle will be broken that way.
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1519
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1520 tp_subclasses:
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1521 A list of weak references can't be part of a cycle; and
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1522 lists have their own tp_clear.
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1523
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1524 slots (in etype):
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1525 A tuple of strings can't be part of a cycle.
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1526 */
c7e807817159 Backport two patches that belong together:
Guido van Rossum <guido@python.org>
parents: 23754
diff changeset
1527
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1528 CLEAR(type->tp_mro);
20239
2745c3dc24af SF bug [#467331] ClassType.__doc__ always None.
Tim Peters <tim.peters@gmail.com>
parents: 20231
diff changeset
1529
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1530 #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
1531
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1532 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
1533 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1534
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1535 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
1536 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
1537 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1538 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
1539 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
1540
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1541 PyTypeObject PyType_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1542 PyObject_HEAD_INIT(&PyType_Type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1543 0, /* ob_size */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1544 "type", /* tp_name */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1545 sizeof(etype), /* tp_basicsize */
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
1546 sizeof(PyMemberDef), /* tp_itemsize */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1547 (destructor)type_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1548 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1549 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1550 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1551 type_compare, /* tp_compare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1552 (reprfunc)type_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1553 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1554 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1555 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1556 (hashfunc)_Py_HashPointer, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1557 (ternaryfunc)type_call, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1558 0, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1559 (getattrofunc)type_getattro, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1560 (setattrofunc)type_setattro, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1561 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
1562 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
1563 Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1564 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
1565 (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
1566 (inquiry)type_clear, /* tp_clear */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1567 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
1568 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
1569 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1570 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1571 type_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1572 type_members, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1573 type_getsets, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1574 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1575 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1576 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1577 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1578 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1579 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1580 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1581 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
1582 _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
1583 (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
1584 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1585
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1586
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1587 /* 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
1588
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1589 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1590 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
1591 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1592 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1593 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1594
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1595 static void
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1596 object_dealloc(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1597 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1598 self->ob_type->tp_free(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1599 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1600
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1601 static PyObject *
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1602 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
1603 {
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1604 PyTypeObject *type;
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
1605 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
1606
19316
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1607 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
1608 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
1609 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
1610 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
1611 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
1612 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
1613 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
1614 }
f7dc21cb1661 Fix object_repr() to include the module (using the same rules as
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
1615 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
1616 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
1617 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
1618 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
1619 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
1620 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
1621 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
1622 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
1623 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
1624 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
1625 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
1626 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
1627 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
1628 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
1629 }
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1630
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1631 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1632 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
1633 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1634 unaryfunc f;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1635
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1636 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
1637 if (f == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1638 f = object_repr;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1639 return f(self);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1640 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
1641
19194
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1642 static long
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1643 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
1644 {
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1645 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
1646 }
7e9f5c59e9f2 Make dynamic types work as intended. Or at least more so.
Guido van Rossum <guido@python.org>
parents: 19192
diff changeset
1647
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1648 static PyObject *
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1649 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
1650 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1651 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
1652 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
1653 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1654
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1655 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1656 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
1657 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1658 return a == b ||
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1659 (a != NULL &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1660 b != NULL &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1661 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
1662 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
1663 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
1664 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
1665 ((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
1666 (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
1667 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1668
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1669 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1670 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
1671 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1672 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
1673 int size;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1674
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1675 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
1676 return 0;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1677 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
1678 return 1;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1679 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
1680 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
1681 size += sizeof(PyObject *);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1682 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
1683 size += sizeof(PyObject *);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1684 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
1685 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1686
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1687 static int
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1688 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
1689 {
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1690 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
1691 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
1692
23140
46ffb149ea73 backport gvanrossum's patch:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 23131
diff changeset
1693 if (value == NULL) {
46ffb149ea73 backport gvanrossum's patch:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 23131
diff changeset
1694 PyErr_SetString(PyExc_TypeError,
46ffb149ea73 backport gvanrossum's patch:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 23131
diff changeset
1695 "can't delete __class__ attribute");
46ffb149ea73 backport gvanrossum's patch:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 23131
diff changeset
1696 return -1;
46ffb149ea73 backport gvanrossum's patch:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 23131
diff changeset
1697 }
20075
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1698 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
1699 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
1700 "__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
1701 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
1702 return -1;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1703 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1704 new = (PyTypeObject *)value;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1705 newbase = new;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1706 oldbase = old;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1707 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
1708 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
1709 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
1710 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
1711 if (newbase != oldbase &&
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1712 (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
1713 !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
1714 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
1715 "__class__ assignment: "
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1716 "'%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
1717 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
1718 old->tp_name);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1719 return -1;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1720 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1721 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
1722 Py_INCREF(new);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1723 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1724 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
1725 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
1726 Py_DECREF(old);
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1727 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1728 return 0;
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1729 }
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1730
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1731 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
1732 {"__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
1733 "the object's class"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1734 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1735 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1736
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1737 static PyObject *
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1738 object_reduce(PyObject *self, PyObject *args)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1739 {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1740 /* Call copy_reg._reduce(self) */
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1741 static PyObject *copy_reg_str;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1742 PyObject *copy_reg, *res;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1743
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1744 if (!copy_reg_str) {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1745 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
1746 if (copy_reg_str == NULL)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1747 return NULL;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1748 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1749 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
1750 if (!copy_reg)
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1751 return NULL;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1752 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
1753 Py_DECREF(copy_reg);
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1754 return res;
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1755 }
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1756
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1757 static PyMethodDef object_methods[] = {
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1758 {"__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
1759 {0}
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1760 };
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1761
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1762 PyTypeObject PyBaseObject_Type = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1763 PyObject_HEAD_INIT(&PyType_Type)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1764 0, /* ob_size */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1765 "object", /* tp_name */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1766 sizeof(PyObject), /* tp_basicsize */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1767 0, /* tp_itemsize */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1768 (destructor)object_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1769 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1770 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1771 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1772 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
1773 object_repr, /* tp_repr */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1774 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1775 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1776 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
1777 object_hash, /* tp_hash */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1778 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
1779 object_str, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1780 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
1781 PyObject_GenericSetAttr, /* tp_setattro */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1782 0, /* tp_as_buffer */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1783 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
1784 "The most base type", /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1785 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1786 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1787 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1788 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1789 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1790 0, /* tp_iternext */
20084
48e93ef3c5b7 - Provisional support for pickling new-style objects. (*)
Guido van Rossum <guido@python.org>
parents: 20076
diff changeset
1791 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
1792 0, /* tp_members */
aae5cf7e5861 Make __class__ assignment possible, when the object structures are the
Guido van Rossum <guido@python.org>
parents: 20063
diff changeset
1793 object_getsets, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1794 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1795 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1796 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1797 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1798 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1799 object_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1800 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
1801 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
1802 _PyObject_Del, /* tp_free */
18933
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
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1805
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1806 /* 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
1807
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1808 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1809 add_methods(PyTypeObject *type, PyMethodDef *meth)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1810 {
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1811 PyObject *dict = type->tp_dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1812
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1813 for (; meth->ml_name != NULL; meth++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1814 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1815 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
1816 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1817 descr = PyDescr_NewMethod(type, meth);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1818 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1819 return -1;
22331
f2eb95fb4529 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22230
diff changeset
1820 if (PyDict_SetItemString(dict,meth->ml_name, descr) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1821 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1822 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1823 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1824 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1825 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1826
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1827 static int
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
1828 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
1829 {
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1830 PyObject *dict = type->tp_dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1831
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1832 for (; memb->name != NULL; memb++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1833 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1834 if (PyDict_GetItemString(dict, memb->name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1835 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1836 descr = PyDescr_NewMember(type, memb);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1837 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1838 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1839 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
1840 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1841 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1842 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1843 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1844 }
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 static int
20015
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
1847 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
1848 {
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
1849 PyObject *dict = type->tp_dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1850
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1851 for (; gsp->name != NULL; gsp++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1852 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1853 if (PyDict_GetItemString(dict, gsp->name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1854 continue;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1855 descr = PyDescr_NewGetSet(type, gsp);
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 if (descr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1858 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1859 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
1860 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1861 Py_DECREF(descr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1862 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1863 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1864 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1865
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1866 static void
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1867 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
1868 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1869 int oldsize, newsize;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1870
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1871 /* 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
1872 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
1873 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
1874 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
1875 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
1876 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1877 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
1878 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
1879 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
1880 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1881 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
1882 (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
1883 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
1884 (!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
1885 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
1886 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
1887 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
1888 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
1889 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1890 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1891 /* Wow */
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1892 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1893 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
1894 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
1895 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
1896 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1897
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1898 /* 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
1899 oldsize = base->tp_basicsize;
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1900 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
1901 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
1902 (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
1903 (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
1904 (!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
1905 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
1906 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
1907 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
1908 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
1909 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
1910 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1911 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
21451
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1912 /* The condition below could use some explanation.
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1913 It appears that tp_new is not inherited for static types
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1914 whose base class is 'object'; this seems to be a precaution
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1915 so that old extension types don't suddenly become
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1916 callable (object.__new__ wouldn't insure the invariants
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1917 that the extension type's own factory function ensures).
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1918 Heap types, of course, are under our control, so they do
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1919 inherit tp_new; static extension types that specify some
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1920 other built-in type as the default are considered
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
1921 new-style-aware so they also inherit object.__new__. */
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1922 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
1923 (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
1924 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
1925 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
1926 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1927 }
19582
13a93a5dcb1d Use new GC API. Remove usage of BASICSIZE macros.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19551
diff changeset
1928 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
1929
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1930 /* 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
1931
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1932 #undef COPYVAL
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1933 #define COPYVAL(SLOT) \
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1934 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
1935
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1936 COPYVAL(tp_itemsize);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1937 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
1938 COPYVAL(tp_weaklistoffset);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1939 }
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1940 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
1941 COPYVAL(tp_dictoffset);
968d169501f2 Non-function fields, like tp_dictoffset and tp_weaklistoffset, should
Guido van Rossum <guido@python.org>
parents: 19194
diff changeset
1942 }
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1943 }
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1944
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1945 static void
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1946 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
1947 {
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1948 PyTypeObject *basebase;
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1949
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1950 #undef SLOTDEFINED
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1951 #undef COPYSLOT
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1952 #undef COPYNUM
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1953 #undef COPYSEQ
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1954 #undef COPYMAP
20386
3c01c34d07f6 Now that COPYBUF is a new local macro, add #undef COPYBUF.
Guido van Rossum <guido@python.org>
parents: 20384
diff changeset
1955 #undef COPYBUF
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1956
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1957 #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
1958 (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
1959 (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
1960
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1961 #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
1962 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
1963
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1964 #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
1965 #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
1966 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
20384
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
1967 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1968
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1969 /* 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
1970 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
1971
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
1972 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
1973 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
1974 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
1975 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1976 COPYNUM(nb_add);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1977 COPYNUM(nb_subtract);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1978 COPYNUM(nb_multiply);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1979 COPYNUM(nb_divide);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1980 COPYNUM(nb_remainder);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1981 COPYNUM(nb_divmod);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1982 COPYNUM(nb_power);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1983 COPYNUM(nb_negative);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1984 COPYNUM(nb_positive);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1985 COPYNUM(nb_absolute);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1986 COPYNUM(nb_nonzero);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1987 COPYNUM(nb_invert);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1988 COPYNUM(nb_lshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1989 COPYNUM(nb_rshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1990 COPYNUM(nb_and);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1991 COPYNUM(nb_xor);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1992 COPYNUM(nb_or);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1993 COPYNUM(nb_coerce);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1994 COPYNUM(nb_int);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1995 COPYNUM(nb_long);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1996 COPYNUM(nb_float);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1997 COPYNUM(nb_oct);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1998 COPYNUM(nb_hex);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
1999 COPYNUM(nb_inplace_add);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2000 COPYNUM(nb_inplace_subtract);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2001 COPYNUM(nb_inplace_multiply);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2002 COPYNUM(nb_inplace_divide);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2003 COPYNUM(nb_inplace_remainder);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2004 COPYNUM(nb_inplace_power);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2005 COPYNUM(nb_inplace_lshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2006 COPYNUM(nb_inplace_rshift);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2007 COPYNUM(nb_inplace_and);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2008 COPYNUM(nb_inplace_xor);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2009 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
2010 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
2011 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
2012 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
2013 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
2014 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
2015 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2016 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2017
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2018 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
2019 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
2020 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
2021 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2022 COPYSEQ(sq_length);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2023 COPYSEQ(sq_concat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2024 COPYSEQ(sq_repeat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2025 COPYSEQ(sq_item);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2026 COPYSEQ(sq_slice);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2027 COPYSEQ(sq_ass_item);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2028 COPYSEQ(sq_ass_slice);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2029 COPYSEQ(sq_contains);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2030 COPYSEQ(sq_inplace_concat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2031 COPYSEQ(sq_inplace_repeat);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2032 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2033
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2034 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
2035 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
2036 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
2037 basebase = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2038 COPYMAP(mp_length);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2039 COPYMAP(mp_subscript);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2040 COPYMAP(mp_ass_subscript);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2041 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2042
20384
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2043 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2044 basebase = base->tp_base;
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2045 if (basebase->tp_as_buffer == NULL)
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2046 basebase = NULL;
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2047 COPYBUF(bf_getreadbuffer);
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2048 COPYBUF(bf_getwritebuffer);
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2049 COPYBUF(bf_getsegcount);
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2050 COPYBUF(bf_getcharbuffer);
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2051 }
2ba4018de2e3 SF bug [#470040] ParseTuple t# vs subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 20382
diff changeset
2052
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2053 basebase = base->tp_base;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2054
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2055 COPYSLOT(tp_dealloc);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2056 COPYSLOT(tp_print);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2057 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
2058 type->tp_getattr = base->tp_getattr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2059 type->tp_getattro = base->tp_getattro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2060 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2061 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
2062 type->tp_setattr = base->tp_setattr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2063 type->tp_setattro = base->tp_setattro;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2064 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2065 /* tp_compare see tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2066 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
2067 /* tp_hash see tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2068 COPYSLOT(tp_call);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2069 COPYSLOT(tp_str);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2070 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
2071 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
2072 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
2073 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
2074 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2075 type->tp_compare = base->tp_compare;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2076 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
2077 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
2078 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2079 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2080 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2081 COPYSLOT(tp_compare);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2082 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2083 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
2084 COPYSLOT(tp_iter);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2085 COPYSLOT(tp_iternext);
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 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
2088 COPYSLOT(tp_descr_get);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2089 COPYSLOT(tp_descr_set);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2090 COPYSLOT(tp_init);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2091 COPYSLOT(tp_alloc);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2092 COPYSLOT(tp_free);
22855
416db8d96216 Backport half a patch from the trunk. This inherits tp_is_gc from a
Guido van Rossum <guido@python.org>
parents: 22845
diff changeset
2093 COPYSLOT(tp_is_gc);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2094 }
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2095 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2096
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2097 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
2098 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
2099
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2100 int
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 19036
diff changeset
2101 PyType_Ready(PyTypeObject *type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2102 {
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2103 PyObject *dict, *bases;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2104 PyTypeObject *base;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2105 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2106
19136
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
2107 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
2108 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
2109 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
2110 }
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
2111 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
2112
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
2113 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
2114
22872
d462c790cc5b - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22855
diff changeset
2115 /* Initialize tp_base (defaults to BaseObject unless that's us) */
d462c790cc5b - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22855
diff changeset
2116 base = type->tp_base;
d462c790cc5b - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22855
diff changeset
2117 if (base == NULL && type != &PyBaseObject_Type)
d462c790cc5b - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22855
diff changeset
2118 base = type->tp_base = &PyBaseObject_Type;
d462c790cc5b - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22855
diff changeset
2119
24826
a7dbe4e7f5de Backport:
Guido van Rossum <guido@python.org>
parents: 24463
diff changeset
2120 /* Initialize the base class */
a7dbe4e7f5de Backport:
Guido van Rossum <guido@python.org>
parents: 24463
diff changeset
2121 if (base && base->tp_dict == NULL) {
a7dbe4e7f5de Backport:
Guido van Rossum <guido@python.org>
parents: 24463
diff changeset
2122 if (PyType_Ready(base) < 0)
a7dbe4e7f5de Backport:
Guido van Rossum <guido@python.org>
parents: 24463
diff changeset
2123 goto error;
a7dbe4e7f5de Backport:
Guido van Rossum <guido@python.org>
parents: 24463
diff changeset
2124 }
a7dbe4e7f5de Backport:
Guido van Rossum <guido@python.org>
parents: 24463
diff changeset
2125
21451
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
2126 /* Initialize ob_type if NULL. This means extensions that want to be
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
2127 compilable separately on Windows can call PyType_Ready() instead of
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
2128 initializing the ob_type field of their type objects. */
be5ca0b25bfd - PyType_Ready(): Initialize the ob_type field to &PyType_Type if it's
Guido van Rossum <guido@python.org>
parents: 21404
diff changeset
2129 if (type->ob_type == NULL)
22872
d462c790cc5b - A type can now inherit its metatype from its base type. Previously,
Guido van Rossum <guido@python.org>
parents: 22855
diff changeset
2130 type->ob_type = base->ob_type;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2131
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2132 /* Initialize tp_bases */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2133 bases = type->tp_bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2134 if (bases == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2135 if (base == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2136 bases = PyTuple_New(0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2137 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2138 bases = Py_BuildValue("(O)", base);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2139 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
2140 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2141 type->tp_bases = bases;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2142 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2143
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2144 /* Initialize tp_dict */
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2145 dict = type->tp_dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2146 if (dict == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2147 dict = PyDict_New();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2148 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
2149 goto error;
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2150 type->tp_dict = dict;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2151 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2152
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2153 /* Add type-specific descriptors to tp_dict */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2154 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
2155 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2156 if (type->tp_methods != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2157 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
2158 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2159 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2160 if (type->tp_members != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2161 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
2162 goto error;
18933
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 if (type->tp_getset != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2165 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
2166 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2167 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2168
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2169 /* Calculate method resolution order */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2170 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
2171 goto error;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2172 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2173
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2174 /* 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
2175 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
2176 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
2177
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2178 /* Initialize tp_dict properly */
20439
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2179 bases = type->tp_mro;
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2180 assert(bases != NULL);
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2181 assert(PyTuple_Check(bases));
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2182 n = PyTuple_GET_SIZE(bases);
85571d5f6545 Completely get rid of __dynamic__ and the corresponding
Guido van Rossum <guido@python.org>
parents: 20436
diff changeset
2183 for (i = 1; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2184 PyObject *b = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2185 if (PyType_Check(b))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2186 inherit_slots(type, (PyTypeObject *)b);
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2187 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2188
21997
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2189 /* if the type dictionary doesn't contain a __doc__, set it from
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2190 the tp_doc slot.
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2191 */
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2192 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2193 if (type->tp_doc != NULL) {
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2194 PyObject *doc = PyString_FromString(type->tp_doc);
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2195 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2196 Py_DECREF(doc);
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2197 } else {
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2198 PyDict_SetItemString(type->tp_dict, "__doc__", Py_None);
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2199 }
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2200 }
cb5b4a54ff12 backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 21451
diff changeset
2201
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2202 /* 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
2203 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
2204 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
2205 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
2206 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
2207 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
2208 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
2209 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
2210 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
2211 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2212
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2213 /* 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
2214 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
2215 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
2216 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2217 PyObject *b = PyTuple_GET_ITEM(bases, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2218 if (PyType_Check(b) &&
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
2219 add_subclass((PyTypeObject *)b, type) < 0)
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2220 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
2221 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2222
19153
64d4ad73a887 - Big changes to fix SF bug #442833 (a nasty multiple inheritance
Guido van Rossum <guido@python.org>
parents: 19136
diff changeset
2223 /* 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
2224 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
2225 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
2226 (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
2227 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
2228
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
2229 error:
6463cd9db2fc Change PyType_Ready() to use the READY and READYING flags. This makes
Guido van Rossum <guido@python.org>
parents: 19109
diff changeset
2230 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
2231 return -1;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2232 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2233
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2234 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
2235 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
2236 {
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2237 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
2238 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
2239
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2240 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
2241 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
2242 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
2243 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
2244 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
2245 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2246 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
2247 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
2248 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
2249 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
2250 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
2251 assert(PyWeakref_CheckRef(ref));
25838
8540887bcec6 Backport of 2.183:
Guido van Rossum <guido@python.org>
parents: 25718
diff changeset
2252 if (PyWeakref_GET_OBJECT(ref) == Py_None) {
8540887bcec6 Backport of 2.183:
Guido van Rossum <guido@python.org>
parents: 25718
diff changeset
2253 i = PyList_SetItem(list, i, new);
8540887bcec6 Backport of 2.183:
Guido van Rossum <guido@python.org>
parents: 25718
diff changeset
2254 Py_DECREF(new);
8540887bcec6 Backport of 2.183:
Guido van Rossum <guido@python.org>
parents: 25718
diff changeset
2255 return i;
8540887bcec6 Backport of 2.183:
Guido van Rossum <guido@python.org>
parents: 25718
diff changeset
2256 }
20315
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2257 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2258 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
2259 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
2260 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
2261 }
7b41f7a2e528 Keep track of a type's subclasses (subtypes), in tp_subclasses, which
Guido van Rossum <guido@python.org>
parents: 20296
diff changeset
2262
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2263
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2264 /* 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
2265
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2266 /* 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
2267 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
2268 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
2269 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
2270 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
2271
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2272 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2273 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
2274 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2275 inquiry func = (inquiry)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2276 int res;
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 if (!PyArg_ParseTuple(args, ""))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2279 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2280 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2281 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2282 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2283 return PyInt_FromLong((long)res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2284 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2285
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2286 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2287 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
2288 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2289 binaryfunc func = (binaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2290 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2291
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2292 if (!PyArg_ParseTuple(args, "O", &other))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2293 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2294 return (*func)(self, other);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2295 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2296
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2297 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
2298 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
2299 {
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2300 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
2301 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
2302
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2303 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
2304 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
2305 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
2306 !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
2307 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
2308 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
2309 }
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2310 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
2311 }
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2312
26b5f8b94872 It turned out not so difficult to support old-style numbers (those
Guido van Rossum <guido@python.org>
parents: 20320
diff changeset
2313 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2314 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
2315 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2316 binaryfunc func = (binaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2317 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2318
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2319 if (!PyArg_ParseTuple(args, "O", &other))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2320 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
2321 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
2322 !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
2323 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
2324 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
2325 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2326 return (*func)(other, self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2327 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2328
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2329 static PyObject *
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2330 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
2331 {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2332 coercion func = (coercion)wrapped;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2333 PyObject *other, *res;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2334 int ok;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2335
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2336 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
2337 return NULL;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2338 ok = func(&self, &other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2339 if (ok < 0)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2340 return NULL;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2341 if (ok > 0) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2342 Py_INCREF(Py_NotImplemented);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2343 return Py_NotImplemented;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2344 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2345 res = PyTuple_New(2);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2346 if (res == NULL) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2347 Py_DECREF(self);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2348 Py_DECREF(other);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2349 return NULL;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2350 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2351 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
2352 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
2353 return res;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2354 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
2355
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2356 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2357 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
2358 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2359 ternaryfunc func = (ternaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2360 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2361 PyObject *third = Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2362
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2363 /* 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
2364
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2365 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
2366 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2367 return (*func)(self, other, third);
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
20168
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2370 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
2371 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
2372 {
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2373 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
2374 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
2375 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
2376
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2377 /* 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
2378
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2379 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
2380 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
2381 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
2382 }
091e9e3ceeaf Ouch. The wrapper for __rpow__ was the same as for __pow__, resulting
Guido van Rossum <guido@python.org>
parents: 20140
diff changeset
2383
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2384 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2385 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
2386 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2387 unaryfunc func = (unaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2388
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2389 if (!PyArg_ParseTuple(args, ""))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2390 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2391 return (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2392 }
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 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2395 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
2396 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2397 intargfunc func = (intargfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2398 int i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2399
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2400 if (!PyArg_ParseTuple(args, "i", &i))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2401 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2402 return (*func)(self, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2403 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2404
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2405 static int
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2406 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
2407 {
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2408 int i;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2409
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2410 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
2411 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
2412 return -1;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2413 if (i < 0) {
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2414 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
2415 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
2416 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
2417 if (n < 0)
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2418 return -1;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2419 i += n;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2420 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2421 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2422 return i;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2423 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2424
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2425 static PyObject *
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2426 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
2427 {
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2428 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
2429 PyObject *arg;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2430 int i;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2431
20229
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2432 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
2433 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
2434 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
2435 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
2436 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2437 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
2438 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2439 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
2440 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
2441 return NULL;
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2442 }
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2443
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2444 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2445 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
2446 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2447 intintargfunc func = (intintargfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2448 int i, j;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2449
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2450 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
2451 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2452 return (*func)(self, i, j);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2453 }
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 static PyObject *
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2456 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
2457 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2458 intobjargproc func = (intobjargproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2459 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
2460 PyObject *arg, *value;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2461
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2462 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
2463 return NULL;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2464 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
2465 if (i == -1 && PyErr_Occurred())
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2466 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2467 res = (*func)(self, i, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2468 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2469 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2470 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2471 return Py_None;
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
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2474 static PyObject *
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2475 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
2476 {
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2477 intobjargproc func = (intobjargproc)wrapped;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2478 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
2479 PyObject *arg;
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2480
19353
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2481 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
2482 return NULL;
051d8e380f38 Address SF bug #442813. The sequence getitem wrappers should do
Guido van Rossum <guido@python.org>
parents: 19349
diff changeset
2483 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
2484 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
2485 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2486 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
2487 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
2488 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2489 Py_INCREF(Py_None);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2490 return Py_None;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2491 }
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2492
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2493 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2494 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
2495 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2496 intintobjargproc func = (intintobjargproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2497 int i, j, res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2498 PyObject *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2499
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2500 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
2501 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2502 res = (*func)(self, i, j, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2503 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2504 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2505 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2506 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2507 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2508
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2509 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
2510 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
2511 {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2512 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
2513 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
2514
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2515 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
2516 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
2517 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
2518 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
2519 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
2520 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
2521 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
2522 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
2523
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2524 /* 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
2525 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2526 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
2527 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2528 objobjproc func = (objobjproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2529 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2530 PyObject *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2531
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2532 if (!PyArg_ParseTuple(args, "O", &value))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2533 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2534 res = (*func)(self, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2535 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2536 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2537 return PyInt_FromLong((long)res);
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
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2540 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2541 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
2542 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2543 objobjargproc func = (objobjargproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2544 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2545 PyObject *key, *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2546
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2547 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
2548 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2549 res = (*func)(self, key, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2550 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2551 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2552 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2553 return Py_None;
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
18941
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2556 static PyObject *
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2557 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
2558 {
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2559 objobjargproc func = (objobjargproc)wrapped;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2560 int res;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2561 PyObject *key;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2562
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2563 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
2564 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2565 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
2566 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
2567 return NULL;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2568 Py_INCREF(Py_None);
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2569 return Py_None;
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2570 }
013cffcfd8a3 Fix SF #442791 (revisited): No __delitem__ wrapper was defined.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2571
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2572 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2573 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
2574 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2575 cmpfunc func = (cmpfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2576 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2577 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2578
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2579 if (!PyArg_ParseTuple(args, "O", &other))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2580 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
2581 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
2582 !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
2583 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
2584 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
2585 "%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
2586 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
2587 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
2588 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
2589 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
2590 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2591 res = (*func)(self, other);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2592 if (PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2593 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2594 return PyInt_FromLong((long)res);
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
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2597 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2598 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
2599 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2600 setattrofunc func = (setattrofunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2601 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2602 PyObject *name, *value;
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 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
2605 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2606 res = (*func)(self, name, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2607 if (res < 0)
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 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2610 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2611 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2612
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2613 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2614 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
2615 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2616 setattrofunc func = (setattrofunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2617 int res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2618 PyObject *name;
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 if (!PyArg_ParseTuple(args, "O", &name))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2621 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2622 res = (*func)(self, name, NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2623 if (res < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2624 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2625 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2626 return Py_None;
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
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2629 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2630 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
2631 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2632 hashfunc func = (hashfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2633 long res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2634
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2635 if (!PyArg_ParseTuple(args, ""))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2636 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2637 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2638 if (res == -1 && PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2639 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2640 return PyInt_FromLong(res);
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
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2643 static PyObject *
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
2644 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
18933
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 ternaryfunc func = (ternaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2647
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
2648 return (*func)(self, args, kwds);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2649 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2650
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2651 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2652 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
2653 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2654 richcmpfunc func = (richcmpfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2655 PyObject *other;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2656
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2657 if (!PyArg_ParseTuple(args, "O", &other))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2658 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2659 return (*func)(self, other, op);
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
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2662 #undef RICHCMP_WRAPPER
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2663 #define RICHCMP_WRAPPER(NAME, OP) \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2664 static PyObject * \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2665 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
2666 { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2667 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
2668 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2669
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
2670 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
2671 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
2672 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
2673 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
2674 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
2675 RICHCMP_WRAPPER(ge, Py_GE)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2676
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2677 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2678 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
2679 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2680 unaryfunc func = (unaryfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2681 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2682
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2683 if (!PyArg_ParseTuple(args, ""))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2684 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2685 res = (*func)(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2686 if (res == NULL && !PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2687 PyErr_SetNone(PyExc_StopIteration);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2688 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2689 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2690
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2691 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2692 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
2693 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2694 descrgetfunc func = (descrgetfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2695 PyObject *obj;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2696 PyObject *type = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2697
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2698 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
2699 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2700 return (*func)(self, obj, type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2701 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2702
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2703 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
2704 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
2705 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2706 descrsetfunc func = (descrsetfunc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2707 PyObject *obj, *value;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2708 int ret;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2709
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2710 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
2711 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2712 ret = (*func)(self, obj, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2713 if (ret < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2714 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2715 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2716 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2717 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2718
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2719 static PyObject *
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
2720 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2721 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2722 initproc func = (initproc)wrapped;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2723
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
2724 if (func(self, args, kwds) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2725 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2726 Py_INCREF(Py_None);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2727 return Py_None;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2728 }
24463
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2729
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2730 static PyObject *
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2731 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2732 {
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2733 descrsetfunc func = (descrsetfunc)wrapped;
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2734 PyObject *obj;
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2735 int ret;
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2736
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2737 if (!PyArg_ParseTuple(args, "O", &obj))
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2738 return NULL;
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2739 ret = (*func)(self, obj, NULL);
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2740 if (ret < 0)
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2741 return NULL;
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2742 Py_INCREF(Py_None);
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2743 return Py_None;
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
2744 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2745
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2746 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
2747 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
2748 {
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2749 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
2750 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
2751
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 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
2753 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
2754 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
2755 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
2756 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
2757 "%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
2758 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
2759 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
2760 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2761 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
2762 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
2763 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
2764 "%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
2765 type->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2766 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
2767 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
2768 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2769 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
2770 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
2771 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
2772 "%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
2773 type->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2774 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2775 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2776 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
2777 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
2778 }
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2779
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2780 /* Check that the use doesn't do something silly and unsafe like
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20753
diff changeset
2781 object.__new__(dict). To do this, we check that the
19429
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2782 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
2783 staticbase = subtype;
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2784 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
2785 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
2786 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
2787 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
2788 "%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
2789 type->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2790 subtype->tp_name,
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2791 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
2792 return NULL;
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2793 }
9cf669581110 Merge changes from r22a2-branch back into trunk. Also, change patch
Barry Warsaw <barry@python.org>
parents: 19353
diff changeset
2794
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
2795 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
2796 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
2797 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
2798 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
2799 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
2800 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2801 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2802
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
2803 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
2804 {"__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
2805 "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
2806 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2807 };
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 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
2810 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
2811 {
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2812 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
2813
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2814 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2815 return 0;
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2816 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
2817 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
2818 return -1;
20445
27ab0327b2fc Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents: 20439
diff changeset
2819 return PyDict_SetItemString(type->tp_dict, "__new__", 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
2820 }
c1daa94c21d8 A totally new way to do the __new__ wrapper. This should address the
Guido van Rossum <guido@python.org>
parents: 18941
diff changeset
2821
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2822 /* Slot wrappers that call the corresponding __foo__ slot. See comments
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
2823 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
2824
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2825 #define SLOT0(FUNCNAME, OPSTR) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2826 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2827 FUNCNAME(PyObject *self) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2828 { \
19540
e141b392b61d Fix a typo in SLOT0 macro for the declaration of cache_str.
Guido van Rossum <guido@python.org>
parents: 19538
diff changeset
2829 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2830 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
2831 }
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2832
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2833 #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
2834 static PyObject * \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2835 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
2836 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2837 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2838 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
2839 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2840
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2841
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2842 #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
2843 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2844 FUNCNAME(PyObject *self, PyObject *other) \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2845 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2846 static PyObject *cache_str, *rcache_str; \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2847 int do_other = self->ob_type != other->ob_type && \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2848 other->ob_type->tp_as_number != NULL && \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2849 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
2850 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
2851 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
2852 PyObject *r; \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2853 if (do_other && \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2854 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2855 r = call_maybe( \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2856 other, ROPSTR, &rcache_str, "(O)", self); \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2857 if (r != Py_NotImplemented) \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2858 return r; \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2859 Py_DECREF(r); \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2860 do_other = 0; \
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2861 } \
19913
ca0f54dacdd8 Add call_maybe(): a variant of call_method() that returns
Guido van Rossum <guido@python.org>
parents: 19911
diff changeset
2862 r = call_maybe( \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2863 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
2864 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
2865 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
2866 return r; \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2867 Py_DECREF(r); \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2868 } \
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2869 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
2870 return call_maybe( \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2871 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
2872 } \
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2873 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
2874 return Py_NotImplemented; \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2875 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2876
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2877 #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
2878 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
2879
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2880 #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
2881 static PyObject * \
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2882 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
2883 { \
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2884 static PyObject *cache_str; \
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2885 return call_method(self, OPSTR, &cache_str, \
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2886 "(" ARGCODES ")", arg1, arg2); \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2887 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2888
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2889 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2890 slot_sq_length(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2891 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2892 static PyObject *len_str;
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2893 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
2894 int len;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2895
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2896 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2897 return -1;
20197
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
2898 len = (int)PyInt_AsLong(res);
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
2899 Py_DECREF(res);
b4f0f10a6c5f slot_sq_length(): squash a leak.
Guido van Rossum <guido@python.org>
parents: 20195
diff changeset
2900 return len;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2901 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2902
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2903 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
2904 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
2905
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2906 /* 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
2907 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
2908 static PyObject *
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2909 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
2910 {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2911 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
2912 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
2913 descrgetfunc f;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2914
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2915 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
2916 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
2917 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
2918 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2919 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2920 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
2921 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
2922 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
2923 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
2924 else
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2925 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
2926 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
2927 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
2928 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
2929 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
2930 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
2931 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
2932 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
2933 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
2934 return retval;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2935 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2936 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2937 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2938 else {
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2939 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
2940 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2941 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
2942 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
2943 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
2944 return NULL;
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2945 }
8bad55edfdf3 *EXPERIMENTAL* speedup of slot_sq_item. This sped up the following
Guido van Rossum <guido@python.org>
parents: 20225
diff changeset
2946
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
2947 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
2948
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2949 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2950 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
2951 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2952 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
2953 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
2954
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2955 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
2956 res = call_method(self, "__delitem__", &delitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2957 "(i)", index);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2958 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2959 res = call_method(self, "__setitem__", &setitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2960 "(iO)", index, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2961 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2962 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2963 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2964 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2965 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2966
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2967 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2968 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
2969 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2970 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
2971 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
2972
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2973 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
2974 res = call_method(self, "__delslice__", &delslice_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2975 "(ii)", i, j);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2976 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
2977 res = call_method(self, "__setslice__", &setslice_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2978 "(iiO)", i, j, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2979 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2980 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2981 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2982 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2983 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2984
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2985 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
2986 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
2987 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2988 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
2989 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
2990
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
2991 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
2992
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2993 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2994 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
2995 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2996 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
2997 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
2998 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
2999 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3000 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3001 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3002 if (res == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3003 return -1;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3004 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
3005 }
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3006 else if (PyErr_Occurred())
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3007 return -1;
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3008 else {
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19803
diff changeset
3009 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
3010 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
3011 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3012 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3013
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3014 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
3015 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
3016
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3017 #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
3018
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3019 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
3020
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3021 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3022 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
3023 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3024 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
3025 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
3026
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3027 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
3028 res = call_method(self, "__delitem__", &delitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3029 "(O)", key);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3030 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3031 res = call_method(self, "__setitem__", &setitem_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3032 "(OO)", key, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3033 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3034 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3035 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3036 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3037 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3038
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3039 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
3040 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
3041 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
3042 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
3043 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
3044 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
3045
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3046 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
3047
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3048 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
3049 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
3050
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3051 static PyObject *
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3052 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
3053 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3054 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
3055
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3056 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
3057 return slot_nb_power_binary(self, other);
23754
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3058 /* Three-arg power doesn't use __rpow__. But ternary_op
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3059 can call this when the second argument's type uses
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3060 slot_nb_power, so check before calling self.__pow__. */
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3061 if (self->ob_type->tp_as_number != NULL &&
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3062 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3063 return call_method(self, "__pow__", &pow_str,
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3064 "(OO)", other, modulus);
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3065 }
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3066 Py_INCREF(Py_NotImplemented);
2c8ea7f9a3ce Backport:
Guido van Rossum <guido@python.org>
parents: 23687
diff changeset
3067 return Py_NotImplemented;
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3068 }
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3069
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3070 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
3071 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
3072 SLOT0(slot_nb_absolute, "__abs__")
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3073
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3074 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3075 slot_nb_nonzero(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3076 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3077 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
3078 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
3079
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3080 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
3081 if (func == NULL) {
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3082 if (PyErr_Occurred())
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3083 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3084 func = lookup_maybe(self, "__len__", &len_str);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3085 if (func == NULL) {
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3086 if (PyErr_Occurred())
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3087 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3088 else
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3089 return 1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3090 }
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3091 }
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3092 res = PyObject_CallObject(func, NULL);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3093 Py_DECREF(func);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3094 if (res == NULL)
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3095 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3096 return PyObject_IsTrue(res);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3097 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3098
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3099 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
3100 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
3101 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
3102 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
3103 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
3104 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
3105
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3106 static int
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3107 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
3108 {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3109 static PyObject *coerce_str;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3110 PyObject *self = *a, *other = *b;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3111
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3112 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
3113 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
3114 PyObject *r;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3115 r = call_maybe(
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3116 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
3117 if (r == NULL)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3118 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3119 if (r == Py_NotImplemented) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3120 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3121 }
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3122 else {
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3123 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3124 PyErr_SetString(PyExc_TypeError,
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3125 "__coerce__ didn't return a 2-tuple");
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3126 Py_DECREF(r);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3127 return -1;
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3128 }
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3129 *a = PyTuple_GET_ITEM(r, 0);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3130 Py_INCREF(*a);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3131 *b = PyTuple_GET_ITEM(r, 1);
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3132 Py_INCREF(*b);
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3133 Py_DECREF(r);
20204
8e6f604b0799 Miscellaneous code fiddling:
Guido van Rossum <guido@python.org>
parents: 20197
diff changeset
3134 return 0;
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3135 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3136 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3137 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
3138 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
3139 PyObject *r;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3140 r = call_maybe(
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3141 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
3142 if (r == NULL)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3143 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3144 if (r == Py_NotImplemented) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3145 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3146 return 1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3147 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3148 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
3149 PyErr_SetString(PyExc_TypeError,
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3150 "__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
3151 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3152 return -1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3153 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3154 *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
3155 Py_INCREF(*a);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3156 *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
3157 Py_INCREF(*b);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3158 Py_DECREF(r);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3159 return 0;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3160 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3161 return 1;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3162 }
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20095
diff changeset
3163
19082
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3164 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
3165 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
3166 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
3167 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
3168 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
3169 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
3170 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
3171 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
3172 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
3173 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
3174 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
3175 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
3176 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
3177 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
3178 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
3179 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
3180 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
3181 "__floordiv__", "__rfloordiv__")
88d124c74766 Proper support for binary operators, including true division and floor
Guido van Rossum <guido@python.org>
parents: 19070
diff changeset
3182 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
3183 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
3184 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
3185
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3186 static int
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3187 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
3188 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3189 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
3190 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
3191 int c;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3192
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
3193 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
3194 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3195 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3196 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3197 else {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3198 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
3199 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3200 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3201 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3202 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
3203 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3204 }
23968
e2fa741c547f Fix SF Bug 572567: Memory leak in object comparison
Raymond Hettinger <python@rcn.com>
parents: 23904
diff changeset
3205 Py_DECREF(func);
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3206 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
3207 if (res == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3208 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3209 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
3210 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3211 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
3212 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3213 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
3214 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3215 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3216 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3217 return 2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3218 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3219
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3220 /* 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
3221 int
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3222 _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
3223 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3224 int c;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3225
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3226 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
3227 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
3228 if (c <= 1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3229 return c;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3230 }
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19964
diff changeset
3231 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
3232 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
3233 if (c < -1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3234 return -2;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3235 if (c <= 1)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3236 return -c;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3237 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3238 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
3239 (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
3240 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3241
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3242 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3243 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
3244 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3245 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
3246 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
3247
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
3248 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
3249 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3250 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
3251 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3252 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3253 }
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
3254 PyErr_Clear();
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19469
diff changeset
3255 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
3256 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
3257 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3258
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3259 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3260 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
3261 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3262 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
3263 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
3264
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 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
3266 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3267 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
3268 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3269 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3270 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3271 else {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3272 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3273 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
3274 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3275 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3276
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3277 static long
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3278 slot_tp_hash(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3279 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3280 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
3281 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
3282
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3283 long h;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3284
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
3285 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
3286
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3287 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3288 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
3289 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3290 if (res == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3291 return -1;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3292 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
3293 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3294 else {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3295 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
3296 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
3297 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3298 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
3299 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
3300 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3301 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3302 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3303 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
3304 return -1;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3305 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3306 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3307 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
3308 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3309 if (h == -1 && !PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3310 h = -2;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3311 return h;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3312 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3313
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3314 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3315 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
3316 {
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
3317 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
3318 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
3319 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3320
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3321 if (meth == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3322 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3323 res = PyObject_Call(meth, args, kwds);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3324 Py_DECREF(meth);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3325 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3326 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3327
20499
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3328 /* There are two slot dispatch functions for tp_getattro.
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3329
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3330 - slot_tp_getattro() is used when __getattribute__ is overridden
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3331 but no __getattr__ hook is present;
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3332
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3333 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3334
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3335 The code in update_slot() and fixup_slot_dispatchers() always installs
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3336 slot_tp_getattr_hook(); this detects the absence of __getattr__ and then
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3337 installs the simpler slot if necessary. */
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3338
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3339 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3340 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
3341 {
20499
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3342 static PyObject *getattribute_str = NULL;
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3343 return call_method(self, "__getattribute__", &getattribute_str,
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3344 "(O)", name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3345 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3346
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3347 static PyObject *
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3348 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
3349 {
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3350 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
3351 PyObject *getattr, *getattribute, *res;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3352 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
3353 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
3354
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3355 if (getattr_str == NULL) {
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3356 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
3357 if (getattr_str == NULL)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3358 return NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3359 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3360 if (getattribute_str == NULL) {
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3361 getattribute_str =
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3362 PyString_InternFromString("__getattribute__");
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3363 if (getattribute_str == NULL)
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3364 return NULL;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3365 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3366 getattr = _PyType_Lookup(tp, getattr_str);
20499
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3367 if (getattr == NULL) {
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3368 /* No __getattr__ hook: use a simpler dispatcher */
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3369 tp->tp_getattro = slot_tp_getattro;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3370 return slot_tp_getattro(self, name);
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3371 }
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3372 getattribute = _PyType_Lookup(tp, getattribute_str);
20499
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3373 if (getattribute == NULL ||
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3374 (getattribute->ob_type == &PyWrapperDescr_Type &&
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3375 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3376 (void *)PyObject_GenericGetAttr))
20028
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3377 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
3378 else
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3379 res = PyObject_CallFunction(getattribute, "OO", self, name);
20499
a0c8c2e5a44d Remove a bunch of stuff that's no longer needed now that update_slot()
Guido van Rossum <guido@python.org>
parents: 20489
diff changeset
3380 if (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
3381 PyErr_Clear();
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3382 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
3383 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3384 return res;
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3385 }
df1b83fd0ebb Add the __getattr__ hook back. The rules are now:
Guido van Rossum <guido@python.org>
parents: 20021
diff changeset
3386
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3387 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3388 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
3389 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3390 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
3391 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
3392
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3393 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
3394 res = call_method(self, "__delattr__", &delattr_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3395 "(O)", name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3396 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3397 res = call_method(self, "__setattr__", &setattr_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3398 "(OO)", name, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3399 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3400 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3401 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3402 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3403 }
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 /* 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
3406 static char *name_op[] = {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3407 "__lt__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3408 "__le__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3409 "__eq__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3410 "__ne__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3411 "__gt__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3412 "__ge__",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3413 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3414
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3415 static PyObject *
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3416 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
3417 {
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3418 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
3419 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
3420
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
3421 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
3422 if (func == NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3423 PyErr_Clear();
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3424 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
3425 return Py_NotImplemented;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3426 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3427 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
3428 if (args == NULL)
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3429 res = NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3430 else {
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3431 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
3432 Py_DECREF(args);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3433 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3434 Py_DECREF(func);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3435 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3436 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3437
19289
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3438 /* 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
3439 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
3440
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3441 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3442 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
3443 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3444 PyObject *res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3445
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3446 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
3447 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
3448 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
3449 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3450 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3451 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3452 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
3453 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
3454 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
3455 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3456 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3457 Py_DECREF(res);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3458 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3459 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
3460 return Py_NotImplemented;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3461 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3462
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3463 static PyObject *
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3464 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
3465 {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3466 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
3467 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
3468
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
3469 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
3470 if (func != NULL) {
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3471 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
3472 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3473 return res;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3474 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3475 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
3476 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
3477 if (func == NULL) {
20183
c6b6189a8397 SF bug [#466173] unpack TypeError unclear
Tim Peters <tim.peters@gmail.com>
parents: 20168
diff changeset
3478 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
3479 return NULL;
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3480 }
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3481 Py_DECREF(func);
5c2bf6cde123 - Another big step in the right direction. All the overridable
Guido van Rossum <guido@python.org>
parents: 19253
diff changeset
3482 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
3483 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3484
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3485 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3486 slot_tp_iternext(PyObject *self)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3487 {
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3488 static PyObject *next_str;
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3489 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
3490 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3491
19339
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3492 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
3493 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
3494 {
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3495 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
3496 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
3497 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
3498
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3499 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
3500 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
3501 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
3502 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
3503 }
6130deeb4cc6 type_new(): look for __dynamic__ at the module level (after looking in
Guido van Rossum <guido@python.org>
parents: 19329
diff changeset
3504 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
3505 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
3506 /* 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
3507 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
3508 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
3509 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
3510 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
3511 }
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
3512 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
3513 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
3514 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
3515 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
3516 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
3517 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3518
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3519 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3520 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
3521 {
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
3522 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
3523 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
3524
45d0e114212e slot_tp_descr_get(): guard against NULL obj or type (bug reported by
Guido van Rossum <guido@python.org>
parents: 19429
diff changeset
3525 if (value == NULL)
21142
842b280421ba Fix for SF bug #485678.
Guido van Rossum <guido@python.org>
parents: 20978
diff changeset
3526 res = call_method(self, "__delete__", &del_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3527 "(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
3528 else
19538
b4a3fc147929 Finish the previous checkin: also avoid getattr when calling the method
Guido van Rossum <guido@python.org>
parents: 19535
diff changeset
3529 res = call_method(self, "__set__", &set_str,
19911
cba449ab5915 call_method():
Guido van Rossum <guido@python.org>
parents: 19898
diff changeset
3530 "(OO)", target, value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3531 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3532 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3533 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3534 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3535 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3536
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3537 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3538 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
3539 {
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
3540 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
3541 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
3542 PyObject *res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3543
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3544 if (meth == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3545 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3546 res = PyObject_Call(meth, args, kwds);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3547 Py_DECREF(meth);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3548 if (res == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3549 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3550 Py_DECREF(res);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3551 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3552 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3553
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3554 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3555 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
3556 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3557 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
3558 PyObject *newargs, *x;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3559 int i, n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3560
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3561 if (func == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3562 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3563 assert(PyTuple_Check(args));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3564 n = PyTuple_GET_SIZE(args);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3565 newargs = PyTuple_New(n+1);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3566 if (newargs == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3567 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3568 Py_INCREF(type);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3569 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
3570 for (i = 0; i < n; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3571 x = PyTuple_GET_ITEM(args, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3572 Py_INCREF(x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3573 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
3574 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3575 x = PyObject_Call(func, newargs, kwds);
20195
3408d929bb94 slot_tp_new(): newargs was leaking.
Guido van Rossum <guido@python.org>
parents: 20190
diff changeset
3576 Py_DECREF(newargs);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3577 Py_DECREF(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3578 return x;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3579 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3580
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3581
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3582 /* 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
3583 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
3584 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
3585 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
3586 __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
3587 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
3588
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3589 typedef struct wrapperbase slotdef;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3590
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3591 #undef TPSLOT
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3592 #undef FLSLOT
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3593 #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
3594 #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
3595 #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
3596 #undef NBSLOT
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3597 #undef UNSLOT
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3598 #undef IBSLOT
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3599 #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
3600 #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
3601
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3602 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3603 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3604 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3605 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3606 DOC, FLAGS}
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3607 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3608 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3609 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3610 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3611 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3612 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3613 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3614 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3615 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3616 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3617 "x." NAME "() <==> " DOC)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3618 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3619 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3620 "x." NAME "(y) <==> x" DOC "y")
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3621 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3622 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3623 "x." NAME "(y) <==> x" DOC "y")
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3624 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3625 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3626 "x." NAME "(y) <==> y" DOC "x")
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3627
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3628 static slotdef slotdefs[] = {
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3629 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3630 "x.__len__() <==> len(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3631 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3632 "x.__add__(y) <==> x+y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3633 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3634 "x.__mul__(n) <==> x*n"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3635 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3636 "x.__rmul__(n) <==> n*x"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3637 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3638 "x.__getitem__(y) <==> x[y]"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3639 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3640 "x.__getslice__(i, j) <==> x[i:j]"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3641 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3642 "x.__setitem__(i, y) <==> x[i]=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3643 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3644 "x.__delitem__(y) <==> del x[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
3645 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3646 wrap_intintobjargproc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3647 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3648 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3649 "x.__delslice__(i, j) <==> del x[i:j]"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3650 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3651 "x.__contains__(y) <==> y in x"),
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3652 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3653 wrap_binaryfunc, "x.__iadd__(y) <==> x+=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
3654 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3655 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3656
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3657 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3658 "x.__len__() <==> len(x)"),
20345
67a7a99f2d5a The slot definition table entry for mp_getitem had a bogus wrapper
Guido van Rossum <guido@python.org>
parents: 20343
diff changeset
3659 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3660 wrap_binaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3661 "x.__getitem__(y) <==> x[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
3662 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3663 wrap_objobjargproc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3664 "x.__setitem__(i, y) <==> x[i]=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
3665 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3666 wrap_delitem,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3667 "x.__delitem__(y) <==> del x[y]"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3668
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3669 BINSLOT("__add__", nb_add, slot_nb_add,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3670 "+"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3671 RBINSLOT("__radd__", nb_add, slot_nb_add,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3672 "+"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3673 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3674 "-"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3675 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3676 "-"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3677 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3678 "*"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3679 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3680 "*"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3681 BINSLOT("__div__", nb_divide, slot_nb_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3682 "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3683 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3684 "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3685 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3686 "%"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3687 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3688 "%"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3689 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3690 "divmod(x, y)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3691 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3692 "divmod(y, x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3693 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3694 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3695 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3696 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3697 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3698 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3699 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3700 "abs(x)"),
22230
fc4056b9cf18 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21997
diff changeset
3701 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3702 "x != 0"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3703 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3704 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3705 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3706 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3707 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3708 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3709 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3710 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3711 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3712 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3713 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3714 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3715 "x.__coerce__(y) <==> coerce(x, y)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3716 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3717 "int(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3718 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3719 "long(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3720 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3721 "float(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3722 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3723 "oct(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3724 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3725 "hex(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3726 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3727 wrap_binaryfunc, "+"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3728 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3729 wrap_binaryfunc, "-"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3730 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3731 wrap_binaryfunc, "*"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3732 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3733 wrap_binaryfunc, "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3734 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3735 wrap_binaryfunc, "%"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3736 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3737 wrap_ternaryfunc, "**"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3738 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3739 wrap_binaryfunc, "<<"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3740 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3741 wrap_binaryfunc, ">>"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3742 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3743 wrap_binaryfunc, "&"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3744 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3745 wrap_binaryfunc, "^"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3746 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3747 wrap_binaryfunc, "|"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3748 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3749 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3750 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3751 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3752 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3753 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3754 IBSLOT("__itruediv__", nb_inplace_true_divide,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3755 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3756
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3757 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3758 "x.__str__() <==> str(x)"),
20737
00a581bdedeb When overriding __str__ or __repr__, set the tp_print slot to NULL.
Guido van Rossum <guido@python.org>
parents: 20725
diff changeset
3759 TPSLOT("__str__", tp_print, NULL, NULL, ""),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3760 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3761 "x.__repr__() <==> repr(x)"),
20737
00a581bdedeb When overriding __str__ or __repr__, set the tp_print slot to NULL.
Guido van Rossum <guido@python.org>
parents: 20725
diff changeset
3762 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3763 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3764 "x.__cmp__(y) <==> cmp(x,y)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3765 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3766 "x.__hash__() <==> hash(x)"),
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3767 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3768 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3769 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3770 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3771 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3772 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3773 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3774 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3775 "x.__setattr__('name', value) <==> x.name = value"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3776 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3777 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3778 "x.__delattr__('name') <==> del x.name"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3779 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3780 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3781 "x.__lt__(y) <==> x<y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3782 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3783 "x.__le__(y) <==> x<=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3784 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3785 "x.__eq__(y) <==> x==y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3786 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3787 "x.__ne__(y) <==> x!=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3788 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3789 "x.__gt__(y) <==> x>y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3790 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3791 "x.__ge__(y) <==> x>=y"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3792 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3793 "x.__iter__() <==> iter(x)"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3794 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3795 "x.next() -> the next value, or raise StopIteration"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3796 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3797 "descr.__get__(obj[, type]) -> value"),
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3798 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3799 "descr.__set__(obj, value)"),
24463
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
3800 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
ebc8667f325a Backport:
Guido van Rossum <guido@python.org>
parents: 24428
diff changeset
3801 wrap_descr_delete, "descr.__delete__(obj)"),
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3802 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
3803 "x.__init__(...) initializes x; "
20599
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3804 "see x.__class__.__doc__ for signature",
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3805 PyWrapperFlag_KEYWORDS),
ff57a1a370b3 Methods of built-in types now properly check for keyword arguments
Guido van Rossum <guido@python.org>
parents: 20595
diff changeset
3806 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3807 {NULL}
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3808 };
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3809
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3810 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
3811 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
3812 {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3813 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
3814
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3815 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
3816 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
3817 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
3818 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
3819 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
3820 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3821 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
3822 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
3823 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
3824 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3825 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
3826 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
3827 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
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 else {
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3830 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
3831 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3832 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
3833 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
3834 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
3835 }
19036
3a7d67ee1011 Cosmetics:
Guido van Rossum <guido@python.org>
parents: 19011
diff changeset
3836
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3837 staticforward int recurse_down_subclasses(PyTypeObject *type,
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3838 slotdef **pp, PyObject *name);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3839
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3840 static int
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3841 update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3842 {
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3843 slotdef **pp;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3844
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3845 for (pp = pp0; *pp; pp++) {
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3846 slotdef *p = *pp;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3847 PyObject *descr;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3848 PyWrapperDescrObject *d;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3849 void *generic = NULL, *specific = NULL;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3850 int use_generic = 0;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3851 int offset = p->offset;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3852 void **ptr = slotptr(type, offset);
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3853 if (ptr == NULL)
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3854 continue;
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3855 do {
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3856 descr = _PyType_Lookup(type, p->name_strobj);
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3857 if (descr == NULL)
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3858 continue;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3859 generic = p->function;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3860 if (descr->ob_type == &PyWrapperDescr_Type) {
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3861 d = (PyWrapperDescrObject *)descr;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3862 if (d->d_base->wrapper == p->wrapper &&
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3863 PyType_IsSubtype(type, d->d_type)) {
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3864 if (specific == NULL ||
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3865 specific == d->d_wrapped)
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3866 specific = d->d_wrapped;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3867 else
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3868 use_generic = 1;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3869 }
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3870 }
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3871 else
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3872 use_generic = 1;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3873 } while ((++p)->offset == offset);
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3874 if (specific && !use_generic)
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3875 *ptr = specific;
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3876 else
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3877 *ptr = generic;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3878 }
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3879 return recurse_down_subclasses(type, pp0, name);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3880 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3881
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3882 static int
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3883 recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3884 {
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3885 PyTypeObject *subclass;
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3886 PyObject *ref, *subclasses, *dict;
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3887 int i, n;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3888
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3889 subclasses = type->tp_subclasses;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3890 if (subclasses == NULL)
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3891 return 0;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3892 assert(PyList_Check(subclasses));
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3893 n = PyList_GET_SIZE(subclasses);
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3894 for (i = 0; i < n; i++) {
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3895 ref = PyList_GET_ITEM(subclasses, i);
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3896 assert(PyWeakref_CheckRef(ref));
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3897 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
23857
5027e7c12a16 Backport:
Guido van Rossum <guido@python.org>
parents: 23758
diff changeset
3898 assert(subclass != NULL);
5027e7c12a16 Backport:
Guido van Rossum <guido@python.org>
parents: 23758
diff changeset
3899 if ((PyObject *)subclass == Py_None)
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3900 continue;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3901 assert(PyType_Check(subclass));
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3902 /* Avoid recursing down into unaffected classes */
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3903 dict = subclass->tp_dict;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3904 if (dict != NULL && PyDict_Check(dict) &&
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3905 PyDict_GetItem(dict, name) != NULL)
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3906 continue;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3907 if (update_these_slots(subclass, pp, name) < 0)
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3908 return -1;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3909 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3910 return 0;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3911 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3912
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3913 static int
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3914 slotdef_cmp(const void *aa, const void *bb)
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3915 {
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3916 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3917 int c = a->offset - b->offset;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3918 if (c != 0)
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3919 return c;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3920 else
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3921 return a - b;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3922 }
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3923
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3924 static void
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3925 init_slotdefs(void)
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3926 {
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3927 slotdef *p;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3928 static int initialized = 0;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3929
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3930 if (initialized)
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3931 return;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3932 for (p = slotdefs; p->name; p++) {
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3933 p->name_strobj = PyString_InternFromString(p->name);
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3934 if (!p->name_strobj)
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3935 Py_FatalError("XXX ouch");
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3936 }
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3937 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3938 slotdef_cmp);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3939 initialized = 1;
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3940 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3941
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3942 static int
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3943 update_slot(PyTypeObject *type, PyObject *name)
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3944 {
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3945 slotdef *ptrs[10];
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3946 slotdef *p;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3947 slotdef **pp;
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3948 int offset;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3949
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3950 init_slotdefs();
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3951 pp = ptrs;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3952 for (p = slotdefs; p->name; p++) {
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3953 /* XXX assume name is interned! */
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3954 if (p->name_strobj == name)
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3955 *pp++ = p;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3956 }
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3957 *pp = NULL;
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3958 for (pp = ptrs; *pp; pp++) {
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3959 p = *pp;
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3960 offset = p->offset;
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3961 while (p > slotdefs && (p-1)->offset == offset)
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3962 --p;
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3963 *pp = p;
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3964 }
20457
4f71dcfd0a93 Refactored the update_slot() code a bit to be hopefully slightly more
Guido van Rossum <guido@python.org>
parents: 20445
diff changeset
3965 return update_these_slots(type, ptrs, name);
20382
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3966 }
be08734b0afd Another step in the right direction: when a new class's attribute
Guido van Rossum <guido@python.org>
parents: 20345
diff changeset
3967
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3968 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
3969 fixup_slot_dispatchers(PyTypeObject *type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
3970 {
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3971 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
3972 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
3973 PyWrapperDescrObject *d;
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3974 int i, n, offset;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3975 void **ptr;
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3976 void *generic, *specific;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3977 int use_generic;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3978
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3979 init_slotdefs();
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
3980 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
3981 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
3982 n = PyTuple_GET_SIZE(mro);
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3983 for (p = slotdefs; p->name; ) {
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3984 offset = p->offset;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3985 ptr = slotptr(type, offset);
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3986 if (!ptr) {
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3987 do {
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3988 ++p;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3989 } while (p->offset == offset);
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3990 continue;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3991 }
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3992 generic = specific = NULL;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3993 use_generic = 0;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3994 do {
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3995 descr = NULL;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
3996 for (i = 0; i < n; i++) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
3997 PyObject *b = PyTuple_GET_ITEM(mro, i);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
3998 PyObject *dict = NULL;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
3999 if (PyType_Check(b))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4000 dict = ((PyTypeObject *)b)->tp_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4001 else if (PyClass_Check(b))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4002 dict = ((PyClassObject *)b)->cl_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4003 if (dict != NULL) {
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4004 descr = PyDict_GetItem(
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4005 dict, p->name_strobj);
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4006 if (descr != NULL)
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4007 break;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4008 }
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4009 }
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4010 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
4011 continue;
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4012 generic = p->function;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4013 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
4014 d = (PyWrapperDescrObject *)descr;
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4015 if (d->d_base->wrapper == p->wrapper &&
20489
e09fb4848765 slot_sq_item(): ensure that self is an instance of the wrapper's
Guido van Rossum <guido@python.org>
parents: 20457
diff changeset
4016 PyType_IsSubtype(type, d->d_type))
e09fb4848765 slot_sq_item(): ensure that self is an instance of the wrapper's
Guido van Rossum <guido@python.org>
parents: 20457
diff changeset
4017 {
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4018 if (specific == NULL ||
20489
e09fb4848765 slot_sq_item(): ensure that self is an instance of the wrapper's
Guido van Rossum <guido@python.org>
parents: 20457
diff changeset
4019 specific == d->d_wrapped)
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4020 specific = d->d_wrapped;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4021 else
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4022 use_generic = 1;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4023 }
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4024 }
20423
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4025 else
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4026 use_generic = 1;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4027 } while ((++p)->offset == offset);
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4028 if (specific && !use_generic)
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4029 *ptr = specific;
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4030 else
4698a3ec5fa8 Redid the slot computation. The initial slot assignments are now done
Guido van Rossum <guido@python.org>
parents: 20386
diff changeset
4031 *ptr = generic;
20343
7bd12270dc3e Halfway checkin. This is still messy, but it's beginning to address
Guido van Rossum <guido@python.org>
parents: 20328
diff changeset
4032 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18068
diff changeset
4033 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4034
20586
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4035 /* This function is called by PyType_Ready() to populate the type's
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4036 dictionary with method descriptors for function slots. For each
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4037 function slot (like tp_repr) that's defined in the type, one or
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4038 more corresponding descriptors are added in the type's tp_dict
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4039 dictionary under the appropriate name (like __repr__). Some
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4040 function slots cause more than one descriptor to be added (for
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4041 example, the nb_add slot adds both __add__ and __radd__
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4042 descriptors) and some function slots compete for the same
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4043 descriptor (for example both sq_item and mp_subscript generate a
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4044 __getitem__ descriptor). This only adds new descriptors and
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4045 doesn't overwrite entries in tp_dict that were previously
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4046 defined. The descriptors contain a reference to the C function
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4047 they must call, so that it's safe if they are copied into a
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4048 subtype's __dict__ and the subtype has a different C function in
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4049 its slot -- calling the method defined by the descriptor will call
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4050 the C function that was used to create it, rather than the C
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4051 function present in the slot when it is called. (This is important
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4052 because a subtype may have a C function in the slot that calls the
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4053 method from the dictionary, and we want to avoid infinite recursion
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4054 here.) */
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4055
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4056 static int
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4057 add_operators(PyTypeObject *type)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4058 {
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4059 PyObject *dict = type->tp_dict;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4060 slotdef *p;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4061 PyObject *descr;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4062 void **ptr;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4063
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4064 init_slotdefs();
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4065 for (p = slotdefs; p->name; p++) {
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4066 if (p->wrapper == NULL)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4067 continue;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4068 ptr = slotptr(type, p->offset);
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4069 if (!ptr || !*ptr)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4070 continue;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4071 if (PyDict_GetItem(dict, p->name_strobj))
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4072 continue;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4073 descr = PyDescr_NewWrapper(type, p, *ptr);
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4074 if (descr == NULL)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4075 return -1;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4076 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4077 return -1;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4078 Py_DECREF(descr);
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4079 }
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4080 if (type->tp_new != NULL) {
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4081 if (add_tp_new_wrapper(type) < 0)
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4082 return -1;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4083 }
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4084 return 0;
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4085 }
9bd603d434f5 Big internal change that should have no external effects: unify the
Guido van Rossum <guido@python.org>
parents: 20516
diff changeset
4086
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4087
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4088 /* Cooperative 'super' */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4089
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4090 typedef struct {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4091 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
4092 PyTypeObject *type;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4093 PyObject *obj;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4094 } superobject;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4095
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
4096 static PyMemberDef super_members[] = {
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
4097 {"__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
4098 "the class invoking super()"},
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19970
diff changeset
4099 {"__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
4100 "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
4101 {0}
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4102 };
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4103
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4104 static void
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4105 super_dealloc(PyObject *self)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4106 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4107 superobject *su = (superobject *)self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4108
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4109 _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
4110 Py_XDECREF(su->obj);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4111 Py_XDECREF(su->type);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4112 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
4113 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4114
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4115 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
4116 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
4117 {
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4118 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
4119
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4120 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
4121 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
4122 "<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
4123 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
4124 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
4125 else
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4126 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
4127 "<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
4128 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
4129 }
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4130
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4131 static PyObject *
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4132 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
4133 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4134 superobject *su = (superobject *)self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4135
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4136 if (su->obj != NULL) {
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4137 PyObject *mro, *res, *tmp, *dict;
22845
d7029679435f backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22348
diff changeset
4138 PyTypeObject *starttype;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4139 descrgetfunc f;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4140 int i, n;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4141
22845
d7029679435f backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22348
diff changeset
4142 starttype = su->obj->ob_type;
d7029679435f backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22348
diff changeset
4143 mro = starttype->tp_mro;
d7029679435f backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22348
diff changeset
4144
19630
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4145 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
4146 n = 0;
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4147 else {
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4148 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
4149 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
4150 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4151 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
4152 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
4153 break;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4154 }
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
4155 if (i >= n && PyType_Check(su->obj)) {
22845
d7029679435f backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22348
diff changeset
4156 starttype = (PyTypeObject *)(su->obj);
d7029679435f backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22348
diff changeset
4157 mro = starttype->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
4158 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
4159 n = 0;
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4160 else {
ae056f114974 Give 'super' a decent repr(), and readonly attributes to access the
Guido van Rossum <guido@python.org>
parents: 19618
diff changeset
4161 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
4162 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
4163 }
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
4164 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
4165 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
4166 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
4167 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
4168 }
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
4169 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4170 i++;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4171 res = NULL;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4172 for (; i < n; i++) {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4173 tmp = PyTuple_GET_ITEM(mro, i);
20978
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4174 if (PyType_Check(tmp))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4175 dict = ((PyTypeObject *)tmp)->tp_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4176 else if (PyClass_Check(tmp))
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4177 dict = ((PyClassObject *)tmp)->cl_dict;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4178 else
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4179 continue;
44f3f5a5bfe2 Changing diapers reminded Guido that he wanted to allow for some measure
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
4180 res = PyDict_GetItem(dict, name);
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4181 if (res != NULL && !PyDescr_IsData(res)) {
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4182 Py_INCREF(res);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4183 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
4184 if (f != NULL) {
22845
d7029679435f backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22348
diff changeset
4185 tmp = f(res, su->obj, (PyObject *)starttype);
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4186 Py_DECREF(res);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4187 res = tmp;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4188 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4189 return res;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4190 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4191 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4192 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4193 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
4194 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4195
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4196 static int
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4197 supercheck(PyTypeObject *type, PyObject *obj)
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4198 {
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4199 if (!PyType_IsSubtype(obj->ob_type, type) &&
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4200 !(PyType_Check(obj) &&
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4201 PyType_IsSubtype((PyTypeObject *)obj, type))) {
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4202 PyErr_SetString(PyExc_TypeError,
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4203 "super(type, obj): "
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4204 "obj must be an instance or subtype of type");
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4205 return -1;
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4206 }
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4207 else
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4208 return 0;
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4209 }
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4210
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4211 static PyObject *
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4212 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
4213 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4214 superobject *su = (superobject *)self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4215 superobject *new;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4216
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4217 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
4218 /* 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
4219 Py_INCREF(self);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4220 return self;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4221 }
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4222 if (su->ob_type != &PySuper_Type)
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4223 /* If su is an instance of a subclass of super,
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4224 call its type */
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4225 return PyObject_CallFunction((PyObject *)su->ob_type,
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4226 "OO", su->type, obj);
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4227 else {
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4228 /* Inline the common case */
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4229 if (supercheck(su->type, obj) < 0)
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4230 return NULL;
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4231 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4232 NULL, NULL);
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4233 if (new == NULL)
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4234 return NULL;
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4235 Py_INCREF(su->type);
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4236 Py_INCREF(obj);
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4237 new->type = su->type;
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4238 new->obj = obj;
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4239 return (PyObject *)new;
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4240 }
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4241 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4242
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4243 static int
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4244 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
4245 {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4246 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
4247 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
4248 PyObject *obj = NULL;
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4249
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4250 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
4251 return -1;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4252 if (obj == Py_None)
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4253 obj = NULL;
21151
918b57354d87 Address SF patch #480716 as well as related issues.
Guido van Rossum <guido@python.org>
parents: 21142
diff changeset
4254 if (obj != NULL && supercheck(type, obj) < 0)
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4255 return -1;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4256 Py_INCREF(type);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4257 Py_XINCREF(obj);
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4258 su->type = type;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4259 su->obj = obj;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4260 return 0;
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4261 }
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4262
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4263 static char super_doc[] =
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4264 "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
4265 "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
4266 "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
4267 "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
4268 "class C(B):\n"
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4269 " 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
4270 " 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
4271
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4272 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
4273 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
4274 {
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4275 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
4276 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
4277
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4278 #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
4279 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
4280 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
4281 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
4282 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
4283 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4284
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4285 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
4286 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
4287
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4288 #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
4289
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4290 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
4291 }
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 20204
diff changeset
4292
19469
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4293 PyTypeObject PySuper_Type = {
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4294 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
4295 0, /* ob_size */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4296 "super", /* tp_name */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4297 sizeof(superobject), /* tp_basicsize */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4298 0, /* tp_itemsize */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4299 /* methods */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4300 super_dealloc, /* tp_dealloc */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4301 0, /* tp_print */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4302 0, /* tp_getattr */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4303 0, /* tp_setattr */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4304 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
4305 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
4306 0, /* tp_as_number */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4307 0, /* tp_as_sequence */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4308 0, /* tp_as_mapping */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4309 0, /* tp_hash */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4310 0, /* tp_call */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4311 0, /* tp_str */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4312 super_getattro, /* tp_getattro */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4313 0, /* tp_setattro */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4314 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
4315 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
4316 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
4317 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
4318 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
4319 0, /* tp_clear */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4320 0, /* tp_richcompare */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4321 0, /* tp_weaklistoffset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4322 0, /* tp_iter */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4323 0, /* tp_iternext */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4324 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
4325 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
4326 0, /* tp_getset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4327 0, /* tp_base */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4328 0, /* tp_dict */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4329 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
4330 0, /* tp_descr_set */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4331 0, /* tp_dictoffset */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4332 super_init, /* tp_init */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4333 PyType_GenericAlloc, /* tp_alloc */
b3c92f013869 Add 'super', another new object type with magical properties.
Guido van Rossum <guido@python.org>
parents: 19464
diff changeset
4334 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
4335 _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
4336 };