annotate Objects/classobject.c @ 13163:a09e8635534d legacy-trunk

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