Mercurial > cpython-lru_cache-weakref
annotate Objects/classobject.c @ 11518:cc412dfd79f7 legacy-trunk
New examples by Tony Ingraldi, using dns-lookup in stead of the funny interslip
module.
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Fri, 03 Dec 1999 15:15:28 +0000 |
| parents | dc9f1f964690 |
| children | 5e6e9e893acd |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2059
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4426
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Class object implementation */ |
| 33 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
34 #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
|
35 #include "structmember.h" |
| 6 | 36 |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
37 /* Forward */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
38 static PyObject *class_lookup |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
39 Py_PROTO((PyClassObject *, PyObject *, PyClassObject **)); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
40 static PyObject *instance_getattr1 Py_PROTO((PyInstanceObject *, PyObject *)); |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
41 |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
42 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
|
43 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
44 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
45 PyClass_New(bases, dict, name) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
46 PyObject *bases; /* NULL or tuple of classobjects! */ |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
47 PyObject *dict; |
|
6430
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
48 PyObject *name; |
| 6 | 49 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
50 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
|
51 static PyObject *docstr, *modstr, *namestr; |
|
4086
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
52 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
|
53 docstr= PyString_InternFromString("__doc__"); |
|
4086
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
54 if (docstr == NULL) |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
55 return NULL; |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
56 } |
|
6309
904a4d783a33
When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents:
6155
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 return NULL; |
|
904a4d783a33
When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents:
6155
diff
changeset
|
61 } |
|
904a4d783a33
When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents:
6155
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 return NULL; |
|
904a4d783a33
When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents:
6155
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 if (name == NULL || !PyString_Check(name)) { |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
68 PyErr_SetString(PyExc_SystemError, |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
69 "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
|
70 return NULL; |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
71 } |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
72 if (dict == NULL || !PyDict_Check(dict)) { |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
73 PyErr_SetString(PyExc_SystemError, |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
74 "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
|
75 return NULL; |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
76 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
77 if (PyDict_GetItem(dict, docstr) == NULL) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
78 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
|
79 return NULL; |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
80 } |
|
6309
904a4d783a33
When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents:
6155
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 return NULL; |
|
904a4d783a33
When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents:
6155
diff
changeset
|
88 } |
|
904a4d783a33
When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents:
6155
diff
changeset
|
89 } |
|
904a4d783a33
When creating a class, set its __module__ attribute to the module
Guido van Rossum <guido@python.org>
parents:
6155
diff
changeset
|
90 } |
|
465
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
91 if (bases == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
92 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
|
93 if (bases == NULL) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1215
diff
changeset
|
94 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
|
95 } |
|
6430
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
96 else { |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
97 int i; |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
98 if (!PyTuple_Check(bases)) { |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
99 PyErr_SetString(PyExc_SystemError, |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
100 "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
|
101 return NULL; |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
102 } |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
103 i = PyTuple_Size(bases); |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
104 while (--i >= 0) { |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
105 if (!PyClass_Check(PyTuple_GetItem(bases, i))) { |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
106 PyErr_SetString(PyExc_SystemError, |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
107 "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
|
108 return NULL; |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
109 } |
|
e78b6174bec9
Check that all base classes are indeed class objects, rather than
Guido van Rossum <guido@python.org>
parents:
6309
diff
changeset
|
110 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
111 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
|
112 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
113 op = PyObject_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
|
114 if (op == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
115 Py_DECREF(bases); |
| 6 | 116 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
|
117 } |
| 6 | 118 op->cl_bases = bases; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
119 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
|
120 op->cl_dict = dict; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
121 Py_XINCREF(name); |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
122 op->cl_name = name; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 delattrstr = PyString_InternFromString("__delattr__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
127 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
128 op->cl_getattr = class_lookup(op, getattrstr, &dummy); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
129 op->cl_setattr = class_lookup(op, setattrstr, &dummy); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
130 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
|
131 Py_XINCREF(op->cl_getattr); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
132 Py_XINCREF(op->cl_setattr); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
133 Py_XINCREF(op->cl_delattr); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
134 return (PyObject *) op; |
| 6 | 135 } |
| 136 | |
| 137 /* Class methods */ | |
| 138 | |
| 139 static void | |
| 140 class_dealloc(op) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
141 PyClassObject *op; |
| 6 | 142 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
143 Py_DECREF(op->cl_bases); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
144 Py_DECREF(op->cl_dict); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
145 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
|
146 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
|
147 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
|
148 Py_XDECREF(op->cl_delattr); |
| 6 | 149 free((ANY *)op); |
| 150 } | |
| 151 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
152 static PyObject * |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
153 class_lookup(cp, name, pclass) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
154 PyClassObject *cp; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
155 PyObject *name; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
156 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
|
157 { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
158 int i, n; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
159 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
|
160 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
|
161 *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
|
162 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
|
163 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
164 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
|
165 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
|
166 /* 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
|
167 PyObject *v = class_lookup( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
168 (PyClassObject *) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
169 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
|
170 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
|
171 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
|
172 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
173 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
|
174 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
175 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
176 static PyObject * |
| 6 | 177 class_getattr(op, name) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
178 register PyClassObject *op; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
179 PyObject *name; |
| 6 | 180 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
181 register PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
182 register char *sname = PyString_AsString(name); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
183 PyClassObject *class; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
184 if (sname[0] == '_' && sname[1] == '_') { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
185 if (strcmp(sname, "__dict__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
186 if (PyEval_GetRestricted()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
187 PyErr_SetString(PyExc_RuntimeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
188 "class.__dict__ not accessible in restricted mode"); |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
189 return NULL; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
190 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
191 Py_INCREF(op->cl_dict); |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
192 return op->cl_dict; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
193 } |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
194 if (strcmp(sname, "__bases__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
195 Py_INCREF(op->cl_bases); |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
196 return op->cl_bases; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
197 } |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
198 if (strcmp(sname, "__name__") == 0) { |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
199 if (op->cl_name == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
200 v = Py_None; |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
201 else |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
202 v = op->cl_name; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
203 Py_INCREF(v); |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
204 return v; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
205 } |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
206 } |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
207 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
|
208 if (v == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
209 PyErr_SetObject(PyExc_AttributeError, name); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
210 return NULL; |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
211 } |
|
5456
7316eecb1e6b
removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
212 Py_INCREF(v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
213 if (PyFunction_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
214 PyObject *w = PyMethod_New(v, (PyObject *)NULL, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
215 (PyObject *)class); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
216 Py_DECREF(v); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
217 v = w; |
| 6 | 218 } |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
219 return v; |
| 6 | 220 } |
| 221 | |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
222 static void |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
223 set_slot(slot, v) |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
224 PyObject **slot; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
225 PyObject *v; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
226 { |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
227 PyObject *temp = *slot; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
228 Py_XINCREF(v); |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
229 *slot = v; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
230 Py_XDECREF(temp); |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
231 } |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
232 |
|
8649
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
233 static void |
|
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
234 set_attr_slots(c) |
|
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
235 PyClassObject *c; |
|
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
236 { |
|
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
237 PyClassObject *dummy; |
|
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
238 |
|
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 } |
|
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
243 |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
244 static char * |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
245 set_dict(c, v) |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
246 PyClassObject *c; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
247 PyObject *v; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
248 { |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 return ""; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
254 } |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
255 |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
256 static char * |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
257 set_bases(c, v) |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
258 PyClassObject *c; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
259 PyObject *v; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
260 { |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
261 int i, n; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
262 |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
263 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
|
264 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
|
265 n = PyTuple_Size(v); |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
266 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
|
267 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
|
268 if (!PyClass_Check(x)) |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
269 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
|
270 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
|
271 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
|
272 } |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
273 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
|
274 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
|
275 return ""; |
|
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 |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
278 static char * |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
279 set_name(c, v) |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
280 PyClassObject *c; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
281 PyObject *v; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
282 { |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
283 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
|
284 return "__name__ must be a string object"; |
|
8496
1d8dab5aa7d1
Keep Microsoft's compiler happy.
Guido van Rossum <guido@python.org>
parents:
8464
diff
changeset
|
285 if ((long)strlen(PyString_AS_STRING(v)) != 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
|
286 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
|
287 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
|
288 return ""; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
289 } |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
290 |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
291 static int |
|
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
292 class_setattr(op, name, v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
293 PyClassObject *op; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
294 PyObject *name; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
295 PyObject *v; |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
296 { |
|
6155
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
297 char *sname; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
298 if (PyEval_GetRestricted()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
299 PyErr_SetString(PyExc_RuntimeError, |
|
2889
e513f0d65368
class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents:
2864
diff
changeset
|
300 "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
|
301 return -1; |
|
e513f0d65368
class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents:
2864
diff
changeset
|
302 } |
|
6155
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
303 sname = PyString_AsString(name); |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
304 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
|
305 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
|
306 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
|
307 char *err = NULL; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
308 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
|
309 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
|
310 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
|
311 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
|
312 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 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
|
320 /* 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
|
321 dictionary as well. */ |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
322 if (err != NULL) { |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
323 if (*err == '\0') |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
324 return 0; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
325 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
|
326 return -1; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
327 } |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
328 } |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
329 } |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
330 if (v == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
331 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
|
332 if (rv < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
333 PyErr_SetString(PyExc_AttributeError, |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
334 "delete non-existing class attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
335 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
336 } |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
337 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
338 return PyDict_SetItem(op->cl_dict, name, v); |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
339 } |
|
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
340 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
341 static PyObject * |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
342 class_repr(op) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
343 PyClassObject *op; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
344 { |
|
6502
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
345 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
346 char buf[140]; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
347 char *name; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
348 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
|
349 name = "?"; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
350 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
351 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
|
352 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
|
353 sprintf(buf, "<class ?.%.100s at %lx>", name, (long)op); |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
354 else |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
355 sprintf(buf, "<class %.50s.%.50s at %lx>", |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
356 PyString_AsString(mod), |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
357 name, (long)op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
358 return PyString_FromString(buf); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
359 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
360 |
|
6502
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
361 static PyObject * |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
362 class_str(op) |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
363 PyClassObject *op; |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
364 { |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
365 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
|
366 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
|
367 PyObject *res; |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
368 int m, n; |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
369 |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
370 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
|
371 return class_repr(op); |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
372 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
|
373 Py_INCREF(name); |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
374 return name; |
|
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 m = PyString_Size(mod); |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
377 n = PyString_Size(name); |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
378 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
|
379 if (res != NULL) { |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
380 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
|
381 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
|
382 s += m; |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
383 *s++ = '.'; |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
384 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
|
385 } |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
386 return res; |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
387 } |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
388 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
389 PyTypeObject PyClass_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
390 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 391 0, |
| 392 "class", | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
393 sizeof(PyClassObject), |
| 6 | 394 0, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
395 (destructor)class_dealloc, /*tp_dealloc*/ |
| 6 | 396 0, /*tp_print*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
397 0, /*tp_getattr*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
398 0, /*tp_setattr*/ |
| 6 | 399 0, /*tp_compare*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
400 (reprfunc)class_repr, /*tp_repr*/ |
| 6 | 401 0, /*tp_as_number*/ |
| 402 0, /*tp_as_sequence*/ | |
| 403 0, /*tp_as_mapping*/ | |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
404 0, /*tp_hash*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
405 0, /*tp_call*/ |
|
6502
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
406 (reprfunc)class_str, /*tp_str*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
407 (getattrofunc)class_getattr, /*tp_getattro*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
408 (setattrofunc)class_setattr, /*tp_setattro*/ |
| 6 | 409 }; |
| 410 | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
411 int |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
412 PyClass_IsSubclass(class, base) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
413 PyObject *class; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
414 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
|
415 { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
416 int i, n; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
417 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
|
418 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
|
419 return 1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
420 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
|
421 return 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
422 cp = (PyClassObject *)class; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
423 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
|
424 for (i = 0; i < n; i++) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
425 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
|
426 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
|
427 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
428 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
|
429 } |
| 6 | 430 |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
431 |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
432 /* Instance objects */ |
| 6 | 433 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
434 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
435 PyInstance_New(class, arg, kw) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
436 PyObject *class; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
437 PyObject *arg; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
438 PyObject *kw; |
| 6 | 439 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
440 register PyInstanceObject *inst; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
441 PyObject *init; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
442 static PyObject *initstr; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
443 if (!PyClass_Check(class)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
444 PyErr_BadInternalCall(); |
| 6 | 445 return NULL; |
| 446 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
447 inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type); |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
448 if (inst == NULL) |
| 6 | 449 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
450 Py_INCREF(class); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
451 inst->in_class = (PyClassObject *)class; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
452 inst->in_dict = PyDict_New(); |
|
5456
7316eecb1e6b
removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
453 if (inst->in_dict == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
454 Py_DECREF(inst); |
| 6 | 455 return NULL; |
| 456 } | |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
457 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
|
458 initstr = PyString_InternFromString("__init__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
459 init = instance_getattr1(inst, initstr); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
460 if (init == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
461 PyErr_Clear(); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
462 if ((arg != NULL && (!PyTuple_Check(arg) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
463 PyTuple_Size(arg) != 0)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
464 || (kw != NULL && (!PyDict_Check(kw) || |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
465 PyDict_Size(kw) != 0))) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
466 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
|
467 "this constructor takes no arguments"); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
468 Py_DECREF(inst); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
469 inst = NULL; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
470 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
471 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
472 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
473 PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
474 Py_DECREF(init); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
475 if (res == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
476 Py_DECREF(inst); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
477 inst = NULL; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
478 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
479 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
480 if (res != Py_None) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
481 PyErr_SetString(PyExc_TypeError, |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
482 "__init__() should return None"); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
483 Py_DECREF(inst); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
484 inst = NULL; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
485 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
486 Py_DECREF(res); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
487 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
488 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
489 return (PyObject *)inst; |
| 6 | 490 } |
| 491 | |
|
174
20fee4e2eecd
Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
492 /* Instance methods */ |
| 6 | 493 |
| 494 static void | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
495 instance_dealloc(inst) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
496 register PyInstanceObject *inst; |
| 6 | 497 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
498 PyObject *error_type, *error_value, *error_traceback; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
499 PyObject *del; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
500 static PyObject *delstr; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
501 /* Call the __del__ method if it exists. First temporarily |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
502 revive the object and save the current exception, if any. */ |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
503 #ifdef Py_TRACE_REFS |
|
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
504 /* 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
|
505 extern long _Py_RefTotal; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
506 inst->ob_type = &PyInstance_Type; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
507 _Py_NewReference(inst); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
508 _Py_RefTotal--; /* compensate for increment in NEWREF */ |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
509 #ifdef COUNT_ALLOCS |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
510 inst->ob_type->tp_alloc--; /* ditto */ |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
511 #endif |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
512 #else /* !Py_TRACE_REFS */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
513 Py_INCREF(inst); |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
514 #endif /* !Py_TRACE_REFS */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
515 PyErr_Fetch(&error_type, &error_value, &error_traceback); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
516 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
|
517 delstr = PyString_InternFromString("__del__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
518 if ((del = instance_getattr1(inst, delstr)) != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
519 PyObject *res = PyEval_CallObject(del, (PyObject *)NULL); |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
520 if (res == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
521 PyObject *f, *t, *v, *tb; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
522 PyErr_Fetch(&t, &v, &tb); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
523 f = PySys_GetObject("stderr"); |
|
4289
0036dcf28402
More detailed error message about exception in __del__
Guido van Rossum <guido@python.org>
parents:
4142
diff
changeset
|
524 if (f != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
525 PyFile_WriteString("Exception ", f); |
|
4583
4f644ecc5bac
Yet more elaborate message for exception in __del__.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
526 if (t) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
527 PyFile_WriteObject(t, f, Py_PRINT_RAW); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
528 if (v && v != Py_None) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
529 PyFile_WriteString(": ", f); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
530 PyFile_WriteObject(v, f, 0); |
|
4583
4f644ecc5bac
Yet more elaborate message for exception in __del__.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
531 } |
|
4f644ecc5bac
Yet more elaborate message for exception in __del__.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
532 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
533 PyFile_WriteString(" in ", f); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
534 PyFile_WriteObject(del, f, 0); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
535 PyFile_WriteString(" ignored\n", f); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
536 PyErr_Clear(); /* Just in case */ |
|
4289
0036dcf28402
More detailed error message about exception in __del__
Guido van Rossum <guido@python.org>
parents:
4142
diff
changeset
|
537 } |
|
4583
4f644ecc5bac
Yet more elaborate message for exception in __del__.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
538 Py_XDECREF(t); |
|
4f644ecc5bac
Yet more elaborate message for exception in __del__.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
539 Py_XDECREF(v); |
|
4f644ecc5bac
Yet more elaborate message for exception in __del__.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
540 Py_XDECREF(tb); |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
541 } |
|
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
542 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
543 Py_DECREF(res); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
544 Py_DECREF(del); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
545 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
546 /* Restore the saved exception and undo the temporary revival */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
547 PyErr_Restore(error_type, error_value, error_traceback); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
548 /* Can't use DECREF here, it would cause a recursive call */ |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
549 if (--inst->ob_refcnt > 0) { |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
550 #ifdef COUNT_ALLOCS |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
551 inst->ob_type->tp_free--; |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
552 #endif |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
553 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
|
554 } |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
555 #ifdef Py_TRACE_REFS |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
556 #ifdef COUNT_ALLOCS |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
557 inst->ob_type->tp_free--; /* compensate for increment in UNREF */ |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
558 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
559 _Py_ForgetReference(inst); |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
560 inst->ob_type = NULL; |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
561 #endif /* Py_TRACE_REFS */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
562 Py_DECREF(inst->in_class); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
563 Py_XDECREF(inst->in_dict); |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
564 free((ANY *)inst); |
| 6 | 565 } |
| 566 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
567 static PyObject * |
|
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
|
568 instance_getattr1(inst, name) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
569 register PyInstanceObject *inst; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
570 PyObject *name; |
| 6 | 571 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
572 register PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
573 register char *sname = PyString_AsString(name); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
574 PyClassObject *class; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
575 if (sname[0] == '_' && sname[1] == '_') { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
576 if (strcmp(sname, "__dict__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
577 if (PyEval_GetRestricted()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
578 PyErr_SetString(PyExc_RuntimeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
579 "instance.__dict__ not accessible in restricted mode"); |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
580 return NULL; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
581 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
582 Py_INCREF(inst->in_dict); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
583 return inst->in_dict; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
584 } |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
585 if (strcmp(sname, "__class__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
586 Py_INCREF(inst->in_class); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
587 return (PyObject *)inst->in_class; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
588 } |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
589 } |
|
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
|
590 class = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
591 v = PyDict_GetItem(inst->in_dict, name); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
592 if (v == NULL) { |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
593 v = class_lookup(inst->in_class, name, &class); |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
594 if (v == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
595 PyErr_SetObject(PyExc_AttributeError, name); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
596 return NULL; |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
597 } |
| 6 | 598 } |
|
5456
7316eecb1e6b
removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
599 Py_INCREF(v); |
|
1351
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
600 if (class != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
601 if (PyFunction_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
602 PyObject *w = PyMethod_New(v, (PyObject *)inst, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
603 (PyObject *)class); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
604 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
|
605 v = w; |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
606 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
607 else if (PyMethod_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
608 PyObject *im_class = PyMethod_Class(v); |
|
1351
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
609 /* Only if classes are compatible */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
610 if (PyClass_IsSubclass((PyObject *)class, im_class)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
611 PyObject *im_func = PyMethod_Function(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
612 PyObject *w = PyMethod_New(im_func, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
613 (PyObject *)inst, im_class); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
614 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
|
615 v = w; |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
616 } |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
617 } |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
618 } |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
619 return v; |
| 6 | 620 } |
| 621 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
622 static PyObject * |
|
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
|
623 instance_getattr(inst, name) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
624 register PyInstanceObject *inst; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
625 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
|
626 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
627 register PyObject *func, *res; |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
628 res = instance_getattr1(inst, name); |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
629 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
|
630 PyObject *args; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
631 PyErr_Clear(); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
632 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
|
633 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
|
634 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
635 res = PyEval_CallObject(func, args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
636 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
|
637 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
638 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
|
639 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
640 |
| 6 | 641 static int |
|
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
|
642 instance_setattr1(inst, name, v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
643 PyInstanceObject *inst; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
644 PyObject *name; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
645 PyObject *v; |
| 6 | 646 { |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
647 if (v == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
648 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
|
649 if (rv < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
650 PyErr_SetString(PyExc_AttributeError, |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
651 "delete non-existing instance attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
652 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
653 } |
| 6 | 654 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
655 return PyDict_SetItem(inst->in_dict, name, v); |
| 6 | 656 } |
| 657 | |
|
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
|
658 static int |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
659 instance_setattr(inst, name, v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
660 PyInstanceObject *inst; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
661 PyObject *name; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
662 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
|
663 { |
|
6155
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
664 PyObject *func, *args, *res, *tmp; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
665 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
|
666 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
|
667 int n = PyString_Size(name); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
668 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
|
669 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
|
670 if (PyEval_GetRestricted()) { |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
671 PyErr_SetString(PyExc_RuntimeError, |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
672 "__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
|
673 return -1; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
674 } |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
675 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
|
676 PyErr_SetString(PyExc_TypeError, |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
677 "__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
|
678 return -1; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
679 } |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
680 tmp = inst->in_dict; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
681 Py_INCREF(v); |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
682 inst->in_dict = v; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
683 Py_DECREF(tmp); |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
684 return 0; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
685 } |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
686 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
|
687 if (PyEval_GetRestricted()) { |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
688 PyErr_SetString(PyExc_RuntimeError, |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
689 "__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
|
690 return -1; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
691 } |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
692 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
|
693 PyErr_SetString(PyExc_TypeError, |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
694 "__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
|
695 return -1; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
696 } |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
697 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
|
698 Py_INCREF(v); |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
699 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
|
700 Py_DECREF(tmp); |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
701 return 0; |
|
47867006f076
Allow assignments to instance.__dict__ and instance.__class__. The
Guido van Rossum <guido@python.org>
parents:
5932
diff
changeset
|
702 } |
|
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
|
703 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
704 } |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
705 if (v == NULL) |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
706 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
|
707 else |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
708 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
|
709 if (func == NULL) |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
710 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
|
711 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
712 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
|
713 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
714 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
|
715 if (args == NULL) |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
716 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
717 res = PyEval_CallObject(func, args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
718 Py_DECREF(args); |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
719 if (res == NULL) |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
720 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
721 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
|
722 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
|
723 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
724 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
725 static PyObject * |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
726 instance_repr(inst) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
727 PyInstanceObject *inst; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
728 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
729 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
730 PyObject *res; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
731 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
|
732 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
733 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
|
734 reprstr = PyString_InternFromString("__repr__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
735 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
|
736 if (func == NULL) { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
737 char buf[140]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
738 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
|
739 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
|
740 inst->in_class->cl_dict, "__module__"); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
741 char *cname; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
742 if (classname != NULL && PyString_Check(classname)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
743 cname = PyString_AsString(classname); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
744 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
745 cname = "?"; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
746 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
|
747 if (mod == NULL || !PyString_Check(mod)) |
|
2662c9fe1b3e
Change the default repr() and str() of class instance objects to look
Guido van Rossum <guido@python.org>
parents:
6595
diff
changeset
|
748 sprintf(buf, "<?.%.100s instance at %lx>", |
|
2662c9fe1b3e
Change the default repr() and str() of class instance objects to look
Guido van Rossum <guido@python.org>
parents:
6595
diff
changeset
|
749 cname, (long)inst); |
|
2662c9fe1b3e
Change the default repr() and str() of class instance objects to look
Guido van Rossum <guido@python.org>
parents:
6595
diff
changeset
|
750 else |
|
2662c9fe1b3e
Change the default repr() and str() of class instance objects to look
Guido van Rossum <guido@python.org>
parents:
6595
diff
changeset
|
751 sprintf(buf, "<%.50s.%.50s instance at %lx>", |
|
2662c9fe1b3e
Change the default repr() and str() of class instance objects to look
Guido van Rossum <guido@python.org>
parents:
6595
diff
changeset
|
752 PyString_AsString(mod), |
|
2662c9fe1b3e
Change the default repr() and str() of class instance objects to look
Guido van Rossum <guido@python.org>
parents:
6595
diff
changeset
|
753 cname, (long)inst); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
754 return PyString_FromString(buf); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
755 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
756 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
757 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
|
758 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
759 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
760 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
761 static PyObject * |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
762 instance_compare1(inst, other) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
763 PyObject *inst, *other; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
764 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
765 return PyInstance_DoBinOp(inst, other, "__cmp__", "__rcmp__", |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
766 instance_compare1); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
767 } |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
768 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
769 static int |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
770 instance_compare(inst, other) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
771 PyObject *inst, *other; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
772 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
773 PyObject *result; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
774 long outcome; |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
775 result = instance_compare1(inst, other); |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
776 if (result == NULL) |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
777 return -1; |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
778 if (!PyInt_Check(result)) { |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
779 Py_DECREF(result); |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
780 PyErr_SetString(PyExc_TypeError, |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
781 "comparison did not return an int"); |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
782 return -1; |
|
1982
33515485ead0
Comparison of two class instances without __cmp__ or __rcmp__ methods
Sjoerd Mullender <sjoerd@acm.org>
parents:
1908
diff
changeset
|
783 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
784 outcome = PyInt_AsLong(result); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
785 Py_DECREF(result); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
786 if (outcome < 0) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
787 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
788 else if (outcome > 0) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
789 return 1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
790 return 0; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
791 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
792 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
793 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
794 instance_hash(inst) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
795 PyInstanceObject *inst; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
796 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
797 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
798 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
|
799 long outcome; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
800 static PyObject *hashstr, *cmpstr; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
801 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
802 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
|
803 hashstr = PyString_InternFromString("__hash__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
804 func = instance_getattr(inst, hashstr); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
805 if (func == NULL) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
806 /* If there is no __cmp__ method, we hash on the address. |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
807 If a __cmp__ method exists, there must be a __hash__. */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
808 PyErr_Clear(); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
809 if (cmpstr == NULL) |
|
5008
f55e82e1e565
Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents:
4583
diff
changeset
|
810 cmpstr = PyString_InternFromString("__cmp__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
811 func = instance_getattr(inst, cmpstr); |
|
1272
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
812 if (func == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
813 PyErr_Clear(); |
|
1272
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
814 outcome = (long)inst; |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
815 if (outcome == -1) |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
816 outcome = -2; |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
817 return outcome; |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
818 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
819 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
|
820 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
821 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
822 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
823 Py_DECREF(func); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
824 if (res == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
825 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
826 if (PyInt_Check(res)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
827 outcome = PyInt_AsLong(res); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
828 if (outcome == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
829 outcome = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
830 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
831 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
832 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
833 "__hash__() should return an int"); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
834 outcome = -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
835 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
836 Py_DECREF(res); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
837 return outcome; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
838 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
839 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
840 static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
841 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
842 static int |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
843 instance_length(inst) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
844 PyInstanceObject *inst; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
845 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
846 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
847 PyObject *res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
848 int outcome; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
849 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
850 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
|
851 lenstr = PyString_InternFromString("__len__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
852 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
|
853 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
854 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
855 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
856 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
|
857 if (res == NULL) |
|
ce718457b172
* classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
858 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
859 if (PyInt_Check(res)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
860 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
|
861 if (outcome < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
862 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
863 "__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
|
864 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
865 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
866 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
867 "__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
|
868 outcome = -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
869 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
870 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
|
871 return outcome; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
872 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
873 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
874 static PyObject * |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
875 instance_subscript(inst, key) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
876 PyInstanceObject *inst; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
877 PyObject *key; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
878 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
879 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
880 PyObject *arg; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
881 PyObject *res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
882 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
883 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
|
884 getitemstr = PyString_InternFromString("__getitem__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
885 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
|
886 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
887 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
888 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
|
889 if (arg == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
890 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
|
891 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
892 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
893 res = PyEval_CallObject(func, arg); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
894 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
895 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
|
896 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
897 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
898 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
899 static int |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
900 instance_ass_subscript(inst, key, value) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
901 PyInstanceObject*inst; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
902 PyObject *key; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
903 PyObject *value; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
904 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
905 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
906 PyObject *arg; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
907 PyObject *res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
908 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
909 if (value == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
910 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
|
911 delitemstr = PyString_InternFromString("__delitem__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
912 func = instance_getattr(inst, delitemstr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
913 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
914 else { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
915 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
|
916 setitemstr = PyString_InternFromString("__setitem__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
917 func = instance_getattr(inst, setitemstr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
918 } |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
919 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
920 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
921 if (value == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
922 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
|
923 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
924 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
|
925 if (arg == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
926 Py_DECREF(func); |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
927 return -1; |
|
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 res = PyEval_CallObject(func, arg); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
930 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
931 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
|
932 if (res == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
933 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
934 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
|
935 return 0; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
936 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
937 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
938 static PyMappingMethods instance_as_mapping = { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
939 (inquiry)instance_length, /*mp_length*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
940 (binaryfunc)instance_subscript, /*mp_subscript*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
941 (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
|
942 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
943 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
944 static PyObject * |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
945 instance_item(inst, i) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
946 PyInstanceObject *inst; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
947 int i; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
948 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
949 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
|
950 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
951 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
|
952 getitemstr = PyString_InternFromString("__getitem__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
953 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
|
954 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
955 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
956 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
|
957 if (arg == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
958 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
|
959 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
960 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
961 res = PyEval_CallObject(func, arg); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
962 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
963 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
|
964 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
965 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
966 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
967 static PyObject * |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
968 instance_slice(inst, i, j) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
969 PyInstanceObject *inst; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
970 int i, j; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
971 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
972 PyObject *func, *arg, *res; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
973 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
|
974 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
975 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
|
976 getslicestr = PyString_InternFromString("__getslice__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
977 func = instance_getattr(inst, getslicestr); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
978 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
979 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
980 arg = Py_BuildValue("(ii)", i, j); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
981 if (arg == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
982 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
|
983 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
984 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
985 res = PyEval_CallObject(func, arg); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
986 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
987 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
|
988 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
989 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
990 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
991 static int |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
992 instance_ass_item(inst, i, item) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
993 PyInstanceObject *inst; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
994 int i; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
995 PyObject *item; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
996 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
997 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
|
998 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
999 if (item == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1000 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
|
1001 delitemstr = PyString_InternFromString("__delitem__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1002 func = instance_getattr(inst, delitemstr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1003 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1004 else { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1005 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
|
1006 setitemstr = PyString_InternFromString("__setitem__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1007 func = instance_getattr(inst, setitemstr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1008 } |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1009 if (func == NULL) |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1010 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1011 if (item == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1012 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
|
1013 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1014 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
|
1015 if (arg == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1016 Py_DECREF(func); |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1017 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1018 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1019 res = PyEval_CallObject(func, arg); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1020 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1021 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
|
1022 if (res == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1023 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1024 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
|
1025 return 0; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1026 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1027 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1028 static int |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1029 instance_ass_slice(inst, i, j, value) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1030 PyInstanceObject *inst; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1031 int i, j; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1032 PyObject *value; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1033 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1034 PyObject *func, *arg, *res; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1035 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
|
1036 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1037 if (value == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1038 if (delslicestr == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1039 delslicestr = |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1040 PyString_InternFromString("__delslice__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1041 func = instance_getattr(inst, delslicestr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1042 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1043 else { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1044 if (setslicestr == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1045 setslicestr = |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1046 PyString_InternFromString("__setslice__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1047 func = instance_getattr(inst, setslicestr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1048 } |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1049 if (func == NULL) |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1050 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1051 if (value == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1052 arg = Py_BuildValue("(ii)", i, j); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1053 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1054 arg = Py_BuildValue("(iiO)", i, j, value); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1055 if (arg == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1056 Py_DECREF(func); |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1057 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1058 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1059 res = PyEval_CallObject(func, arg); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1060 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1061 Py_DECREF(arg); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1062 if (res == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1063 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1064 Py_DECREF(res); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1065 return 0; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1066 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1067 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1068 static PySequenceMethods instance_as_sequence = { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1069 (inquiry)instance_length, /*sq_length*/ |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1070 0, /*sq_concat*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1071 0, /*sq_repeat*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1072 (intargfunc)instance_item, /*sq_item*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1073 (intintargfunc)instance_slice, /*sq_slice*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1074 (intobjargproc)instance_ass_item, /*sq_ass_item*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1075 (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1076 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1077 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1078 static PyObject * |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1079 generic_unary_op(self, methodname) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1080 PyInstanceObject *self; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1081 PyObject *methodname; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1082 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1083 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
|
1084 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1085 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
|
1086 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1087 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1088 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
|
1089 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1090 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1091 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1092 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1093 /* Forward */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1094 static int halfbinop Py_PROTO((PyObject *, PyObject *, char *, PyObject **, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1095 PyObject * (*) Py_PROTO((PyObject *, PyObject *)), int )); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1096 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1097 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1098 /* Implement a binary operator involving at least one class instance. */ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1099 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1100 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1101 PyInstance_DoBinOp(v, w, opname, ropname, thisfunc) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1102 PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1103 PyObject *w; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1104 char *opname; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1105 char *ropname; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1106 PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *)); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1107 { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1108 char buf[256]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1109 PyObject *result = NULL; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1110 if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1111 return result; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1112 if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1113 return result; |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
1114 /* Sigh -- special case for comnparisons */ |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
1115 if (strcmp(opname, "__cmp__") == 0) { |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
1116 long c = (v < w) ? -1 : (v > w) ? 1 : 0; |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
1117 return PyInt_FromLong(c); |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
1118 } |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1119 sprintf(buf, "%s nor %s defined for these operands", opname, ropname); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1120 PyErr_SetString(PyExc_TypeError, buf); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1121 return NULL; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1122 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1123 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1124 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1125 /* Try one half of a binary operator involving a class instance. |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1126 Return value: |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1127 -1 if an exception is to be reported right away |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1128 0 if we have a valid result |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1129 1 if we could try another operation |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1130 */ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1131 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1132 static PyObject *coerce_obj; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1133 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1134 static int |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1135 halfbinop(v, w, opname, r_result, thisfunc, swapped) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1136 PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1137 PyObject *w; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1138 char *opname; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1139 PyObject **r_result; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1140 PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *)); |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1141 int swapped; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1142 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1143 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1144 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
|
1145 PyObject *coercefunc; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1146 PyObject *coerced = NULL; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1147 PyObject *v1; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1148 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1149 if (!PyInstance_Check(v)) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1150 return 1; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1151 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
|
1152 coerce_obj = PyString_InternFromString("__coerce__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1153 if (coerce_obj == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1154 return -1; |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1155 } |
|
6595
5e0b152bf1e6
Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents:
6502
diff
changeset
|
1156 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
|
1157 if (coercefunc == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1158 PyErr_Clear(); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1159 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1160 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1161 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
|
1162 if (args == NULL) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1163 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1164 } |
|
6595
5e0b152bf1e6
Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents:
6502
diff
changeset
|
1165 coerced = PyEval_CallObject(coercefunc, args); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1166 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
|
1167 Py_DECREF(coercefunc); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1168 if (coerced == NULL) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1169 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1170 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1171 if (coerced == Py_None) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1172 Py_DECREF(coerced); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1173 return 1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1174 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1175 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1176 Py_DECREF(coerced); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1177 PyErr_SetString(PyExc_TypeError, |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1178 "coercion should return None or 2-tuple"); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1179 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1180 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1181 v1 = PyTuple_GetItem(coerced, 0); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1182 w = PyTuple_GetItem(coerced, 1); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1183 if (v1 != v) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1184 v = v1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1185 if (!PyInstance_Check(v) && !PyInstance_Check(w)) { |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1186 if (swapped) |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1187 *r_result = (*thisfunc)(w, v); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1188 else |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1189 *r_result = (*thisfunc)(v, w); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1190 Py_DECREF(coerced); |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1191 return *r_result == NULL ? -1 : 0; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1192 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1193 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1194 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1195 func = PyObject_GetAttrString(v, opname); |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1196 if (func == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1197 Py_XDECREF(coerced); |
|
8438
c059bbd14b23
Uses PyErr_ExceptionMatches() instead of comparing PyErr_Occurred().
Guido van Rossum <guido@python.org>
parents:
8325
diff
changeset
|
1198 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1199 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1200 PyErr_Clear(); |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1201 return 1; |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1202 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1203 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
|
1204 if (args == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1205 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1206 Py_XDECREF(coerced); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1207 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1208 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1209 *r_result = PyEval_CallObject(func, args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1210 Py_DECREF(args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1211 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1212 Py_XDECREF(coerced); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1213 return *r_result == NULL ? -1 : 0; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1214 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1215 |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1216 static int |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1217 instance_coerce(pv, pw) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1218 PyObject **pv; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1219 PyObject **pw; |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1220 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1221 PyObject *v = *pv; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1222 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
|
1223 PyObject *coercefunc; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1224 PyObject *args; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1225 PyObject *coerced; |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1226 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1227 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
|
1228 coerce_obj = PyString_InternFromString("__coerce__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1229 if (coerce_obj == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1230 return -1; |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1231 } |
|
6595
5e0b152bf1e6
Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents:
6502
diff
changeset
|
1232 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
|
1233 if (coercefunc == NULL) { |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1234 /* No __coerce__ method: always OK */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1235 PyErr_Clear(); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1236 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1237 Py_INCREF(w); |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1238 return 0; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1239 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1240 /* Has __coerce__ method: call it */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1241 args = Py_BuildValue("(O)", w); |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1242 if (args == NULL) { |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1243 return -1; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1244 } |
|
6595
5e0b152bf1e6
Undo another glitch of the automatic not-so-Grand Renaming; some local
Guido van Rossum <guido@python.org>
parents:
6502
diff
changeset
|
1245 coerced = PyEval_CallObject(coercefunc, args); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1246 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
|
1247 Py_DECREF(coercefunc); |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1248 if (coerced == NULL) { |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1249 /* __coerce__ call raised an exception */ |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1250 return -1; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1251 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1252 if (coerced == Py_None) { |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1253 /* __coerce__ says "I can't do it" */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1254 Py_DECREF(coerced); |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1255 return 1; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1256 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1257 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
|
1258 /* __coerce__ return value is malformed */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1259 Py_DECREF(coerced); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1260 PyErr_SetString(PyExc_TypeError, |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1261 "coercion should return None or 2-tuple"); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1262 return -1; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1263 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1264 /* __coerce__ returned two new values */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1265 *pv = PyTuple_GetItem(coerced, 0); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1266 *pw = PyTuple_GetItem(coerced, 1); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1267 Py_INCREF(*pv); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1268 Py_INCREF(*pw); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1269 Py_DECREF(coerced); |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1270 return 0; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1271 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1272 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1273 |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1274 #define UNARY(funcname, methodname) \ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1275 static PyObject *funcname(self) PyInstanceObject *self; { \ |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1276 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
|
1277 if (o == NULL) o = PyString_InternFromString(methodname); \ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1278 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
|
1279 } |
|
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 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
|
1282 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
|
1283 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
|
1284 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1285 static int |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1286 instance_nonzero(self) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1287 PyInstanceObject *self; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1288 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1289 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
|
1290 long outcome; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1291 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
|
1292 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1293 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
|
1294 nonzerostr = PyString_InternFromString("__nonzero__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1295 if ((func = instance_getattr(self, nonzerostr)) == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1296 PyErr_Clear(); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1297 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
|
1298 lenstr = PyString_InternFromString("__len__"); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1299 if ((func = instance_getattr(self, lenstr)) == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1300 PyErr_Clear(); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1301 /* 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
|
1302 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
|
1303 return 1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1304 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1305 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1306 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1307 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
|
1308 if (res == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1309 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1310 if (!PyInt_Check(res)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1311 Py_DECREF(res); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1312 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1313 "__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
|
1314 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1315 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1316 outcome = PyInt_AsLong(res); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1317 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
|
1318 if (outcome < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1319 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1320 "__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
|
1321 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1322 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1323 return outcome > 0; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1324 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1325 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1326 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
|
1327 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
|
1328 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
|
1329 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
|
1330 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
|
1331 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
|
1332 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1333 /* 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
|
1334 static PyObject * |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1335 instance_pow(v, w, z) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1336 PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1337 PyObject *w; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1338 PyObject *z; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1339 { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1340 /* XXX Doesn't do coercions... */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1341 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1342 PyObject *args; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1343 PyObject *result; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1344 static PyObject *powstr; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1345 |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1346 if (powstr == NULL) |
|
5008
f55e82e1e565
Intern the various string objects created to speed up lookups.
Guido van Rossum <guido@python.org>
parents:
4583
diff
changeset
|
1347 powstr = PyString_InternFromString("__pow__"); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1348 func = PyObject_GetAttr(v, powstr); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1349 if (func == NULL) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1350 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1351 args = Py_BuildValue("(OO)", w, z); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1352 if (args == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1353 Py_DECREF(func); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1354 return NULL; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1355 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1356 result = PyEval_CallObject(func, args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1357 Py_DECREF(func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1358 Py_DECREF(args); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1359 return result; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1360 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1361 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1362 static PyNumberMethods instance_as_number = { |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1363 0, /*nb_add*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1364 0, /*nb_subtract*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1365 0, /*nb_multiply*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1366 0, /*nb_divide*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1367 0, /*nb_remainder*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1368 0, /*nb_divmod*/ |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
1369 (ternaryfunc)instance_pow, /*nb_power*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1370 (unaryfunc)instance_neg, /*nb_negative*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1371 (unaryfunc)instance_pos, /*nb_positive*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1372 (unaryfunc)instance_abs, /*nb_absolute*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1373 (inquiry)instance_nonzero, /*nb_nonzero*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1374 (unaryfunc)instance_invert, /*nb_invert*/ |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1375 0, /*nb_lshift*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1376 0, /*nb_rshift*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1377 0, /*nb_and*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1378 0, /*nb_xor*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1379 0, /*nb_or*/ |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1380 (coercion)instance_coerce, /*nb_coerce*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1381 (unaryfunc)instance_int, /*nb_int*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1382 (unaryfunc)instance_long, /*nb_long*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1383 (unaryfunc)instance_float, /*nb_float*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1384 (unaryfunc)instance_oct, /*nb_oct*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1385 (unaryfunc)instance_hex, /*nb_hex*/ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1386 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1387 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1388 PyTypeObject PyInstance_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1389 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 1390 0, |
|
174
20fee4e2eecd
Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1391 "instance", |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1392 sizeof(PyInstanceObject), |
| 6 | 1393 0, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1394 (destructor)instance_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
1395 0, /*tp_print*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1396 0, /*tp_getattr*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1397 0, /*tp_setattr*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1398 instance_compare, /*tp_compare*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1399 (reprfunc)instance_repr, /*tp_repr*/ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1400 &instance_as_number, /*tp_as_number*/ |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1401 &instance_as_sequence, /*tp_as_sequence*/ |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1402 &instance_as_mapping, /*tp_as_mapping*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1403 (hashfunc)instance_hash, /*tp_hash*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1404 0, /*tp_call*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1405 0, /*tp_str*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1406 (getattrofunc)instance_getattr, /*tp_getattro*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1407 (setattrofunc)instance_setattr, /*tp_setattro*/ |
| 6 | 1408 }; |
| 1409 | |
| 1410 | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1411 /* 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
|
1412 (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
|
1413 (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
|
1414 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
|
1415 */ |
| 6 | 1416 |
|
5932
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1417 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
|
1418 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1419 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1420 PyMethod_New(func, self, class) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1421 PyObject *func; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1422 PyObject *self; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1423 PyObject *class; |
| 6 | 1424 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1425 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
|
1426 if (!PyCallable_Check(func)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1427 PyErr_BadInternalCall(); |
| 6 | 1428 return NULL; |
| 1429 } | |
|
5932
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1430 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
|
1431 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
|
1432 free_list = (PyMethodObject *)(im->im_self); |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1433 im->ob_type = &PyMethod_Type; |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1434 _Py_NewReference(im); |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1435 } |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1436 else { |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1437 im = PyObject_NEW(PyMethodObject, &PyMethod_Type); |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1438 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
|
1439 return NULL; |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1440 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1441 Py_INCREF(func); |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1442 im->im_func = func; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1443 Py_XINCREF(self); |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1444 im->im_self = self; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1445 Py_INCREF(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
|
1446 im->im_class = class; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1447 return (PyObject *)im; |
| 6 | 1448 } |
| 1449 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1450 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1451 PyMethod_Function(im) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1452 register PyObject *im; |
| 6 | 1453 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1454 if (!PyMethod_Check(im)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1455 PyErr_BadInternalCall(); |
| 6 | 1456 return NULL; |
| 1457 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1458 return ((PyMethodObject *)im)->im_func; |
| 6 | 1459 } |
| 1460 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1461 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1462 PyMethod_Self(im) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1463 register PyObject *im; |
| 6 | 1464 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1465 if (!PyMethod_Check(im)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1466 PyErr_BadInternalCall(); |
| 6 | 1467 return NULL; |
| 1468 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1469 return ((PyMethodObject *)im)->im_self; |
| 6 | 1470 } |
| 1471 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1472 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1473 PyMethod_Class(im) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1474 register PyObject *im; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1475 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1476 if (!PyMethod_Check(im)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1477 PyErr_BadInternalCall(); |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1478 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
|
1479 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1480 return ((PyMethodObject *)im)->im_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
|
1481 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1482 |
| 6 | 1483 /* Class method methods */ |
| 1484 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1485 #define OFF(x) offsetof(PyMethodObject, x) |
| 86 | 1486 |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1487 static struct memberlist instancemethod_memberlist[] = { |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1488 {"im_func", T_OBJECT, OFF(im_func)}, |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1489 {"im_self", T_OBJECT, OFF(im_self)}, |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1490 {"im_class", T_OBJECT, OFF(im_class)}, |
|
3549
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1491 /* Dummies that are not handled by getattr() except for __members__ */ |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1492 {"__doc__", T_INT, 0}, |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1493 {"__name__", T_INT, 0}, |
| 86 | 1494 {NULL} /* Sentinel */ |
| 1495 }; | |
| 1496 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1497 static PyObject * |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1498 instancemethod_getattr(im, name) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1499 register PyMethodObject *im; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1500 PyObject *name; |
| 86 | 1501 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1502 char *sname = PyString_AsString(name); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1503 if (sname[0] == '_') { |
|
8651
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1504 /* Inherit __name__ and __doc__ from the callable object |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1505 implementing the method */ |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1506 if (strcmp(sname, "__name__") == 0 || |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1507 strcmp(sname, "__doc__") == 0) |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1508 return PyObject_GetAttr(im->im_func, name); |
|
3549
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1509 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1510 if (PyEval_GetRestricted()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1511 PyErr_SetString(PyExc_RuntimeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1512 "instance-method attributes not accessible in restricted mode"); |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
1513 return NULL; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
1514 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1515 return PyMember_Get((char *)im, instancemethod_memberlist, sname); |
| 86 | 1516 } |
| 1517 | |
| 6 | 1518 static void |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1519 instancemethod_dealloc(im) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1520 register PyMethodObject *im; |
| 6 | 1521 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1522 Py_DECREF(im->im_func); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1523 Py_XDECREF(im->im_self); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1524 Py_DECREF(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
|
1525 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
|
1526 free_list = im; |
| 6 | 1527 } |
| 1528 | |
|
1021
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1529 static int |
|
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1530 instancemethod_compare(a, b) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1531 PyMethodObject *a, *b; |
|
1021
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1532 { |
|
2641
f98a2233b337
change comparing instance methods
Guido van Rossum <guido@python.org>
parents:
2239
diff
changeset
|
1533 if (a->im_self != b->im_self) |
|
f98a2233b337
change comparing instance methods
Guido van Rossum <guido@python.org>
parents:
2239
diff
changeset
|
1534 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
|
1535 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
|
1536 } |
|
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1537 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1538 static PyObject * |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1539 instancemethod_repr(a) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1540 PyMethodObject *a; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1541 { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1542 char buf[240]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1543 PyInstanceObject *self = (PyInstanceObject *)(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
|
1544 PyObject *func = a->im_func; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1545 PyClassObject *class = (PyClassObject *)(a->im_class); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1546 PyObject *fclassname, *iclassname, *funcname; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1547 char *fcname, *icname, *fname; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1548 fclassname = class->cl_name; |
|
8651
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1549 if (PyFunction_Check(func)) { |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1550 funcname = ((PyFunctionObject *)func)->func_name; |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1551 Py_INCREF(funcname); |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1552 } |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1553 else { |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1554 funcname = PyObject_GetAttrString(func,"__name__"); |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1555 if (funcname == NULL) |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1556 PyErr_Clear(); |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1557 } |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1558 if (funcname != NULL && PyString_Check(funcname)) |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1559 fname = PyString_AS_STRING(funcname); |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1560 else |
|
4ba9b70ca56e
Marc-Andre Lemburg's patch to support instance methods with other
Guido van Rossum <guido@python.org>
parents:
8649
diff
changeset
|
1561 fname = "?"; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1562 if (fclassname != NULL && PyString_Check(fclassname)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1563 fcname = PyString_AsString(fclassname); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1564 else |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1565 fcname = "?"; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1566 if (self == NULL) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1567 sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1568 else { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1569 iclassname = self->in_class->cl_name; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1570 if (iclassname != NULL && PyString_Check(iclassname)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1571 icname = PyString_AsString(iclassname); |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1572 else |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1573 icname = "?"; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1574 sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>", |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1575 fcname, fname, icname, (long)self); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1576 } |
|
11378
dc9f1f964690
Fix for PR#98 (Adrian Eyre) -- in instancemethod_repr, the funcname
Guido van Rossum <guido@python.org>
parents:
8806
diff
changeset
|
1577 Py_XDECREF(funcname); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1578 return PyString_FromString(buf); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1579 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1580 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1581 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1582 instancemethod_hash(a) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1583 PyMethodObject *a; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1584 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1585 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
|
1586 if (a->im_self == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1587 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
|
1588 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1589 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
|
1590 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1591 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1592 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
|
1593 if (y == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1594 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1595 return x ^ y; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1596 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1597 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1598 PyTypeObject PyMethod_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1599 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 1600 0, |
|
202
bfffc7e0fcc3
Rename class methods to instance methods (at least where user-visible)
Guido van Rossum <guido@python.org>
parents:
174
diff
changeset
|
1601 "instance method", |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1602 sizeof(PyMethodObject), |
| 6 | 1603 0, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1604 (destructor)instancemethod_dealloc, /*tp_dealloc*/ |
| 6 | 1605 0, /*tp_print*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1606 0, /*tp_getattr*/ |
| 6 | 1607 0, /*tp_setattr*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1608 (cmpfunc)instancemethod_compare, /*tp_compare*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1609 (reprfunc)instancemethod_repr, /*tp_repr*/ |
| 6 | 1610 0, /*tp_as_number*/ |
| 1611 0, /*tp_as_sequence*/ | |
| 1612 0, /*tp_as_mapping*/ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1613 (hashfunc)instancemethod_hash, /*tp_hash*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1614 0, /*tp_call*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1615 0, /*tp_str*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1616 (getattrofunc)instancemethod_getattr, /*tp_getattro*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1617 0, /*tp_setattro*/ |
| 6 | 1618 }; |
|
5932
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1619 |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1620 /* 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
|
1621 |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1622 void |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1623 PyMethod_Fini() |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1624 { |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1625 while (free_list) { |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1626 PyMethodObject *v = free_list; |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1627 free_list = (PyMethodObject *)(v->im_self); |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1628 PyMem_DEL(v); |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1629 } |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
1630 } |
