annotate Objects/classobject.c @ 40354:320dfdee8968 legacy-trunk

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