Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Objects / abstract.c
blobc120769687dbece44d131c6cf9cc7c8b17728240
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Abstract Object Interface (many thanks to Jim Fulton) */
34 #include "Python.h"
35 #include <ctype.h>
37 /* Shorthands to return certain errors */
39 static PyObject *
40 type_error(msg)
41 char *msg;
43 PyErr_SetString(PyExc_TypeError, msg);
44 return NULL;
47 static PyObject *
48 null_error()
50 if (!PyErr_Occurred())
51 PyErr_SetString(PyExc_SystemError,
52 "null argument to internal routine");
53 return NULL;
56 /* Operations on any object */
58 int
59 PyObject_Cmp(o1, o2, result)
60 PyObject *o1;
61 PyObject *o2;
62 int *result;
64 int r;
66 if (o1 == NULL || o2 == NULL) {
67 null_error();
68 return -1;
70 r = PyObject_Compare(o1, o2);
71 if (PyErr_Occurred())
72 return -1;
73 *result = r;
74 return 0;
77 PyObject *
78 PyObject_Type(o)
79 PyObject *o;
81 PyObject *v;
83 if (o == NULL)
84 return null_error();
85 v = (PyObject *)o->ob_type;
86 Py_INCREF(v);
87 return v;
90 int
91 PyObject_Length(o)
92 PyObject *o;
94 PySequenceMethods *m;
96 if (o == NULL) {
97 null_error();
98 return -1;
101 m = o->ob_type->tp_as_sequence;
102 if (m && m->sq_length)
103 return m->sq_length(o);
105 return PyMapping_Length(o);
108 PyObject *
109 PyObject_GetItem(o, key)
110 PyObject *o;
111 PyObject *key;
113 PyMappingMethods *m;
115 if (o == NULL || key == NULL)
116 return null_error();
118 m = o->ob_type->tp_as_mapping;
119 if (m && m->mp_subscript)
120 return m->mp_subscript(o, key);
122 if (o->ob_type->tp_as_sequence) {
123 if (PyInt_Check(key))
124 return PySequence_GetItem(o, PyInt_AsLong(key));
125 return type_error("sequence index must be integer");
128 return type_error("unsubscriptable object");
132 PyObject_SetItem(o, key, value)
133 PyObject *o;
134 PyObject *key;
135 PyObject *value;
137 PyMappingMethods *m;
139 if (o == NULL || key == NULL || value == NULL) {
140 null_error();
141 return -1;
143 m = o->ob_type->tp_as_mapping;
144 if (m && m->mp_ass_subscript)
145 return m->mp_ass_subscript(o, key, value);
147 if (o->ob_type->tp_as_sequence) {
148 if (PyInt_Check(key))
149 return PySequence_SetItem(o, PyInt_AsLong(key), value);
150 type_error("sequence index must be integer");
151 return -1;
154 type_error("object does not support item assignment");
155 return -1;
159 PyObject_DelItem(o, key)
160 PyObject *o;
161 PyObject *key;
163 PyMappingMethods *m;
165 if (o == NULL || key == NULL) {
166 null_error();
167 return -1;
169 m = o->ob_type->tp_as_mapping;
170 if (m && m->mp_ass_subscript)
171 return m->mp_ass_subscript(o, key, (PyObject*)NULL);
173 if (o->ob_type->tp_as_sequence) {
174 if (PyInt_Check(key))
175 return PySequence_DelItem(o, PyInt_AsLong(key));
176 type_error("sequence index must be integer");
177 return -1;
180 type_error("object does not support item deletion");
181 return -1;
184 /* Operations on numbers */
187 PyNumber_Check(o)
188 PyObject *o;
190 return o && o->ob_type->tp_as_number;
193 /* Binary operators */
195 #define BINOP(v, w, opname, ropname, thisfunc) \
196 if (PyInstance_Check(v) || PyInstance_Check(w)) \
197 return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
199 PyObject *
200 PyNumber_Or(v, w)
201 PyObject *v, *w;
203 extern int PyNumber_Coerce();
205 BINOP(v, w, "__or__", "__ror__", PyNumber_Or);
206 if (v->ob_type->tp_as_number != NULL) {
207 PyObject *x = NULL;
208 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
209 if (PyNumber_Coerce(&v, &w) != 0)
210 return NULL;
211 if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
212 x = (*f)(v, w);
213 Py_DECREF(v);
214 Py_DECREF(w);
215 if (f != NULL)
216 return x;
218 return type_error("bad operand type(s) for |");
221 PyObject *
222 PyNumber_Xor(v, w)
223 PyObject *v, *w;
225 extern int PyNumber_Coerce();
227 BINOP(v, w, "__xor__", "__rxor__", PyNumber_Xor);
228 if (v->ob_type->tp_as_number != NULL) {
229 PyObject *x = NULL;
230 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
231 if (PyNumber_Coerce(&v, &w) != 0)
232 return NULL;
233 if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
234 x = (*f)(v, w);
235 Py_DECREF(v);
236 Py_DECREF(w);
237 if (f != NULL)
238 return x;
240 return type_error("bad operand type(s) for ^");
243 PyObject *
244 PyNumber_And(v, w)
245 PyObject *v, *w;
247 BINOP(v, w, "__and__", "__rand__", PyNumber_And);
248 if (v->ob_type->tp_as_number != NULL) {
249 PyObject *x = NULL;
250 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
251 if (PyNumber_Coerce(&v, &w) != 0)
252 return NULL;
253 if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
254 x = (*f)(v, w);
255 Py_DECREF(v);
256 Py_DECREF(w);
257 if (f != NULL)
258 return x;
260 return type_error("bad operand type(s) for &");
263 PyObject *
264 PyNumber_Lshift(v, w)
265 PyObject *v, *w;
267 BINOP(v, w, "__lshift__", "__rlshift__", PyNumber_Lshift);
268 if (v->ob_type->tp_as_number != NULL) {
269 PyObject *x = NULL;
270 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
271 if (PyNumber_Coerce(&v, &w) != 0)
272 return NULL;
273 if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
274 x = (*f)(v, w);
275 Py_DECREF(v);
276 Py_DECREF(w);
277 if (f != NULL)
278 return x;
280 return type_error("bad operand type(s) for <<");
283 PyObject *
284 PyNumber_Rshift(v, w)
285 PyObject *v, *w;
287 BINOP(v, w, "__rshift__", "__rrshift__", PyNumber_Rshift);
288 if (v->ob_type->tp_as_number != NULL) {
289 PyObject *x = NULL;
290 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
291 if (PyNumber_Coerce(&v, &w) != 0)
292 return NULL;
293 if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
294 x = (*f)(v, w);
295 Py_DECREF(v);
296 Py_DECREF(w);
297 if (f != NULL)
298 return x;
300 return type_error("bad operand type(s) for >>");
303 PyObject *
304 PyNumber_Add(v, w)
305 PyObject *v, *w;
307 PySequenceMethods *m;
309 BINOP(v, w, "__add__", "__radd__", PyNumber_Add);
310 m = v->ob_type->tp_as_sequence;
311 if (m && m->sq_concat)
312 return (*m->sq_concat)(v, w);
313 else if (v->ob_type->tp_as_number != NULL) {
314 PyObject *x = NULL;
315 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
316 if (PyNumber_Coerce(&v, &w) != 0)
317 return NULL;
318 if ((f = v->ob_type->tp_as_number->nb_add) != NULL)
319 x = (*f)(v, w);
320 Py_DECREF(v);
321 Py_DECREF(w);
322 if (f != NULL)
323 return x;
325 return type_error("bad operand type(s) for +");
328 PyObject *
329 PyNumber_Subtract(v, w)
330 PyObject *v, *w;
332 BINOP(v, w, "__sub__", "__rsub__", PyNumber_Subtract);
333 if (v->ob_type->tp_as_number != NULL) {
334 PyObject *x = NULL;
335 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
336 if (PyNumber_Coerce(&v, &w) != 0)
337 return NULL;
338 if ((f = v->ob_type->tp_as_number->nb_subtract) != NULL)
339 x = (*f)(v, w);
340 Py_DECREF(v);
341 Py_DECREF(w);
342 if (f != NULL)
343 return x;
345 return type_error("bad operand type(s) for -");
348 PyObject *
349 PyNumber_Multiply(v, w)
350 PyObject *v, *w;
352 PyTypeObject *tp = v->ob_type;
353 PySequenceMethods *m;
355 BINOP(v, w, "__mul__", "__rmul__", PyNumber_Multiply);
356 if (tp->tp_as_number != NULL &&
357 w->ob_type->tp_as_sequence != NULL &&
358 !PyInstance_Check(v)) {
359 /* number*sequence -- swap v and w */
360 PyObject *tmp = v;
361 v = w;
362 w = tmp;
363 tp = v->ob_type;
365 if (tp->tp_as_number != NULL) {
366 PyObject *x = NULL;
367 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
368 if (PyInstance_Check(v)) {
369 /* Instances of user-defined classes get their
370 other argument uncoerced, so they may
371 implement sequence*number as well as
372 number*number. */
373 Py_INCREF(v);
374 Py_INCREF(w);
376 else if (PyNumber_Coerce(&v, &w) != 0)
377 return NULL;
378 if ((f = v->ob_type->tp_as_number->nb_multiply) != NULL)
379 x = (*f)(v, w);
380 Py_DECREF(v);
381 Py_DECREF(w);
382 if (f != NULL)
383 return x;
385 m = tp->tp_as_sequence;
386 if (m && m->sq_repeat) {
387 if (!PyInt_Check(w))
388 return type_error(
389 "can't multiply sequence with non-int");
390 return (*m->sq_repeat)(v, (int)PyInt_AsLong(w));
392 return type_error("bad operand type(s) for *");
395 PyObject *
396 PyNumber_Divide(v, w)
397 PyObject *v, *w;
399 BINOP(v, w, "__div__", "__rdiv__", PyNumber_Divide);
400 if (v->ob_type->tp_as_number != NULL) {
401 PyObject *x = NULL;
402 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
403 if (PyNumber_Coerce(&v, &w) != 0)
404 return NULL;
405 if ((f = v->ob_type->tp_as_number->nb_divide) != NULL)
406 x = (*f)(v, w);
407 Py_DECREF(v);
408 Py_DECREF(w);
409 if (f != NULL)
410 return x;
412 return type_error("bad operand type(s) for /");
415 PyObject *
416 PyNumber_Remainder(v, w)
417 PyObject *v, *w;
419 if (PyString_Check(v))
420 return PyString_Format(v, w);
421 BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder);
422 if (v->ob_type->tp_as_number != NULL) {
423 PyObject *x = NULL;
424 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
425 if (PyNumber_Coerce(&v, &w) != 0)
426 return NULL;
427 if ((f = v->ob_type->tp_as_number->nb_remainder) != NULL)
428 x = (*f)(v, w);
429 Py_DECREF(v);
430 Py_DECREF(w);
431 if (f != NULL)
432 return x;
434 return type_error("bad operand type(s) for %");
437 PyObject *
438 PyNumber_Divmod(v, w)
439 PyObject *v, *w;
441 BINOP(v, w, "__divmod__", "__rdivmod__", PyNumber_Divmod);
442 if (v->ob_type->tp_as_number != NULL) {
443 PyObject *x = NULL;
444 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
445 if (PyNumber_Coerce(&v, &w) != 0)
446 return NULL;
447 if ((f = v->ob_type->tp_as_number->nb_divmod) != NULL)
448 x = (*f)(v, w);
449 Py_DECREF(v);
450 Py_DECREF(w);
451 if (f != NULL)
452 return x;
454 return type_error("bad operand type(s) for divmod()");
457 /* Power (binary or ternary) */
459 static PyObject *
460 do_pow(v, w)
461 PyObject *v, *w;
463 PyObject *res;
464 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *));
465 BINOP(v, w, "__pow__", "__rpow__", do_pow);
466 if (v->ob_type->tp_as_number == NULL ||
467 w->ob_type->tp_as_number == NULL) {
468 PyErr_SetString(PyExc_TypeError,
469 "pow(x, y) requires numeric arguments");
470 return NULL;
472 if (PyNumber_Coerce(&v, &w) != 0)
473 return NULL;
474 if ((f = v->ob_type->tp_as_number->nb_power) != NULL)
475 res = (*f)(v, w, Py_None);
476 else
477 res = type_error("pow(x, y) not defined for these operands");
478 Py_DECREF(v);
479 Py_DECREF(w);
480 return res;
483 PyObject *
484 PyNumber_Power(v, w, z)
485 PyObject *v, *w, *z;
487 PyObject *res;
488 PyObject *v1, *z1, *w2, *z2;
489 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *));
491 if (z == Py_None)
492 return do_pow(v, w);
493 /* XXX The ternary version doesn't do class instance coercions */
494 if (PyInstance_Check(v))
495 return v->ob_type->tp_as_number->nb_power(v, w, z);
496 if (v->ob_type->tp_as_number == NULL ||
497 z->ob_type->tp_as_number == NULL ||
498 w->ob_type->tp_as_number == NULL) {
499 return type_error("pow(x, y, z) requires numeric arguments");
501 if (PyNumber_Coerce(&v, &w) != 0)
502 return NULL;
503 res = NULL;
504 v1 = v;
505 z1 = z;
506 if (PyNumber_Coerce(&v1, &z1) != 0)
507 goto error2;
508 w2 = w;
509 z2 = z1;
510 if (PyNumber_Coerce(&w2, &z2) != 0)
511 goto error1;
512 if ((f = v1->ob_type->tp_as_number->nb_power) != NULL)
513 res = (*f)(v1, w2, z2);
514 else
515 res = type_error(
516 "pow(x, y, z) not defined for these operands");
517 Py_DECREF(w2);
518 Py_DECREF(z2);
519 error1:
520 Py_DECREF(v1);
521 Py_DECREF(z1);
522 error2:
523 Py_DECREF(v);
524 Py_DECREF(w);
525 return res;
528 /* Unary operators and functions */
530 PyObject *
531 PyNumber_Negative(o)
532 PyObject *o;
534 PyNumberMethods *m;
536 if (o == NULL)
537 return null_error();
538 m = o->ob_type->tp_as_number;
539 if (m && m->nb_negative)
540 return (*m->nb_negative)(o);
542 return type_error("bad operand type for unary -");
545 PyObject *
546 PyNumber_Positive(o)
547 PyObject *o;
549 PyNumberMethods *m;
551 if (o == NULL)
552 return null_error();
553 m = o->ob_type->tp_as_number;
554 if (m && m->nb_positive)
555 return (*m->nb_positive)(o);
557 return type_error("bad operand type for unary +");
560 PyObject *
561 PyNumber_Invert(o)
562 PyObject *o;
564 PyNumberMethods *m;
566 if (o == NULL)
567 return null_error();
568 m = o->ob_type->tp_as_number;
569 if (m && m->nb_invert)
570 return (*m->nb_invert)(o);
572 return type_error("bad operand type for unary ~");
575 PyObject *
576 PyNumber_Absolute(o)
577 PyObject *o;
579 PyNumberMethods *m;
581 if (o == NULL)
582 return null_error();
583 m = o->ob_type->tp_as_number;
584 if (m && m->nb_absolute)
585 return m->nb_absolute(o);
587 return type_error("bad operand type for abs()");
590 PyObject *
591 PyNumber_Int(o)
592 PyObject *o;
594 PyNumberMethods *m;
596 if (o == NULL)
597 return null_error();
598 if (PyString_Check(o))
599 return PyInt_FromString(PyString_AS_STRING(o), NULL, 10);
600 m = o->ob_type->tp_as_number;
601 if (m && m->nb_int)
602 return m->nb_int(o);
604 return type_error("object can't be converted to int");
607 /* There are two C API functions for converting a string to a long,
608 * PyNumber_Long() and PyLong_FromString(). Both are used in builtin_long,
609 * reachable from Python with the built-in function long().
611 * The difference is this: PyNumber_Long will raise an exception when the
612 * string cannot be converted to a long. The most common situation is
613 * where a float string is passed in; this raises a ValueError.
614 * PyLong_FromString does not raise an exception; it silently truncates the
615 * float to an integer.
617 * You can see the different behavior from Python with the following:
619 * long('9.5')
620 * => ValueError: invalid literal for long(): 9.5
622 * long('9.5', 10)
623 * => 9L
625 * The first example ends up calling PyNumber_Long(), while the second one
626 * calls PyLong_FromString().
628 static PyObject *
629 long_from_string(v)
630 PyObject *v;
632 char *s, *end;
633 PyObject *x;
634 char buffer[256]; /* For errors */
636 s = PyString_AS_STRING(v);
637 while (*s && isspace(Py_CHARMASK(*s)))
638 s++;
639 x = PyLong_FromString(s, &end, 10);
640 if (x == NULL) {
641 if (PyErr_ExceptionMatches(PyExc_ValueError))
642 goto bad;
643 return NULL;
645 while (*end && isspace(Py_CHARMASK(*end)))
646 end++;
647 if (*end != '\0') {
648 bad:
649 sprintf(buffer, "invalid literal for long(): %.200s", s);
650 PyErr_SetString(PyExc_ValueError, buffer);
651 Py_XDECREF(x);
652 return NULL;
654 else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
655 PyErr_SetString(PyExc_ValueError,
656 "null byte in argument for long()");
657 return NULL;
659 return x;
662 PyObject *
663 PyNumber_Long(o)
664 PyObject *o;
666 PyNumberMethods *m;
668 if (o == NULL)
669 return null_error();
670 if (PyString_Check(o))
671 /* need to do extra error checking that PyLong_FromString()
672 * doesn't do. In particular long('9.5') must raise an
673 * exception, not truncate the float.
675 return long_from_string(o);
676 m = o->ob_type->tp_as_number;
677 if (m && m->nb_long)
678 return m->nb_long(o);
680 return type_error("object can't be converted to long");
683 PyObject *
684 PyNumber_Float(o)
685 PyObject *o;
687 PyNumberMethods *m;
689 if (o == NULL)
690 return null_error();
691 if (PyString_Check(o))
692 return PyFloat_FromString(o, NULL);
693 m = o->ob_type->tp_as_number;
694 if (m && m->nb_float)
695 return m->nb_float(o);
697 return type_error("object can't be converted to float");
700 /* Operations on sequences */
703 PySequence_Check(s)
704 PyObject *s;
706 return s != NULL && s->ob_type->tp_as_sequence;
710 PySequence_Length(s)
711 PyObject *s;
713 PySequenceMethods *m;
715 if (s == NULL) {
716 null_error();
717 return -1;
720 m = s->ob_type->tp_as_sequence;
721 if (m && m->sq_length)
722 return m->sq_length(s);
724 type_error("len() of unsized object");
725 return -1;
728 PyObject *
729 PySequence_Concat(s, o)
730 PyObject *s;
731 PyObject *o;
733 PySequenceMethods *m;
735 if (s == NULL || o == NULL)
736 return null_error();
738 m = s->ob_type->tp_as_sequence;
739 if (m && m->sq_concat)
740 return m->sq_concat(s, o);
742 return type_error("object can't be concatenated");
745 PyObject *
746 PySequence_Repeat(o, count)
747 PyObject *o;
748 int count;
750 PySequenceMethods *m;
752 if (o == NULL)
753 return null_error();
755 m = o->ob_type->tp_as_sequence;
756 if (m && m->sq_repeat)
757 return m->sq_repeat(o, count);
759 return type_error("object can't be repeated");
762 PyObject *
763 PySequence_GetItem(s, i)
764 PyObject *s;
765 int i;
767 PySequenceMethods *m;
769 if (s == NULL)
770 return null_error();
772 m = s->ob_type->tp_as_sequence;
773 if (m && m->sq_item) {
774 if (i < 0) {
775 if (m->sq_length) {
776 int l = (*m->sq_length)(s);
777 if (l < 0)
778 return NULL;
779 i += l;
782 return m->sq_item(s, i);
785 return type_error("unindexable object");
788 PyObject *
789 PySequence_GetSlice(s, i1, i2)
790 PyObject *s;
791 int i1;
792 int i2;
794 PySequenceMethods *m;
796 if (!s) return null_error();
798 m = s->ob_type->tp_as_sequence;
799 if (m && m->sq_slice) {
800 if (i1 < 0 || i2 < 0) {
801 if (m->sq_length) {
802 int l = (*m->sq_length)(s);
803 if (l < 0)
804 return NULL;
805 if (i1 < 0)
806 i1 += l;
807 if (i2 < 0)
808 i2 += l;
811 return m->sq_slice(s, i1, i2);
814 return type_error("unsliceable object");
818 PySequence_SetItem(s, i, o)
819 PyObject *s;
820 int i;
821 PyObject *o;
823 PySequenceMethods *m;
825 if (s == NULL) {
826 null_error();
827 return -1;
830 m = s->ob_type->tp_as_sequence;
831 if (m && m->sq_ass_item) {
832 if (i < 0) {
833 if (m->sq_length) {
834 int l = (*m->sq_length)(s);
835 if (l < 0)
836 return -1;
837 i += l;
840 return m->sq_ass_item(s, i, o);
843 type_error("object doesn't support item assignment");
844 return -1;
848 PySequence_DelItem(s, i)
849 PyObject *s;
850 int i;
852 PySequenceMethods *m;
854 if (s == NULL) {
855 null_error();
856 return -1;
859 m = s->ob_type->tp_as_sequence;
860 if (m && m->sq_ass_item) {
861 if (i < 0) {
862 if (m->sq_length) {
863 int l = (*m->sq_length)(s);
864 if (l < 0)
865 return -1;
866 i += l;
869 return m->sq_ass_item(s, i, (PyObject *)NULL);
872 type_error("object doesn't support item deletion");
873 return -1;
877 PySequence_SetSlice(s, i1, i2, o)
878 PyObject *s;
879 int i1;
880 int i2;
881 PyObject *o;
883 PySequenceMethods *m;
885 if (s == NULL) {
886 null_error();
887 return -1;
890 m = s->ob_type->tp_as_sequence;
891 if (m && m->sq_ass_slice) {
892 if (i1 < 0 || i2 < 0) {
893 if (m->sq_length) {
894 int l = (*m->sq_length)(s);
895 if (l < 0)
896 return -1;
897 if (i1 < 0)
898 i1 += l;
899 if (i2 < 0)
900 i2 += l;
903 return m->sq_ass_slice(s, i1, i2, o);
905 type_error("object doesn't support slice assignment");
906 return -1;
910 PySequence_DelSlice(s, i1, i2)
911 PyObject *s;
912 int i1;
913 int i2;
915 PySequenceMethods *m;
917 if (s == NULL) {
918 null_error();
919 return -1;
922 m = s->ob_type->tp_as_sequence;
923 if (m && m->sq_ass_slice) {
924 if (i1 < 0 || i2 < 0) {
925 if (m->sq_length) {
926 int l = (*m->sq_length)(s);
927 if (l < 0)
928 return -1;
929 if (i1 < 0)
930 i1 += l;
931 if (i2 < 0)
932 i2 += l;
935 return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
937 type_error("object doesn't support slice deletion");
938 return -1;
941 PyObject *
942 PySequence_Tuple(v)
943 PyObject *v;
945 PySequenceMethods *m;
947 if (v == NULL)
948 return null_error();
950 if (PyTuple_Check(v)) {
951 Py_INCREF(v);
952 return v;
955 if (PyList_Check(v))
956 return PyList_AsTuple(v);
958 /* There used to be code for strings here, but tuplifying strings is
959 not a common activity, so I nuked it. Down with code bloat! */
961 /* Generic sequence object */
962 m = v->ob_type->tp_as_sequence;
963 if (m && m->sq_item) {
964 int i;
965 PyObject *t;
966 int n = PySequence_Length(v);
967 if (n < 0)
968 return NULL;
969 t = PyTuple_New(n);
970 if (t == NULL)
971 return NULL;
972 for (i = 0; ; i++) {
973 PyObject *item = (*m->sq_item)(v, i);
974 if (item == NULL) {
975 if (PyErr_ExceptionMatches(PyExc_IndexError))
976 PyErr_Clear();
977 else {
978 Py_DECREF(t);
979 t = NULL;
981 break;
983 if (i >= n) {
984 if (n < 500)
985 n += 10;
986 else
987 n += 100;
988 if (_PyTuple_Resize(&t, n, 0) != 0)
989 break;
991 PyTuple_SET_ITEM(t, i, item);
993 if (i < n && t != NULL)
994 _PyTuple_Resize(&t, i, 0);
995 return t;
998 /* None of the above */
999 return type_error("tuple() argument must be a sequence");
1002 PyObject *
1003 PySequence_List(v)
1004 PyObject *v;
1006 PySequenceMethods *m;
1008 if (v == NULL)
1009 return null_error();
1011 if (PyList_Check(v))
1012 return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
1014 m = v->ob_type->tp_as_sequence;
1015 if (m && m->sq_item) {
1016 int i;
1017 PyObject *l;
1018 int n = PySequence_Length(v);
1019 if (n < 0)
1020 return NULL;
1021 l = PyList_New(n);
1022 if (l == NULL)
1023 return NULL;
1024 for (i = 0; ; i++) {
1025 PyObject *item = (*m->sq_item)(v, i);
1026 if (item == NULL) {
1027 if (PyErr_ExceptionMatches(PyExc_IndexError))
1028 PyErr_Clear();
1029 else {
1030 Py_DECREF(l);
1031 l = NULL;
1033 break;
1035 if (i < n)
1036 PyList_SET_ITEM(l, i, item);
1037 else if (PyList_Append(l, item) < 0) {
1038 Py_DECREF(l);
1039 l = NULL;
1040 break;
1043 if (i < n && l != NULL) {
1044 if (PyList_SetSlice(l, i, n, (PyObject *)NULL) != 0) {
1045 Py_DECREF(l);
1046 l = NULL;
1049 return l;
1051 return type_error("list() argument must be a sequence");
1055 PySequence_Count(s, o)
1056 PyObject *s;
1057 PyObject *o;
1059 int l, i, n, cmp, err;
1060 PyObject *item;
1062 if (s == NULL || o == NULL) {
1063 null_error();
1064 return -1;
1067 l = PySequence_Length(s);
1068 if (l < 0)
1069 return -1;
1071 n = 0;
1072 for (i = 0; i < l; i++) {
1073 item = PySequence_GetItem(s, i);
1074 if (item == NULL)
1075 return -1;
1076 err = PyObject_Cmp(item, o, &cmp);
1077 Py_DECREF(item);
1078 if (err < 0)
1079 return err;
1080 if (cmp == 0)
1081 n++;
1083 return n;
1087 PySequence_Contains(w, v) /* v in w */
1088 PyObject *w;
1089 PyObject *v;
1091 int i, cmp;
1092 PyObject *x;
1093 PySequenceMethods *sq;
1095 /* Special case for char in string */
1096 if (PyString_Check(w)) {
1097 register char *s, *end;
1098 register char c;
1099 if (!PyString_Check(v) || PyString_Size(v) != 1) {
1100 PyErr_SetString(PyExc_TypeError,
1101 "string member test needs char left operand");
1102 return -1;
1104 c = PyString_AsString(v)[0];
1105 s = PyString_AsString(w);
1106 end = s + PyString_Size(w);
1107 while (s < end) {
1108 if (c == *s++)
1109 return 1;
1111 return 0;
1114 sq = w->ob_type->tp_as_sequence;
1115 if (sq == NULL || sq->sq_item == NULL) {
1116 PyErr_SetString(PyExc_TypeError,
1117 "'in' or 'not in' needs sequence right argument");
1118 return -1;
1121 for (i = 0; ; i++) {
1122 x = (*sq->sq_item)(w, i);
1123 if (x == NULL) {
1124 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
1125 PyErr_Clear();
1126 break;
1128 return -1;
1130 cmp = PyObject_Compare(v, x);
1131 Py_XDECREF(x);
1132 if (cmp == 0)
1133 return 1;
1134 if (PyErr_Occurred())
1135 return -1;
1138 return 0;
1141 /* Backwards compatibility */
1142 #undef PySequence_In
1144 PySequence_In(w, v)
1145 PyObject *w;
1146 PyObject *v;
1148 return PySequence_Contains(w, v);
1152 PySequence_Index(s, o)
1153 PyObject *s;
1154 PyObject *o;
1156 int l, i, cmp, err;
1157 PyObject *item;
1159 if (s == NULL || o == NULL) {
1160 null_error();
1161 return -1;
1164 l = PySequence_Length(s);
1165 if (l < 0)
1166 return -1;
1168 for (i = 0; i < l; i++) {
1169 item = PySequence_GetItem(s, i);
1170 if (item == NULL)
1171 return -1;
1172 err = PyObject_Cmp(item, o, &cmp);
1173 Py_DECREF(item);
1174 if (err < 0)
1175 return err;
1176 if (cmp == 0)
1177 return i;
1180 PyErr_SetString(PyExc_ValueError, "sequence.index(x): x not in list");
1181 return -1;
1184 /* Operations on mappings */
1187 PyMapping_Check(o)
1188 PyObject *o;
1190 return o && o->ob_type->tp_as_mapping;
1194 PyMapping_Length(o)
1195 PyObject *o;
1197 PyMappingMethods *m;
1199 if (o == NULL) {
1200 null_error();
1201 return -1;
1204 m = o->ob_type->tp_as_mapping;
1205 if (m && m->mp_length)
1206 return m->mp_length(o);
1208 type_error("len() of unsized object");
1209 return -1;
1212 PyObject *
1213 PyMapping_GetItemString(o, key)
1214 PyObject *o;
1215 char *key;
1217 PyObject *okey, *r;
1219 if (key == NULL)
1220 return null_error();
1222 okey = PyString_FromString(key);
1223 if (okey == NULL)
1224 return NULL;
1225 r = PyObject_GetItem(o, okey);
1226 Py_DECREF(okey);
1227 return r;
1231 PyMapping_SetItemString(o, key, value)
1232 PyObject *o;
1233 char *key;
1234 PyObject *value;
1236 PyObject *okey;
1237 int r;
1239 if (key == NULL) {
1240 null_error();
1241 return -1;
1244 okey = PyString_FromString(key);
1245 if (okey == NULL)
1246 return -1;
1247 r = PyObject_SetItem(o, okey, value);
1248 Py_DECREF(okey);
1249 return r;
1253 PyMapping_HasKeyString(o, key)
1254 PyObject *o;
1255 char *key;
1257 PyObject *v;
1259 v = PyMapping_GetItemString(o, key);
1260 if (v) {
1261 Py_DECREF(v);
1262 return 1;
1264 PyErr_Clear();
1265 return 0;
1269 PyMapping_HasKey(o, key)
1270 PyObject *o;
1271 PyObject *key;
1273 PyObject *v;
1275 v = PyObject_GetItem(o, key);
1276 if (v) {
1277 Py_DECREF(v);
1278 return 1;
1280 PyErr_Clear();
1281 return 0;
1284 /* Operations on callable objects */
1286 /* XXX PyCallable_Check() is in object.c */
1288 PyObject *
1289 PyObject_CallObject(o, a)
1290 PyObject *o, *a;
1292 PyObject *r;
1293 PyObject *args = a;
1295 if (args == NULL) {
1296 args = PyTuple_New(0);
1297 if (args == NULL)
1298 return NULL;
1301 r = PyEval_CallObject(o, args);
1303 if (args != a) {
1304 Py_DECREF(args);
1307 return r;
1310 PyObject *
1311 #ifdef HAVE_STDARG_PROTOTYPES
1312 /* VARARGS 2 */
1313 PyObject_CallFunction(PyObject *callable, char *format, ...)
1314 #else
1315 /* VARARGS */
1316 PyObject_CallFunction(va_alist) va_dcl
1317 #endif
1319 va_list va;
1320 PyObject *args, *retval;
1321 #ifdef HAVE_STDARG_PROTOTYPES
1322 va_start(va, format);
1323 #else
1324 PyObject *callable;
1325 char *format;
1326 va_start(va);
1327 callable = va_arg(va, PyObject *);
1328 format = va_arg(va, char *);
1329 #endif
1331 if (callable == NULL) {
1332 va_end(va);
1333 return null_error();
1336 if (format)
1337 args = Py_VaBuildValue(format, va);
1338 else
1339 args = PyTuple_New(0);
1341 va_end(va);
1343 if (args == NULL)
1344 return NULL;
1346 if (!PyTuple_Check(args)) {
1347 PyObject *a;
1349 a = PyTuple_New(1);
1350 if (a == NULL)
1351 return NULL;
1352 if (PyTuple_SetItem(a, 0, args) < 0)
1353 return NULL;
1354 args = a;
1356 retval = PyObject_CallObject(callable, args);
1358 Py_DECREF(args);
1360 return retval;
1363 PyObject *
1364 #ifdef HAVE_STDARG_PROTOTYPES
1365 /* VARARGS 2 */
1366 PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
1367 #else
1368 /* VARARGS */
1369 PyObject_CallMethod(va_alist) va_dcl
1370 #endif
1372 va_list va;
1373 PyObject *args, *func = 0, *retval;
1374 #ifdef HAVE_STDARG_PROTOTYPES
1375 va_start(va, format);
1376 #else
1377 PyObject *o;
1378 char *name;
1379 char *format;
1380 va_start(va);
1381 o = va_arg(va, PyObject *);
1382 name = va_arg(va, char *);
1383 format = va_arg(va, char *);
1384 #endif
1386 if (o == NULL || name == NULL) {
1387 va_end(va);
1388 return null_error();
1391 func = PyObject_GetAttrString(o, name);
1392 if (func == NULL) {
1393 va_end(va);
1394 PyErr_SetString(PyExc_AttributeError, name);
1395 return 0;
1398 if (!PyCallable_Check(func)) {
1399 va_end(va);
1400 return type_error("call of non-callable attribute");
1403 if (format && *format)
1404 args = Py_VaBuildValue(format, va);
1405 else
1406 args = PyTuple_New(0);
1408 va_end(va);
1410 if (!args)
1411 return NULL;
1413 if (!PyTuple_Check(args)) {
1414 PyObject *a;
1416 a = PyTuple_New(1);
1417 if (a == NULL)
1418 return NULL;
1419 if (PyTuple_SetItem(a, 0, args) < 0)
1420 return NULL;
1421 args = a;
1424 retval = PyObject_CallObject(func, args);
1426 Py_DECREF(args);
1427 Py_DECREF(func);
1429 return retval;