annotate Objects/classobject.c @ 19283:b865cd5c1925 legacy-trunk

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