annotate Objects/classobject.c @ 17581:c1937faabb23 2.1

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