annotate Objects/abstract.c @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 337562330fd0
children 2f8d9c22de62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 The Netherlands.
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
8 documentation for any purpose and without fee is hereby granted,
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
10 both that copyright notice and this permission notice appear in
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
15 permission.
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4344
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* Abstract Object Interface (many thanks to Jim Fulton) */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include "Python.h"
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 #define Py_TRY(E) if(!(E)) return NULL
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 #define Py_ASSERT(EXP,E,V) if(!(EXP)) return PyErr_SetString(E,V), (void*)NULL
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 #define SPAM printf("line %d\n",__LINE__)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 static PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
41 Py_ReturnMethodError(name)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
42 char *name;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 if(! name) name = "Unknown Error";
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 PyErr_SetString(PyExc_AttributeError,name);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
4529
337562330fd0 Make Py_ReturnNullError() statis as it should be.
Guido van Rossum <guido@python.org>
parents: 4528
diff changeset
49 static PyObject *
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 Py_ReturnNullError()
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 if(! PyErr_Occurred())
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 PyErr_SetString(PyExc_SystemError,
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 "null argument to internal routine");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
59 PyObject_Cmp(o1, o2, result)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
60 PyObject *o1;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
61 PyObject *o2;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
62 int *result;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 int r;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if(! o1 || ! o2) return Py_ReturnNullError(),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 r=PyObject_Compare(o1,o2);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 if(PyErr_Occurred()) return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 *result=r;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 #if 0 /* Already in object.c */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
75 PyCallable_Check(x)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
76 PyObject *x;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 if (x == NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 if (x->ob_type->tp_call != NULL ||
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 PyFunction_Check(x) ||
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 PyMethod_Check(x) ||
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 PyCFunction_Check(x) ||
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 PyClass_Check(x))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 return 1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 if (PyInstance_Check(x)) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 PyObject *call = PyObject_GetAttrString(x, "__call__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if (call == NULL) {
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
89 PyErr_Clear();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 /* Could test recursively but don't, for fear of endless
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 recursion if some joker sets self.__call__ = self */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 Py_DECREF(call);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 return 1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 #endif
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
102 PyObject_Type(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
103 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 PyObject *v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 if(! o) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 v = (PyObject *)o->ob_type;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 Py_INCREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 return v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
114 PyObject_Length(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
115 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 PySequenceMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 if(! o) return Py_ReturnNullError(),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 if((m=o->ob_type->tp_as_sequence) && m->sq_length)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return m->sq_length(o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 return PyMapping_Length(o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
128 PyObject_GetItem(o, key)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
129 PyObject *o;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
130 PyObject *key;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 PyMappingMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 if(! o || ! key) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
135
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 if((m=o->ob_type->tp_as_mapping) && m->mp_subscript)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 return m->mp_subscript(o,key);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 if(PyInt_Check(key))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return PySequence_GetItem(o,PyInt_AsLong(key));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 PyErr_SetString(PyExc_TypeError,"expected integer index");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
147 PyObject_SetItem(o, key, value)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
148 PyObject *o;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
149 PyObject *key;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
150 PyObject *value;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 PyMappingMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 if(! o || ! key || ! value) return Py_ReturnNullError(),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 if((m=o->ob_type->tp_as_mapping) && m->mp_ass_subscript)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 return m->mp_ass_subscript(o,key,value);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 if(PyInt_Check(key))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 return PySequence_SetItem(o,PyInt_AsLong(key),value);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 PyErr_SetString(PyExc_TypeError,"expeced integer index");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
165 int
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
166 PyObject_DelItem(o, key)
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
167 PyObject *o;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
168 PyObject *key;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
169 {
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
170 PyMappingMethods *m;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
171
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
172 if(! o || ! key) return Py_ReturnNullError(),-1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
173 if((m=o->ob_type->tp_as_mapping) && m->mp_ass_subscript)
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
174 return m->mp_ass_subscript(o,key,(PyObject*)NULL);
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
175
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
176 if(PyInt_Check(key))
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
177 return PySequence_SetItem(o,PyInt_AsLong(key),(PyObject*)NULL);
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
178
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
179 PyErr_SetString(PyExc_TypeError,"expeced integer index");
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
180 return -1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
181 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
182
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
184 PyNumber_Check(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
185 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 return o && o->ob_type->tp_as_number;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 #define BINOP(opname, ropname, thisfunc) \
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 if (!PyInstance_Check(v) && !PyInstance_Check(w)) \
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 ; \
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 else \
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 PyNumber_Or(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 extern int PyNumber_Coerce();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 BINOP("__or__", "__ror__", PyNumber_Or);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 x = (*f)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 if (f != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for |");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 PyNumber_Xor(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 extern int PyNumber_Coerce();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 BINOP("__xor__", "__rxor__", PyNumber_Xor);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 x = (*f)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 if (f != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for ^");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 PyNumber_And(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 BINOP("__and__", "__rand__", PyNumber_And);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 x = (*f)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 if (f != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for &");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
263
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 PyNumber_Lshift(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 BINOP("__lshift__", "__rlshift__", PyNumber_Lshift);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 x = (*f)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 if (f != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for <<");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 PyNumber_Rshift(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 BINOP("__rshift__", "__rrshift__", PyNumber_Rshift);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 x = (*f)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 if (f != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for >>");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
305
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 PyNumber_Add(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 BINOP("__add__", "__radd__", PyNumber_Add);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 if (v->ob_type->tp_as_sequence != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 return (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 else if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 x = (*v->ob_type->tp_as_number->nb_add)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for +");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
325
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 PyNumber_Subtract(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 BINOP("__sub__", "__rsub__", PyNumber_Subtract);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 x = (*v->ob_type->tp_as_number->nb_subtract)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for -");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
343
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 PyNumber_Multiply(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 PyTypeObject *tp;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 tp = v->ob_type;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 BINOP("__mul__", "__rmul__", PyNumber_Multiply);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 if (tp->tp_as_number != NULL &&
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 w->ob_type->tp_as_sequence != NULL &&
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 !PyInstance_Check(v)) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 /* number*sequence -- swap v and w */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 PyObject *tmp = v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 v = w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 w = tmp;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 tp = v->ob_type;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 if (tp->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 if (PyInstance_Check(v)) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 /* Instances of user-defined classes get their
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 other argument uncoerced, so they may
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 implement sequence*number as well as
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 number*number. */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 Py_INCREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 Py_INCREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 else if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 x = (*v->ob_type->tp_as_number->nb_multiply)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 if (tp->tp_as_sequence != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 if (!PyInt_Check(w)) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 PyErr_SetString(PyExc_TypeError,
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 "can't multiply sequence with non-int");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 return (*tp->tp_as_sequence->sq_repeat)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 (v, (int)PyInt_AsLong(w));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for *");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 PyNumber_Divide(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 BINOP("__div__", "__rdiv__", PyNumber_Divide);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 x = (*v->ob_type->tp_as_number->nb_divide)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for /");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 PyNumber_Remainder(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 if (PyString_Check(v)) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 return PyString_Format(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 BINOP("__mod__", "__rmod__", PyNumber_Remainder);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 if (v->ob_type->tp_as_number != NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 PyObject *x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 x = (*v->ob_type->tp_as_number->nb_remainder)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 return x;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for %");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
428
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 PyNumber_Divmod(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 PyObject *res;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
434
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 if (PyInstance_Check(v) || PyInstance_Check(w))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 return PyInstance_DoBinOp(v, w, "__divmod__", "__rdivmod__",
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 PyNumber_Divmod);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 if (v->ob_type->tp_as_number == NULL ||
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 w->ob_type->tp_as_number == NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 PyErr_SetString(PyExc_TypeError,
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 "divmod() requires numeric or class instance arguments");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 return res;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
451
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
452
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 static PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 do_pow(v, w)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 PyObject *v, *w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 PyObject *res;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 if (PyInstance_Check(v) || PyInstance_Check(w))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 return PyInstance_DoBinOp(v, w, "__pow__", "__rpow__", do_pow);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 if (v->ob_type->tp_as_number == NULL ||
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 w->ob_type->tp_as_number == NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 PyErr_SetString(PyExc_TypeError,
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 "pow() requires numeric arguments");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 if (PyFloat_Check(w) && PyFloat_AsDouble(v) < 0.0) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 if (!PyErr_Occurred())
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 PyErr_SetString(PyExc_ValueError,
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 "negative number to float power");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 res = (*v->ob_type->tp_as_number->nb_power)(v, w, Py_None);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 return res;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
479
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 PyNumber_Power(v,w,z)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 PyObject *v, *w, *z;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 PyObject *res;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 PyObject *v1, *z1, *w2, *z2;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
486
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 if (z == Py_None)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 return do_pow(v, w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 /* XXX The ternary version doesn't do class instance coercions */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 if (PyInstance_Check(v))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 return v->ob_type->tp_as_number->nb_power(v, w, z);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 if (v->ob_type->tp_as_number == NULL ||
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 z->ob_type->tp_as_number == NULL ||
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 w->ob_type->tp_as_number == NULL) {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 PyErr_SetString(PyExc_TypeError, "pow() requires numeric arguments");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 if (PyNumber_Coerce(&v, &w) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 res = NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 v1 = v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 z1 = z;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 if (PyNumber_Coerce(&v1, &z1) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 goto error2;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 w2 = w;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 z2 = z1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 if (PyNumber_Coerce(&w2, &z2) != 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 goto error1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 Py_DECREF(w2);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 Py_DECREF(z2);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 error1:
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 Py_DECREF(v1);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 Py_DECREF(z1);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 error2:
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 Py_DECREF(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 Py_DECREF(w);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 return res;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
520
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
521
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 PyNumber_Negative(v)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 PyObject *v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 if (v->ob_type->tp_as_number != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 return (*v->ob_type->tp_as_number->nb_negative)(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary -");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
531
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 PyNumber_Positive(v)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 PyObject *v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 if (v->ob_type->tp_as_number != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 return (*v->ob_type->tp_as_number->nb_positive)(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary +");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
541
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 PyNumber_Invert(v)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 PyObject *v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 PyObject * (*f) Py_FPROTO((PyObject *));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 if (v->ob_type->tp_as_number != NULL &&
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 (f = v->ob_type->tp_as_number->nb_invert) != NULL)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 return (*f)(v);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary ~");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
553
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
555 PyNumber_Absolute(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
556 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 PyNumberMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
559
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 if(! o) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 if((m=o->ob_type->tp_as_number) && m->nb_absolute)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 return m->nb_absolute(o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
563
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 return Py_ReturnMethodError("__abs__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
566
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
568 PyNumber_Int(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
569 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 PyNumberMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
572
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 if(! o) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 if((m=o->ob_type->tp_as_number) && m->nb_int)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 return m->nb_int(o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 return Py_ReturnMethodError("__int__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
579
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
581 PyNumber_Long(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
582 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 PyNumberMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
585
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 if(! o) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 if((m=o->ob_type->tp_as_number) && m->nb_long)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 return m->nb_long(o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
589
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 return Py_ReturnMethodError("__long__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
592
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
594 PyNumber_Float(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
595 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 PyNumberMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
598
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 if(! o) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 if((m=o->ob_type->tp_as_number) && m->nb_float)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 return m->nb_float(o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
602
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 return Py_ReturnMethodError("__float__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
605
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
606
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
608 PySequence_Check(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
609 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 return o && o->ob_type->tp_as_sequence;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
613
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
615 PySequence_Length(s)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
616 PyObject *s;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 PySequenceMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
619
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 if(! s) return Py_ReturnNullError(),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
621
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 if((m=s->ob_type->tp_as_sequence) && m->sq_length)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 return m->sq_length(s);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
624
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 Py_ReturnMethodError("__len__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
628
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
630 PySequence_Concat(s, o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
631 PyObject *s;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
632 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 PySequenceMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
635
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 if(! s || ! o) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
637
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 if((m=s->ob_type->tp_as_sequence) && m->sq_concat)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 return m->sq_concat(s,o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
640
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 return Py_ReturnMethodError("__concat__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
643
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
645 PySequence_Repeat(o, count)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
646 PyObject *o;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
647 int count;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 PySequenceMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
650
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 if(! o) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
652
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 if((m=o->ob_type->tp_as_sequence) && m->sq_repeat)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 return m->sq_repeat(o,count);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
655
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 return Py_ReturnMethodError("__repeat__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
658
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
660 PySequence_GetItem(s, i)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
661 PyObject *s;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
662 int i;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 PySequenceMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 int l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
666
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 if(! s) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
668
4528
2824cbdbe12c Only call sq_length in Sequence_GetItem for negative index.
Guido van Rossum <guido@python.org>
parents: 4515
diff changeset
669 if(! ((m=s->ob_type->tp_as_sequence) && m->sq_item))
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 return Py_ReturnMethodError("__getitem__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
671
4528
2824cbdbe12c Only call sq_length in Sequence_GetItem for negative index.
Guido van Rossum <guido@python.org>
parents: 4515
diff changeset
672 if(i < 0)
2824cbdbe12c Only call sq_length in Sequence_GetItem for negative index.
Guido van Rossum <guido@python.org>
parents: 4515
diff changeset
673 {
2824cbdbe12c Only call sq_length in Sequence_GetItem for negative index.
Guido van Rossum <guido@python.org>
parents: 4515
diff changeset
674 if(0 > (l=m->sq_length(s))) return NULL;
2824cbdbe12c Only call sq_length in Sequence_GetItem for negative index.
Guido van Rossum <guido@python.org>
parents: 4515
diff changeset
675 i += l;
2824cbdbe12c Only call sq_length in Sequence_GetItem for negative index.
Guido van Rossum <guido@python.org>
parents: 4515
diff changeset
676 }
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
677
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 return m->sq_item(s,i);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
680
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
681 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
682 PySequence_GetSlice(s, i1, i2)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
683 PyObject *s;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
684 int i1;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
685 int i2;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 PySequenceMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 int l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
689
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 if(! s) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
691
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_slice))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 return Py_ReturnMethodError("__getslice__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
694
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 if(0 > (l=m->sq_length(s))) return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
696
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 if(i1 < 0) i1 += l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 if(i2 < 0) i2 += l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
699
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 return m->sq_slice(s,i1,i2);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
702
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
704 PySequence_SetItem(s, i, o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
705 PyObject *s;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
706 int i;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
707 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 PySequenceMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 int l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 if(! s) return Py_ReturnNullError(),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
712
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_item))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 return Py_ReturnMethodError("__setitem__"),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
715
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 if(i < 0)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 if(0 > (l=m->sq_length(s))) return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 i += l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
721
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 return m->sq_ass_item(s,i,o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
724
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
725 int
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
726 PySequence_DelItem(s, i)
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
727 PyObject *s;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
728 int i;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
729 {
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
730 PySequenceMethods *m;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
731 int l;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
732 if(! s) return Py_ReturnNullError(),-1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
733
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
734 if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_item))
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
735 return Py_ReturnMethodError("__delitem__"),-1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
736
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
737 if(i < 0)
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
738 {
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
739 if(0 > (l=m->sq_length(s))) return -1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
740 i += l;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
741 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
742
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
743 return m->sq_ass_item(s,i,(PyObject*)NULL);
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
744 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
745
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
747 PySequence_SetSlice(s, i1, i2, o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
748 PyObject *s;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
749 int i1;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
750 int i2;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
751 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 PySequenceMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 int l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
755
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 if(! s) return Py_ReturnNullError(),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
757
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_slice))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 return Py_ReturnMethodError("__setslice__"),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
760
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 if(0 > (l=m->sq_length(s))) return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
762
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 if(i1 < 0) i1 += l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 if(i2 < 0) i2 += l;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
765
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 return m->sq_ass_slice(s,i1,i2,o);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
768
4090
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
769 int
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
770 PySequence_DelSlice(s, i1, i2)
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
771 PyObject *s;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
772 int i1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
773 int i2;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
774 {
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
775 PySequenceMethods *m;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
776 int l;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
777
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
778 if(! s) return Py_ReturnNullError(),-1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
779
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
780 if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_slice))
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
781 return Py_ReturnMethodError("__delslice__"),-1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
782
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
783 if(0 > (l=m->sq_length(s))) return -1;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
784
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
785 if(i1 < 0) i1 += l;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
786 if(i2 < 0) i2 += l;
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
787
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
788 return m->sq_ass_slice(s,i1,i2,(PyObject*)NULL);
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
789 }
0b40c8d5b21d Added PyObject_DelItem and PySequence_Del{Item,Slice}.
Guido van Rossum <guido@python.org>
parents: 3097
diff changeset
790
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
792 PySequence_Tuple(s)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
793 PyObject *s;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
795 int l, i;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 PyObject *t, *item;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
797
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 if(! s) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
799
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 Py_TRY((l=PySequence_Length(s)) != -1);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 Py_TRY(t=PyTuple_New(l));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
802
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 for(i=0; i < l; i++)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
804 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 if(item=PySequence_GetItem(s,i))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 if(PyTuple_SetItem(t,i,item) == -1)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 Py_DECREF(item);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 Py_DECREF(t);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 else
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 Py_DECREF(t);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 return t;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
822
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
823 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
824 PySequence_Count(s, o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
825 PyObject *s;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
826 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
828 int l, i, n=0, not_equal, err;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
829 PyObject *item;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
830
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
831 if(! s || ! o) return Py_ReturnNullError(), -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
832 Py_TRY((l=PySequence_Length(s)) != -1),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
833
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
834 for(i=0; i < l; i++)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
835 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 Py_TRY(item=PySequence_GetItem(s,i)),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 err=PyObject_Cmp(item,o,&not_equal) == -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
838 Py_DECREF(item);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 if(err) return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
840 n += ! not_equal;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 return n;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
843 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
844
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
845 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
846 PySequence_In(s, o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
847 PyObject *s;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
848 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 int l, i, not_equal, err;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 PyObject *item;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
852
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
853 if(! o || ! s) return Py_ReturnNullError(), -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 Py_TRY((l=PySequence_Length(s)) != -1),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
855
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 for(i=0; i < l; i++)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 Py_TRY(item=PySequence_GetItem(s,i)),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
859 err=PyObject_Cmp(item,o,&not_equal) == -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 Py_DECREF(item);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 if(err) return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
862 if(! not_equal) return 1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
864 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
865 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
866
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
867 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
868 PySequence_Index(s, o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
869 PyObject *s;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
870 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
871 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
872 int l, i, n=0, not_equal, err;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 PyObject *item;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
874
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
875 if(! s || ! o) return Py_ReturnNullError(), -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 Py_TRY((l=PySequence_Length(s)) != -1),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
877
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 for(i=0; i < l; i++)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
879 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 Py_TRY(item=PySequence_GetItem(s,i)),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
881 err=PyObject_Cmp(item,o,&not_equal) == -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
882 Py_DECREF(item);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
883 if(err) return -1;
4515
a75858fef550 correct typo in return variable for PySequence_Index()
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
884 if(! not_equal) return i;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
885 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
886 return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
888
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
889 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
890 PyMapping_Check(o)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
891 PyObject *o;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
892 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
893 return o && o->ob_type->tp_as_mapping;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
895
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
896 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
897 PyMapping_Length(s)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
898 PyObject *s;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 PyMappingMethods *m;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
901
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
902 if(! s) return Py_ReturnNullError(),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
903
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
904 if((m=s->ob_type->tp_as_mapping) && m->mp_length)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
905 return m->mp_length(s);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
906
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
907 Py_ReturnMethodError("__len__");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
908 return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
909 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
910
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
911 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
912 PyMapping_HasKeyString(o, key)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
913 PyObject *o;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
914 char *key;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
916 PyObject *v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
917
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
918 v=PyMapping_GetItemString(o,key);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
919 if(v) return 1;
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
920 PyErr_Clear();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
922 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
923
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
924 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
925 PyMapping_HasKey(o, key)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
926 PyObject *o;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
927 PyObject *key;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
928 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
929 PyObject *v;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
930
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 v=PyObject_GetItem(o,key);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
932 if(v) return 1;
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
933 PyErr_Clear();
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
934 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
935 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
936
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
938 PyObject_CallObject(o, a)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
939 PyObject *o, *a;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
940 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 PyObject *r;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
942
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
943 if(a) return PyEval_CallObject(o,a);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
944
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
945 if(! (a=PyTuple_New(0)))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
947 r=PyEval_CallObject(o,a);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
948 Py_DECREF(a);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
949 return r;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
951
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
952 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
953 #ifdef HAVE_STDARG_PROTOTYPES
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
954 /* VARARGS 2 */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
955 PyObject_CallFunction(PyObject *callable, char *format, ...)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
956 #else
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 /* VARARGS */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 PyObject_CallFunction(va_alist) va_dcl
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
959 #endif
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
960 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
961 va_list va;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
962 PyObject *args, *retval;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
963 #ifdef HAVE_STDARG_PROTOTYPES
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
964 va_start(va, format);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
965 #else
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 PyObject *callable;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 char *format;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 va_start(va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
969 callable = va_arg(va, PyObject *);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 format = va_arg(va, char *);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 #endif
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
972
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 if( ! callable)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 va_end(va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
978
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 if(format)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 args = Py_VaBuildValue(format, va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 else
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 args = PyTuple_New(0);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
983
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
984 va_end(va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 if(! args) return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
986
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
987 if(! PyTuple_Check(args))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
988 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 PyObject *a;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
990
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 Py_TRY(a=PyTuple_New(1));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 Py_TRY(PyTuple_SetItem(a,0,args) != -1);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 args=a;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
994 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 retval = PyObject_CallObject(callable,args);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 Py_DECREF(args);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 return retval;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
999
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 PyObject *
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 #ifdef HAVE_STDARG_PROTOTYPES
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 /* VARARGS 2 */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003 PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 #else
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005 /* VARARGS */
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 PyObject_CallMethod(va_alist) va_dcl
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 #endif
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1009 va_list va;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1010 PyObject *args, *method=0, *retval;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1011 #ifdef HAVE_STDARG_PROTOTYPES
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 va_start(va, format);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013 #else
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 PyObject *o;
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
1015 char *name;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 char *format;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 va_start(va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018 o = va_arg(va, PyObject *);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 name = va_arg(va, char *);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1020 format = va_arg(va, char *);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021 #endif
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 if( ! o || ! name)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 va_end(va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026 return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1029 method=PyObject_GetAttrString(o,name);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1030 if(! method)
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032 va_end(va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 PyErr_SetString(PyExc_AttributeError,name);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1037 if(! (PyCallable_Check(method)))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 va_end(va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1040 PyErr_SetString(PyExc_TypeError,"call of non-callable attribute");
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041 return 0;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043
4344
5cbdba4d2e15 Fix subtle bug detected by Jim F.
Guido van Rossum <guido@python.org>
parents: 4090
diff changeset
1044 if(format && *format)
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 args = Py_VaBuildValue(format, va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1046 else
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1047 args = PyTuple_New(0);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1048
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049 va_end(va);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1051 if(! args) return NULL;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1052
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 if(! PyTuple_Check(args))
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055 PyObject *a;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057 Py_TRY(a=PyTuple_New(1));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058 Py_TRY(PyTuple_SetItem(a,0,args) != -1);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 args=a;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1062 retval = PyObject_CallObject(method,args);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063 Py_DECREF(args);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1064 Py_DECREF(method);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 return retval;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1066 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068 PyObject *
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
1069 PyMapping_GetItemString(o, key)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
1070 PyObject *o;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
1071 char *key;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1072 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1073 PyObject *okey, *r;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1074
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075 if( ! key) return Py_ReturnNullError();
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1076 Py_TRY(okey=PyString_FromString(key));
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1077 r = PyObject_GetItem(o,okey);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1078 Py_DECREF(okey);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079 return r;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080 }
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1081
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1082 int
3097
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
1083 PyMapping_SetItemString(o, key, value)
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
1084 PyObject *o;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
1085 char *key;
8aa6918a88c6 adapted to K&R C
Guido van Rossum <guido@python.org>
parents: 2840
diff changeset
1086 PyObject *value;
2840
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087 {
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088 PyObject *okey;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1089 int r;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1090
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1091 if( ! key) return Py_ReturnNullError(),-1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1092 if (!(okey=PyString_FromString(key))) return -1;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 r = PyObject_SetItem(o,okey,value);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1094 Py_DECREF(okey);
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 return r;
d5398acafa2c Generic Abstract Object Interface
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 }