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