blob: 5361b1d5c1b4551f8d1da204876db899bd94999b [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
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00003
Guido van Rossume15dee51995-07-18 14:12:02 +00004#include "Python.h"
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00005#include <ctype.h>
Neil Schemenauer5a1f0152001-01-04 01:39:06 +00006#include "structmember.h" /* we need the offsetof() macro from there */
7
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 Rossum21308241998-08-13 16:44:44 +0000105 return type_error("sequence index must be integer");
106 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000107
108 return type_error("unsubscriptable object");
Guido van Rossume15dee51995-07-18 14:12:02 +0000109}
110
111int
Fred Drake79912472000-07-09 04:06:11 +0000112PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
Guido van Rossume15dee51995-07-18 14:12:02 +0000113{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000114 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000115
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000116 if (o == NULL || key == NULL || value == NULL) {
117 null_error();
118 return -1;
119 }
120 m = o->ob_type->tp_as_mapping;
121 if (m && m->mp_ass_subscript)
122 return m->mp_ass_subscript(o, key, value);
Guido van Rossume15dee51995-07-18 14:12:02 +0000123
Guido van Rossum21308241998-08-13 16:44:44 +0000124 if (o->ob_type->tp_as_sequence) {
125 if (PyInt_Check(key))
126 return PySequence_SetItem(o, PyInt_AsLong(key), value);
Andrew M. Kuchling0f223d22000-02-23 22:21:50 +0000127 else if (PyLong_Check(key)) {
128 long key_value = PyLong_AsLong(key);
129 if (key_value == -1 && PyErr_Occurred())
130 return -1;
131 return PySequence_SetItem(o, key_value, value);
132 }
Guido van Rossum21308241998-08-13 16:44:44 +0000133 type_error("sequence index must be integer");
134 return -1;
135 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000136
137 type_error("object does not support item assignment");
138 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000139}
140
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000141int
Fred Drake79912472000-07-09 04:06:11 +0000142PyObject_DelItem(PyObject *o, PyObject *key)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000143{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000144 PyMappingMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000145
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000146 if (o == NULL || key == NULL) {
147 null_error();
148 return -1;
149 }
150 m = o->ob_type->tp_as_mapping;
151 if (m && m->mp_ass_subscript)
152 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000153
Guido van Rossum21308241998-08-13 16:44:44 +0000154 if (o->ob_type->tp_as_sequence) {
155 if (PyInt_Check(key))
156 return PySequence_DelItem(o, PyInt_AsLong(key));
Andrew M. Kuchling0f223d22000-02-23 22:21:50 +0000157 else if (PyLong_Check(key)) {
158 long key_value = PyLong_AsLong(key);
159 if (key_value == -1 && PyErr_Occurred())
160 return -1;
161 return PySequence_DelItem(o, key_value);
162 }
Guido van Rossum21308241998-08-13 16:44:44 +0000163 type_error("sequence index must be integer");
164 return -1;
165 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000166
167 type_error("object does not support item deletion");
168 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000169}
170
Guido van Rossum4c08d552000-03-10 22:55:18 +0000171int PyObject_AsCharBuffer(PyObject *obj,
172 const char **buffer,
173 int *buffer_len)
174{
175 PyBufferProcs *pb;
176 const char *pp;
177 int len;
178
179 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
180 null_error();
181 return -1;
182 }
183 pb = obj->ob_type->tp_as_buffer;
184 if ( pb == NULL ||
185 pb->bf_getcharbuffer == NULL ||
186 pb->bf_getsegcount == NULL ) {
187 PyErr_SetString(PyExc_TypeError,
188 "expected a character buffer object");
189 goto onError;
190 }
191 if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
192 PyErr_SetString(PyExc_TypeError,
193 "expected a single-segment buffer object");
194 goto onError;
195 }
196 len = (*pb->bf_getcharbuffer)(obj,0,&pp);
197 if (len < 0)
198 goto onError;
199 *buffer = pp;
200 *buffer_len = len;
201 return 0;
202
203 onError:
204 return -1;
205}
206
207int PyObject_AsReadBuffer(PyObject *obj,
208 const void **buffer,
209 int *buffer_len)
210{
211 PyBufferProcs *pb;
212 void *pp;
213 int len;
214
215 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
216 null_error();
217 return -1;
218 }
219 pb = obj->ob_type->tp_as_buffer;
220 if ( pb == NULL ||
221 pb->bf_getreadbuffer == NULL ||
222 pb->bf_getsegcount == NULL ) {
223 PyErr_SetString(PyExc_TypeError,
224 "expected a readable buffer object");
225 goto onError;
226 }
227 if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
228 PyErr_SetString(PyExc_TypeError,
229 "expected a single-segment buffer object");
230 goto onError;
231 }
232 len = (*pb->bf_getreadbuffer)(obj,0,&pp);
233 if (len < 0)
234 goto onError;
235 *buffer = pp;
236 *buffer_len = len;
237 return 0;
238
239 onError:
240 return -1;
241}
242
243int PyObject_AsWriteBuffer(PyObject *obj,
244 void **buffer,
245 int *buffer_len)
246{
247 PyBufferProcs *pb;
248 void*pp;
249 int len;
250
251 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
252 null_error();
253 return -1;
254 }
255 pb = obj->ob_type->tp_as_buffer;
256 if ( pb == NULL ||
257 pb->bf_getwritebuffer == NULL ||
258 pb->bf_getsegcount == NULL ) {
259 PyErr_SetString(PyExc_TypeError,
260 "expected a writeable buffer object");
261 goto onError;
262 }
263 if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
264 PyErr_SetString(PyExc_TypeError,
265 "expected a single-segment buffer object");
266 goto onError;
267 }
268 len = (*pb->bf_getwritebuffer)(obj,0,&pp);
269 if (len < 0)
270 goto onError;
271 *buffer = pp;
272 *buffer_len = len;
273 return 0;
274
275 onError:
276 return -1;
277}
278
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000279/* Operations on numbers */
280
281int
Fred Drake79912472000-07-09 04:06:11 +0000282PyNumber_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000283{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000284 return o && o->ob_type->tp_as_number;
Guido van Rossume15dee51995-07-18 14:12:02 +0000285}
286
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000287/* Binary operators */
Guido van Rossume15dee51995-07-18 14:12:02 +0000288
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000289/* New style number protocol support */
Guido van Rossume15dee51995-07-18 14:12:02 +0000290
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000291#define NB_SLOT(x) offsetof(PyNumberMethods, x)
292#define NB_BINOP(nb_methods, slot) \
293 ((binaryfunc*)(& ((char*)nb_methods)[slot] ))
294#define NB_TERNOP(nb_methods, slot) \
295 ((ternaryfunc*)(& ((char*)nb_methods)[slot] ))
296
297/*
298 Calling scheme used for binary operations:
299
300 v w Action
301 -------------------------------------------------------------------
302 new new v.op(v,w), w.op(v,w)
303 new old v.op(v,w), coerce(v,w), v.op(v,w)
304 old new w.op(v,w), coerce(v,w), v.op(v,w)
305 old old coerce(v,w), v.op(v,w)
306
307 Legend:
308 -------
309 * new == new style number
310 * old == old style number
311 * Action indicates the order in which operations are tried until either
312 a valid result is produced or an error occurs.
313
314 */
315
316static PyObject *
317binary_op1(PyObject *v, PyObject *w, const int op_slot)
Guido van Rossume15dee51995-07-18 14:12:02 +0000318{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000319 PyObject *x;
320 binaryfunc *slot;
321 if (v->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(v)) {
322 slot = NB_BINOP(v->ob_type->tp_as_number, op_slot);
323 if (*slot) {
324 x = (*slot)(v, w);
325 if (x != Py_NotImplemented) {
326 return x;
327 }
328 Py_DECREF(x); /* can't do it */
329 }
330 if (v->ob_type == w->ob_type) {
331 goto binop_error;
332 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000333 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000334 if (w->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(w)) {
335 slot = NB_BINOP(w->ob_type->tp_as_number, op_slot);
336 if (*slot) {
337 x = (*slot)(v, w);
338 if (x != Py_NotImplemented) {
339 return x;
340 }
341 Py_DECREF(x); /* can't do it */
342 }
343 }
344 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w)) {
345 int err = PyNumber_CoerceEx(&v, &w);
346 if (err < 0) {
347 return NULL;
348 }
349 if (err == 0) {
350 PyNumberMethods *mv = v->ob_type->tp_as_number;
351 if (mv) {
352 slot = NB_BINOP(mv, op_slot);
353 if (*slot) {
354 PyObject *x = (*slot)(v, w);
355 Py_DECREF(v);
356 Py_DECREF(w);
357 return x;
358 }
359 }
360 /* CoerceEx incremented the reference counts */
361 Py_DECREF(v);
362 Py_DECREF(w);
363 }
364 }
365binop_error:
366 Py_INCREF(Py_NotImplemented);
367 return Py_NotImplemented;
368}
369
370static PyObject *
371binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
372{
373 PyObject *result = binary_op1(v, w, op_slot);
374 if (result == Py_NotImplemented) {
375 Py_DECREF(Py_NotImplemented);
376 PyErr_Format(PyExc_TypeError,
377 "unsupported operand type(s) for %s", op_name);
378 return NULL;
379 }
380 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000381}
382
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000383
384/*
385 Calling scheme used for ternary operations:
386
387 v w z Action
388 -------------------------------------------------------------------
389 new new new v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
390 new old new v.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
391 old new new w.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
392 old old new z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
393 new new old v.op(v,w,z), w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
394 new old old v.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
395 old new old w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
396 old old old coerce(v,w,z), v.op(v,w,z)
397
398 Legend:
399 -------
400 * new == new style number
401 * old == old style number
402 * Action indicates the order in which operations are tried until either
403 a valid result is produced or an error occurs.
404 * coerce(v,w,z) actually does: coerce(v,w), coerce(v,z), coerce(w,z) and
405 only if z != Py_None; if z == Py_None, then it is treated as absent
406 variable and only coerce(v,w) is tried.
407
408 */
409
410static PyObject *
411ternary_op(PyObject *v,
412 PyObject *w,
413 PyObject *z,
414 const int op_slot,
415 const char *op_name)
Guido van Rossume15dee51995-07-18 14:12:02 +0000416{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000417 PyNumberMethods *mv, *mw, *mz;
418 register PyObject *x = NULL;
419 register ternaryfunc *slot;
420
421 mv = v->ob_type->tp_as_number;
422 if (mv != NULL && NEW_STYLE_NUMBER(v)) {
423 /* try v.op(v,w,z) */
424 slot = NB_TERNOP(mv, op_slot);
425 if (*slot) {
426 x = (*slot)(v, w, z);
427 if (x != Py_NotImplemented)
428 return x;
429 /* Can't do it... fall through */
430 Py_DECREF(x);
431 }
432 if (v->ob_type == w->ob_type &&
433 (z == Py_None || z->ob_type == v->ob_type)) {
434 goto ternary_error;
435 }
436 }
437 mw = w->ob_type->tp_as_number;
438 if (mw != NULL && NEW_STYLE_NUMBER(w)) {
439 /* try w.op(v,w,z) */
440 slot = NB_TERNOP(mw,op_slot);
441 if (*slot) {
442 x = (*slot)(v, w, z);
443 if (x != Py_NotImplemented)
444 return x;
445 /* Can't do it... fall through */
446 Py_DECREF(x);
447 }
448 if (NEW_STYLE_NUMBER(v) &&
449 (z == Py_None || z->ob_type == v->ob_type)) {
450 goto ternary_error;
451 }
452 }
453 mz = z->ob_type->tp_as_number;
454 if (mz != NULL && NEW_STYLE_NUMBER(z)) {
455 /* try: z.op(v,w,z) */
456 slot = NB_TERNOP(mz, op_slot);
457 if (*slot) {
458 x = (*slot)(v, w, z);
459 if (x != Py_NotImplemented)
460 return x;
461 /* Can't do it... fall through */
462 Py_DECREF(x);
463 }
464 }
465
466 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w) ||
467 (z != Py_None && !NEW_STYLE_NUMBER(z))) {
468 /* we have an old style operand, coerce */
469 PyObject *v1, *z1, *w2, *z2;
470 int c;
471
472 c = PyNumber_Coerce(&v, &w);
473 if (c != 0)
474 goto error3;
475
476 /* Special case: if the third argument is None, it is
477 treated as absent argument and not coerced. */
478 if (z == Py_None) {
479 if (v->ob_type->tp_as_number) {
480 slot = NB_TERNOP(v->ob_type->tp_as_number,
481 op_slot);
482 if (*slot)
483 x = (*slot)(v, w, z);
484 else
485 c = -1;
486 }
487 else
488 c = -1;
489 goto error2;
490 }
491 v1 = v;
492 z1 = z;
493 c = PyNumber_Coerce(&v1, &z1);
494 if (c != 0)
495 goto error2;
496 w2 = w;
497 z2 = z1;
498 c = PyNumber_Coerce(&w2, &z2);
499 if (c != 0)
500 goto error1;
501
502 if (v1->ob_type->tp_as_number != NULL) {
503 slot = NB_TERNOP(v1->ob_type->tp_as_number,
504 op_slot);
505 if (*slot)
506 x = (*slot)(v1, w2, z2);
507 else
508 c = -1;
509 }
510 else
511 c = -1;
512
513 Py_DECREF(w2);
514 Py_DECREF(z2);
515 error1:
516 Py_DECREF(v1);
517 Py_DECREF(z1);
518 error2:
Guido van Rossume15dee51995-07-18 14:12:02 +0000519 Py_DECREF(v);
520 Py_DECREF(w);
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000521 error3:
522 if (c >= 0)
Guido van Rossume15dee51995-07-18 14:12:02 +0000523 return x;
524 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000525
526ternary_error:
527 PyErr_Format(PyExc_TypeError, "unsupported operand type(s) for %s",
528 op_name);
529 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000530}
531
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000532#define BINARY_FUNC(func, op, op_name) \
533 PyObject * \
534 func(PyObject *v, PyObject *w) { \
535 return binary_op(v, w, NB_SLOT(op), op_name); \
536 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000537
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000538BINARY_FUNC(PyNumber_Or, nb_or, "|")
539BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
540BINARY_FUNC(PyNumber_And, nb_and, "&")
541BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
542BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
543BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
544BINARY_FUNC(PyNumber_Multiply, nb_multiply, "*")
545BINARY_FUNC(PyNumber_Divide, nb_divide, "/")
546BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
Guido van Rossume15dee51995-07-18 14:12:02 +0000547
548PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000549PyNumber_Add(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000550{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000551 PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
552 if (result == Py_NotImplemented) {
553 PySequenceMethods *m = v->ob_type->tp_as_sequence;
554 Py_DECREF(Py_NotImplemented);
555 if (m && m->sq_concat) {
556 result = (*m->sq_concat)(v, w);
Andrew M. Kuchling1991ddc2000-02-14 22:22:04 +0000557 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000558 else {
559 PyErr_SetString(PyExc_TypeError,
560 "unsupported operand types for +");
561 result = NULL;
562 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000563 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000564 return result;
Guido van Rossume15dee51995-07-18 14:12:02 +0000565}
566
567PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000568PyNumber_FloorDivide(PyObject *v, PyObject *w)
569{
570 /* XXX tp_flags test */
571 return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
572}
573
574PyObject *
575PyNumber_TrueDivide(PyObject *v, PyObject *w)
576{
577 /* XXX tp_flags test */
578 return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
579}
580
581PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000582PyNumber_Remainder(PyObject *v, PyObject *w)
Guido van Rossume15dee51995-07-18 14:12:02 +0000583{
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +0000584 if (PyString_Check(v))
Guido van Rossume15dee51995-07-18 14:12:02 +0000585 return PyString_Format(v, w);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000586#ifdef Py_USING_UNICODE
Guido van Rossum4c08d552000-03-10 22:55:18 +0000587 else if (PyUnicode_Check(v))
588 return PyUnicode_Format(v, w);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000589#endif
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000590 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
Guido van Rossume15dee51995-07-18 14:12:02 +0000591}
592
593PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000594PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossume15dee51995-07-18 14:12:02 +0000595{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000596 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000597}
598
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000599/* Binary in-place operators */
600
601/* The in-place operators are defined to fall back to the 'normal',
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000602 non in-place operations, if the in-place methods are not in place.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000603
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000604 - If the left hand object has the appropriate struct members, and
605 they are filled, call the appropriate function and return the
606 result. No coercion is done on the arguments; the left-hand object
607 is the one the operation is performed on, and it's up to the
608 function to deal with the right-hand object.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000609
610 - Otherwise, in-place modification is not supported. Handle it exactly as
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000611 a non in-place operation of the same kind.
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000612
613 */
614
615#define HASINPLACE(t) PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS)
616
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000617static PyObject *
618binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
619 const char *op_name)
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000620{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000621 PyNumberMethods *mv = v->ob_type->tp_as_number;
622 if (mv != NULL && HASINPLACE(v)) {
623 binaryfunc *slot = NB_BINOP(mv, iop_slot);
624 if (*slot) {
625 PyObject *x = (*slot)(v, w);
626 if (x != Py_NotImplemented) {
627 return x;
628 }
629 Py_DECREF(x);
630 }
Thomas Woutersf2b332d2000-09-02 08:34:40 +0000631 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000632 return binary_op(v, w, op_slot, op_name);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000633}
634
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000635#define INPLACE_BINOP(func, iop, op, op_name) \
636 PyObject * \
637 func(PyObject *v, PyObject *w) { \
638 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000639 }
640
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000641INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
642INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
643INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
644INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
645INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
646INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
647INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=")
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000648
649PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000650PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
651{
652 /* XXX tp_flags test */
653 return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
654 NB_SLOT(nb_floor_divide), "//=");
655}
656
657PyObject *
658PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
659{
660 /* XXX tp_flags test */
661 return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
662 NB_SLOT(nb_true_divide), "/=");
663}
664
665PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000666PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
667{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000668 binaryfunc f = NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000669
Guido van Rossumbb8be932000-09-01 23:27:32 +0000670 if (v->ob_type->tp_as_sequence != NULL) {
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000671 if (HASINPLACE(v))
672 f = v->ob_type->tp_as_sequence->sq_inplace_concat;
673 if (f == NULL)
674 f = v->ob_type->tp_as_sequence->sq_concat;
Guido van Rossumbb8be932000-09-01 23:27:32 +0000675 if (f != NULL)
676 return (*f)(v, w);
677 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000678 return binary_iop(v, w, NB_SLOT(nb_inplace_add), NB_SLOT(nb_add), "+=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000679}
680
681PyObject *
682PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
683{
Thomas Woutersf2b332d2000-09-02 08:34:40 +0000684 PyObject * (*g)(PyObject *, int) = NULL;
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000685 if (HASINPLACE(v) && v->ob_type->tp_as_sequence &&
686 (g = v->ob_type->tp_as_sequence->sq_inplace_repeat)) {
687 long n;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000688 if (PyInt_Check(w)) {
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000689 n = PyInt_AsLong(w);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000690 }
691 else if (PyLong_Check(w)) {
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000692 n = PyLong_AsLong(w);
693 if (n == -1 && PyErr_Occurred())
694 return NULL;
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000695 }
696 else {
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000697 return type_error("can't multiply sequence to non-int");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000698 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000699 return (*g)(v, (int)n);
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000700 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000701 return binary_iop(v, w, NB_SLOT(nb_inplace_multiply),
702 NB_SLOT(nb_multiply), "*=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000703}
704
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000705
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000706
707PyObject *
708PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
709{
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000710 if (PyString_Check(v))
711 return PyString_Format(v, w);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000712#ifdef Py_USING_UNICODE
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000713 else if (PyUnicode_Check(v))
714 return PyUnicode_Format(v, w);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000715#endif
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000716 else
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000717 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
718 NB_SLOT(nb_remainder), "%=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000719}
720
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000721
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000722PyObject *
723PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
724{
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000725 if (HASINPLACE(v) && v->ob_type->tp_as_number &&
726 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
727 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000728 }
Neil Schemenauer5a1f0152001-01-04 01:39:06 +0000729 else {
730 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
731 }
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000732}
733
734
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000735/* Unary operators and functions */
Guido van Rossume15dee51995-07-18 14:12:02 +0000736
737PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000738PyNumber_Negative(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000739{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000740 PyNumberMethods *m;
741
742 if (o == NULL)
743 return null_error();
744 m = o->ob_type->tp_as_number;
745 if (m && m->nb_negative)
746 return (*m->nb_negative)(o);
747
748 return type_error("bad operand type for unary -");
Guido van Rossume15dee51995-07-18 14:12:02 +0000749}
750
751PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000752PyNumber_Positive(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000753{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000754 PyNumberMethods *m;
755
756 if (o == NULL)
757 return null_error();
758 m = o->ob_type->tp_as_number;
759 if (m && m->nb_positive)
760 return (*m->nb_positive)(o);
761
762 return type_error("bad operand type for unary +");
Guido van Rossume15dee51995-07-18 14:12:02 +0000763}
764
765PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000766PyNumber_Invert(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000767{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000768 PyNumberMethods *m;
769
770 if (o == NULL)
771 return null_error();
772 m = o->ob_type->tp_as_number;
773 if (m && m->nb_invert)
774 return (*m->nb_invert)(o);
775
776 return type_error("bad operand type for unary ~");
Guido van Rossume15dee51995-07-18 14:12:02 +0000777}
778
779PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000780PyNumber_Absolute(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000781{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000782 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000783
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000784 if (o == NULL)
785 return null_error();
786 m = o->ob_type->tp_as_number;
787 if (m && m->nb_absolute)
788 return m->nb_absolute(o);
Guido van Rossume15dee51995-07-18 14:12:02 +0000789
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000790 return type_error("bad operand type for abs()");
Guido van Rossume15dee51995-07-18 14:12:02 +0000791}
792
Guido van Rossum9e896b32000-04-05 20:11:21 +0000793/* Add a check for embedded NULL-bytes in the argument. */
794static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000795int_from_string(const char *s, int len)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000796{
797 char *end;
798 PyObject *x;
799
800 x = PyInt_FromString((char*)s, &end, 10);
801 if (x == NULL)
802 return NULL;
803 if (end != s + len) {
804 PyErr_SetString(PyExc_ValueError,
805 "null byte in argument for int()");
806 Py_DECREF(x);
807 return NULL;
808 }
809 return x;
810}
811
Guido van Rossume15dee51995-07-18 14:12:02 +0000812PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000813PyNumber_Int(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000814{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000815 PyNumberMethods *m;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000816 const char *buffer;
817 int buffer_len;
Guido van Rossume15dee51995-07-18 14:12:02 +0000818
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000819 if (o == NULL)
820 return null_error();
Guido van Rossum9e896b32000-04-05 20:11:21 +0000821 if (PyInt_Check(o)) {
822 Py_INCREF(o);
823 return o;
824 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000825 if (PyString_Check(o))
Guido van Rossum9e896b32000-04-05 20:11:21 +0000826 return int_from_string(PyString_AS_STRING(o),
827 PyString_GET_SIZE(o));
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000828#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000829 if (PyUnicode_Check(o))
830 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
831 PyUnicode_GET_SIZE(o),
832 10);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000833#endif
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000834 m = o->ob_type->tp_as_number;
835 if (m && m->nb_int)
836 return m->nb_int(o);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000837 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
Guido van Rossum9e896b32000-04-05 20:11:21 +0000838 return int_from_string((char*)buffer, buffer_len);
Guido van Rossume15dee51995-07-18 14:12:02 +0000839
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000840 return type_error("object can't be converted to int");
Guido van Rossume15dee51995-07-18 14:12:02 +0000841}
842
Guido van Rossum9e896b32000-04-05 20:11:21 +0000843/* Add a check for embedded NULL-bytes in the argument. */
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000844static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000845long_from_string(const char *s, int len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000846{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000847 char *end;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000848 PyObject *x;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000849
Guido van Rossum4c08d552000-03-10 22:55:18 +0000850 x = PyLong_FromString((char*)s, &end, 10);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000851 if (x == NULL)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000852 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000853 if (end != s + len) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000854 PyErr_SetString(PyExc_ValueError,
855 "null byte in argument for long()");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000856 Py_DECREF(x);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000857 return NULL;
858 }
859 return x;
860}
861
Guido van Rossume15dee51995-07-18 14:12:02 +0000862PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000863PyNumber_Long(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000864{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000865 PyNumberMethods *m;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000866 const char *buffer;
867 int buffer_len;
Guido van Rossume15dee51995-07-18 14:12:02 +0000868
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000869 if (o == NULL)
870 return null_error();
Guido van Rossum9e896b32000-04-05 20:11:21 +0000871 if (PyLong_Check(o)) {
872 Py_INCREF(o);
873 return o;
874 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000875 if (PyString_Check(o))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000876 /* need to do extra error checking that PyLong_FromString()
877 * doesn't do. In particular long('9.5') must raise an
878 * exception, not truncate the float.
879 */
Guido van Rossum4c08d552000-03-10 22:55:18 +0000880 return long_from_string(PyString_AS_STRING(o),
881 PyString_GET_SIZE(o));
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000882#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000883 if (PyUnicode_Check(o))
884 /* The above check is done in PyLong_FromUnicode(). */
885 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
886 PyUnicode_GET_SIZE(o),
887 10);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000888#endif
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000889 m = o->ob_type->tp_as_number;
890 if (m && m->nb_long)
891 return m->nb_long(o);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000892 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
893 return long_from_string(buffer, buffer_len);
Guido van Rossume15dee51995-07-18 14:12:02 +0000894
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000895 return type_error("object can't be converted to long");
Guido van Rossume15dee51995-07-18 14:12:02 +0000896}
897
898PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000899PyNumber_Float(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000900{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000901 PyNumberMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000902
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000903 if (o == NULL)
904 return null_error();
Guido van Rossum9e896b32000-04-05 20:11:21 +0000905 if (PyFloat_Check(o)) {
906 Py_INCREF(o);
907 return o;
908 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000909 if (!PyString_Check(o)) {
910 m = o->ob_type->tp_as_number;
911 if (m && m->nb_float)
912 return m->nb_float(o);
913 }
914 return PyFloat_FromString(o, NULL);
Guido van Rossume15dee51995-07-18 14:12:02 +0000915}
916
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000917/* Operations on sequences */
Guido van Rossume15dee51995-07-18 14:12:02 +0000918
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000919int
Fred Drake79912472000-07-09 04:06:11 +0000920PySequence_Check(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +0000921{
Guido van Rossum8700b422001-09-07 20:20:11 +0000922 return s != NULL && s->ob_type->tp_as_sequence &&
923 s->ob_type->tp_as_sequence->sq_item != NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +0000924}
925
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000926int
Jeremy Hylton6253f832000-07-12 12:56:19 +0000927PySequence_Size(PyObject *s)
Guido van Rossume15dee51995-07-18 14:12:02 +0000928{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000929 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000930
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000931 if (s == NULL) {
932 null_error();
933 return -1;
934 }
Guido van Rossume15dee51995-07-18 14:12:02 +0000935
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000936 m = s->ob_type->tp_as_sequence;
937 if (m && m->sq_length)
938 return m->sq_length(s);
Guido van Rossume15dee51995-07-18 14:12:02 +0000939
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000940 type_error("len() of unsized object");
941 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +0000942}
943
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000944#undef PySequence_Length
945int
946PySequence_Length(PyObject *s)
947{
948 return PySequence_Size(s);
949}
950#define PySequence_Length PySequence_Size
951
Guido van Rossume15dee51995-07-18 14:12:02 +0000952PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000953PySequence_Concat(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +0000954{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000955 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000956
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000957 if (s == NULL || o == NULL)
958 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +0000959
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000960 m = s->ob_type->tp_as_sequence;
961 if (m && m->sq_concat)
962 return m->sq_concat(s, o);
963
964 return type_error("object can't be concatenated");
Guido van Rossume15dee51995-07-18 14:12:02 +0000965}
966
967PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000968PySequence_Repeat(PyObject *o, int count)
Guido van Rossume15dee51995-07-18 14:12:02 +0000969{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000970 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +0000971
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000972 if (o == NULL)
973 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +0000974
Guido van Rossumcea1c8c1998-05-22 00:47:05 +0000975 m = o->ob_type->tp_as_sequence;
976 if (m && m->sq_repeat)
977 return m->sq_repeat(o, count);
978
979 return type_error("object can't be repeated");
Guido van Rossume15dee51995-07-18 14:12:02 +0000980}
981
982PyObject *
Thomas Wouterse289e0b2000-08-24 20:08:19 +0000983PySequence_InPlaceConcat(PyObject *s, PyObject *o)
984{
985 PySequenceMethods *m;
986
987 if (s == NULL || o == NULL)
988 return null_error();
989
990 m = s->ob_type->tp_as_sequence;
991 if (m && HASINPLACE(s) && m->sq_inplace_concat)
992 return m->sq_inplace_concat(s, o);
993 if (m && m->sq_concat)
994 return m->sq_concat(s, o);
995
996 return type_error("object can't be concatenated");
997}
998
999PyObject *
1000PySequence_InPlaceRepeat(PyObject *o, int count)
1001{
1002 PySequenceMethods *m;
1003
1004 if (o == NULL)
1005 return null_error();
1006
1007 m = o->ob_type->tp_as_sequence;
1008 if (m && HASINPLACE(o) && m->sq_inplace_repeat)
1009 return m->sq_inplace_repeat(o, count);
1010 if (m && m->sq_repeat)
1011 return m->sq_repeat(o, count);
1012
1013 return type_error("object can't be repeated");
1014}
1015
1016PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001017PySequence_GetItem(PyObject *s, int i)
Guido van Rossume15dee51995-07-18 14:12:02 +00001018{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001019 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001020
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001021 if (s == NULL)
1022 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001023
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001024 m = s->ob_type->tp_as_sequence;
1025 if (m && m->sq_item) {
1026 if (i < 0) {
1027 if (m->sq_length) {
1028 int l = (*m->sq_length)(s);
1029 if (l < 0)
1030 return NULL;
1031 i += l;
1032 }
1033 }
1034 return m->sq_item(s, i);
1035 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001036
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001037 return type_error("unindexable object");
Guido van Rossume15dee51995-07-18 14:12:02 +00001038}
1039
Thomas Wouters1d75a792000-08-17 22:37:32 +00001040static PyObject *
1041sliceobj_from_intint(int i, int j)
1042{
1043 PyObject *start, *end, *slice;
1044 start = PyInt_FromLong((long)i);
1045 if (!start)
1046 return NULL;
1047 end = PyInt_FromLong((long)j);
1048 if (!end) {
1049 Py_DECREF(start);
1050 return NULL;
1051 }
1052 slice = PySlice_New(start, end, NULL);
1053 Py_DECREF(start);
1054 Py_DECREF(end);
1055 return slice;
1056}
1057
Guido van Rossume15dee51995-07-18 14:12:02 +00001058PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001059PySequence_GetSlice(PyObject *s, int i1, int i2)
Guido van Rossume15dee51995-07-18 14:12:02 +00001060{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001061 PySequenceMethods *m;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001062 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001063
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001064 if (!s) return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001065
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001066 m = s->ob_type->tp_as_sequence;
1067 if (m && m->sq_slice) {
1068 if (i1 < 0 || i2 < 0) {
1069 if (m->sq_length) {
1070 int l = (*m->sq_length)(s);
1071 if (l < 0)
1072 return NULL;
1073 if (i1 < 0)
1074 i1 += l;
1075 if (i2 < 0)
1076 i2 += l;
1077 }
1078 }
1079 return m->sq_slice(s, i1, i2);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001080 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) {
1081 PyObject *res;
1082 PyObject *slice = sliceobj_from_intint(i1, i2);
1083 if (!slice)
1084 return NULL;
1085 res = mp->mp_subscript(s, slice);
1086 Py_DECREF(slice);
1087 return res;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001088 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001089
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001090 return type_error("unsliceable object");
Guido van Rossume15dee51995-07-18 14:12:02 +00001091}
1092
1093int
Fred Drake79912472000-07-09 04:06:11 +00001094PySequence_SetItem(PyObject *s, int i, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001095{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001096 PySequenceMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001097
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001098 if (s == NULL) {
1099 null_error();
1100 return -1;
1101 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001102
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001103 m = s->ob_type->tp_as_sequence;
1104 if (m && m->sq_ass_item) {
1105 if (i < 0) {
1106 if (m->sq_length) {
1107 int l = (*m->sq_length)(s);
1108 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001109 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001110 i += l;
1111 }
1112 }
1113 return m->sq_ass_item(s, i, o);
1114 }
1115
1116 type_error("object doesn't support item assignment");
1117 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001118}
1119
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001120int
Fred Drake79912472000-07-09 04:06:11 +00001121PySequence_DelItem(PyObject *s, int i)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001122{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001123 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001124
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001125 if (s == NULL) {
1126 null_error();
1127 return -1;
1128 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001129
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001130 m = s->ob_type->tp_as_sequence;
1131 if (m && m->sq_ass_item) {
1132 if (i < 0) {
1133 if (m->sq_length) {
1134 int l = (*m->sq_length)(s);
1135 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001136 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001137 i += l;
1138 }
1139 }
1140 return m->sq_ass_item(s, i, (PyObject *)NULL);
1141 }
1142
1143 type_error("object doesn't support item deletion");
1144 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001145}
1146
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001147int
Fred Drake79912472000-07-09 04:06:11 +00001148PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001149{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001150 PySequenceMethods *m;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001151 PyMappingMethods *mp;
Guido van Rossume15dee51995-07-18 14:12:02 +00001152
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001153 if (s == NULL) {
1154 null_error();
1155 return -1;
1156 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001157
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001158 m = s->ob_type->tp_as_sequence;
1159 if (m && m->sq_ass_slice) {
1160 if (i1 < 0 || i2 < 0) {
1161 if (m->sq_length) {
1162 int l = (*m->sq_length)(s);
1163 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001164 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001165 if (i1 < 0)
1166 i1 += l;
1167 if (i2 < 0)
1168 i2 += l;
1169 }
1170 }
1171 return m->sq_ass_slice(s, i1, i2, o);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001172 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
1173 int res;
1174 PyObject *slice = sliceobj_from_intint(i1, i2);
1175 if (!slice)
1176 return -1;
1177 res = mp->mp_ass_subscript(s, slice, o);
1178 Py_DECREF(slice);
1179 return res;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001180 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001181
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001182 type_error("object doesn't support slice assignment");
1183 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001184}
1185
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001186int
Fred Drake79912472000-07-09 04:06:11 +00001187PySequence_DelSlice(PyObject *s, int i1, int i2)
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001188{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001189 PySequenceMethods *m;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001190
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001191 if (s == NULL) {
1192 null_error();
1193 return -1;
1194 }
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001195
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001196 m = s->ob_type->tp_as_sequence;
1197 if (m && m->sq_ass_slice) {
1198 if (i1 < 0 || i2 < 0) {
1199 if (m->sq_length) {
1200 int l = (*m->sq_length)(s);
1201 if (l < 0)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001202 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001203 if (i1 < 0)
1204 i1 += l;
1205 if (i2 < 0)
1206 i2 += l;
1207 }
1208 }
1209 return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
1210 }
1211 type_error("object doesn't support slice deletion");
1212 return -1;
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001213}
1214
Guido van Rossume15dee51995-07-18 14:12:02 +00001215PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001216PySequence_Tuple(PyObject *v)
Guido van Rossume15dee51995-07-18 14:12:02 +00001217{
Tim Peters6912d4d2001-05-05 03:56:37 +00001218 PyObject *it; /* iter(v) */
1219 int n; /* guess for result tuple size */
1220 PyObject *result;
1221 int j;
Guido van Rossume15dee51995-07-18 14:12:02 +00001222
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001223 if (v == NULL)
1224 return null_error();
Guido van Rossume15dee51995-07-18 14:12:02 +00001225
Tim Peters6912d4d2001-05-05 03:56:37 +00001226 /* Special-case the common tuple and list cases, for efficiency. */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001227 if (PyTuple_Check(v)) {
1228 Py_INCREF(v);
1229 return v;
Guido van Rossume15dee51995-07-18 14:12:02 +00001230 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001231 if (PyList_Check(v))
1232 return PyList_AsTuple(v);
1233
Tim Peters6912d4d2001-05-05 03:56:37 +00001234 /* Get iterator. */
1235 it = PyObject_GetIter(v);
1236 if (it == NULL)
1237 return type_error("tuple() argument must support iteration");
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001238
Tim Peters6912d4d2001-05-05 03:56:37 +00001239 /* Guess result size and allocate space. */
1240 n = PySequence_Size(v);
1241 if (n < 0) {
1242 PyErr_Clear();
1243 n = 10; /* arbitrary */
1244 }
1245 result = PyTuple_New(n);
1246 if (result == NULL)
1247 goto Fail;
1248
1249 /* Fill the tuple. */
1250 for (j = 0; ; ++j) {
1251 PyObject *item = PyIter_Next(it);
1252 if (item == NULL) {
1253 if (PyErr_Occurred())
1254 goto Fail;
1255 break;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001256 }
Tim Peters6912d4d2001-05-05 03:56:37 +00001257 if (j >= n) {
1258 if (n < 500)
1259 n += 10;
1260 else
1261 n += 100;
Tim Peters4324aa32001-05-28 22:30:08 +00001262 if (_PyTuple_Resize(&result, n) != 0) {
Tim Peters12d0a6c2001-05-05 04:10:25 +00001263 Py_DECREF(item);
Tim Peters6912d4d2001-05-05 03:56:37 +00001264 goto Fail;
Tim Peters12d0a6c2001-05-05 04:10:25 +00001265 }
Tim Peters6912d4d2001-05-05 03:56:37 +00001266 }
1267 PyTuple_SET_ITEM(result, j, item);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001268 }
1269
Tim Peters6912d4d2001-05-05 03:56:37 +00001270 /* Cut tuple back if guess was too large. */
1271 if (j < n &&
Tim Peters4324aa32001-05-28 22:30:08 +00001272 _PyTuple_Resize(&result, j) != 0)
Tim Peters6912d4d2001-05-05 03:56:37 +00001273 goto Fail;
1274
1275 Py_DECREF(it);
1276 return result;
1277
1278Fail:
1279 Py_XDECREF(result);
1280 Py_DECREF(it);
1281 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001282}
1283
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001284PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001285PySequence_List(PyObject *v)
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001286{
Tim Petersf553f892001-05-01 20:45:31 +00001287 PyObject *it; /* iter(v) */
1288 PyObject *result; /* result list */
1289 int n; /* guess for result list size */
1290 int i;
Guido van Rossum4669fb41997-04-02 05:31:09 +00001291
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001292 if (v == NULL)
1293 return null_error();
1294
Tim Petersf553f892001-05-01 20:45:31 +00001295 /* Special-case list(a_list), for speed. */
Guido van Rossum5dba9e81998-07-10 18:03:50 +00001296 if (PyList_Check(v))
1297 return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
1298
Tim Petersf553f892001-05-01 20:45:31 +00001299 /* Get iterator. There may be some low-level efficiency to be gained
1300 * by caching the tp_iternext slot instead of using PyIter_Next()
1301 * later, but premature optimization is the root etc.
1302 */
1303 it = PyObject_GetIter(v);
1304 if (it == NULL)
1305 return NULL;
1306
1307 /* Guess a result list size. */
1308 n = -1; /* unknown */
1309 if (PySequence_Check(v) &&
1310 v->ob_type->tp_as_sequence->sq_length) {
1311 n = PySequence_Size(v);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001312 if (n < 0)
Tim Petersf553f892001-05-01 20:45:31 +00001313 PyErr_Clear();
1314 }
1315 if (n < 0)
1316 n = 8; /* arbitrary */
1317 result = PyList_New(n);
1318 if (result == NULL) {
1319 Py_DECREF(it);
1320 return NULL;
1321 }
1322
1323 /* Run iterator to exhaustion. */
1324 for (i = 0; ; i++) {
1325 PyObject *item = PyIter_Next(it);
1326 if (item == NULL) {
Tim Petersf553f892001-05-01 20:45:31 +00001327 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001328 Py_DECREF(result);
1329 result = NULL;
Guido van Rossumbfc725b1998-07-10 16:22:44 +00001330 }
Tim Petersf553f892001-05-01 20:45:31 +00001331 break;
Guido van Rossumbfc725b1998-07-10 16:22:44 +00001332 }
Tim Petersf553f892001-05-01 20:45:31 +00001333 if (i < n)
Tim Peters6ad22c42001-05-02 07:12:39 +00001334 PyList_SET_ITEM(result, i, item); /* steals ref */
1335 else {
1336 int status = PyList_Append(result, item);
1337 Py_DECREF(item); /* append creates a new ref */
1338 if (status < 0) {
1339 Py_DECREF(result);
1340 result = NULL;
1341 break;
1342 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001343 }
Guido van Rossum4669fb41997-04-02 05:31:09 +00001344 }
Tim Petersf553f892001-05-01 20:45:31 +00001345
1346 /* Cut back result list if initial guess was too large. */
1347 if (i < n && result != NULL) {
1348 if (PyList_SetSlice(result, i, n, (PyObject *)NULL) != 0) {
1349 Py_DECREF(result);
1350 result = NULL;
1351 }
1352 }
1353 Py_DECREF(it);
1354 return result;
Guido van Rossum3c5936a1996-12-05 21:51:24 +00001355}
1356
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001357PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001358PySequence_Fast(PyObject *v, const char *m)
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001359{
1360 if (v == NULL)
1361 return null_error();
1362
1363 if (PyList_Check(v) || PyTuple_Check(v)) {
1364 Py_INCREF(v);
1365 return v;
1366 }
1367
1368 v = PySequence_Tuple(v);
1369 if (v == NULL && PyErr_ExceptionMatches(PyExc_TypeError))
1370 return type_error(m);
1371
1372 return v;
1373}
1374
Tim Peters16a77ad2001-09-08 04:00:12 +00001375/* Iterate over seq. Result depends on the operation:
1376 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1377 PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq;
1378 set ValueError and return -1 if none found; also return -1 on error.
1379 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1380*/
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001381int
Tim Peters16a77ad2001-09-08 04:00:12 +00001382_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
Guido van Rossume15dee51995-07-18 14:12:02 +00001383{
Tim Peters16a77ad2001-09-08 04:00:12 +00001384 int n;
1385 int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1386 PyObject *it; /* iter(seq) */
Guido van Rossume15dee51995-07-18 14:12:02 +00001387
Tim Peters16a77ad2001-09-08 04:00:12 +00001388 if (seq == NULL || obj == NULL) {
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001389 null_error();
1390 return -1;
1391 }
Tim Peters75f8e352001-05-05 11:33:43 +00001392
Tim Peters16a77ad2001-09-08 04:00:12 +00001393 it = PyObject_GetIter(seq);
Tim Peters75f8e352001-05-05 11:33:43 +00001394 if (it == NULL) {
Tim Peters16a77ad2001-09-08 04:00:12 +00001395 type_error("iterable argument required");
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001396 return -1;
Tim Peters75f8e352001-05-05 11:33:43 +00001397 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001398
Tim Peters16a77ad2001-09-08 04:00:12 +00001399 n = wrapped = 0;
Tim Peters75f8e352001-05-05 11:33:43 +00001400 for (;;) {
1401 int cmp;
1402 PyObject *item = PyIter_Next(it);
1403 if (item == NULL) {
1404 if (PyErr_Occurred())
1405 goto Fail;
1406 break;
1407 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001408
1409 cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001410 Py_DECREF(item);
Tim Peters75f8e352001-05-05 11:33:43 +00001411 if (cmp < 0)
1412 goto Fail;
1413 if (cmp > 0) {
Tim Peters16a77ad2001-09-08 04:00:12 +00001414 switch (operation) {
1415 case PY_ITERSEARCH_COUNT:
1416 ++n;
1417 if (n <= 0) {
1418 PyErr_SetString(PyExc_OverflowError,
Tim Peters75f8e352001-05-05 11:33:43 +00001419 "count exceeds C int size");
Tim Peters16a77ad2001-09-08 04:00:12 +00001420 goto Fail;
1421 }
1422 break;
1423
1424 case PY_ITERSEARCH_INDEX:
1425 if (wrapped) {
1426 PyErr_SetString(PyExc_OverflowError,
1427 "index exceeds C int size");
1428 goto Fail;
1429 }
1430 goto Done;
1431
1432 case PY_ITERSEARCH_CONTAINS:
1433 n = 1;
1434 goto Done;
1435
1436 default:
1437 assert(!"unknown operation");
Tim Peters75f8e352001-05-05 11:33:43 +00001438 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001439 }
1440
1441 if (operation == PY_ITERSEARCH_INDEX) {
1442 ++n;
1443 if (n <= 0)
1444 wrapped = 1;
Tim Peters75f8e352001-05-05 11:33:43 +00001445 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001446 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001447
1448 if (operation != PY_ITERSEARCH_INDEX)
1449 goto Done;
1450
1451 PyErr_SetString(PyExc_ValueError,
1452 "sequence.index(x): x not in sequence");
1453 /* fall into failure code */
1454Fail:
1455 n = -1;
1456 /* fall through */
1457Done:
Tim Peters75f8e352001-05-05 11:33:43 +00001458 Py_DECREF(it);
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001459 return n;
Tim Peters75f8e352001-05-05 11:33:43 +00001460
Guido van Rossume15dee51995-07-18 14:12:02 +00001461}
1462
Tim Peters16a77ad2001-09-08 04:00:12 +00001463/* Return # of times o appears in s. */
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001464int
Tim Peters16a77ad2001-09-08 04:00:12 +00001465PySequence_Count(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001466{
Tim Peters16a77ad2001-09-08 04:00:12 +00001467 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
Guido van Rossume15dee51995-07-18 14:12:02 +00001468}
1469
Tim Peterscb8d3682001-05-05 21:05:01 +00001470/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
Tim Peters16a77ad2001-09-08 04:00:12 +00001471 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001472 */
1473int
1474PySequence_Contains(PyObject *seq, PyObject *ob)
1475{
1476 if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
1477 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1478 if (sqm != NULL && sqm->sq_contains != NULL)
1479 return (*sqm->sq_contains)(seq, ob);
1480 }
Tim Peters16a77ad2001-09-08 04:00:12 +00001481 return _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
Tim Peterscb8d3682001-05-05 21:05:01 +00001482}
1483
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001484/* Backwards compatibility */
1485#undef PySequence_In
1486int
Fred Drake79912472000-07-09 04:06:11 +00001487PySequence_In(PyObject *w, PyObject *v)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001488{
1489 return PySequence_Contains(w, v);
1490}
1491
1492int
Fred Drake79912472000-07-09 04:06:11 +00001493PySequence_Index(PyObject *s, PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001494{
Tim Peters16a77ad2001-09-08 04:00:12 +00001495 return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
Guido van Rossume15dee51995-07-18 14:12:02 +00001496}
1497
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001498/* Operations on mappings */
1499
1500int
Fred Drake79912472000-07-09 04:06:11 +00001501PyMapping_Check(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001502{
Guido van Rossum8700b422001-09-07 20:20:11 +00001503 return o && o->ob_type->tp_as_mapping &&
1504 o->ob_type->tp_as_mapping->mp_subscript;
Guido van Rossume15dee51995-07-18 14:12:02 +00001505}
1506
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001507int
Jeremy Hylton6253f832000-07-12 12:56:19 +00001508PyMapping_Size(PyObject *o)
Guido van Rossume15dee51995-07-18 14:12:02 +00001509{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001510 PyMappingMethods *m;
Guido van Rossume15dee51995-07-18 14:12:02 +00001511
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001512 if (o == NULL) {
1513 null_error();
1514 return -1;
1515 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001516
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001517 m = o->ob_type->tp_as_mapping;
1518 if (m && m->mp_length)
1519 return m->mp_length(o);
Guido van Rossume15dee51995-07-18 14:12:02 +00001520
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001521 type_error("len() of unsized object");
1522 return -1;
Guido van Rossume15dee51995-07-18 14:12:02 +00001523}
1524
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001525#undef PyMapping_Length
1526int
1527PyMapping_Length(PyObject *o)
1528{
1529 return PyMapping_Size(o);
1530}
1531#define PyMapping_Length PyMapping_Size
1532
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001533PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001534PyMapping_GetItemString(PyObject *o, char *key)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001535{
1536 PyObject *okey, *r;
1537
1538 if (key == NULL)
1539 return null_error();
1540
1541 okey = PyString_FromString(key);
1542 if (okey == NULL)
1543 return NULL;
1544 r = PyObject_GetItem(o, okey);
1545 Py_DECREF(okey);
1546 return r;
1547}
1548
1549int
Fred Drake79912472000-07-09 04:06:11 +00001550PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001551{
1552 PyObject *okey;
1553 int r;
1554
1555 if (key == NULL) {
1556 null_error();
1557 return -1;
1558 }
1559
1560 okey = PyString_FromString(key);
1561 if (okey == NULL)
Guido van Rossumed6219b1998-05-29 02:59:33 +00001562 return -1;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001563 r = PyObject_SetItem(o, okey, value);
1564 Py_DECREF(okey);
1565 return r;
1566}
1567
1568int
Fred Drake79912472000-07-09 04:06:11 +00001569PyMapping_HasKeyString(PyObject *o, char *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00001570{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001571 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00001572
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001573 v = PyMapping_GetItemString(o, key);
1574 if (v) {
1575 Py_DECREF(v);
1576 return 1;
1577 }
1578 PyErr_Clear();
1579 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00001580}
1581
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001582int
Fred Drake79912472000-07-09 04:06:11 +00001583PyMapping_HasKey(PyObject *o, PyObject *key)
Guido van Rossume15dee51995-07-18 14:12:02 +00001584{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001585 PyObject *v;
Guido van Rossume15dee51995-07-18 14:12:02 +00001586
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001587 v = PyObject_GetItem(o, key);
1588 if (v) {
1589 Py_DECREF(v);
1590 return 1;
1591 }
1592 PyErr_Clear();
1593 return 0;
Guido van Rossume15dee51995-07-18 14:12:02 +00001594}
1595
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001596/* Operations on callable objects */
1597
1598/* XXX PyCallable_Check() is in object.c */
1599
Guido van Rossume15dee51995-07-18 14:12:02 +00001600PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001601PyObject_CallObject(PyObject *o, PyObject *a)
Guido van Rossume15dee51995-07-18 14:12:02 +00001602{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001603 PyObject *r;
1604 PyObject *args = a;
Guido van Rossume15dee51995-07-18 14:12:02 +00001605
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001606 if (args == NULL) {
1607 args = PyTuple_New(0);
1608 if (args == NULL)
1609 return NULL;
1610 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001611
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001612 r = PyEval_CallObject(o, args);
1613
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00001614 if (args != a) {
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001615 Py_DECREF(args);
Guido van Rossumfa0b6ab1998-05-22 15:23:36 +00001616 }
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001617
1618 return r;
1619}
Guido van Rossume15dee51995-07-18 14:12:02 +00001620
1621PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001622PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
1623{
1624 ternaryfunc call;
1625
1626 if ((call = func->ob_type->tp_call) != NULL) {
1627 PyObject *result = (*call)(func, arg, kw);
1628 if (result == NULL && !PyErr_Occurred())
1629 PyErr_SetString(
1630 PyExc_SystemError,
1631 "NULL result without error in PyObject_Call");
1632 return result;
1633 }
1634 PyErr_Format(PyExc_TypeError, "object is not callable: %s",
1635 PyString_AS_STRING(PyObject_Repr(func)));
1636 return NULL;
1637}
1638
1639PyObject *
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001640PyObject_CallFunction(PyObject *callable, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00001641{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001642 va_list va;
1643 PyObject *args, *retval;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001644 va_start(va, format);
Guido van Rossume15dee51995-07-18 14:12:02 +00001645
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001646 if (callable == NULL) {
1647 va_end(va);
1648 return null_error();
1649 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001650
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001651 if (format)
1652 args = Py_VaBuildValue(format, va);
1653 else
1654 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00001655
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001656 va_end(va);
1657
1658 if (args == NULL)
1659 return NULL;
1660
1661 if (!PyTuple_Check(args)) {
1662 PyObject *a;
1663
1664 a = PyTuple_New(1);
1665 if (a == NULL)
1666 return NULL;
1667 if (PyTuple_SetItem(a, 0, args) < 0)
1668 return NULL;
1669 args = a;
1670 }
1671 retval = PyObject_CallObject(callable, args);
1672
1673 Py_DECREF(args);
1674
1675 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00001676}
1677
1678PyObject *
Guido van Rossume15dee51995-07-18 14:12:02 +00001679PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
Guido van Rossume15dee51995-07-18 14:12:02 +00001680{
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001681 va_list va;
1682 PyObject *args, *func = 0, *retval;
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001683 va_start(va, format);
Guido van Rossume15dee51995-07-18 14:12:02 +00001684
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001685 if (o == NULL || name == NULL) {
1686 va_end(va);
1687 return null_error();
1688 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001689
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001690 func = PyObject_GetAttrString(o, name);
1691 if (func == NULL) {
1692 va_end(va);
1693 PyErr_SetString(PyExc_AttributeError, name);
1694 return 0;
1695 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001696
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001697 if (!PyCallable_Check(func)) {
1698 va_end(va);
1699 return type_error("call of non-callable attribute");
1700 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001701
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001702 if (format && *format)
1703 args = Py_VaBuildValue(format, va);
1704 else
1705 args = PyTuple_New(0);
Guido van Rossume15dee51995-07-18 14:12:02 +00001706
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001707 va_end(va);
Guido van Rossume15dee51995-07-18 14:12:02 +00001708
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001709 if (!args)
1710 return NULL;
Guido van Rossume15dee51995-07-18 14:12:02 +00001711
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001712 if (!PyTuple_Check(args)) {
1713 PyObject *a;
Guido van Rossume15dee51995-07-18 14:12:02 +00001714
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001715 a = PyTuple_New(1);
1716 if (a == NULL)
1717 return NULL;
1718 if (PyTuple_SetItem(a, 0, args) < 0)
1719 return NULL;
1720 args = a;
1721 }
Guido van Rossume15dee51995-07-18 14:12:02 +00001722
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001723 retval = PyObject_CallObject(func, args);
Guido van Rossume15dee51995-07-18 14:12:02 +00001724
Guido van Rossumcea1c8c1998-05-22 00:47:05 +00001725 Py_DECREF(args);
1726 Py_DECREF(func);
1727
1728 return retval;
Guido van Rossume15dee51995-07-18 14:12:02 +00001729}
Guido van Rossum823649d2001-03-21 18:40:58 +00001730
1731
1732/* isinstance(), issubclass() */
1733
1734static int
1735abstract_issubclass(PyObject *derived, PyObject *cls, int first)
1736{
1737 static PyObject *__bases__ = NULL;
1738 PyObject *bases;
1739 int i, n;
1740 int r = 0;
1741
1742 if (__bases__ == NULL) {
1743 __bases__ = PyString_FromString("__bases__");
1744 if (__bases__ == NULL)
1745 return -1;
1746 }
1747
1748 if (first) {
1749 bases = PyObject_GetAttr(cls, __bases__);
1750 if (bases == NULL || !PyTuple_Check(bases)) {
1751 Py_XDECREF(bases);
1752 PyErr_SetString(PyExc_TypeError,
1753 "issubclass() arg 2 must be a class");
1754 return -1;
1755 }
1756 Py_DECREF(bases);
1757 }
1758
1759 if (derived == cls)
1760 return 1;
1761
1762 bases = PyObject_GetAttr(derived, __bases__);
1763 if (bases == NULL || !PyTuple_Check(bases)) {
1764 Py_XDECREF(bases);
1765 PyErr_SetString(PyExc_TypeError,
1766 "issubclass() arg 1 must be a class");
1767 return -1;
1768 }
1769
1770 n = PyTuple_GET_SIZE(bases);
1771 for (i = 0; i < n; i++) {
1772 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls, 0);
1773 if (r != 0)
1774 break;
1775 }
1776
1777 Py_DECREF(bases);
1778
1779 return r;
1780}
1781
1782int
1783PyObject_IsInstance(PyObject *inst, PyObject *cls)
1784{
1785 PyObject *icls;
1786 static PyObject *__class__ = NULL;
1787 int retval = 0;
1788
1789 if (PyClass_Check(cls)) {
1790 if (PyInstance_Check(inst)) {
1791 PyObject *inclass =
1792 (PyObject*)((PyInstanceObject*)inst)->in_class;
1793 retval = PyClass_IsSubclass(inclass, cls);
1794 }
1795 }
1796 else if (PyType_Check(cls)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001797 retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
Guido van Rossum823649d2001-03-21 18:40:58 +00001798 }
1799 else if (!PyInstance_Check(inst)) {
1800 if (__class__ == NULL) {
1801 __class__ = PyString_FromString("__class__");
1802 if (__class__ == NULL)
1803 return -1;
1804 }
1805 icls = PyObject_GetAttr(inst, __class__);
1806 if (icls != NULL) {
1807 retval = abstract_issubclass(icls, cls, 1);
1808 Py_DECREF(icls);
1809 if (retval < 0 &&
1810 !PyErr_ExceptionMatches(PyExc_TypeError))
1811 return -1;
1812 }
1813 else
1814 retval = -1;
1815 }
1816 else
1817 retval = -1;
1818
1819 if (retval < 0) {
1820 PyErr_SetString(PyExc_TypeError,
1821 "isinstance() arg 2 must be a class or type");
1822 }
1823 return retval;
1824}
1825
1826int
1827PyObject_IsSubclass(PyObject *derived, PyObject *cls)
1828{
1829 int retval;
1830
1831 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1832 retval = abstract_issubclass(derived, cls, 1);
1833 }
1834 else {
1835 /* shortcut */
1836 if (!(retval = (derived == cls)))
1837 retval = PyClass_IsSubclass(derived, cls);
1838 }
1839
1840 return retval;
1841}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001842
1843PyObject *
1844PyObject_GetIter(PyObject *o)
1845{
1846 PyTypeObject *t = o->ob_type;
1847 getiterfunc f = NULL;
1848 if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER))
1849 f = t->tp_iter;
1850 if (f == NULL) {
1851 if (PySequence_Check(o))
Guido van Rossum213c7a62001-04-23 14:08:49 +00001852 return PySeqIter_New(o);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001853 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
1854 return NULL;
1855 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00001856 else {
1857 PyObject *res = (*f)(o);
1858 if (res != NULL && !PyIter_Check(res)) {
1859 PyErr_Format(PyExc_TypeError,
1860 "iter() returned non-iterator "
1861 "of type '%.100s'",
1862 res->ob_type->tp_name);
1863 Py_DECREF(res);
1864 res = NULL;
1865 }
1866 return res;
1867 }
1868}
1869
Tim Petersf4848da2001-05-05 00:14:56 +00001870/* Return next item.
1871 * If an error occurs, return NULL. PyErr_Occurred() will be true.
1872 * If the iteration terminates normally, return NULL and clear the
1873 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
1874 * will be false.
1875 * Else return the next object. PyErr_Occurred() will be false.
1876 */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001877PyObject *
1878PyIter_Next(PyObject *iter)
1879{
Tim Petersf4848da2001-05-05 00:14:56 +00001880 PyObject *result;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001881 if (!PyIter_Check(iter)) {
1882 PyErr_Format(PyExc_TypeError,
1883 "'%.100s' object is not an iterator",
1884 iter->ob_type->tp_name);
1885 return NULL;
1886 }
Tim Petersf4848da2001-05-05 00:14:56 +00001887 result = (*iter->ob_type->tp_iternext)(iter);
1888 if (result == NULL &&
1889 PyErr_Occurred() &&
1890 PyErr_ExceptionMatches(PyExc_StopIteration))
1891 PyErr_Clear();
1892 return result;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001893}