blob: bc36c6fdd4311ee3975f721162d8e01561269a44 [file] [log] [blame]
Guido van Rossume15dee51995-07-18 14:12:02 +00001/* Abstract Object Interface (many thanks to Jim Fulton) */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00002
Guido van Rossume15dee51995-07-18 14:12:02 +00003#include "Python.h"
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00004#include <ctype.h>
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00005#include "structmember.h" /* we need the offsetof() macro from there */
Tim Peters64b5ce32001-09-10 20:52:51 +00006#include "longintrepr.h"
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00007
8#define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \
Guido van Rossumc3189692001-01-17 15:29:42 +00009 Py_TPFLAGS_CHECKTYPES)
Guido van Rossume15dee51995-07-18 14:12:02 +000010
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000011/* Shorthands to return certain errors */
Guido van Rossume15dee51995-07-18 14:12:02 +000012
13static PyObject *
Fred Drake4201b9e2000-07-09 04:34:13 +000014type_error(const char *msg)
Guido van Rossume15dee51995-07-18 14:12:02 +000015{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000016 PyErr_SetString(PyExc_TypeError, msg);
17 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000018}
19
Guido van Rossum052b7e11996-11-11 15:08:19 +000020static PyObject *
Fred Drake79912472000-07-09 04:06:11 +000021null_error(void)
Guido van Rossume15dee51995-07-18 14:12:02 +000022{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000023 if (!PyErr_Occurred())
24 PyErr_SetString(PyExc_SystemError,
25 "null argument to internal routine");
26 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +000027}
28
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000029/* Operations on any object */
30
31int
Fred Drake79912472000-07-09 04:06:11 +000032PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000033{
34 int r;
35
36 if (o1 == NULL || o2 == NULL) {
37 null_error();
38 return -1;
39 }
40 r = PyObject_Compare(o1, o2);
41 if (PyErr_Occurred())
42 return -1;
43 *result = r;
Guido van Rossume15dee51995-07-18 14:12:02 +000044 return 0;
45}
Guido van Rossume15dee51995-07-18 14:12:02 +000046
47PyObject *
Fred Drake79912472000-07-09 04:06:11 +000048PyObject_Type(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000049{
50 PyObject *v;
51
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000052 if (o == NULL)
53 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +000054 v = (PyObject *)o->ob_type;
55 Py_INCREF(v);
56 return v;
57}
58
59int
Jeremy Hylton6253f832000-07-12 12:56:19 +000060PyObject_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +000061{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000062 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000063
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000064 if (o == NULL) {
65 null_error();
66 return -1;
67 }
Guido van Rossume15dee51995-07-18 14:12:02 +000068
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000069 m = o->ob_type->tp_as_sequence;
70 if (m && m->sq_length)
71 return m->sq_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000072
Jeremy Hylton6253f832000-07-12 12:56:19 +000073 return PyMapping_Size(o);
Guido van Rossume15dee51995-07-18 14:12:02 +000074}
75
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +000076#undef PyObject_Length
77int
78PyObject_Length(PyObject *o)
79{
80 return PyObject_Size(o);
81}
82#define PyObject_Length PyObject_Size
83
Guido van Rossume15dee51995-07-18 14:12:02 +000084PyObject *
Fred Drake79912472000-07-09 04:06:11 +000085PyObject_GetItem(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +000086{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000087 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +000088
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000089 if (o == NULL || key == NULL)
90 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +000091
Guido van Rossumcea1c8c1998-05-22 00:47:05 +000092 m = o->ob_type->tp_as_mapping;
93 if (m && m->mp_subscript)
94 return m->mp_subscript(o, key);
Guido van Rossume15dee51995-07-18 14:12:02 +000095
Guido van Rossum21308241998-08-13 16:44:44 +000096 if (o->ob_type->tp_as_sequence) {
97 if (PyInt_Check(key))
98 return PySequence_GetItem(o, PyInt_AsLong(key));
Andrew M. Kuchling0f223d22000-02-23 22:21:50 +000099 else if (PyLong_Check(key)) {
100 long key_value = PyLong_AsLong(key);
101 if (key_value == -1 && PyErr_Occurred())
102 return NULL;
103 return PySequence_GetItem(o, key_value);
104 }
Guido van Rossum64585f62001-11-24 18:24:47 +0000105 else if (o->ob_type->tp_as_sequence->sq_item)
106 return type_error("sequence index must be integer");
Guido van Rossum21308241998-08-13 16:44:44 +0000107 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000108
109 return type_error("unsubscriptable object");
Guido van Rossume15dee51995-07-18 14:12:02 +0000110}
111
112int
Fred Drake79912472000-07-09 04:06:11 +0000113PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000114{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000115 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000116
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000117 if (o == NULL || key == NULL || value == NULL) {
118 null_error();
119 return -1;
120 }
121 m = o->ob_type->tp_as_mapping;
122 if (m && m->mp_ass_subscript)
123 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000124
Guido van Rossum21308241998-08-13 16:44:44 +0000125 if (o->ob_type->tp_as_sequence) {
126 if (PyInt_Check(key))
127 return PySequence_SetItem(o, PyInt_AsLong(key), value);
Andrew M. Kuchling0f223d22000-02-23 22:21:50 +0000128 else if (PyLong_Check(key)) {
129 long key_value = PyLong_AsLong(key);
130 if (key_value == -1 && PyErr_Occurred())
131 return -1;
132 return PySequence_SetItem(o, key_value, value);
133 }
Guido van Rossum64585f62001-11-24 18:24:47 +0000134 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
135 type_error("sequence index must be integer");
136 return -1;
137 }
Guido van Rossum21308241998-08-13 16:44:44 +0000138 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000139
140 type_error("object does not support item assignment");
141 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000142}
143
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000144int
Fred Drake79912472000-07-09 04:06:11 +0000145PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000146{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000147 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000148
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000149 if (o == NULL || key == NULL) {
150 null_error();
151 return -1;
152 }
153 m = o->ob_type->tp_as_mapping;
154 if (m && m->mp_ass_subscript)
155 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000156
Guido van Rossum21308241998-08-13 16:44:44 +0000157 if (o->ob_type->tp_as_sequence) {
158 if (PyInt_Check(key))
159 return PySequence_DelItem(o, PyInt_AsLong(key));
Andrew M. Kuchling0f223d22000-02-23 22:21:50 +0000160 else if (PyLong_Check(key)) {
161 long key_value = PyLong_AsLong(key);
162 if (key_value == -1 && PyErr_Occurred())
163 return -1;
164 return PySequence_DelItem(o, key_value);
165 }
Guido van Rossum64585f62001-11-24 18:24:47 +0000166 else if (o->ob_type->tp_as_sequence->sq_ass_item) {
167 type_error("sequence index must be integer");
168 return -1;
169 }
Guido van Rossum21308241998-08-13 16:44:44 +0000170 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000171
172 type_error("object does not support item deletion");
173 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000174}
175
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000176int
177PyObject_DelItemString(PyObject *o, char *key)
178{
179 PyObject *okey;
180 int ret;
181
182 if (o == NULL || key == NULL) {
183 null_error();
184 return -1;
185 }
186 okey = PyString_FromString(key);
187 if (okey == NULL)
188 return -1;
189 ret = PyObject_DelItem(o, okey);
190 Py_DECREF(okey);
191 return ret;
192}
193
Guido van Rossum4c08d552000-03-10 22:55:18 +0000194int PyObject_AsCharBuffer(PyObject *obj,
195 const char **buffer,
196 int *buffer_len)
197{
198 PyBufferProcs *pb;
199 const char *pp;
200 int len;
201
202 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
203 null_error();
204 return -1;
205 }
206 pb = obj->ob_type->tp_as_buffer;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000207 if (pb == NULL ||
Guido van Rossum4c08d552000-03-10 22:55:18 +0000208 pb->bf_getcharbuffer == NULL ||
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000209 pb->bf_getsegcount == NULL) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000210 PyErr_SetString(PyExc_TypeError,
211 "expected a character buffer object");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000212 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000213 }
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000214 if ((*pb->bf_getsegcount)(obj,NULL) != 1) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000215 PyErr_SetString(PyExc_TypeError,
216 "expected a single-segment buffer object");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000217 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000218 }
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000219 len = (*pb->bf_getcharbuffer)(obj, 0, &pp);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000220 if (len < 0)
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000221 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000222 *buffer = pp;
223 *buffer_len = len;
224 return 0;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000225}
Guido van Rossum4c08d552000-03-10 22:55:18 +0000226
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000227int
228PyObject_CheckReadBuffer(PyObject *obj)
229{
230 PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
231
232 if (pb == NULL ||
233 pb->bf_getreadbuffer == NULL ||
234 pb->bf_getsegcount == NULL ||
235 (*pb->bf_getsegcount)(obj, NULL) != 1)
236 return 0;
237 return 1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000238}
239
240int PyObject_AsReadBuffer(PyObject *obj,
241 const void **buffer,
242 int *buffer_len)
243{
244 PyBufferProcs *pb;
245 void *pp;
246 int len;
247
248 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
249 null_error();
250 return -1;
251 }
252 pb = obj->ob_type->tp_as_buffer;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000253 if (pb == NULL ||
Guido van Rossum4c08d552000-03-10 22:55:18 +0000254 pb->bf_getreadbuffer == NULL ||
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000255 pb->bf_getsegcount == NULL) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000256 PyErr_SetString(PyExc_TypeError,
257 "expected a readable buffer object");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000258 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000259 }
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000260 if ((*pb->bf_getsegcount)(obj, NULL) != 1) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000261 PyErr_SetString(PyExc_TypeError,
262 "expected a single-segment buffer object");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000263 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000264 }
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000265 len = (*pb->bf_getreadbuffer)(obj, 0, &pp);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000266 if (len < 0)
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000267 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000268 *buffer = pp;
269 *buffer_len = len;
270 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000271}
272
273int PyObject_AsWriteBuffer(PyObject *obj,
274 void **buffer,
275 int *buffer_len)
276{
277 PyBufferProcs *pb;
278 void*pp;
279 int len;
280
281 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
282 null_error();
283 return -1;
284 }
285 pb = obj->ob_type->tp_as_buffer;
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000286 if (pb == NULL ||
Guido van Rossum4c08d552000-03-10 22:55:18 +0000287 pb->bf_getwritebuffer == NULL ||
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000288 pb->bf_getsegcount == NULL) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000289 PyErr_SetString(PyExc_TypeError,
290 "expected a writeable buffer object");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000291 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000292 }
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000293 if ((*pb->bf_getsegcount)(obj, NULL) != 1) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000294 PyErr_SetString(PyExc_TypeError,
295 "expected a single-segment buffer object");
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000296 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000297 }
298 len = (*pb->bf_getwritebuffer)(obj,0,&pp);
299 if (len < 0)
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000300 return -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000301 *buffer = pp;
302 *buffer_len = len;
303 return 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000304}
305
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000306/* Operations on numbers */
307
308int
Fred Drake79912472000-07-09 04:06:11 +0000309PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000310{
Guido van Rossum6921eca2003-02-18 16:36:28 +0000311 return o && o->ob_type->tp_as_number &&
312 (o->ob_type->tp_as_number->nb_int ||
313 o->ob_type->tp_as_number->nb_float);
Guido van Rossume15dee51995-07-18 14:12:02 +0000314}
315
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000316/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000317
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000318/* New style number protocol support */
Guido van Rossume15dee51995-07-18 14:12:02 +0000319
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000320#define NB_SLOT(x) offsetof(PyNumberMethods, x)
321#define NB_BINOP(nb_methods, slot) \
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000322 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000323#define NB_TERNOP(nb_methods, slot) \
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000324 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000325
326/*
327 Calling scheme used for binary operations:
328
329 v w Action
330 -------------------------------------------------------------------
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000331 new new w.op(v,w)[*], v.op(v,w), w.op(v,w)
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000332 new old v.op(v,w), coerce(v,w), v.op(v,w)
333 old new w.op(v,w), coerce(v,w), v.op(v,w)
334 old old coerce(v,w), v.op(v,w)
335
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000336 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
337 v->ob_type
338
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000339 Legend:
340 -------
341 * new == new style number
342 * old == old style number
343 * Action indicates the order in which operations are tried until either
344 a valid result is produced or an error occurs.
345
346 */
347
348static PyObject *
349binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000350{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000351 PyObject *x;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000352 binaryfunc slotv = NULL;
353 binaryfunc slotw = NULL;
354
355 if (v->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(v))
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000356 slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000357 if (w->ob_type != v->ob_type &&
358 w->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(w)) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000359 slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000360 if (slotw == slotv)
361 slotw = NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000362 }
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000363 if (slotv) {
Guido van Rossum89c42642001-10-01 17:10:18 +0000364 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
365 x = slotw(v, w);
366 if (x != Py_NotImplemented)
367 return x;
368 Py_DECREF(x); /* can't do it */
369 slotw = NULL;
370 }
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000371 x = slotv(v, w);
372 if (x != Py_NotImplemented)
373 return x;
374 Py_DECREF(x); /* can't do it */
375 }
376 if (slotw) {
377 x = slotw(v, w);
378 if (x != Py_NotImplemented)
379 return x;
380 Py_DECREF(x); /* can't do it */
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000381 }
382 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w)) {
383 int err = PyNumber_CoerceEx(&v, &w);
384 if (err < 0) {
385 return NULL;
386 }
387 if (err == 0) {
388 PyNumberMethods *mv = v->ob_type->tp_as_number;
389 if (mv) {
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000390 binaryfunc slot;
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000391 slot = NB_BINOP(mv, op_slot);
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000392 if (slot) {
393 PyObject *x = slot(v, w);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000394 Py_DECREF(v);
395 Py_DECREF(w);
396 return x;
397 }
398 }
399 /* CoerceEx incremented the reference counts */
400 Py_DECREF(v);
401 Py_DECREF(w);
402 }
403 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000404 Py_INCREF(Py_NotImplemented);
405 return Py_NotImplemented;
406}
Guido van Rossum77660912002-04-16 16:32:50 +0000407
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000408static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000409binop_type_error(PyObject *v, PyObject *w, const char *op_name)
410{
411 PyErr_Format(PyExc_TypeError,
412 "unsupported operand type(s) for %s: '%s' and '%s'",
413 op_name,
414 v->ob_type->tp_name,
415 w->ob_type->tp_name);
416 return NULL;
417}
418
419static PyObject *
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000420binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
421{
422 PyObject *result = binary_op1(v, w, op_slot);
423 if (result == Py_NotImplemented) {
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000424 Py_DECREF(result);
425 return binop_type_error(v, w, op_name);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000426 }
427 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000428}
429
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000430
431/*
432 Calling scheme used for ternary operations:
433
Guido van Rossum84675ac2001-09-29 01:05:03 +0000434 *** In some cases, w.op is called before v.op; see binary_op1. ***
435
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000436 v w z Action
437 -------------------------------------------------------------------
438 new new new v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
439 new old new v.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
440 old new new w.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
441 old old new z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
442 new new old v.op(v,w,z), w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
443 new old old v.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
444 old new old w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
445 old old old coerce(v,w,z), v.op(v,w,z)
446
447 Legend:
448 -------
449 * new == new style number
450 * old == old style number
451 * Action indicates the order in which operations are tried until either
452 a valid result is produced or an error occurs.
453 * coerce(v,w,z) actually does: coerce(v,w), coerce(v,z), coerce(w,z) and
454 only if z != Py_None; if z == Py_None, then it is treated as absent
455 variable and only coerce(v,w) is tried.
456
457 */
458
459static PyObject *
460ternary_op(PyObject *v,
461 PyObject *w,
462 PyObject *z,
463 const int op_slot,
464 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000465{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000466 PyNumberMethods *mv, *mw, *mz;
Guido van Rossum84675ac2001-09-29 01:05:03 +0000467 PyObject *x = NULL;
468 ternaryfunc slotv = NULL;
469 ternaryfunc slotw = NULL;
470 ternaryfunc slotz = NULL;
Guido van Rossum77660912002-04-16 16:32:50 +0000471
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000472 mv = v->ob_type->tp_as_number;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000473 mw = w->ob_type->tp_as_number;
Guido van Rossum84675ac2001-09-29 01:05:03 +0000474 if (mv != NULL && NEW_STYLE_NUMBER(v))
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000475 slotv = NB_TERNOP(mv, op_slot);
Guido van Rossum84675ac2001-09-29 01:05:03 +0000476 if (w->ob_type != v->ob_type &&
Raymond Hettingerc2e095f2002-12-07 10:05:27 +0000477 mw != NULL && NEW_STYLE_NUMBER(w)) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000478 slotw = NB_TERNOP(mw, op_slot);
Guido van Rossum84675ac2001-09-29 01:05:03 +0000479 if (slotw == slotv)
480 slotw = NULL;
Guido van Rossum4bb1e362001-09-28 23:49:48 +0000481 }
Guido van Rossum84675ac2001-09-29 01:05:03 +0000482 if (slotv) {
Guido van Rossum89c42642001-10-01 17:10:18 +0000483 if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
484 x = slotw(v, w, z);
485 if (x != Py_NotImplemented)
486 return x;
487 Py_DECREF(x); /* can't do it */
488 slotw = NULL;
489 }
Guido van Rossum84675ac2001-09-29 01:05:03 +0000490 x = slotv(v, w, z);
491 if (x != Py_NotImplemented)
492 return x;
493 Py_DECREF(x); /* can't do it */
494 }
495 if (slotw) {
496 x = slotw(v, w, z);
497 if (x != Py_NotImplemented)
498 return x;
499 Py_DECREF(x); /* can't do it */
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000500 }
501 mz = z->ob_type->tp_as_number;
502 if (mz != NULL && NEW_STYLE_NUMBER(z)) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000503 slotz = NB_TERNOP(mz, op_slot);
Guido van Rossum84675ac2001-09-29 01:05:03 +0000504 if (slotz == slotv || slotz == slotw)
505 slotz = NULL;
506 if (slotz) {
507 x = slotz(v, w, z);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000508 if (x != Py_NotImplemented)
509 return x;
Guido van Rossum84675ac2001-09-29 01:05:03 +0000510 Py_DECREF(x); /* can't do it */
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000511 }
512 }
513
514 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w) ||
515 (z != Py_None && !NEW_STYLE_NUMBER(z))) {
516 /* we have an old style operand, coerce */
517 PyObject *v1, *z1, *w2, *z2;
518 int c;
Guido van Rossum77660912002-04-16 16:32:50 +0000519
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000520 c = PyNumber_Coerce(&v, &w);
521 if (c != 0)
522 goto error3;
523
524 /* Special case: if the third argument is None, it is
525 treated as absent argument and not coerced. */
526 if (z == Py_None) {
527 if (v->ob_type->tp_as_number) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000528 slotz = NB_TERNOP(v->ob_type->tp_as_number,
529 op_slot);
Guido van Rossum84675ac2001-09-29 01:05:03 +0000530 if (slotz)
531 x = slotz(v, w, z);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000532 else
533 c = -1;
534 }
535 else
536 c = -1;
537 goto error2;
538 }
539 v1 = v;
540 z1 = z;
541 c = PyNumber_Coerce(&v1, &z1);
542 if (c != 0)
543 goto error2;
544 w2 = w;
545 z2 = z1;
546 c = PyNumber_Coerce(&w2, &z2);
547 if (c != 0)
548 goto error1;
549
550 if (v1->ob_type->tp_as_number != NULL) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000551 slotv = NB_TERNOP(v1->ob_type->tp_as_number,
552 op_slot);
Guido van Rossum84675ac2001-09-29 01:05:03 +0000553 if (slotv)
554 x = slotv(v1, w2, z2);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000555 else
556 c = -1;
557 }
558 else
559 c = -1;
560
561 Py_DECREF(w2);
562 Py_DECREF(z2);
563 error1:
564 Py_DECREF(v1);
565 Py_DECREF(z1);
566 error2:
Guido van Rossume15dee51995-07-18 14:12:02 +0000567 Py_DECREF(v);
568 Py_DECREF(w);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000569 error3:
570 if (c >= 0)
Guido van Rossume15dee51995-07-18 14:12:02 +0000571 return x;
572 }
Guido van Rossum5c66a262001-10-22 04:12:44 +0000573
574 if (z == Py_None)
575 PyErr_Format(
576 PyExc_TypeError,
577 "unsupported operand type(s) for ** or pow(): "
578 "'%s' and '%s'",
579 v->ob_type->tp_name,
580 w->ob_type->tp_name);
581 else
582 PyErr_Format(
583 PyExc_TypeError,
584 "unsupported operand type(s) for pow(): "
585 "'%s', '%s', '%s'",
586 v->ob_type->tp_name,
587 w->ob_type->tp_name,
588 z->ob_type->tp_name);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000589 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000590}
591
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000592#define BINARY_FUNC(func, op, op_name) \
593 PyObject * \
594 func(PyObject *v, PyObject *w) { \
595 return binary_op(v, w, NB_SLOT(op), op_name); \
596 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000597
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000598BINARY_FUNC(PyNumber_Or, nb_or, "|")
599BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
600BINARY_FUNC(PyNumber_And, nb_and, "&")
601BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
602BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
603BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000604BINARY_FUNC(PyNumber_Divide, nb_divide, "/")
605BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000606
607PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000608PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000609{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000610 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
611 if (result == Py_NotImplemented) {
612 PySequenceMethods *m = v->ob_type->tp_as_sequence;
Jeremy Hylton0522d982002-03-08 21:28:54 +0000613 if (m && m->sq_concat) {
614 Py_DECREF(result);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000615 result = (*m->sq_concat)(v, w);
Jeremy Hylton0522d982002-03-08 21:28:54 +0000616 }
Jeremy Hylton6ae6a432002-03-08 21:11:37 +0000617 if (result == Py_NotImplemented) {
Jeremy Hylton0522d982002-03-08 21:28:54 +0000618 Py_DECREF(result);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000619 return binop_type_error(v, w, "+");
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000620 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000621 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000622 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000623}
624
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000625static PyObject *
626sequence_repeat(intargfunc repeatfunc, PyObject *seq, PyObject *n)
627{
628 long count;
629 if (PyInt_Check(n)) {
630 count = PyInt_AsLong(n);
631 }
632 else if (PyLong_Check(n)) {
633 count = PyLong_AsLong(n);
634 if (count == -1 && PyErr_Occurred())
635 return NULL;
636 }
637 else {
638 return type_error(
Raymond Hettinger10c66062004-05-12 21:35:06 +0000639 "can't multiply sequence by non-int");
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000640 }
641#if LONG_MAX != INT_MAX
642 if (count > INT_MAX) {
643 PyErr_SetString(PyExc_ValueError,
644 "sequence repeat count too large");
645 return NULL;
646 }
647 else if (count < INT_MIN)
648 count = INT_MIN;
649 /* XXX Why don't I either
650
651 - set count to -1 whenever it's negative (after all,
652 sequence repeat usually treats negative numbers
653 as zero(); or
654
655 - raise an exception when it's less than INT_MIN?
656
657 I'm thinking about a hypothetical use case where some
658 sequence type might use a negative value as a flag of
659 some kind. In those cases I don't want to break the
660 code by mapping all negative values to -1. But I also
661 don't want to break e.g. []*(-sys.maxint), which is
662 perfectly safe, returning []. As a compromise, I do
663 map out-of-range negative values.
664 */
665#endif
666 return (*repeatfunc)(seq, (int)count);
667}
668
669PyObject *
670PyNumber_Multiply(PyObject *v, PyObject *w)
671{
672 PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
673 if (result == Py_NotImplemented) {
674 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
675 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
Guido van Rossuma3a243e2002-12-31 19:50:03 +0000676 Py_DECREF(result);
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000677 if (mv && mv->sq_repeat) {
678 return sequence_repeat(mv->sq_repeat, v, w);
679 }
680 else if (mw && mw->sq_repeat) {
681 return sequence_repeat(mw->sq_repeat, w, v);
682 }
683 result = binop_type_error(v, w, "*");
684 }
685 return result;
686}
687
Guido van Rossume15dee51995-07-18 14:12:02 +0000688PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000689PyNumber_FloorDivide(PyObject *v, PyObject *w)
690{
691 /* XXX tp_flags test */
692 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
693}
694
695PyObject *
696PyNumber_TrueDivide(PyObject *v, PyObject *w)
697{
698 /* XXX tp_flags test */
699 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
700}
701
702PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000703PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000704{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000705 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +0000706}
707
708PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000709PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +0000710{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000711 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000712}
713
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000714/* Binary in-place operators */
715
716/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000717 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000718
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000719 - If the left hand object has the appropriate struct members, and
720 they are filled, call the appropriate function and return the
721 result. No coercion is done on the arguments; the left-hand object
722 is the one the operation is performed on, and it's up to the
723 function to deal with the right-hand object.
Guido van Rossum77660912002-04-16 16:32:50 +0000724
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000725 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000726 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000727
728 */
729
Guido van Rossum77660912002-04-16 16:32:50 +0000730#define HASINPLACE(t) \
731 PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000732
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000733static PyObject *
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000734binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000735{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000736 PyNumberMethods *mv = v->ob_type->tp_as_number;
737 if (mv != NULL && HASINPLACE(v)) {
Neil Schemenauerb808c5c2002-11-24 01:34:49 +0000738 binaryfunc slot = NB_BINOP(mv, iop_slot);
739 if (slot) {
740 PyObject *x = (slot)(v, w);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000741 if (x != Py_NotImplemented) {
742 return x;
743 }
744 Py_DECREF(x);
745 }
Thomas Woutersf2b332d2000-09-02 08:34:40 +0000746 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000747 return binary_op1(v, w, op_slot);
748}
749
750static PyObject *
751binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
752 const char *op_name)
753{
754 PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
755 if (result == Py_NotImplemented) {
756 Py_DECREF(result);
757 return binop_type_error(v, w, op_name);
758 }
759 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000760}
761
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000762#define INPLACE_BINOP(func, iop, op, op_name) \
763 PyObject * \
764 func(PyObject *v, PyObject *w) { \
765 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000766 }
767
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000768INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
769INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
770INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
771INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
772INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
773INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
774INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000775
776PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000777PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
778{
779 /* XXX tp_flags test */
780 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
781 NB_SLOT(nb_floor_divide), "//=");
782}
783
784PyObject *
785PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
786{
787 /* XXX tp_flags test */
788 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
789 NB_SLOT(nb_true_divide), "/=");
790}
791
792PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000793PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
794{
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000795 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
796 NB_SLOT(nb_add));
797 if (result == Py_NotImplemented) {
798 PySequenceMethods *m = v->ob_type->tp_as_sequence;
799 Py_DECREF(result);
800 if (m != NULL) {
801 binaryfunc f = NULL;
802 if (HASINPLACE(v))
803 f = m->sq_inplace_concat;
804 if (f == NULL)
805 f = m->sq_concat;
806 if (f != NULL)
807 return (*f)(v, w);
808 }
809 result = binop_type_error(v, w, "+=");
Guido van Rossumbb8be932000-09-01 23:27:32 +0000810 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000811 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000812}
813
814PyObject *
815PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
816{
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000817 PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
818 NB_SLOT(nb_multiply));
819 if (result == Py_NotImplemented) {
820 intargfunc f = NULL;
821 PySequenceMethods *mv = v->ob_type->tp_as_sequence;
822 PySequenceMethods *mw = w->ob_type->tp_as_sequence;
823 Py_DECREF(result);
824 if (mv != NULL) {
825 if (HASINPLACE(v))
826 f = mv->sq_inplace_repeat;
827 if (f == NULL)
828 f = mv->sq_repeat;
829 if (f != NULL)
830 return sequence_repeat(f, v, w);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000831 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000832 else if (mw != NULL) {
833 /* Note that the right hand operand should not be
834 * mutated in this case so sq_inplace_repeat is not
835 * used. */
836 if (mw->sq_repeat)
837 return sequence_repeat(mw->sq_repeat, w, v);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000838 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000839 result = binop_type_error(v, w, "*=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000840 }
Neil Schemenauerd4b0fea2002-12-30 20:18:15 +0000841 return result;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000842}
843
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000844PyObject *
845PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
846{
Neil Schemenauerd46fbc32002-11-24 01:07:42 +0000847 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
848 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000849}
850
851PyObject *
852PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
853{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000854 if (HASINPLACE(v) && v->ob_type->tp_as_number &&
855 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
856 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000857 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000858 else {
859 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
860 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000861}
862
863
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000864/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +0000865
866PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000867PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000868{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000869 PyNumberMethods *m;
870
871 if (o == NULL)
872 return null_error();
873 m = o->ob_type->tp_as_number;
874 if (m && m->nb_negative)
875 return (*m->nb_negative)(o);
876
877 return type_error("bad operand type for unary -");
Guido van Rossume15dee51995-07-18 14:12:02 +0000878}
879
880PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000881PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000882{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000883 PyNumberMethods *m;
884
885 if (o == NULL)
886 return null_error();
887 m = o->ob_type->tp_as_number;
888 if (m && m->nb_positive)
889 return (*m->nb_positive)(o);
890
891 return type_error("bad operand type for unary +");
Guido van Rossume15dee51995-07-18 14:12:02 +0000892}
893
894PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000895PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000896{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000897 PyNumberMethods *m;
898
899 if (o == NULL)
900 return null_error();
901 m = o->ob_type->tp_as_number;
902 if (m && m->nb_invert)
903 return (*m->nb_invert)(o);
904
905 return type_error("bad operand type for unary ~");
Guido van Rossume15dee51995-07-18 14:12:02 +0000906}
907
908PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000909PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000910{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000911 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000912
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000913 if (o == NULL)
914 return null_error();
915 m = o->ob_type->tp_as_number;
916 if (m && m->nb_absolute)
917 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000918
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000919 return type_error("bad operand type for abs()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000920}
921
Guido van Rossum9e896b32000-04-05 20:11:21 +0000922/* Add a check for embedded NULL-bytes in the argument. */
923static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000924int_from_string(const char *s, int len)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000925{
926 char *end;
927 PyObject *x;
928
929 x = PyInt_FromString((char*)s, &end, 10);
930 if (x == NULL)
931 return NULL;
932 if (end != s + len) {
933 PyErr_SetString(PyExc_ValueError,
934 "null byte in argument for int()");
935 Py_DECREF(x);
936 return NULL;
937 }
938 return x;
939}
940
Guido van Rossume15dee51995-07-18 14:12:02 +0000941PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000942PyNumber_Int(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000943{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000944 PyNumberMethods *m;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000945 const char *buffer;
946 int buffer_len;
Guido van Rossume15dee51995-07-18 14:12:02 +0000947
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000948 if (o == NULL)
949 return null_error();
Tim Peters64b5ce32001-09-10 20:52:51 +0000950 if (PyInt_CheckExact(o)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000951 Py_INCREF(o);
952 return o;
953 }
Tim Peters64b5ce32001-09-10 20:52:51 +0000954 if (PyInt_Check(o)) {
955 PyIntObject *io = (PyIntObject*)o;
956 return PyInt_FromLong(io->ob_ival);
957 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000958 if (PyString_Check(o))
Guido van Rossum77660912002-04-16 16:32:50 +0000959 return int_from_string(PyString_AS_STRING(o),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000960 PyString_GET_SIZE(o));
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000961#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000962 if (PyUnicode_Check(o))
963 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
964 PyUnicode_GET_SIZE(o),
965 10);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000966#endif
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000967 m = o->ob_type->tp_as_number;
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000968 if (m && m->nb_int) {
969 PyObject *res = m->nb_int(o);
970 if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
971 PyErr_Format(PyExc_TypeError,
972 "__int__ returned non-int (type %.200s)",
973 res->ob_type->tp_name);
974 Py_DECREF(res);
975 return NULL;
976 }
977 return res;
978 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000979 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
Guido van Rossum9e896b32000-04-05 20:11:21 +0000980 return int_from_string((char*)buffer, buffer_len);
Guido van Rossume15dee51995-07-18 14:12:02 +0000981
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000982 return type_error("int() argument must be a string or a number");
Guido van Rossume15dee51995-07-18 14:12:02 +0000983}
984
Guido van Rossum9e896b32000-04-05 20:11:21 +0000985/* Add a check for embedded NULL-bytes in the argument. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000986static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000987long_from_string(const char *s, int len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000988{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000989 char *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000990 PyObject *x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000991
Guido van Rossum4c08d552000-03-10 22:55:18 +0000992 x = PyLong_FromString((char*)s, &end, 10);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000993 if (x == NULL)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000994 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000995 if (end != s + len) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000996 PyErr_SetString(PyExc_ValueError,
997 "null byte in argument for long()");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000998 Py_DECREF(x);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000999 return NULL;
1000 }
1001 return x;
1002}
1003
Guido van Rossume15dee51995-07-18 14:12:02 +00001004PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001005PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001006{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001007 PyNumberMethods *m;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001008 const char *buffer;
1009 int buffer_len;
Guido van Rossume15dee51995-07-18 14:12:02 +00001010
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001011 if (o == NULL)
1012 return null_error();
Tim Peters64b5ce32001-09-10 20:52:51 +00001013 if (PyLong_CheckExact(o)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +00001014 Py_INCREF(o);
1015 return o;
1016 }
Tim Petersdb30ac42002-03-02 04:14:21 +00001017 if (PyLong_Check(o))
1018 return _PyLong_Copy((PyLongObject *)o);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001019 if (PyString_Check(o))
Guido van Rossum77660912002-04-16 16:32:50 +00001020 /* need to do extra error checking that PyLong_FromString()
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001021 * doesn't do. In particular long('9.5') must raise an
1022 * exception, not truncate the float.
1023 */
Guido van Rossum4c08d552000-03-10 22:55:18 +00001024 return long_from_string(PyString_AS_STRING(o),
1025 PyString_GET_SIZE(o));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001026#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +00001027 if (PyUnicode_Check(o))
1028 /* The above check is done in PyLong_FromUnicode(). */
1029 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
1030 PyUnicode_GET_SIZE(o),
1031 10);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001032#endif
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001033 m = o->ob_type->tp_as_number;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001034 if (m && m->nb_long) {
1035 PyObject *res = m->nb_long(o);
1036 if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
1037 PyErr_Format(PyExc_TypeError,
1038 "__long__ returned non-long (type %.200s)",
1039 res->ob_type->tp_name);
1040 Py_DECREF(res);
1041 return NULL;
1042 }
1043 return res;
1044 }
Guido van Rossum4c08d552000-03-10 22:55:18 +00001045 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
1046 return long_from_string(buffer, buffer_len);
Guido van Rossume15dee51995-07-18 14:12:02 +00001047
Raymond Hettinger478d47a2002-06-06 15:45:38 +00001048 return type_error("long() argument must be a string or a number");
Guido van Rossume15dee51995-07-18 14:12:02 +00001049}
1050
1051PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001052PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001053{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001054 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001055
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001056 if (o == NULL)
1057 return null_error();
Tim Peters7a50f252001-09-10 21:28:20 +00001058 if (PyFloat_CheckExact(o)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +00001059 Py_INCREF(o);
1060 return o;
1061 }
Tim Peters7a50f252001-09-10 21:28:20 +00001062 if (PyFloat_Check(o)) {
1063 PyFloatObject *po = (PyFloatObject *)o;
1064 return PyFloat_FromDouble(po->ob_fval);
1065 }
Guido van Rossum4c08d552000-03-10 22:55:18 +00001066 if (!PyString_Check(o)) {
1067 m = o->ob_type->tp_as_number;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001068 if (m && m->nb_float) {
1069 PyObject *res = m->nb_float(o);
1070 if (res && !PyFloat_Check(res)) {
1071 PyErr_Format(PyExc_TypeError,
1072 "__float__ returned non-float (type %.200s)",
1073 res->ob_type->tp_name);
1074 Py_DECREF(res);
1075 return NULL;
1076 }
1077 return res;
1078 }
Guido van Rossum4c08d552000-03-10 22:55:18 +00001079 }
1080 return PyFloat_FromString(o, NULL);
Guido van Rossume15dee51995-07-18 14:12:02 +00001081}
1082
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001083/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +00001084
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001085int
Fred Drake79912472000-07-09 04:06:11 +00001086PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001087{
Raymond Hettingere2eda602004-04-04 08:51:41 +00001088 if (PyInstance_Check(s))
1089 return PyObject_HasAttrString(s, "__getitem__");
Guido van Rossum8700b422001-09-07 20:20:11 +00001090 return s != NULL && s->ob_type->tp_as_sequence &&
1091 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001092}
1093
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001094int
Jeremy Hylton6253f832000-07-12 12:56:19 +00001095PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +00001096{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001097 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001098
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001099 if (s == NULL) {
1100 null_error();
1101 return -1;
1102 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001103
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001104 m = s->ob_type->tp_as_sequence;
1105 if (m && m->sq_length)
1106 return m->sq_length(s);
Guido van Rossume15dee51995-07-18 14:12:02 +00001107
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001108 type_error("len() of unsized object");
1109 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001110}
1111
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001112#undef PySequence_Length
1113int
1114PySequence_Length(PyObject *s)
1115{
1116 return PySequence_Size(s);
1117}
1118#define PySequence_Length PySequence_Size
1119
Guido van Rossume15dee51995-07-18 14:12:02 +00001120PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001121PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001122{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001123 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001124
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001125 if (s == NULL || o == NULL)
1126 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001127
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001128 m = s->ob_type->tp_as_sequence;
1129 if (m && m->sq_concat)
1130 return m->sq_concat(s, o);
1131
1132 return type_error("object can't be concatenated");
Guido van Rossume15dee51995-07-18 14:12:02 +00001133}
1134
1135PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001136PySequence_Repeat(PyObject *o, int count)
Guido van Rossume15dee51995-07-18 14:12:02 +00001137{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001138 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001139
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001140 if (o == NULL)
1141 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001142
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001143 m = o->ob_type->tp_as_sequence;
1144 if (m && m->sq_repeat)
1145 return m->sq_repeat(o, count);
1146
1147 return type_error("object can't be repeated");
Guido van Rossume15dee51995-07-18 14:12:02 +00001148}
1149
1150PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001151PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1152{
1153 PySequenceMethods *m;
1154
1155 if (s == NULL || o == NULL)
1156 return null_error();
1157
1158 m = s->ob_type->tp_as_sequence;
1159 if (m && HASINPLACE(s) && m->sq_inplace_concat)
1160 return m->sq_inplace_concat(s, o);
1161 if (m && m->sq_concat)
1162 return m->sq_concat(s, o);
1163
1164 return type_error("object can't be concatenated");
1165}
1166
1167PyObject *
1168PySequence_InPlaceRepeat(PyObject *o, int count)
1169{
1170 PySequenceMethods *m;
1171
1172 if (o == NULL)
1173 return null_error();
1174
1175 m = o->ob_type->tp_as_sequence;
1176 if (m && HASINPLACE(o) && m->sq_inplace_repeat)
1177 return m->sq_inplace_repeat(o, count);
1178 if (m && m->sq_repeat)
1179 return m->sq_repeat(o, count);
1180
1181 return type_error("object can't be repeated");
1182}
1183
1184PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001185PySequence_GetItem(PyObject *s, int i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001186{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001187 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001188
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001189 if (s == NULL)
1190 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001191
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001192 m = s->ob_type->tp_as_sequence;
1193 if (m && m->sq_item) {
1194 if (i < 0) {
1195 if (m->sq_length) {
1196 int l = (*m->sq_length)(s);
1197 if (l < 0)
1198 return NULL;
1199 i += l;
1200 }
1201 }
1202 return m->sq_item(s, i);
1203 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001204
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001205 return type_error("unindexable object");
Guido van Rossume15dee51995-07-18 14:12:02 +00001206}
1207
Thomas Wouters1d75a792000-08-17 22:37:32 +00001208static PyObject *
1209sliceobj_from_intint(int i, int j)
1210{
1211 PyObject *start, *end, *slice;
1212 start = PyInt_FromLong((long)i);
1213 if (!start)
1214 return NULL;
1215 end = PyInt_FromLong((long)j);
1216 if (!end) {
1217 Py_DECREF(start);
1218 return NULL;
1219 }
1220 slice = PySlice_New(start, end, NULL);
1221 Py_DECREF(start);
1222 Py_DECREF(end);
1223 return slice;
1224}
1225
Guido van Rossume15dee51995-07-18 14:12:02 +00001226PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001227PySequence_GetSlice(PyObject *s, int i1, int i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001228{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001229 PySequenceMethods *m;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001230 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001231
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001232 if (!s) return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001233
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001234 m = s->ob_type->tp_as_sequence;
1235 if (m && m->sq_slice) {
1236 if (i1 < 0 || i2 < 0) {
1237 if (m->sq_length) {
1238 int l = (*m->sq_length)(s);
1239 if (l < 0)
1240 return NULL;
1241 if (i1 < 0)
1242 i1 += l;
1243 if (i2 < 0)
1244 i2 += l;
1245 }
1246 }
1247 return m->sq_slice(s, i1, i2);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001248 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) {
1249 PyObject *res;
1250 PyObject *slice = sliceobj_from_intint(i1, i2);
1251 if (!slice)
1252 return NULL;
1253 res = mp->mp_subscript(s, slice);
1254 Py_DECREF(slice);
1255 return res;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001256 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001257
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001258 return type_error("unsliceable object");
Guido van Rossume15dee51995-07-18 14:12:02 +00001259}
1260
1261int
Fred Drake79912472000-07-09 04:06:11 +00001262PySequence_SetItem(PyObject *s, int i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001263{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001264 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001265
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001266 if (s == NULL) {
1267 null_error();
1268 return -1;
1269 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001270
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001271 m = s->ob_type->tp_as_sequence;
1272 if (m && m->sq_ass_item) {
1273 if (i < 0) {
1274 if (m->sq_length) {
1275 int l = (*m->sq_length)(s);
1276 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001277 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001278 i += l;
1279 }
1280 }
1281 return m->sq_ass_item(s, i, o);
1282 }
1283
Raymond Hettinger98779e02003-10-27 09:22:16 +00001284 type_error("object does not support item assignment");
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001285 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001286}
1287
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001288int
Fred Drake79912472000-07-09 04:06:11 +00001289PySequence_DelItem(PyObject *s, int i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001290{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001291 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001292
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001293 if (s == NULL) {
1294 null_error();
1295 return -1;
1296 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001297
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001298 m = s->ob_type->tp_as_sequence;
1299 if (m && m->sq_ass_item) {
1300 if (i < 0) {
1301 if (m->sq_length) {
1302 int l = (*m->sq_length)(s);
1303 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001304 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001305 i += l;
1306 }
1307 }
1308 return m->sq_ass_item(s, i, (PyObject *)NULL);
1309 }
1310
1311 type_error("object doesn't support item deletion");
1312 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001313}
1314
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001315int
Fred Drake79912472000-07-09 04:06:11 +00001316PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001317{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001318 PySequenceMethods *m;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001319 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001320
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001321 if (s == NULL) {
1322 null_error();
1323 return -1;
1324 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001325
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001326 m = s->ob_type->tp_as_sequence;
1327 if (m && m->sq_ass_slice) {
1328 if (i1 < 0 || i2 < 0) {
1329 if (m->sq_length) {
1330 int l = (*m->sq_length)(s);
1331 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001332 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001333 if (i1 < 0)
1334 i1 += l;
1335 if (i2 < 0)
1336 i2 += l;
1337 }
1338 }
1339 return m->sq_ass_slice(s, i1, i2, o);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001340 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
1341 int res;
1342 PyObject *slice = sliceobj_from_intint(i1, i2);
1343 if (!slice)
1344 return -1;
1345 res = mp->mp_ass_subscript(s, slice, o);
1346 Py_DECREF(slice);
1347 return res;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001348 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001349
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001350 type_error("object doesn't support slice assignment");
1351 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001352}
1353
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001354int
Fred Drake79912472000-07-09 04:06:11 +00001355PySequence_DelSlice(PyObject *s, int i1, int i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001356{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001357 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001358
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001359 if (s == NULL) {
1360 null_error();
1361 return -1;
1362 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001363
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001364 m = s->ob_type->tp_as_sequence;
1365 if (m && m->sq_ass_slice) {
1366 if (i1 < 0 || i2 < 0) {
1367 if (m->sq_length) {
1368 int l = (*m->sq_length)(s);
1369 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001370 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001371 if (i1 < 0)
1372 i1 += l;
1373 if (i2 < 0)
1374 i2 += l;
1375 }
1376 }
1377 return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
1378 }
1379 type_error("object doesn't support slice deletion");
1380 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001381}
1382
Guido van Rossume15dee51995-07-18 14:12:02 +00001383PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001384PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001385{
Tim Peters6912d4d2001-05-05 03:56:37 +00001386 PyObject *it; /* iter(v) */
1387 int n; /* guess for result tuple size */
1388 PyObject *result;
1389 int j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001390
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001391 if (v == NULL)
1392 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001393
Tim Peters6912d4d2001-05-05 03:56:37 +00001394 /* Special-case the common tuple and list cases, for efficiency. */
Tim Peters4c3a0a32001-09-10 23:37:46 +00001395 if (PyTuple_CheckExact(v)) {
1396 /* Note that we can't know whether it's safe to return
1397 a tuple *subclass* instance as-is, hence the restriction
Tim Peters8ff70a92001-09-10 23:53:53 +00001398 to exact tuples here. In contrast, lists always make
1399 a copy, so there's no need for exactness below. */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001400 Py_INCREF(v);
1401 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +00001402 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001403 if (PyList_Check(v))
1404 return PyList_AsTuple(v);
1405
Tim Peters6912d4d2001-05-05 03:56:37 +00001406 /* Get iterator. */
1407 it = PyObject_GetIter(v);
1408 if (it == NULL)
Tim Peters1fc240e2001-10-26 05:06:50 +00001409 return NULL;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001410
Tim Peters6912d4d2001-05-05 03:56:37 +00001411 /* Guess result size and allocate space. */
Raymond Hettinger7832cd62004-01-04 06:08:16 +00001412 n = PyObject_Size(v);
Tim Peters6912d4d2001-05-05 03:56:37 +00001413 if (n < 0) {
1414 PyErr_Clear();
1415 n = 10; /* arbitrary */
1416 }
1417 result = PyTuple_New(n);
1418 if (result == NULL)
1419 goto Fail;
1420
1421 /* Fill the tuple. */
1422 for (j = 0; ; ++j) {
1423 PyObject *item = PyIter_Next(it);
1424 if (item == NULL) {
1425 if (PyErr_Occurred())
1426 goto Fail;
1427 break;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001428 }
Tim Peters6912d4d2001-05-05 03:56:37 +00001429 if (j >= n) {
1430 if (n < 500)
1431 n += 10;
1432 else
1433 n += 100;
Tim Peters4324aa32001-05-28 22:30:08 +00001434 if (_PyTuple_Resize(&result, n) != 0) {
Tim Peters12d0a6c2001-05-05 04:10:25 +00001435 Py_DECREF(item);
Tim Peters6912d4d2001-05-05 03:56:37 +00001436 goto Fail;
Tim Peters12d0a6c2001-05-05 04:10:25 +00001437 }
Tim Peters6912d4d2001-05-05 03:56:37 +00001438 }
1439 PyTuple_SET_ITEM(result, j, item);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001440 }
1441
Tim Peters6912d4d2001-05-05 03:56:37 +00001442 /* Cut tuple back if guess was too large. */
1443 if (j < n &&
Tim Peters4324aa32001-05-28 22:30:08 +00001444 _PyTuple_Resize(&result, j) != 0)
Tim Peters6912d4d2001-05-05 03:56:37 +00001445 goto Fail;
1446
1447 Py_DECREF(it);
1448 return result;
1449
1450Fail:
1451 Py_XDECREF(result);
1452 Py_DECREF(it);
1453 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001454}
1455
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001456PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001457PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001458{
Tim Petersf553f892001-05-01 20:45:31 +00001459 PyObject *result; /* result list */
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +00001460 PyObject *rv; /* return value from PyList_Extend */
Guido van Rossum4669fb41997-04-02 05:31:09 +00001461
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001462 if (v == NULL)
1463 return null_error();
1464
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +00001465 result = PyList_New(0);
1466 if (result == NULL)
Tim Petersf553f892001-05-01 20:45:31 +00001467 return NULL;
1468
Raymond Hettinger8ca92ae2004-03-11 09:13:12 +00001469 rv = _PyList_Extend((PyListObject *)result, v);
1470 if (rv == NULL) {
1471 Py_DECREF(result);
Tim Petersf553f892001-05-01 20:45:31 +00001472 return NULL;
1473 }
Raymond Hettinger969d8c02004-03-17 05:24:23 +00001474 Py_DECREF(rv);
Tim Petersf553f892001-05-01 20:45:31 +00001475 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001476}
1477
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001478PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001479PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001480{
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001481 PyObject *it;
1482
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001483 if (v == NULL)
1484 return null_error();
1485
Michael W. Hudson2801fe12002-11-05 18:05:49 +00001486 if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001487 Py_INCREF(v);
1488 return v;
1489 }
1490
Raymond Hettinger2fb70292004-01-11 23:26:51 +00001491 it = PyObject_GetIter(v);
1492 if (it == NULL) {
1493 if (PyErr_ExceptionMatches(PyExc_TypeError))
1494 return type_error(m);
1495 return NULL;
1496 }
1497
1498 v = PySequence_Tuple(it);
1499 Py_DECREF(it);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001500
1501 return v;
1502}
1503
Tim Peters16a77ad2001-09-08 04:00:12 +00001504/* Iterate over seq. Result depends on the operation:
1505 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1506 PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq;
1507 set ValueError and return -1 if none found; also return -1 on error.
1508 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1509*/
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001510int
Tim Peters16a77ad2001-09-08 04:00:12 +00001511_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001512{
Tim Peters16a77ad2001-09-08 04:00:12 +00001513 int n;
1514 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1515 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001516
Tim Peters16a77ad2001-09-08 04:00:12 +00001517 if (seq == NULL || obj == NULL) {
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001518 null_error();
1519 return -1;
1520 }
Tim Peters75f8e352001-05-05 11:33:43 +00001521
Tim Peters16a77ad2001-09-08 04:00:12 +00001522 it = PyObject_GetIter(seq);
Tim Peters75f8e352001-05-05 11:33:43 +00001523 if (it == NULL) {
Tim Peters16a77ad2001-09-08 04:00:12 +00001524 type_error("iterable argument required");
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001525 return -1;
Tim Peters75f8e352001-05-05 11:33:43 +00001526 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001527
Tim Peters16a77ad2001-09-08 04:00:12 +00001528 n = wrapped = 0;
Tim Peters75f8e352001-05-05 11:33:43 +00001529 for (;;) {
1530 int cmp;
1531 PyObject *item = PyIter_Next(it);
1532 if (item == NULL) {
1533 if (PyErr_Occurred())
1534 goto Fail;
1535 break;
1536 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001537
1538 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001539 Py_DECREF(item);
Tim Peters75f8e352001-05-05 11:33:43 +00001540 if (cmp < 0)
1541 goto Fail;
1542 if (cmp > 0) {
Tim Peters16a77ad2001-09-08 04:00:12 +00001543 switch (operation) {
1544 case PY_ITERSEARCH_COUNT:
1545 ++n;
1546 if (n <= 0) {
1547 PyErr_SetString(PyExc_OverflowError,
Tim Peters75f8e352001-05-05 11:33:43 +00001548 "count exceeds C int size");
Tim Peters16a77ad2001-09-08 04:00:12 +00001549 goto Fail;
1550 }
1551 break;
1552
1553 case PY_ITERSEARCH_INDEX:
1554 if (wrapped) {
1555 PyErr_SetString(PyExc_OverflowError,
1556 "index exceeds C int size");
1557 goto Fail;
1558 }
1559 goto Done;
1560
1561 case PY_ITERSEARCH_CONTAINS:
1562 n = 1;
1563 goto Done;
1564
1565 default:
1566 assert(!"unknown operation");
Tim Peters75f8e352001-05-05 11:33:43 +00001567 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001568 }
1569
1570 if (operation == PY_ITERSEARCH_INDEX) {
1571 ++n;
1572 if (n <= 0)
1573 wrapped = 1;
Tim Peters75f8e352001-05-05 11:33:43 +00001574 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001575 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001576
1577 if (operation != PY_ITERSEARCH_INDEX)
1578 goto Done;
1579
1580 PyErr_SetString(PyExc_ValueError,
1581 "sequence.index(x): x not in sequence");
1582 /* fall into failure code */
1583Fail:
1584 n = -1;
1585 /* fall through */
1586Done:
Tim Peters75f8e352001-05-05 11:33:43 +00001587 Py_DECREF(it);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001588 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00001589
Guido van Rossume15dee51995-07-18 14:12:02 +00001590}
1591
Tim Peters16a77ad2001-09-08 04:00:12 +00001592/* Return # of times o appears in s. */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001593int
Tim Peters16a77ad2001-09-08 04:00:12 +00001594PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001595{
Tim Peters16a77ad2001-09-08 04:00:12 +00001596 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00001597}
1598
Tim Peterscb8d3682001-05-05 21:05:01 +00001599/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00001600 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001601 */
1602int
1603PySequence_Contains(PyObject *seq, PyObject *ob)
1604{
1605 if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
1606 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1607 if (sqm != NULL && sqm->sq_contains != NULL)
1608 return (*sqm->sq_contains)(seq, ob);
1609 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001610 return _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
Tim Peterscb8d3682001-05-05 21:05:01 +00001611}
1612
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001613/* Backwards compatibility */
1614#undef PySequence_In
1615int
Fred Drake79912472000-07-09 04:06:11 +00001616PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001617{
1618 return PySequence_Contains(w, v);
1619}
1620
1621int
Fred Drake79912472000-07-09 04:06:11 +00001622PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001623{
Tim Peters16a77ad2001-09-08 04:00:12 +00001624 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00001625}
1626
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001627/* Operations on mappings */
1628
1629int
Fred Drake79912472000-07-09 04:06:11 +00001630PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001631{
Raymond Hettingere2eda602004-04-04 08:51:41 +00001632 if (PyInstance_Check(o))
1633 return PyObject_HasAttrString(o, "__getitem__");
1634
1635 return o && o->ob_type->tp_as_mapping &&
1636 o->ob_type->tp_as_mapping->mp_subscript &&
Raymond Hettingered9192e2004-04-05 08:14:48 +00001637 !(o->ob_type->tp_as_sequence &&
1638 o->ob_type->tp_as_sequence->sq_slice);
Guido van Rossume15dee51995-07-18 14:12:02 +00001639}
1640
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001641int
Jeremy Hylton6253f832000-07-12 12:56:19 +00001642PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001643{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001644 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001645
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001646 if (o == NULL) {
1647 null_error();
1648 return -1;
1649 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001650
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001651 m = o->ob_type->tp_as_mapping;
1652 if (m && m->mp_length)
1653 return m->mp_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001654
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001655 type_error("len() of unsized object");
1656 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001657}
1658
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001659#undef PyMapping_Length
1660int
1661PyMapping_Length(PyObject *o)
1662{
1663 return PyMapping_Size(o);
1664}
1665#define PyMapping_Length PyMapping_Size
1666
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001667PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001668PyMapping_GetItemString(PyObject *o, char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001669{
1670 PyObject *okey, *r;
1671
1672 if (key == NULL)
1673 return null_error();
1674
1675 okey = PyString_FromString(key);
1676 if (okey == NULL)
1677 return NULL;
1678 r = PyObject_GetItem(o, okey);
1679 Py_DECREF(okey);
1680 return r;
1681}
1682
1683int
Fred Drake79912472000-07-09 04:06:11 +00001684PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001685{
1686 PyObject *okey;
1687 int r;
1688
1689 if (key == NULL) {
1690 null_error();
1691 return -1;
1692 }
1693
1694 okey = PyString_FromString(key);
1695 if (okey == NULL)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001696 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001697 r = PyObject_SetItem(o, okey, value);
1698 Py_DECREF(okey);
1699 return r;
1700}
1701
1702int
Fred Drake79912472000-07-09 04:06:11 +00001703PyMapping_HasKeyString(PyObject *o, char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00001704{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001705 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00001706
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001707 v = PyMapping_GetItemString(o, key);
1708 if (v) {
1709 Py_DECREF(v);
1710 return 1;
1711 }
1712 PyErr_Clear();
1713 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00001714}
1715
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001716int
Fred Drake79912472000-07-09 04:06:11 +00001717PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00001718{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001719 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00001720
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001721 v = PyObject_GetItem(o, key);
1722 if (v) {
1723 Py_DECREF(v);
1724 return 1;
1725 }
1726 PyErr_Clear();
1727 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00001728}
1729
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001730/* Operations on callable objects */
1731
1732/* XXX PyCallable_Check() is in object.c */
1733
Guido van Rossume15dee51995-07-18 14:12:02 +00001734PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001735PyObject_CallObject(PyObject *o, PyObject *a)
Guido van Rossume15dee51995-07-18 14:12:02 +00001736{
Guido van Rossum5560b742001-09-14 16:47:50 +00001737 return PyEval_CallObjectWithKeywords(o, a, NULL);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001738}
Guido van Rossume15dee51995-07-18 14:12:02 +00001739
1740PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001741PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
1742{
1743 ternaryfunc call;
1744
1745 if ((call = func->ob_type->tp_call) != NULL) {
1746 PyObject *result = (*call)(func, arg, kw);
1747 if (result == NULL && !PyErr_Occurred())
1748 PyErr_SetString(
1749 PyExc_SystemError,
1750 "NULL result without error in PyObject_Call");
1751 return result;
1752 }
Fred Drake573395a2001-11-01 20:26:12 +00001753 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
1754 func->ob_type->tp_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001755 return NULL;
1756}
1757
1758PyObject *
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001759PyObject_CallFunction(PyObject *callable, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00001760{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001761 va_list va;
1762 PyObject *args, *retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00001763
Fred Drakeb92cf062001-10-27 06:16:31 +00001764 if (callable == NULL)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001765 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001766
Fred Drakeb92cf062001-10-27 06:16:31 +00001767 if (format && *format) {
1768 va_start(va, format);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001769 args = Py_VaBuildValue(format, va);
Fred Drakeb92cf062001-10-27 06:16:31 +00001770 va_end(va);
1771 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001772 else
1773 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00001774
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001775 if (args == NULL)
1776 return NULL;
1777
1778 if (!PyTuple_Check(args)) {
1779 PyObject *a;
1780
1781 a = PyTuple_New(1);
1782 if (a == NULL)
1783 return NULL;
1784 if (PyTuple_SetItem(a, 0, args) < 0)
1785 return NULL;
1786 args = a;
1787 }
Guido van Rossum84b2bed2002-08-16 17:01:09 +00001788 retval = PyObject_Call(callable, args, NULL);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001789
1790 Py_DECREF(args);
1791
1792 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00001793}
1794
1795PyObject *
Guido van Rossume15dee51995-07-18 14:12:02 +00001796PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00001797{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001798 va_list va;
1799 PyObject *args, *func = 0, *retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00001800
Fred Drakeb92cf062001-10-27 06:16:31 +00001801 if (o == NULL || name == NULL)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001802 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001803
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001804 func = PyObject_GetAttrString(o, name);
1805 if (func == NULL) {
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001806 PyErr_SetString(PyExc_AttributeError, name);
1807 return 0;
1808 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001809
Fred Drakeb92cf062001-10-27 06:16:31 +00001810 if (!PyCallable_Check(func))
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001811 return type_error("call of non-callable attribute");
Guido van Rossume15dee51995-07-18 14:12:02 +00001812
Fred Drakeb92cf062001-10-27 06:16:31 +00001813 if (format && *format) {
1814 va_start(va, format);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001815 args = Py_VaBuildValue(format, va);
Fred Drakeb92cf062001-10-27 06:16:31 +00001816 va_end(va);
1817 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001818 else
1819 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00001820
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001821 if (!args)
1822 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001823
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001824 if (!PyTuple_Check(args)) {
1825 PyObject *a;
Guido van Rossume15dee51995-07-18 14:12:02 +00001826
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001827 a = PyTuple_New(1);
1828 if (a == NULL)
1829 return NULL;
1830 if (PyTuple_SetItem(a, 0, args) < 0)
1831 return NULL;
1832 args = a;
1833 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001834
Guido van Rossum84b2bed2002-08-16 17:01:09 +00001835 retval = PyObject_Call(func, args, NULL);
Guido van Rossume15dee51995-07-18 14:12:02 +00001836
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001837 Py_DECREF(args);
1838 Py_DECREF(func);
1839
1840 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00001841}
Guido van Rossum823649d2001-03-21 18:40:58 +00001842
1843
Fred Drakeb421b8c2001-10-26 16:21:32 +00001844static PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00001845objargs_mktuple(va_list va)
Fred Drakeb421b8c2001-10-26 16:21:32 +00001846{
1847 int i, n = 0;
1848 va_list countva;
1849 PyObject *result, *tmp;
1850
1851#ifdef VA_LIST_IS_ARRAY
1852 memcpy(countva, va, sizeof(va_list));
1853#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +00001854#ifdef __va_copy
1855 __va_copy(countva, va);
1856#else
Fred Drakeb421b8c2001-10-26 16:21:32 +00001857 countva = va;
1858#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +00001859#endif
Fred Drakeb421b8c2001-10-26 16:21:32 +00001860
1861 while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
1862 ++n;
1863 result = PyTuple_New(n);
1864 if (result != NULL && n > 0) {
1865 for (i = 0; i < n; ++i) {
1866 tmp = (PyObject *)va_arg(va, PyObject *);
1867 PyTuple_SET_ITEM(result, i, tmp);
1868 Py_INCREF(tmp);
1869 }
1870 }
1871 return result;
1872}
1873
1874PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00001875PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00001876{
1877 PyObject *args, *tmp;
1878 va_list vargs;
1879
1880 if (callable == NULL || name == NULL)
1881 return null_error();
1882
1883 callable = PyObject_GetAttr(callable, name);
1884 if (callable == NULL)
1885 return NULL;
1886
1887 /* count the args */
1888 va_start(vargs, name);
Fred Drakeb0c079e2001-10-28 02:39:03 +00001889 args = objargs_mktuple(vargs);
Fred Drakeb421b8c2001-10-26 16:21:32 +00001890 va_end(vargs);
1891 if (args == NULL) {
1892 Py_DECREF(callable);
1893 return NULL;
1894 }
1895 tmp = PyObject_Call(callable, args, NULL);
1896 Py_DECREF(args);
1897 Py_DECREF(callable);
1898
1899 return tmp;
1900}
1901
1902PyObject *
Fred Drakeb0c079e2001-10-28 02:39:03 +00001903PyObject_CallFunctionObjArgs(PyObject *callable, ...)
Fred Drakeb421b8c2001-10-26 16:21:32 +00001904{
1905 PyObject *args, *tmp;
1906 va_list vargs;
1907
1908 if (callable == NULL)
1909 return null_error();
1910
1911 /* count the args */
1912 va_start(vargs, callable);
Fred Drakeb0c079e2001-10-28 02:39:03 +00001913 args = objargs_mktuple(vargs);
Fred Drakeb421b8c2001-10-26 16:21:32 +00001914 va_end(vargs);
1915 if (args == NULL)
1916 return NULL;
1917 tmp = PyObject_Call(callable, args, NULL);
1918 Py_DECREF(args);
1919
1920 return tmp;
1921}
1922
1923
Guido van Rossum823649d2001-03-21 18:40:58 +00001924/* isinstance(), issubclass() */
1925
Barry Warsawf16951c2002-04-23 22:45:44 +00001926/* abstract_get_bases() has logically 4 return states, with a sort of 0th
1927 * state that will almost never happen.
1928 *
1929 * 0. creating the __bases__ static string could get a MemoryError
1930 * 1. getattr(cls, '__bases__') could raise an AttributeError
1931 * 2. getattr(cls, '__bases__') could raise some other exception
1932 * 3. getattr(cls, '__bases__') could return a tuple
1933 * 4. getattr(cls, '__bases__') could return something other than a tuple
1934 *
1935 * Only state #3 is a non-error state and only it returns a non-NULL object
1936 * (it returns the retrieved tuple).
1937 *
1938 * Any raised AttributeErrors are masked by clearing the exception and
1939 * returning NULL. If an object other than a tuple comes out of __bases__,
1940 * then again, the return value is NULL. So yes, these two situations
1941 * produce exactly the same results: NULL is returned and no error is set.
1942 *
1943 * If some exception other than AttributeError is raised, then NULL is also
1944 * returned, but the exception is not cleared. That's because we want the
1945 * exception to be propagated along.
1946 *
1947 * Callers are expected to test for PyErr_Occurred() when the return value
1948 * is NULL to decide whether a valid exception should be propagated or not.
1949 * When there's no exception to propagate, it's customary for the caller to
1950 * set a TypeError.
1951 */
Neil Schemenauer6b471292001-10-18 03:18:43 +00001952static PyObject *
1953abstract_get_bases(PyObject *cls)
Guido van Rossum823649d2001-03-21 18:40:58 +00001954{
1955 static PyObject *__bases__ = NULL;
1956 PyObject *bases;
Guido van Rossum823649d2001-03-21 18:40:58 +00001957
1958 if (__bases__ == NULL) {
1959 __bases__ = PyString_FromString("__bases__");
1960 if (__bases__ == NULL)
Neil Schemenauer6b471292001-10-18 03:18:43 +00001961 return NULL;
Guido van Rossum823649d2001-03-21 18:40:58 +00001962 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00001963 bases = PyObject_GetAttr(cls, __bases__);
Barry Warsawf16951c2002-04-23 22:45:44 +00001964 if (bases == NULL) {
1965 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1966 PyErr_Clear();
Neil Schemenauer6b471292001-10-18 03:18:43 +00001967 return NULL;
Guido van Rossum823649d2001-03-21 18:40:58 +00001968 }
Barry Warsawf16951c2002-04-23 22:45:44 +00001969 if (!PyTuple_Check(bases)) {
1970 Py_DECREF(bases);
1971 return NULL;
1972 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00001973 return bases;
1974}
1975
1976
1977static int
1978abstract_issubclass(PyObject *derived, PyObject *cls)
1979{
1980 PyObject *bases;
1981 int i, n;
1982 int r = 0;
1983
1984
Guido van Rossum823649d2001-03-21 18:40:58 +00001985 if (derived == cls)
1986 return 1;
1987
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00001988 if (PyTuple_Check(cls)) {
1989 /* Not a general sequence -- that opens up the road to
1990 recursion and stack overflow. */
1991 n = PyTuple_GET_SIZE(cls);
1992 for (i = 0; i < n; i++) {
1993 if (derived == PyTuple_GET_ITEM(cls, i))
1994 return 1;
1995 }
1996 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00001997 bases = abstract_get_bases(derived);
Barry Warsawf16951c2002-04-23 22:45:44 +00001998 if (bases == NULL) {
1999 if (PyErr_Occurred())
2000 return -1;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002001 return 0;
Barry Warsawf16951c2002-04-23 22:45:44 +00002002 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002003 n = PyTuple_GET_SIZE(bases);
2004 for (i = 0; i < n; i++) {
Neil Schemenauer6b471292001-10-18 03:18:43 +00002005 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002006 if (r != 0)
2007 break;
2008 }
2009
2010 Py_DECREF(bases);
2011
2012 return r;
2013}
2014
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002015static int
2016check_class(PyObject *cls, const char *error)
2017{
2018 PyObject *bases = abstract_get_bases(cls);
2019 if (bases == NULL) {
2020 /* Do not mask errors. */
2021 if (!PyErr_Occurred())
2022 PyErr_SetString(PyExc_TypeError, error);
2023 return 0;
2024 }
2025 Py_DECREF(bases);
2026 return -1;
2027}
2028
Brett Cannon4f653312004-03-20 22:52:14 +00002029static int
2030recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth)
Guido van Rossum823649d2001-03-21 18:40:58 +00002031{
2032 PyObject *icls;
2033 static PyObject *__class__ = NULL;
2034 int retval = 0;
2035
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002036 if (__class__ == NULL) {
2037 __class__ = PyString_FromString("__class__");
2038 if (__class__ == NULL)
2039 return -1;
2040 }
2041
Neil Schemenauer6b471292001-10-18 03:18:43 +00002042 if (PyClass_Check(cls) && PyInstance_Check(inst)) {
2043 PyObject *inclass =
2044 (PyObject*)((PyInstanceObject*)inst)->in_class;
2045 retval = PyClass_IsSubclass(inclass, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002046 }
2047 else if (PyType_Check(cls)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002048 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002049 if (retval == 0) {
2050 PyObject *c = PyObject_GetAttr(inst, __class__);
2051 if (c == NULL) {
2052 PyErr_Clear();
2053 }
2054 else {
Guido van Rossume5b130b2003-02-12 03:36:05 +00002055 if (c != (PyObject *)(inst->ob_type) &&
2056 PyType_Check(c))
Guido van Rossum03bc7d32003-02-12 03:32:58 +00002057 retval = PyType_IsSubtype(
2058 (PyTypeObject *)c,
2059 (PyTypeObject *)cls);
2060 Py_DECREF(c);
2061 }
2062 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002063 }
Guido van Rossum03290ec2001-10-07 20:54:12 +00002064 else if (PyTuple_Check(cls)) {
Guido van Rossum03290ec2001-10-07 20:54:12 +00002065 int i, n;
2066
Brett Cannon4f653312004-03-20 22:52:14 +00002067 if (!recursion_depth) {
2068 PyErr_SetString(PyExc_RuntimeError,
2069 "nest level of tuple too deep");
Tim Peters5f112eb2004-03-21 16:59:09 +00002070 return -1;
Brett Cannon4f653312004-03-20 22:52:14 +00002071 }
2072
Guido van Rossum03290ec2001-10-07 20:54:12 +00002073 n = PyTuple_GET_SIZE(cls);
2074 for (i = 0; i < n; i++) {
Brett Cannon4f653312004-03-20 22:52:14 +00002075 retval = recursive_isinstance(
2076 inst,
2077 PyTuple_GET_ITEM(cls, i),
2078 recursion_depth-1);
Guido van Rossum03290ec2001-10-07 20:54:12 +00002079 if (retval != 0)
2080 break;
2081 }
Guido van Rossum03290ec2001-10-07 20:54:12 +00002082 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00002083 else {
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002084 if (!check_class(cls,
2085 "isinstance() arg 2 must be a class, type,"
2086 " or tuple of classes and types"))
Neil Schemenauer6b471292001-10-18 03:18:43 +00002087 return -1;
Guido van Rossum823649d2001-03-21 18:40:58 +00002088 icls = PyObject_GetAttr(inst, __class__);
Neil Schemenauer6b471292001-10-18 03:18:43 +00002089 if (icls == NULL) {
2090 PyErr_Clear();
2091 retval = 0;
Guido van Rossum823649d2001-03-21 18:40:58 +00002092 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00002093 else {
2094 retval = abstract_issubclass(icls, cls);
2095 Py_DECREF(icls);
2096 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002097 }
Guido van Rossum823649d2001-03-21 18:40:58 +00002098
Guido van Rossum823649d2001-03-21 18:40:58 +00002099 return retval;
2100}
2101
2102int
Brett Cannon4f653312004-03-20 22:52:14 +00002103PyObject_IsInstance(PyObject *inst, PyObject *cls)
2104{
2105 return recursive_isinstance(inst, cls, Py_GetRecursionLimit());
2106}
2107
2108static int
2109recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth)
Guido van Rossum823649d2001-03-21 18:40:58 +00002110{
2111 int retval;
2112
2113 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
Guido van Rossumf6c9ba82003-02-10 16:05:43 +00002114 if (!check_class(derived,
2115 "issubclass() arg 1 must be a class"))
Neil Schemenauer6b471292001-10-18 03:18:43 +00002116 return -1;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002117
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002118 if (PyTuple_Check(cls)) {
2119 int i;
2120 int n = PyTuple_GET_SIZE(cls);
Brett Cannon4f653312004-03-20 22:52:14 +00002121
2122 if (!recursion_depth) {
2123 PyErr_SetString(PyExc_RuntimeError,
2124 "nest level of tuple too deep");
Tim Peters5f112eb2004-03-21 16:59:09 +00002125 return -1;
Brett Cannon4f653312004-03-20 22:52:14 +00002126 }
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002127 for (i = 0; i < n; ++i) {
Brett Cannon4f653312004-03-20 22:52:14 +00002128 retval = recursive_issubclass(
2129 derived,
2130 PyTuple_GET_ITEM(cls, i),
2131 recursion_depth-1);
Guido van Rossumf6c9ba82003-02-10 16:05:43 +00002132 if (retval != 0) {
2133 /* either found it, or got an error */
Walter Dörwald7e5c6a02002-12-12 19:14:08 +00002134 return retval;
Guido van Rossumf6c9ba82003-02-10 16:05:43 +00002135 }
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002136 }
Walter Dörwald7e5c6a02002-12-12 19:14:08 +00002137 return 0;
Neil Schemenauer6b471292001-10-18 03:18:43 +00002138 }
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002139 else {
2140 if (!check_class(cls,
2141 "issubclass() arg 2 must be a class"
2142 " or tuple of classes"))
2143 return -1;
2144 }
Neil Schemenauer6b471292001-10-18 03:18:43 +00002145
2146 retval = abstract_issubclass(derived, cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00002147 }
2148 else {
2149 /* shortcut */
2150 if (!(retval = (derived == cls)))
2151 retval = PyClass_IsSubclass(derived, cls);
2152 }
2153
2154 return retval;
2155}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002156
Brett Cannon4f653312004-03-20 22:52:14 +00002157int
2158PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2159{
2160 return recursive_issubclass(derived, cls, Py_GetRecursionLimit());
2161}
2162
2163
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002164PyObject *
2165PyObject_GetIter(PyObject *o)
2166{
2167 PyTypeObject *t = o->ob_type;
2168 getiterfunc f = NULL;
2169 if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER))
2170 f = t->tp_iter;
2171 if (f == NULL) {
2172 if (PySequence_Check(o))
Guido van Rossum213c7a62001-04-23 14:08:49 +00002173 return PySeqIter_New(o);
Guido van Rossum77660912002-04-16 16:32:50 +00002174 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton89c3a222001-11-09 21:59:42 +00002175 "iteration over non-sequence");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002176 return NULL;
2177 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002178 else {
2179 PyObject *res = (*f)(o);
2180 if (res != NULL && !PyIter_Check(res)) {
2181 PyErr_Format(PyExc_TypeError,
2182 "iter() returned non-iterator "
2183 "of type '%.100s'",
2184 res->ob_type->tp_name);
2185 Py_DECREF(res);
2186 res = NULL;
2187 }
2188 return res;
2189 }
2190}
2191
Tim Petersf4848da2001-05-05 00:14:56 +00002192/* Return next item.
2193 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2194 * If the iteration terminates normally, return NULL and clear the
2195 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2196 * will be false.
2197 * Else return the next object. PyErr_Occurred() will be false.
2198 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002199PyObject *
2200PyIter_Next(PyObject *iter)
2201{
Tim Petersf4848da2001-05-05 00:14:56 +00002202 PyObject *result;
Raymond Hettinger8049dde2003-03-01 01:44:32 +00002203 assert(PyIter_Check(iter));
Tim Petersf4848da2001-05-05 00:14:56 +00002204 result = (*iter->ob_type->tp_iternext)(iter);
2205 if (result == NULL &&
2206 PyErr_Occurred() &&
2207 PyErr_ExceptionMatches(PyExc_StopIteration))
2208 PyErr_Clear();
2209 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002210}