annotate Objects/classobject.c @ 24042:63fbbed10714 legacy-trunk

Simplify HTTPSConnection constructor. See discussion in SF bug 458463.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Fri, 28 Jun 2002 23:38:14 +0000
parents ea9d333f9034
children bf3421a28744
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 /* Class 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: 5008
diff changeset
4 #include "Python.h"
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
5 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
20501
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
7 #define TP_DESCR_GET(t) \
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
8 (PyType_HasFeature(t, Py_TPFLAGS_HAVE_CLASS) ? (t)->tp_descr_get : NULL)
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
9
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
10
1861
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
11 /* Forward */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13430
diff changeset
12 static PyObject *class_lookup(PyClassObject *, PyObject *,
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13430
diff changeset
13 PyClassObject **);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13430
diff changeset
14 static PyObject *instance_getattr1(PyInstanceObject *, PyObject *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13430
diff changeset
15 static PyObject *instance_getattr2(PyInstanceObject *, PyObject *);
1861
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
16
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
17 static PyObject *getattrstr, *setattrstr, *delattrstr;
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
18
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
19
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
20 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
21 PyClass_New(PyObject *bases, PyObject *dict, PyObject *name)
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
22 /* bases is NULL or tuple of classobjects! */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
24 PyClassObject *op, *dummy;
6309
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
25 static PyObject *docstr, *modstr, *namestr;
4086
831b420e411e More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents: 4065
diff changeset
26 if (docstr == NULL) {
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
27 docstr= PyString_InternFromString("__doc__");
4086
831b420e411e More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents: 4065
diff changeset
28 if (docstr == NULL)
831b420e411e More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents: 4065
diff changeset
29 return NULL;
831b420e411e More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents: 4065
diff changeset
30 }
6309
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
31 if (modstr == NULL) {
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
32 modstr= PyString_InternFromString("__module__");
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
33 if (modstr == NULL)
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
34 return NULL;
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
35 }
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
36 if (namestr == NULL) {
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
37 namestr= PyString_InternFromString("__name__");
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
38 if (namestr == NULL)
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
39 return NULL;
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
40 }
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
41 if (name == NULL || !PyString_Check(name)) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
42 PyErr_SetString(PyExc_TypeError,
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
43 "PyClass_New: name must be a string");
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
44 return NULL;
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
45 }
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
46 if (dict == NULL || !PyDict_Check(dict)) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
47 PyErr_SetString(PyExc_TypeError,
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
48 "PyClass_New: dict must be a dictionary");
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
49 return NULL;
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
50 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
51 if (PyDict_GetItem(dict, docstr) == NULL) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
52 if (PyDict_SetItem(dict, docstr, Py_None) < 0)
2084
abbf2a4b488b * Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
53 return NULL;
abbf2a4b488b * Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
54 }
6309
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
55 if (PyDict_GetItem(dict, modstr) == NULL) {
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
56 PyObject *globals = PyEval_GetGlobals();
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
57 if (globals != NULL) {
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
58 PyObject *modname = PyDict_GetItem(globals, namestr);
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
59 if (modname != NULL) {
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
60 if (PyDict_SetItem(dict, modstr, modname) < 0)
6309
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
61 return NULL;
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
62 }
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
63 }
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
64 }
465
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
65 if (bases == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
66 bases = PyTuple_New(0);
465
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
67 if (bases == NULL)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1215
diff changeset
68 return NULL;
465
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
69 }
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
70 else {
19805
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
71 int i, n;
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
72 PyObject *base;
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
73 if (!PyTuple_Check(bases)) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
74 PyErr_SetString(PyExc_TypeError,
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
75 "PyClass_New: bases must be a tuple");
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
76 return NULL;
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
77 }
19805
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
78 n = PyTuple_Size(bases);
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
79 for (i = 0; i < n; i++) {
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
80 base = PyTuple_GET_ITEM(bases, i);
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
81 if (!PyClass_Check(base)) {
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
82 if (PyCallable_Check(
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
83 (PyObject *) base->ob_type))
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
84 return PyObject_CallFunction(
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
85 (PyObject *) base->ob_type,
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
86 "OOO",
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
87 name,
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
88 bases,
560155e75087 PyClass_New(): put the extended Don Beaudry hook back in. When one of
Guido van Rossum <guido@python.org>
parents: 19751
diff changeset
89 dict);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
90 PyErr_SetString(PyExc_TypeError,
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
91 "PyClass_New: base must be a class");
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
92 return NULL;
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
93 }
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
94 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
95 Py_INCREF(bases);
6430
e78b6174bec9 Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents: 6309
diff changeset
96 }
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
97 op = PyObject_GC_New(PyClassObject, &PyClass_Type);
465
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
98 if (op == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
99 Py_DECREF(bases);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return NULL;
465
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
101 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 op->cl_bases = bases;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
103 Py_INCREF(dict);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
104 op->cl_dict = dict;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
105 Py_XINCREF(name);
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
106 op->cl_name = name;
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
107 if (getattrstr == NULL) {
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
108 getattrstr = PyString_InternFromString("__getattr__");
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
109 setattrstr = PyString_InternFromString("__setattr__");
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
110 delattrstr = PyString_InternFromString("__delattr__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
111 }
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
112 op->cl_getattr = class_lookup(op, getattrstr, &dummy);
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
113 op->cl_setattr = class_lookup(op, setattrstr, &dummy);
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
114 op->cl_delattr = class_lookup(op, delattrstr, &dummy);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
115 Py_XINCREF(op->cl_getattr);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
116 Py_XINCREF(op->cl_setattr);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
117 Py_XINCREF(op->cl_delattr);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
118 _PyObject_GC_TRACK(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
119 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
19751
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
122 PyObject *
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
123 PyMethod_Function(PyObject *im)
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
124 {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
125 if (!PyMethod_Check(im)) {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
126 PyErr_BadInternalCall();
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
127 return NULL;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
128 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
129 return ((PyMethodObject *)im)->im_func;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
130 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
131
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
132 PyObject *
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
133 PyMethod_Self(PyObject *im)
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
134 {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
135 if (!PyMethod_Check(im)) {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
136 PyErr_BadInternalCall();
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
137 return NULL;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
138 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
139 return ((PyMethodObject *)im)->im_self;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
140 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
141
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
142 PyObject *
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
143 PyMethod_Class(PyObject *im)
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
144 {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
145 if (!PyMethod_Check(im)) {
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
146 PyErr_BadInternalCall();
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
147 return NULL;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
148 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
149 return ((PyMethodObject *)im)->im_class;
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
150 }
3d76dfc34c57 Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
151
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
152 PyDoc_STRVAR(class_doc,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
153 "classobj(name, bases, dict)\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
154 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
155 Create a class object. The name must be a string; the second argument\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
156 a tuple of classes, and the third a dictionary.");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
157
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
158 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
159 class_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
160 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
161 PyObject *name, *bases, *dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
162 static char *kwlist[] = {"name", "bases", "dict", 0};
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
163
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
164 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", kwlist,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
165 &name, &bases, &dict))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
166 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
167 return PyClass_New(bases, dict, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
168 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
169
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 /* Class methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 static void
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
173 class_dealloc(PyClassObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
175 _PyObject_GC_UNTRACK(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
176 Py_DECREF(op->cl_bases);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
177 Py_DECREF(op->cl_dict);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
178 Py_XDECREF(op->cl_name);
8806
8e9d2146ae93 Fix a memory leak -- the cached values of __getattr__ etc. were never
Guido van Rossum <guido@python.org>
parents: 8657
diff changeset
179 Py_XDECREF(op->cl_getattr);
8e9d2146ae93 Fix a memory leak -- the cached values of __getattr__ etc. were never
Guido van Rossum <guido@python.org>
parents: 8657
diff changeset
180 Py_XDECREF(op->cl_setattr);
8e9d2146ae93 Fix a memory leak -- the cached values of __getattr__ etc. were never
Guido van Rossum <guido@python.org>
parents: 8657
diff changeset
181 Py_XDECREF(op->cl_delattr);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
182 PyObject_GC_Del(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
185 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
186 class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass)
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
187 {
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
188 int i, n;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
189 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
190 if (value != NULL) {
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
191 *pclass = cp;
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
192 return value;
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
193 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
194 n = PyTuple_Size(cp->cl_bases);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
195 for (i = 0; i < n; i++) {
6309
904a4d783a33 When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents: 6155
diff changeset
196 /* XXX What if one of the bases is not a class? */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
197 PyObject *v = class_lookup(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
198 (PyClassObject *)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
199 PyTuple_GetItem(cp->cl_bases, i), name, pclass);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
200 if (v != NULL)
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
201 return v;
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
202 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
203 return NULL;
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
204 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
205
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
206 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
207 class_getattr(register PyClassObject *op, PyObject *name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
209 register PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
210 register char *sname = PyString_AsString(name);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
211 PyClassObject *class;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
212 descrgetfunc f;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
213
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
214 if (sname[0] == '_' && sname[1] == '_') {
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
215 if (strcmp(sname, "__dict__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
216 if (PyEval_GetRestricted()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
217 PyErr_SetString(PyExc_RuntimeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
218 "class.__dict__ not accessible in restricted mode");
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
219 return NULL;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
220 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
221 Py_INCREF(op->cl_dict);
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
222 return op->cl_dict;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
223 }
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
224 if (strcmp(sname, "__bases__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
225 Py_INCREF(op->cl_bases);
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
226 return op->cl_bases;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
227 }
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
228 if (strcmp(sname, "__name__") == 0) {
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
229 if (op->cl_name == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
230 v = Py_None;
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
231 else
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
232 v = op->cl_name;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
233 Py_INCREF(v);
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
234 return v;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
235 }
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
236 }
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
237 v = class_lookup(op, name, &class);
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
238 if (v == NULL) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
239 PyErr_Format(PyExc_AttributeError,
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
240 "class %.50s has no attribute '%.400s'",
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
241 PyString_AS_STRING(op->cl_name), sname);
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
242 return NULL;
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
243 }
20501
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
244 f = TP_DESCR_GET(v->ob_type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
245 if (f == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
246 Py_INCREF(v);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
247 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
248 v = f(v, (PyObject *)NULL, (PyObject *)op);
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
249 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
252 static void
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
253 set_slot(PyObject **slot, PyObject *v)
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
254 {
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
255 PyObject *temp = *slot;
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
256 Py_XINCREF(v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
257 *slot = v;
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
258 Py_XDECREF(temp);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
259 }
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
260
8649
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
261 static void
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
262 set_attr_slots(PyClassObject *c)
8649
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
263 {
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
264 PyClassObject *dummy;
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
265
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
266 set_slot(&c->cl_getattr, class_lookup(c, getattrstr, &dummy));
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
267 set_slot(&c->cl_setattr, class_lookup(c, setattrstr, &dummy));
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
268 set_slot(&c->cl_delattr, class_lookup(c, delattrstr, &dummy));
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
269 }
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
270
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
271 static char *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
272 set_dict(PyClassObject *c, PyObject *v)
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
273 {
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
274 if (v == NULL || !PyDict_Check(v))
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
275 return "__dict__ must be a dictionary object";
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
276 set_slot(&c->cl_dict, v);
8649
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
277 set_attr_slots(c);
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
278 return "";
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
279 }
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
280
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
281 static char *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
282 set_bases(PyClassObject *c, PyObject *v)
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
283 {
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
284 int i, n;
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
285
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
286 if (v == NULL || !PyTuple_Check(v))
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
287 return "__bases__ must be a tuple object";
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
288 n = PyTuple_Size(v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
289 for (i = 0; i < n; i++) {
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
290 PyObject *x = PyTuple_GET_ITEM(v, i);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
291 if (!PyClass_Check(x))
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
292 return "__bases__ items must be classes";
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
293 if (PyClass_IsSubclass(x, (PyObject *)c))
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
294 return "a __bases__ item causes an inheritance cycle";
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
295 }
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
296 set_slot(&c->cl_bases, v);
8649
9af95d7270df Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents: 8496
diff changeset
297 set_attr_slots(c);
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
298 return "";
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
299 }
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
300
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
301 static char *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
302 set_name(PyClassObject *c, PyObject *v)
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
303 {
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
304 if (v == NULL || !PyString_Check(v))
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
305 return "__name__ must be a string object";
13089
bf1ebe869878 Trent Mick: change a few casts for Win64 compatibility.
Guido van Rossum <guido@python.org>
parents: 12993
diff changeset
306 if (strlen(PyString_AS_STRING(v)) != (size_t)PyString_GET_SIZE(v))
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
307 return "__name__ must not contain null bytes";
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
308 set_slot(&c->cl_name, v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
309 return "";
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
310 }
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
311
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
312 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
313 class_setattr(PyClassObject *op, PyObject *name, PyObject *v)
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
314 {
6155
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
315 char *sname;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
316 if (PyEval_GetRestricted()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
317 PyErr_SetString(PyExc_RuntimeError,
2889
e513f0d65368 class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
318 "classes are read-only in restricted mode");
e513f0d65368 class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
319 return -1;
e513f0d65368 class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
320 }
6155
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
321 sname = PyString_AsString(name);
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
322 if (sname[0] == '_' && sname[1] == '_') {
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
323 int n = PyString_Size(name);
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
324 if (sname[n-1] == '_' && sname[n-2] == '_') {
8464
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
325 char *err = NULL;
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
326 if (strcmp(sname, "__dict__") == 0)
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
327 err = set_dict(op, v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
328 else if (strcmp(sname, "__bases__") == 0)
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
329 err = set_bases(op, v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
330 else if (strcmp(sname, "__name__") == 0)
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
331 err = set_name(op, v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
332 else if (strcmp(sname, "__getattr__") == 0)
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
333 set_slot(&op->cl_getattr, v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
334 else if (strcmp(sname, "__setattr__") == 0)
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
335 set_slot(&op->cl_setattr, v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
336 else if (strcmp(sname, "__delattr__") == 0)
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
337 set_slot(&op->cl_delattr, v);
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
338 /* For the last three, we fall through to update the
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
339 dictionary as well. */
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
340 if (err != NULL) {
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
341 if (*err == '\0')
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
342 return 0;
7160e529e9a1 Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents: 8438
diff changeset
343 PyErr_SetString(PyExc_TypeError, err);
6155
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
344 return -1;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
345 }
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
346 }
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
347 }
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
348 if (v == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
349 int rv = PyDict_DelItem(op->cl_dict, name);
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
350 if (rv < 0)
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
351 PyErr_Format(PyExc_AttributeError,
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
352 "class %.50s has no attribute '%.400s'",
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
353 PyString_AS_STRING(op->cl_name), sname);
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
354 return rv;
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
355 }
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
356 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
357 return PyDict_SetItem(op->cl_dict, name, v);
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
358 }
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
359
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
360 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
361 class_repr(PyClassObject *op)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
362 {
6502
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
363 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
364 char *name;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
365 if (op->cl_name == NULL || !PyString_Check(op->cl_name))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
366 name = "?";
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
367 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
368 name = PyString_AsString(op->cl_name);
6502
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
369 if (mod == NULL || !PyString_Check(mod))
19966
48689346e8bb Properly repr classes without module names.
Martin v. Löwis <martin@v.loewis.de>
parents: 19956
diff changeset
370 return PyString_FromFormat("<class ?.%s at %p>", name, op);
6502
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
371 else
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
372 return PyString_FromFormat("<class %s.%s at %p>",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
373 PyString_AsString(mod),
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
374 name, op);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
375 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
376
6502
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
377 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
378 class_str(PyClassObject *op)
6502
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
379 {
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
380 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
381 PyObject *name = op->cl_name;
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
382 PyObject *res;
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
383 int m, n;
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
384
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
385 if (name == NULL || !PyString_Check(name))
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
386 return class_repr(op);
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
387 if (mod == NULL || !PyString_Check(mod)) {
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
388 Py_INCREF(name);
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
389 return name;
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
390 }
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
391 m = PyString_Size(mod);
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
392 n = PyString_Size(name);
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
393 res = PyString_FromStringAndSize((char *)NULL, m+1+n);
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
394 if (res != NULL) {
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
395 char *s = PyString_AsString(res);
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
396 memcpy(s, PyString_AsString(mod), m);
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
397 s += m;
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
398 *s++ = '.';
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
399 memcpy(s, PyString_AsString(name), n);
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
400 }
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
401 return res;
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
402 }
2a2e5b731d1c Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents: 6430
diff changeset
403
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
404 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
405 class_traverse(PyClassObject *o, visitproc visit, void *arg)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
406 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
407 int err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
408 if (o->cl_bases) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
409 err = visit(o->cl_bases, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
410 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
411 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
412 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
413 if (o->cl_dict) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
414 err = visit(o->cl_dict, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
415 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
416 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
417 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
418 if (o->cl_name) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
419 err = visit(o->cl_name, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
420 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
421 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
422 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
423 if (o->cl_getattr) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
424 err = visit(o->cl_getattr, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
425 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
426 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
427 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
428 if (o->cl_setattr) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
429 err = visit(o->cl_setattr, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
430 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
431 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
432 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
433 if (o->cl_delattr) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
434 err = visit(o->cl_delattr, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
435 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
436 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
437 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
438 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
439 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
440
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
441 PyTypeObject PyClass_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
442 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 0,
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
444 "classobj",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
445 sizeof(PyClassObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 0,
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
447 (destructor)class_dealloc, /* tp_dealloc */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
448 0, /* tp_print */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
449 0, /* tp_getattr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
450 0, /* tp_setattr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
451 0, /* tp_compare */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
452 (reprfunc)class_repr, /* tp_repr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
453 0, /* tp_as_number */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
454 0, /* tp_as_sequence */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
455 0, /* tp_as_mapping */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
456 0, /* tp_hash */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
457 PyInstance_New, /* tp_call */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
458 (reprfunc)class_str, /* tp_str */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
459 (getattrofunc)class_getattr, /* tp_getattro */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
460 (setattrofunc)class_setattr, /* tp_setattro */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
461 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
462 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
463 class_doc, /* tp_doc */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
464 (traverseproc)class_traverse, /* tp_traverse */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
465 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
466 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
467 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
468 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
469 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
470 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
471 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
472 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
473 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
474 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
475 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
476 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
477 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
478 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
479 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
480 class_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
483 int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
484 PyClass_IsSubclass(PyObject *class, PyObject *base)
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
485 {
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
486 int i, n;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
487 PyClassObject *cp;
1293
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1290
diff changeset
488 if (class == base)
ac3c80abbf43 * classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents: 1290
diff changeset
489 return 1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
490 if (class == NULL || !PyClass_Check(class))
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
491 return 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
492 cp = (PyClassObject *)class;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
493 n = PyTuple_Size(cp->cl_bases);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
494 for (i = 0; i < n; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
495 if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
496 return 1;
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
497 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
498 return 0;
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
499 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
500
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
501
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
502 /* Instance objects */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
503
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
504 PyObject *
16314
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
505 PyInstance_NewRaw(PyObject *klass, PyObject *dict)
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
506 {
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
507 PyInstanceObject *inst;
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
508
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
509 if (!PyClass_Check(klass)) {
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
510 PyErr_BadInternalCall();
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
511 return NULL;
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
512 }
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
513 if (dict == NULL) {
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
514 dict = PyDict_New();
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
515 if (dict == NULL)
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
516 return NULL;
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
517 }
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
518 else {
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
519 if (!PyDict_Check(dict)) {
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
520 PyErr_BadInternalCall();
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
521 return NULL;
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
522 }
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
523 Py_INCREF(dict);
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
524 }
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
525 inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type);
16314
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
526 if (inst == NULL) {
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
527 Py_DECREF(dict);
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
528 return NULL;
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
529 }
17154
3b8a5bbfd2e5 A small change to the C API for weakly-referencable types: Such types
Fred Drake <fdrake@acm.org>
parents: 16768
diff changeset
530 inst->in_weakreflist = NULL;
16314
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
531 Py_INCREF(klass);
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
532 inst->in_class = (PyClassObject *)klass;
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
533 inst->in_dict = dict;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
534 _PyObject_GC_TRACK(inst);
16314
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
535 return (PyObject *)inst;
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
536 }
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
537
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
538 PyObject *
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
539 PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
541 register PyInstanceObject *inst;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
542 PyObject *init;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
543 static PyObject *initstr;
16314
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
544
24e457ba1f92 Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents: 16004
diff changeset
545 inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
546 if (inst == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 return NULL;
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
548 if (initstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
549 initstr = PyString_InternFromString("__init__");
12504
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
550 init = instance_getattr2(inst, initstr);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
551 if (init == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
552 if ((arg != NULL && (!PyTuple_Check(arg) ||
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
553 PyTuple_Size(arg) != 0))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
554 || (kw != NULL && (!PyDict_Check(kw) ||
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
555 PyDict_Size(kw) != 0))) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
556 PyErr_SetString(PyExc_TypeError,
2864
5f951912e995 changes for keyword args to built-in functions and classes
Guido van Rossum <guido@python.org>
parents: 2827
diff changeset
557 "this constructor takes no arguments");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
558 Py_DECREF(inst);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
559 inst = NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
560 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
561 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
562 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
563 PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
564 Py_DECREF(init);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
565 if (res == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
566 Py_DECREF(inst);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
567 inst = NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
568 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
569 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
570 if (res != Py_None) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
571 PyErr_SetString(PyExc_TypeError,
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
572 "__init__() should return None");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
573 Py_DECREF(inst);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
574 inst = NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
575 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
576 Py_DECREF(res);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
577 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
578 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
579 return (PyObject *)inst;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
581
174
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
582 /* Instance methods */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
583
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
584 PyDoc_STRVAR(instance_doc,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
585 "instance(class[, dict])\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
586 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
587 Create an instance without calling its __init__() method.\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
588 The class must be a classic class.\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
589 If present, dict must be a dictionary or None.");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
590
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
591 static PyObject *
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
592 instance_new(PyTypeObject* type, PyObject* args, PyObject *kw)
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
593 {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
594 PyObject *klass;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
595 PyObject *dict = Py_None;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
596
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
597 if (!PyArg_ParseTuple(args, "O!|O:instance",
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
598 &PyClass_Type, &klass, &dict))
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
599 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
600
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
601 if (dict == Py_None)
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
602 dict = NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
603 else if (!PyDict_Check(dict)) {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
604 PyErr_SetString(PyExc_TypeError,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
605 "instance() second arg must be dictionary or None");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
606 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
607 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
608 return PyInstance_NewRaw(klass, dict);
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
609 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
610
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
611
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 static void
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
613 instance_dealloc(register PyInstanceObject *inst)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
615 PyObject *error_type, *error_value, *error_traceback;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
616 PyObject *del;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
617 static PyObject *delstr;
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
618 #ifdef Py_REF_DEBUG
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13141
diff changeset
619 extern long _Py_RefTotal;
13430
088c5f3b7094 _Py_RefTotal should only be declared here when Py_TRACE_REFS are #define'd
Skip Montanaro <skip@pobox.com>
parents: 13265
diff changeset
620 #endif
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
621 _PyObject_GC_UNTRACK(inst);
20698
b229208a2a72 Be smarter about clearing the weakref lists for instances, instance methods,
Fred Drake <fdrake@acm.org>
parents: 20601
diff changeset
622 if (inst->in_weakreflist != NULL)
b229208a2a72 Be smarter about clearing the weakref lists for instances, instance methods,
Fred Drake <fdrake@acm.org>
parents: 20601
diff changeset
623 PyObject_ClearWeakRefs((PyObject *) inst);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents: 16356
diff changeset
624
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
625 /* Temporarily resurrect the object. */
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 3549
diff changeset
626 #ifdef Py_TRACE_REFS
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
627 #ifndef Py_REF_DEBUG
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
628 # error "Py_TRACE_REFS defined but Py_REF_DEBUG not."
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
629 #endif
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 3549
diff changeset
630 /* much too complicated if Py_TRACE_REFS defined */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
631 inst->ob_type = &PyInstance_Type;
11673
5e6e9e893acd The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents: 11378
diff changeset
632 _Py_NewReference((PyObject *)inst);
3022
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
633 #ifdef COUNT_ALLOCS
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
634 /* compensate for boost in _Py_NewReference; note that
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
635 * _Py_RefTotal was also boosted; we'll knock that down later.
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
636 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
637 inst->ob_type->tp_allocs--;
3022
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
638 #endif
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 3549
diff changeset
639 #else /* !Py_TRACE_REFS */
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
640 /* Py_INCREF boosts _Py_RefTotal if Py_REF_DEBUG is defined */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
641 Py_INCREF(inst);
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 3549
diff changeset
642 #endif /* !Py_TRACE_REFS */
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
643
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
644 /* Save the current exception, if any. */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
645 PyErr_Fetch(&error_type, &error_value, &error_traceback);
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
646 /* Execute __del__ method, if any. */
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
647 if (delstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
648 delstr = PyString_InternFromString("__del__");
12504
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
649 if ((del = instance_getattr2(inst, delstr)) != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
650 PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
651 if (res == NULL)
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14180
diff changeset
652 PyErr_WriteUnraisable(del);
4003
138fa15a1032 Disable support for access statement
Guido van Rossum <guido@python.org>
parents: 3987
diff changeset
653 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
654 Py_DECREF(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
655 Py_DECREF(del);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
656 }
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
657 /* Restore the saved exception. */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
658 PyErr_Restore(error_type, error_value, error_traceback);
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
659 /* Undo the temporary resurrection; can't use DECREF here, it would
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
660 * cause a recursive call.
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
661 */
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
662 #ifdef Py_REF_DEBUG
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
663 /* _Py_RefTotal was boosted either by _Py_NewReference or
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
664 * Py_INCREF above.
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
665 */
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
666 _Py_RefTotal--;
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
667 #endif
3022
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
668 if (--inst->ob_refcnt > 0) {
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
669 #ifdef COUNT_ALLOCS
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
670 inst->ob_type->tp_frees--;
3022
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
671 #endif
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
672 _PyObject_GC_TRACK(inst);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
673 return; /* __del__ added a reference; don't delete now */
3022
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
674 }
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 3549
diff changeset
675 #ifdef Py_TRACE_REFS
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
676 _Py_ForgetReference((PyObject *)inst);
3022
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
677 #ifdef COUNT_ALLOCS
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
678 /* compensate for increment in _Py_ForgetReference */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
679 inst->ob_type->tp_frees--;
3022
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
680 #endif
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13141
diff changeset
681 #ifndef WITH_CYCLE_GC
3022
c5d852735e7f Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents: 2889
diff changeset
682 inst->ob_type = NULL;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13141
diff changeset
683 #endif
14653
3d6fe64bcd21 Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents: 14605
diff changeset
684 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
685 Py_DECREF(inst->in_class);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
686 Py_XDECREF(inst->in_dict);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
687 PyObject_GC_Del(inst);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
689
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
690 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
691 instance_getattr1(register PyInstanceObject *inst, PyObject *name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
693 register PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
694 register char *sname = PyString_AsString(name);
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
695 if (sname[0] == '_' && sname[1] == '_') {
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
696 if (strcmp(sname, "__dict__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
697 if (PyEval_GetRestricted()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
698 PyErr_SetString(PyExc_RuntimeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
699 "instance.__dict__ not accessible in restricted mode");
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
700 return NULL;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
701 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
702 Py_INCREF(inst->in_dict);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1351
diff changeset
703 return inst->in_dict;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1351
diff changeset
704 }
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
705 if (strcmp(sname, "__class__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
706 Py_INCREF(inst->in_class);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
707 return (PyObject *)inst->in_class;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1351
diff changeset
708 }
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
709 }
12504
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
710 v = instance_getattr2(inst, name);
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
711 if (v == NULL) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
712 PyErr_Format(PyExc_AttributeError,
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
713 "%.50s instance has no attribute '%.400s'",
12504
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
714 PyString_AS_STRING(inst->in_class->cl_name), sname);
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
715 }
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
716 return v;
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
717 }
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
718
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
719 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
720 instance_getattr2(register PyInstanceObject *inst, PyObject *name)
12504
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
721 {
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
722 register PyObject *v;
44e521a93648 potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12321
diff changeset
723 PyClassObject *class;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
724 descrgetfunc f;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
725
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
726 v = PyDict_GetItem(inst->in_dict, name);
19334
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
727 if (v != NULL) {
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
728 Py_INCREF(v);
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
729 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 }
19334
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
731 v = class_lookup(inst->in_class, name, &class);
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
732 if (v != NULL) {
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
733 Py_INCREF(v);
20501
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
734 f = TP_DESCR_GET(v->ob_type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
735 if (f != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
736 PyObject *w = f(v, (PyObject *)inst,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
737 (PyObject *)(inst->in_class));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
738 Py_DECREF(v);
1351
e149c77d173c Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
739 v = w;
e149c77d173c Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
740 }
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
741 }
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
742 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
744
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
745 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
746 instance_getattr(register PyInstanceObject *inst, PyObject *name)
1860
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
747 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
748 register PyObject *func, *res;
1861
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
749 res = instance_getattr1(inst, name);
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
750 if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
751 PyObject *args;
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
752 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
753 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
754 PyErr_Clear();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
755 args = Py_BuildValue("(OO)", inst, name);
1861
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
756 if (args == NULL)
1860
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
757 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
758 res = PyEval_CallObject(func, args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
759 Py_DECREF(args);
1860
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
760 }
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
761 return res;
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
762 }
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
763
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
765 instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 {
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
767 if (v == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
768 int rv = PyDict_DelItem(inst->in_dict, name);
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
769 if (rv < 0)
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
770 PyErr_Format(PyExc_AttributeError,
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
771 "%.50s instance has no attribute '%.400s'",
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
772 PyString_AS_STRING(inst->in_class->cl_name),
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15010
diff changeset
773 PyString_AS_STRING(name));
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
774 return rv;
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
775 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
777 return PyDict_SetItem(inst->in_dict, name, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779
1860
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
780 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
781 instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v)
1860
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
782 {
6155
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
783 PyObject *func, *args, *res, *tmp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
784 char *sname = PyString_AsString(name);
6155
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
785 if (sname[0] == '_' && sname[1] == '_') {
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
786 int n = PyString_Size(name);
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
787 if (sname[n-1] == '_' && sname[n-2] == '_') {
6155
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
788 if (strcmp(sname, "__dict__") == 0) {
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
789 if (PyEval_GetRestricted()) {
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
790 PyErr_SetString(PyExc_RuntimeError,
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
791 "__dict__ not accessible in restricted mode");
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
792 return -1;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
793 }
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
794 if (v == NULL || !PyDict_Check(v)) {
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
795 PyErr_SetString(PyExc_TypeError,
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
796 "__dict__ must be set to a dictionary");
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
797 return -1;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
798 }
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
799 tmp = inst->in_dict;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
800 Py_INCREF(v);
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
801 inst->in_dict = v;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
802 Py_DECREF(tmp);
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
803 return 0;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
804 }
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
805 if (strcmp(sname, "__class__") == 0) {
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
806 if (PyEval_GetRestricted()) {
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
807 PyErr_SetString(PyExc_RuntimeError,
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
808 "__class__ not accessible in restricted mode");
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
809 return -1;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
810 }
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
811 if (v == NULL || !PyClass_Check(v)) {
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
812 PyErr_SetString(PyExc_TypeError,
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
813 "__class__ must be set to a class");
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
814 return -1;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
815 }
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
816 tmp = (PyObject *)(inst->in_class);
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
817 Py_INCREF(v);
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
818 inst->in_class = (PyClassObject *)v;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
819 Py_DECREF(tmp);
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
820 return 0;
47867006f076 Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents: 5932
diff changeset
821 }
1860
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
822 }
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
823 }
1861
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
824 if (v == NULL)
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
825 func = inst->in_class->cl_delattr;
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
826 else
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
827 func = inst->in_class->cl_setattr;
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
828 if (func == NULL)
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
829 return instance_setattr1(inst, name, v);
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
830 if (v == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
831 args = Py_BuildValue("(OO)", inst, name);
1861
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
832 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
833 args = Py_BuildValue("(OOO)", inst, name, v);
1861
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
834 if (args == NULL)
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
835 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
836 res = PyEval_CallObject(func, args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
837 Py_DECREF(args);
1861
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
838 if (res == NULL)
d405e3e8ffd3 Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1860
diff changeset
839 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
840 Py_DECREF(res);
1860
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
841 return 0;
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
842 }
78898b04a65b Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents: 1781
diff changeset
843
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
844 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
845 instance_repr(PyInstanceObject *inst)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
846 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
847 PyObject *func;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
848 PyObject *res;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
849 static PyObject *reprstr;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
850
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
851 if (reprstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
852 reprstr = PyString_InternFromString("__repr__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
853 func = instance_getattr(inst, reprstr);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
854 if (func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
855 PyObject *classname, *mod;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
856 char *cname;
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
857 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
858 return NULL;
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
859 PyErr_Clear();
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
860 classname = inst->in_class->cl_name;
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
861 mod = PyDict_GetItemString(inst->in_class->cl_dict,
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
862 "__module__");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
863 if (classname != NULL && PyString_Check(classname))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
864 cname = PyString_AsString(classname);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
865 else
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
866 cname = "?";
6727
2662c9fe1b3e Change the default repr() and str() of class instance objects to look
Guido van Rossum <guido@python.org>
parents: 6595
diff changeset
867 if (mod == NULL || !PyString_Check(mod))
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
868 return PyString_FromFormat("<?.%s instance at %p>",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
869 cname, inst);
6727
2662c9fe1b3e Change the default repr() and str() of class instance objects to look
Guido van Rossum <guido@python.org>
parents: 6595
diff changeset
870 else
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
871 return PyString_FromFormat("<%s.%s instance at %p>",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
872 PyString_AsString(mod),
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
873 cname, inst);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
874 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
875 res = PyEval_CallObject(func, (PyObject *)NULL);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
876 Py_DECREF(func);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
877 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
878 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
879
17643
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
880 static PyObject *
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
881 instance_str(PyInstanceObject *inst)
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
882 {
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
883 PyObject *func;
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
884 PyObject *res;
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
885 static PyObject *strstr;
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
886
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
887 if (strstr == NULL)
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
888 strstr = PyString_InternFromString("__str__");
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
889 func = instance_getattr(inst, strstr);
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
890 if (func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
891 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
892 return NULL;
17643
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
893 PyErr_Clear();
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
894 return instance_repr(inst);
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
895 }
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
896 res = PyEval_CallObject(func, (PyObject *)NULL);
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
897 Py_DECREF(func);
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
898 return res;
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
899 }
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
900
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
901 static long
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
902 instance_hash(PyInstanceObject *inst)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
903 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
904 PyObject *func;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
905 PyObject *res;
1272
7b7411f9b116 Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
906 long outcome;
16004
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
907 static PyObject *hashstr, *eqstr, *cmpstr;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
908
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
909 if (hashstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
910 hashstr = PyString_InternFromString("__hash__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
911 func = instance_getattr(inst, hashstr);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
912 if (func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
913 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
914 return -1;
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
915 PyErr_Clear();
16004
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
916 /* If there is no __eq__ and no __cmp__ method, we hash on the
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
917 address. If an __eq__ or __cmp__ method exists, there must
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
918 be a __hash__. */
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
919 if (eqstr == NULL)
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
920 eqstr = PyString_InternFromString("__eq__");
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
921 func = instance_getattr(inst, eqstr);
1272
7b7411f9b116 Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
922 if (func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
923 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
924 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
925 PyErr_Clear();
16004
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
926 if (cmpstr == NULL)
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
927 cmpstr = PyString_InternFromString("__cmp__");
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
928 func = instance_getattr(inst, cmpstr);
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
929 if (func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
930 if (!PyErr_ExceptionMatches(
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
931 PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
932 return -1;
16004
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
933 PyErr_Clear();
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
934 return _Py_HashPointer(inst);
9251cf70ed1f Rich comparisons fallout: instance_hash() should check for both
Guido van Rossum <guido@python.org>
parents: 15972
diff changeset
935 }
1272
7b7411f9b116 Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
936 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
937 PyErr_SetString(PyExc_TypeError, "unhashable instance");
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
938 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
939 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
940 res = PyEval_CallObject(func, (PyObject *)NULL);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
941 Py_DECREF(func);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
942 if (res == NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
943 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
944 if (PyInt_Check(res)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
945 outcome = PyInt_AsLong(res);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
946 if (outcome == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
947 outcome = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
948 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
949 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
950 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
951 "__hash__() should return an int");
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
952 outcome = -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
953 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
954 Py_DECREF(res);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
955 return outcome;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
956 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
957
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
958 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
959 instance_traverse(PyInstanceObject *o, visitproc visit, void *arg)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
960 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
961 int err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
962 if (o->in_class) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
963 err = visit((PyObject *)(o->in_class), arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
964 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
965 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
966 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
967 if (o->in_dict) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
968 err = visit(o->in_dict, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
969 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
970 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
971 }
12992
3f990ef66667 traverse functions should return 0 on success
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12990
diff changeset
972 return 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
973 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
974
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
975 static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
976 static PyObject *iterstr, *nextstr;
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
977
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
978 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
979 instance_length(PyInstanceObject *inst)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
980 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
981 PyObject *func;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
982 PyObject *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
983 int outcome;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
984
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
985 if (lenstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
986 lenstr = PyString_InternFromString("__len__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
987 func = instance_getattr(inst, lenstr);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
988 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
989 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
990 res = PyEval_CallObject(func, (PyObject *)NULL);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
991 Py_DECREF(func);
1096
ce718457b172 * classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
992 if (res == NULL)
ce718457b172 * classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
993 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
994 if (PyInt_Check(res)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
995 outcome = PyInt_AsLong(res);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
996 if (outcome < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
997 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
998 "__len__() should return >= 0");
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
999 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1000 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1001 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1002 "__len__() should return an int");
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1003 outcome = -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1004 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1005 Py_DECREF(res);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1006 return outcome;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1007 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1008
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1009 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1010 instance_subscript(PyInstanceObject *inst, PyObject *key)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1011 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1012 PyObject *func;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1013 PyObject *arg;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1014 PyObject *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1015
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1016 if (getitemstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1017 getitemstr = PyString_InternFromString("__getitem__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1018 func = instance_getattr(inst, getitemstr);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1019 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1020 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1021 arg = Py_BuildValue("(O)", key);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1022 if (arg == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1023 Py_DECREF(func);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1024 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1025 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1026 res = PyEval_CallObject(func, arg);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1027 Py_DECREF(func);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1028 Py_DECREF(arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1029 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1030 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1031
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1032 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1033 instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1034 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1035 PyObject *func;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1036 PyObject *arg;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1037 PyObject *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1038
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1039 if (value == NULL) {
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1040 if (delitemstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1041 delitemstr = PyString_InternFromString("__delitem__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1042 func = instance_getattr(inst, delitemstr);
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1043 }
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1044 else {
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1045 if (setitemstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1046 setitemstr = PyString_InternFromString("__setitem__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1047 func = instance_getattr(inst, setitemstr);
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1048 }
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1049 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1050 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1051 if (value == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1052 arg = Py_BuildValue("(O)", key);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1053 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1054 arg = Py_BuildValue("(OO)", key, value);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1055 if (arg == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1056 Py_DECREF(func);
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1057 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1058 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1059 res = PyEval_CallObject(func, arg);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1060 Py_DECREF(func);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1061 Py_DECREF(arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1062 if (res == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1063 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1064 Py_DECREF(res);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1065 return 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1066 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1067
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1068 static PyMappingMethods instance_as_mapping = {
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1069 (inquiry)instance_length, /* mp_length */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1070 (binaryfunc)instance_subscript, /* mp_subscript */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1071 (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1072 };
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1073
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1074 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1075 instance_item(PyInstanceObject *inst, int i)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1076 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1077 PyObject *func, *arg, *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1078
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1079 if (getitemstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1080 getitemstr = PyString_InternFromString("__getitem__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1081 func = instance_getattr(inst, getitemstr);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1082 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1083 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1084 arg = Py_BuildValue("(i)", i);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1085 if (arg == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1086 Py_DECREF(func);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1087 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1088 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1089 res = PyEval_CallObject(func, arg);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1090 Py_DECREF(func);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1091 Py_DECREF(arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1092 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1093 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1094
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1095 static PyObject *
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1096 sliceobj_from_intint(int i, int j)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1097 {
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1098 PyObject *start, *end, *res;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1099
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1100 start = PyInt_FromLong((long)i);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1101 if (!start)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1102 return NULL;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1103
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1104 end = PyInt_FromLong((long)j);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1105 if (!end) {
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1106 Py_DECREF(start);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1107 return NULL;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1108 }
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1109 res = PySlice_New(start, end, NULL);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1110 Py_DECREF(start);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1111 Py_DECREF(end);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1112 return res;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1113 }
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1114
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1115
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1116 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1117 instance_slice(PyInstanceObject *inst, int i, int j)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1118 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1119 PyObject *func, *arg, *res;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1120 static PyObject *getslicestr;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1121
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1122 if (getslicestr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1123 getslicestr = PyString_InternFromString("__getslice__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1124 func = instance_getattr(inst, getslicestr);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1125
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1126 if (func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1127 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1128 return NULL;
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1129 PyErr_Clear();
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1130
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1131 if (getitemstr == NULL)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1132 getitemstr = PyString_InternFromString("__getitem__");
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1133 func = instance_getattr(inst, getitemstr);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1134 if (func == NULL)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1135 return NULL;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1136 arg = Py_BuildValue("(N)", sliceobj_from_intint(i, j));
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1137 } else
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1138 arg = Py_BuildValue("(ii)", i, j);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1139
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1140 if (arg == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1141 Py_DECREF(func);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1142 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1143 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1144 res = PyEval_CallObject(func, arg);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1145 Py_DECREF(func);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1146 Py_DECREF(arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1147 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1148 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1149
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1150 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1151 instance_ass_item(PyInstanceObject *inst, int i, PyObject *item)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1152 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1153 PyObject *func, *arg, *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1154
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1155 if (item == NULL) {
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1156 if (delitemstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1157 delitemstr = PyString_InternFromString("__delitem__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1158 func = instance_getattr(inst, delitemstr);
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1159 }
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1160 else {
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1161 if (setitemstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1162 setitemstr = PyString_InternFromString("__setitem__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1163 func = instance_getattr(inst, setitemstr);
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1164 }
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1165 if (func == NULL)
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1166 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1167 if (item == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1168 arg = Py_BuildValue("i", i);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1169 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1170 arg = Py_BuildValue("(iO)", i, item);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1171 if (arg == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1172 Py_DECREF(func);
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1173 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1174 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1175 res = PyEval_CallObject(func, arg);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1176 Py_DECREF(func);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1177 Py_DECREF(arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1178 if (res == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1179 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1180 Py_DECREF(res);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1181 return 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1182 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1183
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1184 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1185 instance_ass_slice(PyInstanceObject *inst, int i, int j, PyObject *value)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1186 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1187 PyObject *func, *arg, *res;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1188 static PyObject *setslicestr, *delslicestr;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1189
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1190 if (value == NULL) {
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1191 if (delslicestr == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1192 delslicestr =
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1193 PyString_InternFromString("__delslice__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1194 func = instance_getattr(inst, delslicestr);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1195 if (func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1196 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1197 return -1;
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1198 PyErr_Clear();
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1199 if (delitemstr == NULL)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1200 delitemstr =
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1201 PyString_InternFromString("__delitem__");
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1202 func = instance_getattr(inst, delitemstr);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1203 if (func == NULL)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1204 return -1;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1205
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1206 arg = Py_BuildValue("(N)",
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1207 sliceobj_from_intint(i, j));
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1208 } else
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1209 arg = Py_BuildValue("(ii)", i, j);
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1210 }
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1211 else {
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1212 if (setslicestr == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1213 setslicestr =
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1214 PyString_InternFromString("__setslice__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1215 func = instance_getattr(inst, setslicestr);
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1216 if (func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1217 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1218 return -1;
14037
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1219 PyErr_Clear();
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1220 if (setitemstr == NULL)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1221 setitemstr =
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1222 PyString_InternFromString("__setitem__");
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1223 func = instance_getattr(inst, setitemstr);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1224 if (func == NULL)
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1225 return -1;
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1226
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1227 arg = Py_BuildValue("(NO)",
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1228 sliceobj_from_intint(i, j), value);
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1229 } else
91f847a783d2 Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents: 13678
diff changeset
1230 arg = Py_BuildValue("(iiO)", i, j, value);
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1231 }
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1232 if (arg == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1233 Py_DECREF(func);
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1234 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1235 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1236 res = PyEval_CallObject(func, arg);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1237 Py_DECREF(func);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1238 Py_DECREF(arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1239 if (res == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1240 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1241 Py_DECREF(res);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1242 return 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1243 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1244
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1245 static int
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1246 instance_contains(PyInstanceObject *inst, PyObject *member)
11869
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1247 {
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1248 static PyObject *__contains__;
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1249 PyObject *func;
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1250
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1251 /* Try __contains__ first.
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1252 * If that can't be done, try iterator-based searching.
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1253 */
11869
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1254
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1255 if(__contains__ == NULL) {
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1256 __contains__ = PyString_InternFromString("__contains__");
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1257 if(__contains__ == NULL)
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1258 return -1;
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1259 }
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1260 func = instance_getattr(inst, __contains__);
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1261 if (func) {
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1262 PyObject *res;
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1263 int ret;
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1264 PyObject *arg = Py_BuildValue("(O)", member);
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1265 if(arg == NULL) {
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1266 Py_DECREF(func);
11869
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1267 return -1;
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1268 }
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1269 res = PyEval_CallObject(func, arg);
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1270 Py_DECREF(func);
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1271 Py_DECREF(arg);
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1272 if(res == NULL)
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1273 return -1;
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1274 ret = PyObject_IsTrue(res);
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1275 Py_DECREF(res);
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1276 return ret;
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1277 }
11869
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1278
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1279 /* Couldn't find __contains__. */
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1280 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1281 /* Assume the failure was simply due to that there is no
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1282 * __contains__ attribute, and try iterating instead.
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1283 */
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1284 PyErr_Clear();
19808
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19805
diff changeset
1285 return _PySequence_IterSearch((PyObject *)inst, member,
0584c9132525 Generalize operator.indexOf (PySequence_Index) to work with any
Tim Peters <tim.peters@gmail.com>
parents: 19805
diff changeset
1286 PY_ITERSEARCH_CONTAINS);
11869
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1287 }
17692
1812476b7fe4 Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents: 17669
diff changeset
1288 else
11869
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1289 return -1;
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1290 }
32c59e2889c4 Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1291
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1292 static PySequenceMethods
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1293 instance_as_sequence = {
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1294 (inquiry)instance_length, /* sq_length */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1295 0, /* sq_concat */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1296 0, /* sq_repeat */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1297 (intargfunc)instance_item, /* sq_item */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1298 (intintargfunc)instance_slice, /* sq_slice */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1299 (intobjargproc)instance_ass_item, /* sq_ass_item */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1300 (intintobjargproc)instance_ass_slice, /* sq_ass_slice */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1301 (objobjproc)instance_contains, /* sq_contains */
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1302 };
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1303
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1304 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1305 generic_unary_op(PyInstanceObject *self, PyObject *methodname)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1306 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1307 PyObject *func, *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1308
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1309 if ((func = instance_getattr(self, methodname)) == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1310 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1311 res = PyEval_CallObject(func, (PyObject *)NULL);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1312 Py_DECREF(func);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1313 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1314 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1315
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1316 static PyObject *
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1317 generic_binary_op(PyObject *v, PyObject *w, char *opname)
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1318 {
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1319 PyObject *result;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1320 PyObject *args;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1321 PyObject *func = PyObject_GetAttrString(v, opname);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1322 if (func == NULL) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1323 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1324 return NULL;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1325 PyErr_Clear();
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1326 Py_INCREF(Py_NotImplemented);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1327 return Py_NotImplemented;
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
1328 }
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1329 args = Py_BuildValue("(O)", w);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1330 if (args == NULL) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1331 Py_DECREF(func);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1332 return NULL;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1333 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1334 result = PyEval_CallObject(func, args);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1335 Py_DECREF(args);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1336 Py_DECREF(func);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1337 return result;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1338 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1339
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1340
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1341 static PyObject *coerce_obj;
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1342
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1343 /* Try one half of a binary operator involving a class instance. */
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1344 static PyObject *
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1345 half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1346 int swapped)
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1347 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1348 PyObject *args;
6595
5e0b152bf1e6 Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents: 6502
diff changeset
1349 PyObject *coercefunc;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1350 PyObject *coerced = NULL;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1351 PyObject *v1;
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1352 PyObject *result;
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1353
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1354 if (!PyInstance_Check(v)) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1355 Py_INCREF(Py_NotImplemented);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1356 return Py_NotImplemented;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1357 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1358
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1359 if (coerce_obj == NULL) {
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1360 coerce_obj = PyString_InternFromString("__coerce__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1361 if (coerce_obj == NULL)
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1362 return NULL;
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1363 }
6595
5e0b152bf1e6 Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents: 6502
diff changeset
1364 coercefunc = PyObject_GetAttr(v, coerce_obj);
5e0b152bf1e6 Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents: 6502
diff changeset
1365 if (coercefunc == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1366 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1367 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1368 PyErr_Clear();
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1369 return generic_binary_op(v, w, opname);
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1370 }
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1371
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1372 args = Py_BuildValue("(O)", w);
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1373 if (args == NULL) {
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1374 return NULL;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1375 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1376 coerced = PyEval_CallObject(coercefunc, args);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1377 Py_DECREF(args);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1378 Py_DECREF(coercefunc);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1379 if (coerced == NULL) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1380 return NULL;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1381 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1382 if (coerced == Py_None || coerced == Py_NotImplemented) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1383 Py_DECREF(coerced);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1384 return generic_binary_op(v, w, opname);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1385 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1386 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1387 Py_DECREF(coerced);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1388 PyErr_SetString(PyExc_TypeError,
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1389 "coercion should return None or 2-tuple");
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1390 return NULL;
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1391 }
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1392 v1 = PyTuple_GetItem(coerced, 0);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1393 w = PyTuple_GetItem(coerced, 1);
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1394 if (v1->ob_type == v->ob_type && PyInstance_Check(v)) {
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1395 /* prevent recursion if __coerce__ returns self as the first
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1396 * argument */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1397 result = generic_binary_op(v1, w, opname);
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1398 } else {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1399 if (swapped)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1400 result = (thisfunc)(w, v1);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1401 else
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1402 result = (thisfunc)(v1, w);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1403 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1404 Py_DECREF(coerced);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1405 return result;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1406 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1407
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1408 /* Implement a binary operator involving at least one class instance. */
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1409 static PyObject *
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1410 do_binop(PyObject *v, PyObject *w, char *opname, char *ropname,
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1411 binaryfunc thisfunc)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1412 {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1413 PyObject *result = half_binop(v, w, opname, thisfunc, 0);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1414 if (result == Py_NotImplemented) {
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1415 Py_DECREF(result);
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1416 result = half_binop(w, v, ropname, thisfunc, 1);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1417 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1418 return result;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1419 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1420
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1421 static PyObject *
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1422 do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname,
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1423 char *ropname, binaryfunc thisfunc)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1424 {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1425 PyObject *result = half_binop(v, w, iopname, thisfunc, 0);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1426 if (result == Py_NotImplemented) {
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1427 Py_DECREF(result);
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1428 result = do_binop(v, w, opname, ropname, thisfunc);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1429 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1430 return result;
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1431 }
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1432
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1433 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1434 instance_coerce(PyObject **pv, PyObject **pw)
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1435 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1436 PyObject *v = *pv;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1437 PyObject *w = *pw;
6595
5e0b152bf1e6 Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents: 6502
diff changeset
1438 PyObject *coercefunc;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1439 PyObject *args;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1440 PyObject *coerced;
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1441
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1442 if (coerce_obj == NULL) {
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1443 coerce_obj = PyString_InternFromString("__coerce__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1444 if (coerce_obj == NULL)
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1445 return -1;
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1446 }
6595
5e0b152bf1e6 Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents: 6502
diff changeset
1447 coercefunc = PyObject_GetAttr(v, coerce_obj);
5e0b152bf1e6 Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents: 6502
diff changeset
1448 if (coercefunc == NULL) {
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1449 /* No __coerce__ method */
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1450 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1451 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1452 PyErr_Clear();
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1453 return 1;
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1454 }
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1455 /* Has __coerce__ method: call it */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1456 args = Py_BuildValue("(O)", w);
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1457 if (args == NULL) {
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1458 return -1;
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1459 }
6595
5e0b152bf1e6 Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents: 6502
diff changeset
1460 coerced = PyEval_CallObject(coercefunc, args);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1461 Py_DECREF(args);
6595
5e0b152bf1e6 Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents: 6502
diff changeset
1462 Py_DECREF(coercefunc);
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1463 if (coerced == NULL) {
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1464 /* __coerce__ call raised an exception */
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1465 return -1;
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1466 }
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1467 if (coerced == Py_None || coerced == Py_NotImplemented) {
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1468 /* __coerce__ says "I can't do it" */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1469 Py_DECREF(coerced);
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1470 return 1;
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1471 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1472 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1473 /* __coerce__ return value is malformed */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1474 Py_DECREF(coerced);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1475 PyErr_SetString(PyExc_TypeError,
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1476 "coercion should return None or 2-tuple");
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1477 return -1;
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1478 }
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1479 /* __coerce__ returned two new values */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1480 *pv = PyTuple_GetItem(coerced, 0);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1481 *pw = PyTuple_GetItem(coerced, 1);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1482 Py_INCREF(*pv);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1483 Py_INCREF(*pw);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1484 Py_DECREF(coerced);
2112
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1485 return 0;
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1486 }
2669ef14c205 implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
1487
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1488 #define UNARY(funcname, methodname) \
13678
3177e2829167 ANSIfy functions that were hiding inside a macro.
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1489 static PyObject *funcname(PyInstanceObject *self) { \
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1490 static PyObject *o; \
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1491 if (o == NULL) o = PyString_InternFromString(methodname); \
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1492 return generic_unary_op(self, o); \
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1493 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1494
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1495 #define BINARY(f, m, n) \
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1496 static PyObject *f(PyObject *v, PyObject *w) { \
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1497 return do_binop(v, w, "__" m "__", "__r" m "__", n); \
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1498 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1499
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1500 #define BINARY_INPLACE(f, m, n) \
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1501 static PyObject *f(PyObject *v, PyObject *w) { \
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1502 return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1503 "__r" m "__", n); \
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1504 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1505
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1506 UNARY(instance_neg, "__neg__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1507 UNARY(instance_pos, "__pos__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1508 UNARY(instance_abs, "__abs__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1509
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1510 BINARY(instance_or, "or", PyNumber_Or)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1511 BINARY(instance_and, "and", PyNumber_And)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1512 BINARY(instance_xor, "xor", PyNumber_Xor)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1513 BINARY(instance_lshift, "lshift", PyNumber_Lshift)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1514 BINARY(instance_rshift, "rshift", PyNumber_Rshift)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1515 BINARY(instance_add, "add", PyNumber_Add)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1516 BINARY(instance_sub, "sub", PyNumber_Subtract)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1517 BINARY(instance_mul, "mul", PyNumber_Multiply)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1518 BINARY(instance_div, "div", PyNumber_Divide)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1519 BINARY(instance_mod, "mod", PyNumber_Remainder)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1520 BINARY(instance_divmod, "divmod", PyNumber_Divmod)
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18934
diff changeset
1521 BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18934
diff changeset
1522 BINARY(instance_truediv, "truediv", PyNumber_TrueDivide)
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1523
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1524 BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1525 BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1526 BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1527 BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1528 BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1529 BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1530 BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1531 BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1532 BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1533 BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder)
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18934
diff changeset
1534 BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18934
diff changeset
1535 BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide)
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1536
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1537 /* Try a 3-way comparison, returning an int; v is an instance. Return:
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1538 -2 for an exception;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1539 -1 if v < w;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1540 0 if v == w;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1541 1 if v > w;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1542 2 if this particular 3-way comparison is not implemented or undefined.
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1543 */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1544 static int
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1545 half_cmp(PyObject *v, PyObject *w)
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1546 {
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1547 static PyObject *cmp_obj;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1548 PyObject *args;
16356
4ff2858d7a85 Rename dubiously named local variable 'cmpfunc' -- this is also a
Guido van Rossum <guido@python.org>
parents: 16352
diff changeset
1549 PyObject *cmp_func;
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1550 PyObject *result;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1551 long l;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1552
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1553 assert(PyInstance_Check(v));
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1554
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1555 if (cmp_obj == NULL) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1556 cmp_obj = PyString_InternFromString("__cmp__");
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1557 if (cmp_obj == NULL)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1558 return -2;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1559 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1560
16356
4ff2858d7a85 Rename dubiously named local variable 'cmpfunc' -- this is also a
Guido van Rossum <guido@python.org>
parents: 16352
diff changeset
1561 cmp_func = PyObject_GetAttr(v, cmp_obj);
4ff2858d7a85 Rename dubiously named local variable 'cmpfunc' -- this is also a
Guido van Rossum <guido@python.org>
parents: 16352
diff changeset
1562 if (cmp_func == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1563 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1564 return -2;
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1565 PyErr_Clear();
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1566 return 2;
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1567 }
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1568
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1569 args = Py_BuildValue("(O)", w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1570 if (args == NULL)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1571 return -2;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1572
16356
4ff2858d7a85 Rename dubiously named local variable 'cmpfunc' -- this is also a
Guido van Rossum <guido@python.org>
parents: 16352
diff changeset
1573 result = PyEval_CallObject(cmp_func, args);
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1574 Py_DECREF(args);
16356
4ff2858d7a85 Rename dubiously named local variable 'cmpfunc' -- this is also a
Guido van Rossum <guido@python.org>
parents: 16352
diff changeset
1575 Py_DECREF(cmp_func);
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1576
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1577 if (result == NULL)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1578 return -2;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1579
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1580 if (result == Py_NotImplemented) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1581 Py_DECREF(result);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1582 return 2;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1583 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1584
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1585 l = PyInt_AsLong(result);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1586 Py_DECREF(result);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1587 if (l == -1 && PyErr_Occurred()) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1588 PyErr_SetString(PyExc_TypeError,
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1589 "comparison did not return an int");
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1590 return -2;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1591 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1592
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1593 return l < 0 ? -1 : l > 0 ? 1 : 0;
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1594 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1595
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1596 /* Try a 3-way comparison, returning an int; either v or w is an instance.
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1597 We first try a coercion. Return:
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1598 -2 for an exception;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1599 -1 if v < w;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1600 0 if v == w;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1601 1 if v > w;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1602 2 if this particular 3-way comparison is not implemented or undefined.
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1603 THIS IS ONLY CALLED FROM object.c!
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1604 */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1605 static int
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1606 instance_compare(PyObject *v, PyObject *w)
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1607 {
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1608 int c;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1609
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1610 c = PyNumber_CoerceEx(&v, &w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1611 if (c < 0)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1612 return -2;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1613 if (c == 0) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1614 /* If neither is now an instance, use regular comparison */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1615 if (!PyInstance_Check(v) && !PyInstance_Check(w)) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1616 c = PyObject_Compare(v, w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1617 Py_DECREF(v);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1618 Py_DECREF(w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1619 if (PyErr_Occurred())
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1620 return -2;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1621 return c < 0 ? -1 : c > 0 ? 1 : 0;
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1622 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1623 }
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1624 else {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1625 /* The coercion didn't do anything.
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1626 Treat this the same as returning v and w unchanged. */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1627 Py_INCREF(v);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1628 Py_INCREF(w);
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1629 }
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1630
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1631 if (PyInstance_Check(v)) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1632 c = half_cmp(v, w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1633 if (c <= 1) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1634 Py_DECREF(v);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1635 Py_DECREF(w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1636 return c;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1637 }
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1638 }
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1639 if (PyInstance_Check(w)) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1640 c = half_cmp(w, v);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1641 if (c <= 1) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1642 Py_DECREF(v);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1643 Py_DECREF(w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1644 if (c >= -1)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1645 c = -c;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1646 return c;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1647 }
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1648 }
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1649 Py_DECREF(v);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1650 Py_DECREF(w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1651 return 2;
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1652 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1653
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1654 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1655 instance_nonzero(PyInstanceObject *self)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1656 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1657 PyObject *func, *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1658 long outcome;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1659 static PyObject *nonzerostr;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1660
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1661 if (nonzerostr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1662 nonzerostr = PyString_InternFromString("__nonzero__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1663 if ((func = instance_getattr(self, nonzerostr)) == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1664 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1665 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1666 PyErr_Clear();
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1667 if (lenstr == NULL)
5008
f55e82e1e565 Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents: 4583
diff changeset
1668 lenstr = PyString_InternFromString("__len__");
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1669 if ((func = instance_getattr(self, lenstr)) == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1670 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1671 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1672 PyErr_Clear();
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1673 /* Fall back to the default behavior:
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1674 all instances are nonzero */
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1675 return 1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1676 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1677 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1678 res = PyEval_CallObject(func, (PyObject *)NULL);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1679 Py_DECREF(func);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1680 if (res == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1681 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1682 if (!PyInt_Check(res)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1683 Py_DECREF(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1684 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1685 "__nonzero__ should return an int");
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1686 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1687 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1688 outcome = PyInt_AsLong(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1689 Py_DECREF(res);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1690 if (outcome < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1691 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1692 "__nonzero__ should return >= 0");
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1693 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1694 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1695 return outcome > 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1696 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1697
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
1698 UNARY(instance_invert, "__invert__")
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
1699 UNARY(instance_int, "__int__")
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
1700 UNARY(instance_long, "__long__")
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
1701 UNARY(instance_float, "__float__")
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
1702 UNARY(instance_oct, "__oct__")
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
1703 UNARY(instance_hex, "__hex__")
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
1704
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1705 static PyObject *
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1706 bin_power(PyObject *v, PyObject *w)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1707 {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1708 return PyNumber_Power(v, w, Py_None);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1709 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1710
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1711 /* This version is for ternary calls only (z != None) */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1712 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1713 instance_pow(PyObject *v, PyObject *w, PyObject *z)
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1714 {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1715 if (z == Py_None) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1716 return do_binop(v, w, "__pow__", "__rpow__", bin_power);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1717 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1718 else {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1719 PyObject *func;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1720 PyObject *args;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1721 PyObject *result;
3987
6fbc5c665812 Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents: 3769
diff changeset
1722
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1723 /* XXX Doesn't do coercions... */
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1724 func = PyObject_GetAttrString(v, "__pow__");
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1725 if (func == NULL)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1726 return NULL;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1727 args = Py_BuildValue("(OO)", w, z);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1728 if (args == NULL) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1729 Py_DECREF(func);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1730 return NULL;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1731 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1732 result = PyEval_CallObject(func, args);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1733 Py_DECREF(func);
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1734 Py_DECREF(args);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1735 return result;
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1736 }
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1737 }
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1861
diff changeset
1738
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14049
diff changeset
1739 static PyObject *
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1740 bin_inplace_power(PyObject *v, PyObject *w)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14049
diff changeset
1741 {
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1742 return PyNumber_InPlacePower(v, w, Py_None);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14049
diff changeset
1743 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14049
diff changeset
1744
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14049
diff changeset
1745
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1746 static PyObject *
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1747 instance_ipow(PyObject *v, PyObject *w, PyObject *z)
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1748 {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1749 if (z == Py_None) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1750 return do_binop_inplace(v, w, "__ipow__", "__pow__",
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1751 "__rpow__", bin_inplace_power);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1752 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1753 else {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1754 /* XXX Doesn't do coercions... */
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1755 PyObject *func;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1756 PyObject *args;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1757 PyObject *result;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1758
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1759 func = PyObject_GetAttrString(v, "__ipow__");
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1760 if (func == NULL) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1761 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1762 return NULL;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1763 PyErr_Clear();
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1764 return instance_pow(v, w, z);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1765 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1766 args = Py_BuildValue("(OO)", w, z);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1767 if (args == NULL) {
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1768 Py_DECREF(func);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1769 return NULL;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1770 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1771 result = PyEval_CallObject(func, args);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1772 Py_DECREF(func);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1773 Py_DECREF(args);
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1774 return result;
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1775 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1776 }
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1777
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1778
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1779 /* Map rich comparison operators to their __xx__ namesakes */
17783
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1780 #define NAME_OPS 6
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1781 static PyObject **name_op = NULL;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1782
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1783 static int
17894
14da5314664d init_name_op(): add (void) to the argument list to make it a valid
Guido van Rossum <guido@python.org>
parents: 17783
diff changeset
1784 init_name_op(void)
17783
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1785 {
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1786 int i;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1787 char *_name_op[] = {
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1788 "__lt__",
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1789 "__le__",
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1790 "__eq__",
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1791 "__ne__",
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1792 "__gt__",
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1793 "__ge__",
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1794 };
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1795
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1796 name_op = (PyObject **)malloc(sizeof(PyObject *) * NAME_OPS);
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1797 if (name_op == NULL)
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1798 return -1;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1799 for (i = 0; i < NAME_OPS; ++i) {
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1800 name_op[i] = PyString_InternFromString(_name_op[i]);
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1801 if (name_op[i] == NULL)
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1802 return -1;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1803 }
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1804 return 0;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1805 }
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1806
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1807 static PyObject *
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1808 half_richcompare(PyObject *v, PyObject *w, int op)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1809 {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1810 PyObject *method;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1811 PyObject *args;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1812 PyObject *res;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1813
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1814 assert(PyInstance_Check(v));
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1815
17783
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1816 if (name_op == NULL) {
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1817 if (init_name_op() < 0)
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1818 return NULL;
17783
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1819 }
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1820 /* If the instance doesn't define an __getattr__ method, use
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1821 instance_getattr2 directly because it will not set an
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1822 exception on failure. */
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1823 if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL) {
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1824 method = instance_getattr2((PyInstanceObject *)v,
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1825 name_op[op]);
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1826 if (method == NULL) {
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1827 assert(!PyErr_Occurred());
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1828 res = Py_NotImplemented;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1829 Py_INCREF(res);
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1830 return res;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1831 }
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1832 } else {
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1833 method = PyObject_GetAttr(v, name_op[op]);
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1834 if (method == NULL) {
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1835 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1836 return NULL;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1837 PyErr_Clear();
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1838 res = Py_NotImplemented;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1839 Py_INCREF(res);
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1840 return res;
1dacd0d60637 Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17692
diff changeset
1841 }
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1842 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1843
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1844 args = Py_BuildValue("(O)", w);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1845 if (args == NULL) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1846 Py_DECREF(method);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1847 return NULL;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1848 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1849
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1850 res = PyEval_CallObject(method, args);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1851 Py_DECREF(args);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1852 Py_DECREF(method);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1853
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1854 return res;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1855 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1856
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1857 /* Map rich comparison operators to their swapped version, e.g. LT --> GT */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1858 static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1859
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1860 static PyObject *
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1861 instance_richcompare(PyObject *v, PyObject *w, int op)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1862 {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1863 PyObject *res;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1864
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1865 if (PyInstance_Check(v)) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1866 res = half_richcompare(v, w, op);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1867 if (res != Py_NotImplemented)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1868 return res;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1869 Py_DECREF(res);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1870 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1871
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1872 if (PyInstance_Check(w)) {
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1873 res = half_richcompare(w, v, swapped_op[op]);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1874 if (res != Py_NotImplemented)
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1875 return res;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1876 Py_DECREF(res);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1877 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1878
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1879 Py_INCREF(Py_NotImplemented);
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1880 return Py_NotImplemented;
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1881 }
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1882
15722
3e862a51861f Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1883
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1884 /* Get the iterator */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1885 static PyObject *
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1886 instance_getiter(PyInstanceObject *self)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1887 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1888 PyObject *func;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1889
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1890 if (iterstr == NULL) {
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1891 iterstr = PyString_InternFromString("__iter__");
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1892 if (iterstr == NULL)
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1893 return NULL;
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1894 }
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1895 if (getitemstr == NULL) {
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1896 getitemstr = PyString_InternFromString("__getitem__");
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1897 if (getitemstr == NULL)
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1898 return NULL;
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1899 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1900
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1901 if ((func = instance_getattr(self, iterstr)) != NULL) {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1902 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1903 Py_DECREF(func);
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1904 if (res != NULL && !PyIter_Check(res)) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1905 PyErr_Format(PyExc_TypeError,
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1906 "__iter__ returned non-iterator "
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1907 "of type '%.100s'",
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1908 res->ob_type->tp_name);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1909 Py_DECREF(res);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1910 res = NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1911 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1912 return res;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1913 }
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1914 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1915 return NULL;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1916 PyErr_Clear();
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1917 if ((func = instance_getattr(self, getitemstr)) == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1918 PyErr_SetString(PyExc_TypeError,
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1919 "iteration over non-sequence");
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1920 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1921 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1922 Py_DECREF(func);
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1923 return PySeqIter_New((PyObject *)self);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1924 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1925
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1926
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1927 /* Call the iterator's next */
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1928 static PyObject *
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1929 instance_iternext(PyInstanceObject *self)
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1930 {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1931 PyObject *func;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1932
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1933 if (nextstr == NULL)
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1934 nextstr = PyString_InternFromString("next");
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1935
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1936 if ((func = instance_getattr(self, nextstr)) != NULL) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1937 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1938 Py_DECREF(func);
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1939 if (res != NULL) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1940 return res;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1941 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1942 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1943 PyErr_Clear();
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1944 return NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1945 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1946 return NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1947 }
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1948 PyErr_SetString(PyExc_TypeError, "instance has no next() method");
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
1949 return NULL;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1950 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1951
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1952 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1953 instance_call(PyObject *func, PyObject *arg, PyObject *kw)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1954 {
23839
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1955 PyThreadState *tstate = PyThreadState_GET();
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1956 PyObject *res, *call = PyObject_GetAttrString(func, "__call__");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1957 if (call == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1958 PyInstanceObject *inst = (PyInstanceObject*) func;
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1959 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
1960 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1961 PyErr_Clear();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1962 PyErr_Format(PyExc_AttributeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1963 "%.200s instance has no __call__ method",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1964 PyString_AsString(inst->in_class->cl_name));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1965 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1966 }
23839
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1967 /* We must check and increment the recursion depth here. Scenario:
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1968 class A:
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1969 pass
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1970 A.__call__ = A() # that's right
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1971 a = A() # ok
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1972 a() # infinite recursion
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1973 This bounces between instance_call() and PyObject_Call() without
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1974 ever hitting eval_frame() (which has the main recursion check). */
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1975 if (tstate->recursion_depth++ > Py_GetRecursionLimit()) {
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1976 PyErr_SetString(PyExc_RuntimeError,
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1977 "maximum __call__ recursion depth exceeded");
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1978 res = NULL;
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1979 }
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1980 else
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1981 res = PyObject_Call(call, arg, kw);
c8815bd01ff5 Fix for SF bug 532646. This is a little simpler than what Neal
Guido van Rossum <guido@python.org>
parents: 20698
diff changeset
1982 tstate->recursion_depth--;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1983 Py_DECREF(call);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1984 return res;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1985 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
1986
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
1987
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
1988 static PyNumberMethods instance_as_number = {
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1989 (binaryfunc)instance_add, /* nb_add */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1990 (binaryfunc)instance_sub, /* nb_subtract */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1991 (binaryfunc)instance_mul, /* nb_multiply */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1992 (binaryfunc)instance_div, /* nb_divide */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1993 (binaryfunc)instance_mod, /* nb_remainder */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1994 (binaryfunc)instance_divmod, /* nb_divmod */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1995 (ternaryfunc)instance_pow, /* nb_power */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1996 (unaryfunc)instance_neg, /* nb_negative */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1997 (unaryfunc)instance_pos, /* nb_positive */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1998 (unaryfunc)instance_abs, /* nb_absolute */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
1999 (inquiry)instance_nonzero, /* nb_nonzero */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2000 (unaryfunc)instance_invert, /* nb_invert */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2001 (binaryfunc)instance_lshift, /* nb_lshift */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2002 (binaryfunc)instance_rshift, /* nb_rshift */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2003 (binaryfunc)instance_and, /* nb_and */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2004 (binaryfunc)instance_xor, /* nb_xor */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2005 (binaryfunc)instance_or, /* nb_or */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2006 (coercion)instance_coerce, /* nb_coerce */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2007 (unaryfunc)instance_int, /* nb_int */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2008 (unaryfunc)instance_long, /* nb_long */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2009 (unaryfunc)instance_float, /* nb_float */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2010 (unaryfunc)instance_oct, /* nb_oct */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2011 (unaryfunc)instance_hex, /* nb_hex */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2012 (binaryfunc)instance_iadd, /* nb_inplace_add */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2013 (binaryfunc)instance_isub, /* nb_inplace_subtract */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2014 (binaryfunc)instance_imul, /* nb_inplace_multiply */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2015 (binaryfunc)instance_idiv, /* nb_inplace_divide */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2016 (binaryfunc)instance_imod, /* nb_inplace_remainder */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2017 (ternaryfunc)instance_ipow, /* nb_inplace_power */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2018 (binaryfunc)instance_ilshift, /* nb_inplace_lshift */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2019 (binaryfunc)instance_irshift, /* nb_inplace_rshift */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2020 (binaryfunc)instance_iand, /* nb_inplace_and */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2021 (binaryfunc)instance_ixor, /* nb_inplace_xor */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2022 (binaryfunc)instance_ior, /* nb_inplace_or */
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18934
diff changeset
2023 (binaryfunc)instance_floordiv, /* nb_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18934
diff changeset
2024 (binaryfunc)instance_truediv, /* nb_true_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18934
diff changeset
2025 (binaryfunc)instance_ifloordiv, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18934
diff changeset
2026 (binaryfunc)instance_itruediv, /* nb_inplace_true_divide */
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
2027 };
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
2028
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2029 PyTypeObject PyInstance_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2030 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2031 0,
174
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2032 "instance",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
2033 sizeof(PyInstanceObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2034 0,
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2035 (destructor)instance_dealloc, /* tp_dealloc */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2036 0, /* tp_print */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2037 0, /* tp_getattr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2038 0, /* tp_setattr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2039 instance_compare, /* tp_compare */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2040 (reprfunc)instance_repr, /* tp_repr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2041 &instance_as_number, /* tp_as_number */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2042 &instance_as_sequence, /* tp_as_sequence */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2043 &instance_as_mapping, /* tp_as_mapping */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2044 (hashfunc)instance_hash, /* tp_hash */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2045 instance_call, /* tp_call */
17643
56c5445ec835 Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2046 (reprfunc)instance_str, /* tp_str */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2047 (getattrofunc)instance_getattr, /* tp_getattro */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2048 (setattrofunc)instance_setattr, /* tp_setattro */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2049 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
2050 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2051 instance_doc, /* tp_doc */
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2052 (traverseproc)instance_traverse, /* tp_traverse */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2053 0, /* tp_clear */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2054 instance_richcompare, /* tp_richcompare */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
2055 offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17170
diff changeset
2056 (getiterfunc)instance_getiter, /* tp_iter */
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2057 (iternextfunc)instance_iternext, /* tp_iternext */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2058 0, /* tp_methods */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2059 0, /* tp_members */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2060 0, /* tp_getset */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2061 0, /* tp_base */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2062 0, /* tp_dict */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2063 0, /* tp_descr_get */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2064 0, /* tp_descr_set */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2065 0, /* tp_dictoffset */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2066 0, /* tp_init */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2067 0, /* tp_alloc */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2068 instance_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2069 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2070
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2071
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2072 /* Instance method objects are used for two purposes:
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2073 (a) as bound instance methods (returned by instancename.methodname)
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2074 (b) as unbound methods (returned by ClassName.methodname)
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2075 In case (b), im_self is NULL
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2076 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2077
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2078 static PyMethodObject *free_list;
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2079
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2080 PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2081 PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2082 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2083 register PyMethodObject *im;
8651
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8649
diff changeset
2084 if (!PyCallable_Check(func)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2085 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2086 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2087 }
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2088 im = free_list;
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2089 if (im != NULL) {
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2090 free_list = (PyMethodObject *)(im->im_self);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12504
diff changeset
2091 PyObject_INIT(im, &PyMethod_Type);
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2092 }
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2093 else {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
2094 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2095 if (im == NULL)
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2096 return NULL;
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2097 }
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 17154
diff changeset
2098 im->im_weakreflist = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2099 Py_INCREF(func);
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
2100 im->im_func = func;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2101 Py_XINCREF(self);
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
2102 im->im_self = self;
19332
0613fa57ae6d Instance methods: allow a NULL value for im_class.
Guido van Rossum <guido@python.org>
parents: 19323
diff changeset
2103 Py_XINCREF(class);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2104 im->im_class = class;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
2105 _PyObject_GC_TRACK(im);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2106 return (PyObject *)im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2107 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2108
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2109 /* Descriptors for PyMethod attributes */
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2110
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2111 /* im_class, im_func and im_self are stored in the PyMethod object */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2112
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2113 #define OFF(x) offsetof(PyMethodObject, x)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
2114
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19966
diff changeset
2115 static PyMemberDef instancemethod_memberlist[] = {
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19966
diff changeset
2116 {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19966
diff changeset
2117 "the class associated with a method"},
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19966
diff changeset
2118 {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19966
diff changeset
2119 "the function (or other callable) implementing a method"},
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19966
diff changeset
2120 {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19966
diff changeset
2121 "the instance to which a method is bound; None for unbound methods"},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
2122 {NULL} /* Sentinel */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
2123 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
2124
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2125 /* The getattr() implementation for PyMethod objects is similar to
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2126 PyObject_GenericGetAttr(), but instead of looking in __dict__ it
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2127 asks im_self for the attribute. Then the error handling is a bit
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2128 different because we want to preserve the exception raised by the
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2129 delegate, unless we have an alternative from our class. */
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 15722
diff changeset
2130
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2131 static PyObject *
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2132 instancemethod_getattro(PyObject *obj, PyObject *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
2133 {
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2134 PyMethodObject *im = (PyMethodObject *)obj;
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2135 PyTypeObject *tp = obj->ob_type;
20501
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2136 PyObject *descr = NULL, *res;
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2137 descrgetfunc f = NULL;
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2138
20501
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2139 if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) {
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2140 if (tp->tp_dict == NULL) {
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2141 if (PyType_Ready(tp) < 0)
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2142 return NULL;
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2143 }
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2144 descr = _PyType_Lookup(tp, name);
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2145 }
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2146
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2147 f = NULL;
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2148 if (descr != NULL) {
20501
733cb268ad2f Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents: 20183
diff changeset
2149 f = TP_DESCR_GET(descr->ob_type);
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2150 if (f != NULL && PyDescr_IsData(descr))
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2151 return f(descr, obj, (PyObject *)obj->ob_type);
3549
f66306bc0663 Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents: 3096
diff changeset
2152 }
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2153
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2154 res = PyObject_GetAttr(im->im_func, name);
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2155 if (res != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2156 return res;
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2157
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2158 if (f != NULL) {
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2159 PyErr_Clear();
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2160 return f(descr, obj, (PyObject *)obj->ob_type);
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2084
diff changeset
2161 }
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 15722
diff changeset
2162
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2163 if (descr != NULL) {
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 15722
diff changeset
2164 PyErr_Clear();
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2165 Py_INCREF(descr);
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2166 return descr;
15894
1c6b0d3a58ea Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents: 15722
diff changeset
2167 }
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2168
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2169 assert(PyErr_Occurred());
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2170 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
2171 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
2172
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2173 PyDoc_STRVAR(instancemethod_doc,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2174 "instancemethod(function, instance, class)\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2175 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2176 Create an instance method object.");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2177
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2178 static PyObject *
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2179 instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2180 {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2181 PyObject *func;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2182 PyObject *self;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2183 PyObject *classObj;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2184
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2185 if (!PyArg_ParseTuple(args, "OOO:instancemethod",
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2186 &func, &self, &classObj))
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2187 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2188 if (!PyCallable_Check(func)) {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2189 PyErr_SetString(PyExc_TypeError,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2190 "first argument must be callable");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2191 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2192 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2193 if (self == Py_None)
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2194 self = NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2195 return PyMethod_New(func, self, classObj);
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2196 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2197
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2198 static void
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2199 instancemethod_dealloc(register PyMethodObject *im)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2200 {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
2201 _PyObject_GC_UNTRACK(im);
20698
b229208a2a72 Be smarter about clearing the weakref lists for instances, instance methods,
Fred Drake <fdrake@acm.org>
parents: 20601
diff changeset
2202 if (im->im_weakreflist != NULL)
b229208a2a72 Be smarter about clearing the weakref lists for instances, instance methods,
Fred Drake <fdrake@acm.org>
parents: 20601
diff changeset
2203 PyObject_ClearWeakRefs((PyObject *)im);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2204 Py_DECREF(im->im_func);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2205 Py_XDECREF(im->im_self);
19332
0613fa57ae6d Instance methods: allow a NULL value for im_class.
Guido van Rossum <guido@python.org>
parents: 19323
diff changeset
2206 Py_XDECREF(im->im_class);
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2207 im->im_self = (PyObject *)free_list;
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2208 free_list = im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2209 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2210
1021
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
2211 static int
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2212 instancemethod_compare(PyMethodObject *a, PyMethodObject *b)
1021
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
2213 {
2641
f98a2233b337 change comparing instance methods
Guido van Rossum <guido@python.org>
parents: 2239
diff changeset
2214 if (a->im_self != b->im_self)
f98a2233b337 change comparing instance methods
Guido van Rossum <guido@python.org>
parents: 2239
diff changeset
2215 return (a->im_self < b->im_self) ? -1 : 1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2216 return PyObject_Compare(a->im_func, b->im_func);
1021
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
2217 }
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
2218
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2219 static PyObject *
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2220 instancemethod_repr(PyMethodObject *a)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
2221 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2222 PyObject *self = a->im_self;
8651
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8649
diff changeset
2223 PyObject *func = a->im_func;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2224 PyObject *klass = a->im_class;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2225 PyObject *funcname = NULL, *klassname = NULL, *result = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2226 char *sfuncname = "?", *sklassname = "?";
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2227
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2228 funcname = PyObject_GetAttrString(func, "__name__");
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2229 if (funcname == NULL) {
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2230 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2231 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2232 PyErr_Clear();
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2233 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2234 else if (!PyString_Check(funcname)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2235 Py_DECREF(funcname);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2236 funcname = NULL;
8651
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8649
diff changeset
2237 }
4ba9b70ca56e Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents: 8649
diff changeset
2238 else
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2239 sfuncname = PyString_AS_STRING(funcname);
19336
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2240 if (klass == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2241 klassname = NULL;
19336
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2242 else {
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2243 klassname = PyObject_GetAttrString(klass, "__name__");
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2244 if (klassname == NULL) {
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2245 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2246 return NULL;
19336
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2247 PyErr_Clear();
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2248 }
19336
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2249 else if (!PyString_Check(klassname)) {
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2250 Py_DECREF(klassname);
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2251 klassname = NULL;
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2252 }
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2253 else
ebfb74ce7e56 Fix core dump in repr() of instancemethod whose class==NULL.
Guido van Rossum <guido@python.org>
parents: 19334
diff changeset
2254 sklassname = PyString_AS_STRING(klassname);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2255 }
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2256 if (self == NULL)
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
2257 result = PyString_FromFormat("<unbound method %s.%s>",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
2258 sklassname, sfuncname);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2259 else {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2260 /* XXX Shouldn't use repr() here! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2261 PyObject *selfrepr = PyObject_Repr(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2262 if (selfrepr == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2263 goto fail;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2264 if (!PyString_Check(selfrepr)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2265 Py_DECREF(selfrepr);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2266 goto fail;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2267 }
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
2268 result = PyString_FromFormat("<bound method %s.%s of %s>",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
2269 sklassname, sfuncname,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19336
diff changeset
2270 PyString_AS_STRING(selfrepr));
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2271 Py_DECREF(selfrepr);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2272 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2273 fail:
11378
dc9f1f964690 Fix for PR#98 (Adrian Eyre) -- in instancemethod_repr, the funcname
Guido van Rossum <guido@python.org>
parents: 8806
diff changeset
2274 Py_XDECREF(funcname);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2275 Py_XDECREF(klassname);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2276 return result;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
2277 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
2278
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2279 static long
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2280 instancemethod_hash(PyMethodObject *a)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2281 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2282 long x, y;
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2283 if (a->im_self == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2284 x = PyObject_Hash(Py_None);
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
2285 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2286 x = PyObject_Hash(a->im_self);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2287 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2288 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2289 y = PyObject_Hash(a->im_func);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2290 if (y == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2291 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2292 return x ^ y;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2293 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2294
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2295 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2296 instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2297 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2298 int err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2299 if (im->im_func) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2300 err = visit(im->im_func, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2301 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2302 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2303 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2304 if (im->im_self) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2305 err = visit(im->im_self, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2306 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2307 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2308 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2309 if (im->im_class) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2310 err = visit(im->im_class, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2311 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2312 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2313 }
12992
3f990ef66667 traverse functions should return 0 on success
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12990
diff changeset
2314 return 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2315 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2316
19480
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2317 static char *
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2318 getclassname(PyObject *class)
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2319 {
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2320 PyObject *name;
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2321
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2322 if (class == NULL)
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2323 name = NULL;
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2324 else
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2325 name = PyObject_GetAttrString(class, "__name__");
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2326 if (name == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2327 /* This function cannot return an exception */
19480
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2328 PyErr_Clear();
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2329 return "?";
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2330 }
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2331 if (!PyString_Check(name)) {
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2332 Py_DECREF(name);
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2333 return "?";
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2334 }
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2335 PyString_InternInPlace(&name);
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2336 Py_DECREF(name);
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2337 return PyString_AS_STRING(name);
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2338 }
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2339
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2340 static char *
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2341 getinstclassname(PyObject *inst)
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2342 {
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2343 PyObject *class;
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2344 char *name;
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2345
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2346 if (inst == NULL)
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2347 return "nothing";
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2348
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2349 class = PyObject_GetAttrString(inst, "__class__");
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2350 if (class == NULL) {
23842
6c619c161b87 Major cleanup operation: whenever there's a call that looks for an
Guido van Rossum <guido@python.org>
parents: 23839
diff changeset
2351 /* This function cannot return an exception */
19480
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2352 PyErr_Clear();
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2353 class = (PyObject *)(inst->ob_type);
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2354 Py_INCREF(class);
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2355 }
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2356 name = getclassname(class);
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2357 Py_XDECREF(class);
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2358 return name;
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2359 }
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2360
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2361 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2362 instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2363 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2364 PyObject *self = PyMethod_GET_SELF(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2365 PyObject *class = PyMethod_GET_CLASS(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2366 PyObject *result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2367
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2368 func = PyMethod_GET_FUNCTION(func);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2369 if (self == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2370 /* Unbound methods must be called with an instance of
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2371 the class (or a derived class) as first argument */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2372 int ok;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2373 if (PyTuple_Size(arg) >= 1)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2374 self = PyTuple_GET_ITEM(arg, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2375 if (self == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2376 ok = 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2377 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2378 ok = PyObject_IsInstance(self, class);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2379 if (ok < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2380 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2381 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2382 if (!ok) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2383 PyErr_Format(PyExc_TypeError,
19480
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2384 "unbound method %s%s must be called with "
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2385 "%s instance as first argument "
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2386 "(got %s%s instead)",
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2387 PyEval_GetFuncName(func),
19480
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2388 PyEval_GetFuncDesc(func),
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2389 getclassname(class),
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2390 getinstclassname(self),
984ffa124606 Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
2391 self == NULL ? "" : " instance");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2392 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2393 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2394 Py_INCREF(arg);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2395 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2396 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2397 int argcount = PyTuple_Size(arg);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2398 PyObject *newarg = PyTuple_New(argcount + 1);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2399 int i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2400 if (newarg == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2401 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2402 Py_INCREF(self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2403 PyTuple_SET_ITEM(newarg, 0, self);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2404 for (i = 0; i < argcount; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2405 PyObject *v = PyTuple_GET_ITEM(arg, i);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2406 Py_XINCREF(v);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2407 PyTuple_SET_ITEM(newarg, i+1, v);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2408 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2409 arg = newarg;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2410 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2411 result = PyObject_Call((PyObject *)func, arg, kw);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2412 Py_DECREF(arg);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2413 return result;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2414 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2415
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2416 static PyObject *
19334
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
2417 instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *class)
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2418 {
19334
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
2419 /* Don't rebind an already bound method, or an unbound method
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
2420 of a class that's not a base class of class */
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
2421 if (PyMethod_GET_SELF(meth) != NULL ||
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
2422 (PyMethod_GET_CLASS(meth) != NULL &&
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
2423 !PyObject_IsSubclass(class, PyMethod_GET_CLASS(meth)))) {
19323
261e28e7fa01 classobject.c:instancemethod_descr_get(): when a bound method is
Guido van Rossum <guido@python.org>
parents: 19280
diff changeset
2424 Py_INCREF(meth);
261e28e7fa01 classobject.c:instancemethod_descr_get(): when a bound method is
Guido van Rossum <guido@python.org>
parents: 19280
diff changeset
2425 return meth;
261e28e7fa01 classobject.c:instancemethod_descr_get(): when a bound method is
Guido van Rossum <guido@python.org>
parents: 19280
diff changeset
2426 }
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2427 if (obj == Py_None)
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2428 obj = NULL;
19334
60264d1e100e instance_getattr2(): rewritten to remove unnecessary stuff and
Guido van Rossum <guido@python.org>
parents: 19332
diff changeset
2429 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, class);
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2430 }
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2431
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2432 PyTypeObject PyMethod_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5008
diff changeset
2433 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2434 0,
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2435 "instancemethod",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
2436 sizeof(PyMethodObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2437 0,
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2438 (destructor)instancemethod_dealloc, /* tp_dealloc */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2439 0, /* tp_print */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2440 0, /* tp_getattr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2441 0, /* tp_setattr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2442 (cmpfunc)instancemethod_compare, /* tp_compare */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2443 (reprfunc)instancemethod_repr, /* tp_repr */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2444 0, /* tp_as_number */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2445 0, /* tp_as_sequence */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2446 0, /* tp_as_mapping */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2447 (hashfunc)instancemethod_hash, /* tp_hash */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17894
diff changeset
2448 instancemethod_call, /* tp_call */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2449 0, /* tp_str */
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2450 (getattrofunc)instancemethod_getattro, /* tp_getattro */
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2451 PyObject_GenericSetAttr, /* tp_setattro */
15943
5b063baebef8 Rich comparisons.
Guido van Rossum <guido@python.org>
parents: 15894
diff changeset
2452 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
2453 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2454 instancemethod_doc, /* tp_doc */
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2455 (traverseproc)instancemethod_traverse, /* tp_traverse */
17170
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 17154
diff changeset
2456 0, /* tp_clear */
962196ce7231 Add support for weak references to the function and method types.
Fred Drake <fdrake@acm.org>
parents: 17154
diff changeset
2457 0, /* tp_richcompare */
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2458 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2459 0, /* tp_iter */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2460 0, /* tp_iternext */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2461 0, /* tp_methods */
19956
33669c8914b4 Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents: 19808
diff changeset
2462 instancemethod_memberlist, /* tp_members */
20601
5c1dfd92877d Fix for SF bug #472940: can't getattr() attribute shown by dir()
Guido van Rossum <guido@python.org>
parents: 20501
diff changeset
2463 0, /* tp_getset */
19280
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2464 0, /* tp_base */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2465 0, /* tp_dict */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2466 instancemethod_descr_get, /* tp_descr_get */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2467 0, /* tp_descr_set */
f10b68eceefc PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents: 19051
diff changeset
2468 0, /* tp_dictoffset */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2469 0, /* tp_init */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2470 0, /* tp_alloc */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23842
diff changeset
2471 instancemethod_new, /* tp_new */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2472 };
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2473
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2474 /* Clear out the free list */
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2475
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2476 void
13449
0409a98cd582 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2477 PyMethod_Fini(void)
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2478 {
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2479 while (free_list) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12504
diff changeset
2480 PyMethodObject *im = free_list;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12504
diff changeset
2481 free_list = (PyMethodObject *)(im->im_self);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19480
diff changeset
2482 PyObject_GC_Del(im);
5932
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2483 }
40cfc905c776 Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2484 }