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