annotate Objects/classobject.c @ 8553:4357b4eb5c19 legacy-trunk

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