py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Objects / abstract.c
blob63fe7d51225177da639b0f20c3345e2308113f0c
1 /* Abstract Object Interface (many thanks to Jim Fulton) */
4 #include "Python.h"
5 #include <ctype.h>
6 #include "structmember.h" /* we need the offsetof() macro from there */
8 #define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \
9 Py_TPFLAGS_CHECKTYPES)
11 /* Shorthands to return certain errors */
13 static PyObject *
14 type_error(const char *msg)
16 PyErr_SetString(PyExc_TypeError, msg);
17 return NULL;
20 static PyObject *
21 null_error(void)
23 if (!PyErr_Occurred())
24 PyErr_SetString(PyExc_SystemError,
25 "null argument to internal routine");
26 return NULL;
29 /* Operations on any object */
31 int
32 PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
34 int r;
36 if (o1 == NULL || o2 == NULL) {
37 null_error();
38 return -1;
40 r = PyObject_Compare(o1, o2);
41 if (PyErr_Occurred())
42 return -1;
43 *result = r;
44 return 0;
47 PyObject *
48 PyObject_Type(PyObject *o)
50 PyObject *v;
52 if (o == NULL)
53 return null_error();
54 v = (PyObject *)o->ob_type;
55 Py_INCREF(v);
56 return v;
59 int
60 PyObject_Size(PyObject *o)
62 PySequenceMethods *m;
64 if (o == NULL) {
65 null_error();
66 return -1;
69 m = o->ob_type->tp_as_sequence;
70 if (m && m->sq_length)
71 return m->sq_length(o);
73 return PyMapping_Size(o);
76 #undef PyObject_Length
77 int
78 PyObject_Length(PyObject *o)
80 return PyObject_Size(o);
82 #define PyObject_Length PyObject_Size
84 PyObject *
85 PyObject_GetItem(PyObject *o, PyObject *key)
87 PyMappingMethods *m;
89 if (o == NULL || key == NULL)
90 return null_error();
92 m = o->ob_type->tp_as_mapping;
93 if (m && m->mp_subscript)
94 return m->mp_subscript(o, key);
96 if (o->ob_type->tp_as_sequence) {
97 if (PyInt_Check(key))
98 return PySequence_GetItem(o, PyInt_AsLong(key));
99 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);
105 return type_error("sequence index must be integer");
108 return type_error("unsubscriptable object");
112 PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
114 PyMappingMethods *m;
116 if (o == NULL || key == NULL || value == NULL) {
117 null_error();
118 return -1;
120 m = o->ob_type->tp_as_mapping;
121 if (m && m->mp_ass_subscript)
122 return m->mp_ass_subscript(o, key, value);
124 if (o->ob_type->tp_as_sequence) {
125 if (PyInt_Check(key))
126 return PySequence_SetItem(o, PyInt_AsLong(key), value);
127 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);
133 type_error("sequence index must be integer");
134 return -1;
137 type_error("object does not support item assignment");
138 return -1;
142 PyObject_DelItem(PyObject *o, PyObject *key)
144 PyMappingMethods *m;
146 if (o == NULL || key == NULL) {
147 null_error();
148 return -1;
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);
154 if (o->ob_type->tp_as_sequence) {
155 if (PyInt_Check(key))
156 return PySequence_DelItem(o, PyInt_AsLong(key));
157 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);
163 type_error("sequence index must be integer");
164 return -1;
167 type_error("object does not support item deletion");
168 return -1;
171 int PyObject_AsCharBuffer(PyObject *obj,
172 const char **buffer,
173 int *buffer_len)
175 PyBufferProcs *pb;
176 const char *pp;
177 int len;
179 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
180 null_error();
181 return -1;
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;
191 if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
192 PyErr_SetString(PyExc_TypeError,
193 "expected a single-segment buffer object");
194 goto onError;
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;
203 onError:
204 return -1;
207 int PyObject_AsReadBuffer(PyObject *obj,
208 const void **buffer,
209 int *buffer_len)
211 PyBufferProcs *pb;
212 void *pp;
213 int len;
215 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
216 null_error();
217 return -1;
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;
227 if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
228 PyErr_SetString(PyExc_TypeError,
229 "expected a single-segment buffer object");
230 goto onError;
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;
239 onError:
240 return -1;
243 int PyObject_AsWriteBuffer(PyObject *obj,
244 void **buffer,
245 int *buffer_len)
247 PyBufferProcs *pb;
248 void*pp;
249 int len;
251 if (obj == NULL || buffer == NULL || buffer_len == NULL) {
252 null_error();
253 return -1;
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;
263 if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
264 PyErr_SetString(PyExc_TypeError,
265 "expected a single-segment buffer object");
266 goto onError;
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;
275 onError:
276 return -1;
279 /* Operations on numbers */
282 PyNumber_Check(PyObject *o)
284 return o && o->ob_type->tp_as_number;
287 /* Binary operators */
289 /* New style number protocol support */
291 #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] ))
298 Calling scheme used for binary operations:
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)
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.
316 static PyObject *
317 binary_op1(PyObject *v, PyObject *w, const int op_slot)
319 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;
328 Py_DECREF(x); /* can't do it */
330 if (v->ob_type == w->ob_type) {
331 goto binop_error;
334 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;
341 Py_DECREF(x); /* can't do it */
344 if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w)) {
345 int err = PyNumber_CoerceEx(&v, &w);
346 if (err < 0) {
347 return NULL;
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;
360 /* CoerceEx incremented the reference counts */
361 Py_DECREF(v);
362 Py_DECREF(w);
365 binop_error:
366 Py_INCREF(Py_NotImplemented);
367 return Py_NotImplemented;
370 static PyObject *
371 binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
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;
380 return result;
385 Calling scheme used for ternary operations:
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)
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.
410 static PyObject *
411 ternary_op(PyObject *v,
412 PyObject *w,
413 PyObject *z,
414 const int op_slot,
415 const char *op_name)
417 PyNumberMethods *mv, *mw, *mz;
418 register PyObject *x = NULL;
419 register ternaryfunc *slot;
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);
432 if (v->ob_type == w->ob_type &&
433 (z == Py_None || z->ob_type == v->ob_type)) {
434 goto ternary_error;
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);
448 if (NEW_STYLE_NUMBER(v) &&
449 (z == Py_None || z->ob_type == v->ob_type)) {
450 goto ternary_error;
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);
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;
472 c = PyNumber_Coerce(&v, &w);
473 if (c != 0)
474 goto error3;
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;
487 else
488 c = -1;
489 goto error2;
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;
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;
510 else
511 c = -1;
513 Py_DECREF(w2);
514 Py_DECREF(z2);
515 error1:
516 Py_DECREF(v1);
517 Py_DECREF(z1);
518 error2:
519 Py_DECREF(v);
520 Py_DECREF(w);
521 error3:
522 if (c >= 0)
523 return x;
526 ternary_error:
527 PyErr_Format(PyExc_TypeError, "unsupported operand type(s) for %s",
528 op_name);
529 return NULL;
532 #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); \
538 BINARY_FUNC(PyNumber_Or, nb_or, "|")
539 BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
540 BINARY_FUNC(PyNumber_And, nb_and, "&")
541 BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
542 BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
543 BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
544 BINARY_FUNC(PyNumber_Multiply, nb_multiply, "*")
545 BINARY_FUNC(PyNumber_Divide, nb_divide, "/")
546 BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
548 PyObject *
549 PyNumber_Add(PyObject *v, PyObject *w)
551 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);
558 else {
559 PyErr_SetString(PyExc_TypeError,
560 "unsupported operand types for +");
561 result = NULL;
564 return result;
567 PyObject *
568 PyNumber_Remainder(PyObject *v, PyObject *w)
570 if (PyString_Check(v))
571 return PyString_Format(v, w);
572 else if (PyUnicode_Check(v))
573 return PyUnicode_Format(v, w);
574 return binary_op(v, w, NB_SLOT(nb_remainder), "%");
577 PyObject *
578 PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
580 return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
583 /* Binary in-place operators */
585 /* The in-place operators are defined to fall back to the 'normal',
586 non in-place operations, if the in-place methods are not in place.
588 - If the left hand object has the appropriate struct members, and
589 they are filled, call the appropriate function and return the
590 result. No coercion is done on the arguments; the left-hand object
591 is the one the operation is performed on, and it's up to the
592 function to deal with the right-hand object.
594 - Otherwise, in-place modification is not supported. Handle it exactly as
595 a non in-place operation of the same kind.
599 #define HASINPLACE(t) PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS)
601 static PyObject *
602 binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
603 const char *op_name)
605 PyNumberMethods *mv = v->ob_type->tp_as_number;
606 if (mv != NULL && HASINPLACE(v)) {
607 binaryfunc *slot = NB_BINOP(mv, iop_slot);
608 if (*slot) {
609 PyObject *x = (*slot)(v, w);
610 if (x != Py_NotImplemented) {
611 return x;
613 Py_DECREF(x);
616 return binary_op(v, w, op_slot, op_name);
619 #define INPLACE_BINOP(func, iop, op, op_name) \
620 PyObject * \
621 func(PyObject *v, PyObject *w) { \
622 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
625 INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
626 INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
627 INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
628 INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
629 INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
630 INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
631 INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=")
633 PyObject *
634 PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
636 binaryfunc f = NULL;
638 if (v->ob_type->tp_as_sequence != NULL) {
639 if (HASINPLACE(v))
640 f = v->ob_type->tp_as_sequence->sq_inplace_concat;
641 if (f == NULL)
642 f = v->ob_type->tp_as_sequence->sq_concat;
643 if (f != NULL)
644 return (*f)(v, w);
646 return binary_iop(v, w, NB_SLOT(nb_inplace_add), NB_SLOT(nb_add), "+=");
649 PyObject *
650 PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
652 PyObject * (*g)(PyObject *, int) = NULL;
653 if (HASINPLACE(v) && v->ob_type->tp_as_sequence &&
654 (g = v->ob_type->tp_as_sequence->sq_inplace_repeat)) {
655 long n;
656 if (PyInt_Check(w)) {
657 n = PyInt_AsLong(w);
659 else if (PyLong_Check(w)) {
660 n = PyLong_AsLong(w);
661 if (n == -1 && PyErr_Occurred())
662 return NULL;
664 else {
665 return type_error("can't multiply sequence to non-int");
667 return (*g)(v, (int)n);
669 return binary_iop(v, w, NB_SLOT(nb_inplace_multiply),
670 NB_SLOT(nb_multiply), "*=");
675 PyObject *
676 PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
678 if (PyString_Check(v))
679 return PyString_Format(v, w);
680 else if (PyUnicode_Check(v))
681 return PyUnicode_Format(v, w);
682 else
683 return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
684 NB_SLOT(nb_remainder), "%=");
688 PyObject *
689 PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
691 if (HASINPLACE(v) && v->ob_type->tp_as_number &&
692 v->ob_type->tp_as_number->nb_inplace_power != NULL) {
693 return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
695 else {
696 return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
701 /* Unary operators and functions */
703 PyObject *
704 PyNumber_Negative(PyObject *o)
706 PyNumberMethods *m;
708 if (o == NULL)
709 return null_error();
710 m = o->ob_type->tp_as_number;
711 if (m && m->nb_negative)
712 return (*m->nb_negative)(o);
714 return type_error("bad operand type for unary -");
717 PyObject *
718 PyNumber_Positive(PyObject *o)
720 PyNumberMethods *m;
722 if (o == NULL)
723 return null_error();
724 m = o->ob_type->tp_as_number;
725 if (m && m->nb_positive)
726 return (*m->nb_positive)(o);
728 return type_error("bad operand type for unary +");
731 PyObject *
732 PyNumber_Invert(PyObject *o)
734 PyNumberMethods *m;
736 if (o == NULL)
737 return null_error();
738 m = o->ob_type->tp_as_number;
739 if (m && m->nb_invert)
740 return (*m->nb_invert)(o);
742 return type_error("bad operand type for unary ~");
745 PyObject *
746 PyNumber_Absolute(PyObject *o)
748 PyNumberMethods *m;
750 if (o == NULL)
751 return null_error();
752 m = o->ob_type->tp_as_number;
753 if (m && m->nb_absolute)
754 return m->nb_absolute(o);
756 return type_error("bad operand type for abs()");
759 /* Add a check for embedded NULL-bytes in the argument. */
760 static PyObject *
761 int_from_string(const char *s, int len)
763 char *end;
764 PyObject *x;
766 x = PyInt_FromString((char*)s, &end, 10);
767 if (x == NULL)
768 return NULL;
769 if (end != s + len) {
770 PyErr_SetString(PyExc_ValueError,
771 "null byte in argument for int()");
772 Py_DECREF(x);
773 return NULL;
775 return x;
778 PyObject *
779 PyNumber_Int(PyObject *o)
781 PyNumberMethods *m;
782 const char *buffer;
783 int buffer_len;
785 if (o == NULL)
786 return null_error();
787 if (PyInt_Check(o)) {
788 Py_INCREF(o);
789 return o;
791 if (PyString_Check(o))
792 return int_from_string(PyString_AS_STRING(o),
793 PyString_GET_SIZE(o));
794 if (PyUnicode_Check(o))
795 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
796 PyUnicode_GET_SIZE(o),
797 10);
798 m = o->ob_type->tp_as_number;
799 if (m && m->nb_int)
800 return m->nb_int(o);
801 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
802 return int_from_string((char*)buffer, buffer_len);
804 return type_error("object can't be converted to int");
807 /* Add a check for embedded NULL-bytes in the argument. */
808 static PyObject *
809 long_from_string(const char *s, int len)
811 char *end;
812 PyObject *x;
814 x = PyLong_FromString((char*)s, &end, 10);
815 if (x == NULL)
816 return NULL;
817 if (end != s + len) {
818 PyErr_SetString(PyExc_ValueError,
819 "null byte in argument for long()");
820 Py_DECREF(x);
821 return NULL;
823 return x;
826 PyObject *
827 PyNumber_Long(PyObject *o)
829 PyNumberMethods *m;
830 const char *buffer;
831 int buffer_len;
833 if (o == NULL)
834 return null_error();
835 if (PyLong_Check(o)) {
836 Py_INCREF(o);
837 return o;
839 if (PyString_Check(o))
840 /* need to do extra error checking that PyLong_FromString()
841 * doesn't do. In particular long('9.5') must raise an
842 * exception, not truncate the float.
844 return long_from_string(PyString_AS_STRING(o),
845 PyString_GET_SIZE(o));
846 if (PyUnicode_Check(o))
847 /* The above check is done in PyLong_FromUnicode(). */
848 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
849 PyUnicode_GET_SIZE(o),
850 10);
851 m = o->ob_type->tp_as_number;
852 if (m && m->nb_long)
853 return m->nb_long(o);
854 if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
855 return long_from_string(buffer, buffer_len);
857 return type_error("object can't be converted to long");
860 PyObject *
861 PyNumber_Float(PyObject *o)
863 PyNumberMethods *m;
865 if (o == NULL)
866 return null_error();
867 if (PyFloat_Check(o)) {
868 Py_INCREF(o);
869 return o;
871 if (!PyString_Check(o)) {
872 m = o->ob_type->tp_as_number;
873 if (m && m->nb_float)
874 return m->nb_float(o);
876 return PyFloat_FromString(o, NULL);
879 /* Operations on sequences */
882 PySequence_Check(PyObject *s)
884 return s != NULL && s->ob_type->tp_as_sequence;
888 PySequence_Size(PyObject *s)
890 PySequenceMethods *m;
892 if (s == NULL) {
893 null_error();
894 return -1;
897 m = s->ob_type->tp_as_sequence;
898 if (m && m->sq_length)
899 return m->sq_length(s);
901 type_error("len() of unsized object");
902 return -1;
905 #undef PySequence_Length
907 PySequence_Length(PyObject *s)
909 return PySequence_Size(s);
911 #define PySequence_Length PySequence_Size
913 PyObject *
914 PySequence_Concat(PyObject *s, PyObject *o)
916 PySequenceMethods *m;
918 if (s == NULL || o == NULL)
919 return null_error();
921 m = s->ob_type->tp_as_sequence;
922 if (m && m->sq_concat)
923 return m->sq_concat(s, o);
925 return type_error("object can't be concatenated");
928 PyObject *
929 PySequence_Repeat(PyObject *o, int count)
931 PySequenceMethods *m;
933 if (o == NULL)
934 return null_error();
936 m = o->ob_type->tp_as_sequence;
937 if (m && m->sq_repeat)
938 return m->sq_repeat(o, count);
940 return type_error("object can't be repeated");
943 PyObject *
944 PySequence_InPlaceConcat(PyObject *s, PyObject *o)
946 PySequenceMethods *m;
948 if (s == NULL || o == NULL)
949 return null_error();
951 m = s->ob_type->tp_as_sequence;
952 if (m && HASINPLACE(s) && m->sq_inplace_concat)
953 return m->sq_inplace_concat(s, o);
954 if (m && m->sq_concat)
955 return m->sq_concat(s, o);
957 return type_error("object can't be concatenated");
960 PyObject *
961 PySequence_InPlaceRepeat(PyObject *o, int count)
963 PySequenceMethods *m;
965 if (o == NULL)
966 return null_error();
968 m = o->ob_type->tp_as_sequence;
969 if (m && HASINPLACE(o) && m->sq_inplace_repeat)
970 return m->sq_inplace_repeat(o, count);
971 if (m && m->sq_repeat)
972 return m->sq_repeat(o, count);
974 return type_error("object can't be repeated");
977 PyObject *
978 PySequence_GetItem(PyObject *s, int i)
980 PySequenceMethods *m;
982 if (s == NULL)
983 return null_error();
985 m = s->ob_type->tp_as_sequence;
986 if (m && m->sq_item) {
987 if (i < 0) {
988 if (m->sq_length) {
989 int l = (*m->sq_length)(s);
990 if (l < 0)
991 return NULL;
992 i += l;
995 return m->sq_item(s, i);
998 return type_error("unindexable object");
1001 static PyObject *
1002 sliceobj_from_intint(int i, int j)
1004 PyObject *start, *end, *slice;
1005 start = PyInt_FromLong((long)i);
1006 if (!start)
1007 return NULL;
1008 end = PyInt_FromLong((long)j);
1009 if (!end) {
1010 Py_DECREF(start);
1011 return NULL;
1013 slice = PySlice_New(start, end, NULL);
1014 Py_DECREF(start);
1015 Py_DECREF(end);
1016 return slice;
1019 PyObject *
1020 PySequence_GetSlice(PyObject *s, int i1, int i2)
1022 PySequenceMethods *m;
1023 PyMappingMethods *mp;
1025 if (!s) return null_error();
1027 m = s->ob_type->tp_as_sequence;
1028 if (m && m->sq_slice) {
1029 if (i1 < 0 || i2 < 0) {
1030 if (m->sq_length) {
1031 int l = (*m->sq_length)(s);
1032 if (l < 0)
1033 return NULL;
1034 if (i1 < 0)
1035 i1 += l;
1036 if (i2 < 0)
1037 i2 += l;
1040 return m->sq_slice(s, i1, i2);
1041 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) {
1042 PyObject *res;
1043 PyObject *slice = sliceobj_from_intint(i1, i2);
1044 if (!slice)
1045 return NULL;
1046 res = mp->mp_subscript(s, slice);
1047 Py_DECREF(slice);
1048 return res;
1051 return type_error("unsliceable object");
1055 PySequence_SetItem(PyObject *s, int i, PyObject *o)
1057 PySequenceMethods *m;
1059 if (s == NULL) {
1060 null_error();
1061 return -1;
1064 m = s->ob_type->tp_as_sequence;
1065 if (m && m->sq_ass_item) {
1066 if (i < 0) {
1067 if (m->sq_length) {
1068 int l = (*m->sq_length)(s);
1069 if (l < 0)
1070 return -1;
1071 i += l;
1074 return m->sq_ass_item(s, i, o);
1077 type_error("object doesn't support item assignment");
1078 return -1;
1082 PySequence_DelItem(PyObject *s, int i)
1084 PySequenceMethods *m;
1086 if (s == NULL) {
1087 null_error();
1088 return -1;
1091 m = s->ob_type->tp_as_sequence;
1092 if (m && m->sq_ass_item) {
1093 if (i < 0) {
1094 if (m->sq_length) {
1095 int l = (*m->sq_length)(s);
1096 if (l < 0)
1097 return -1;
1098 i += l;
1101 return m->sq_ass_item(s, i, (PyObject *)NULL);
1104 type_error("object doesn't support item deletion");
1105 return -1;
1109 PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o)
1111 PySequenceMethods *m;
1112 PyMappingMethods *mp;
1114 if (s == NULL) {
1115 null_error();
1116 return -1;
1119 m = s->ob_type->tp_as_sequence;
1120 if (m && m->sq_ass_slice) {
1121 if (i1 < 0 || i2 < 0) {
1122 if (m->sq_length) {
1123 int l = (*m->sq_length)(s);
1124 if (l < 0)
1125 return -1;
1126 if (i1 < 0)
1127 i1 += l;
1128 if (i2 < 0)
1129 i2 += l;
1132 return m->sq_ass_slice(s, i1, i2, o);
1133 } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
1134 int res;
1135 PyObject *slice = sliceobj_from_intint(i1, i2);
1136 if (!slice)
1137 return -1;
1138 res = mp->mp_ass_subscript(s, slice, o);
1139 Py_DECREF(slice);
1140 return res;
1143 type_error("object doesn't support slice assignment");
1144 return -1;
1148 PySequence_DelSlice(PyObject *s, int i1, int i2)
1150 PySequenceMethods *m;
1152 if (s == NULL) {
1153 null_error();
1154 return -1;
1157 m = s->ob_type->tp_as_sequence;
1158 if (m && m->sq_ass_slice) {
1159 if (i1 < 0 || i2 < 0) {
1160 if (m->sq_length) {
1161 int l = (*m->sq_length)(s);
1162 if (l < 0)
1163 return -1;
1164 if (i1 < 0)
1165 i1 += l;
1166 if (i2 < 0)
1167 i2 += l;
1170 return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
1172 type_error("object doesn't support slice deletion");
1173 return -1;
1176 PyObject *
1177 PySequence_Tuple(PyObject *v)
1179 PyObject *it; /* iter(v) */
1180 int n; /* guess for result tuple size */
1181 PyObject *result;
1182 int j;
1184 if (v == NULL)
1185 return null_error();
1187 /* Special-case the common tuple and list cases, for efficiency. */
1188 if (PyTuple_Check(v)) {
1189 Py_INCREF(v);
1190 return v;
1192 if (PyList_Check(v))
1193 return PyList_AsTuple(v);
1195 /* Get iterator. */
1196 it = PyObject_GetIter(v);
1197 if (it == NULL)
1198 return type_error("tuple() argument must support iteration");
1200 /* Guess result size and allocate space. */
1201 n = PySequence_Size(v);
1202 if (n < 0) {
1203 PyErr_Clear();
1204 n = 10; /* arbitrary */
1206 result = PyTuple_New(n);
1207 if (result == NULL)
1208 goto Fail;
1210 /* Fill the tuple. */
1211 for (j = 0; ; ++j) {
1212 PyObject *item = PyIter_Next(it);
1213 if (item == NULL) {
1214 if (PyErr_Occurred())
1215 goto Fail;
1216 break;
1218 if (j >= n) {
1219 if (n < 500)
1220 n += 10;
1221 else
1222 n += 100;
1223 if (_PyTuple_Resize(&result, n) != 0) {
1224 Py_DECREF(item);
1225 goto Fail;
1228 PyTuple_SET_ITEM(result, j, item);
1231 /* Cut tuple back if guess was too large. */
1232 if (j < n &&
1233 _PyTuple_Resize(&result, j) != 0)
1234 goto Fail;
1236 Py_DECREF(it);
1237 return result;
1239 Fail:
1240 Py_XDECREF(result);
1241 Py_DECREF(it);
1242 return NULL;
1245 PyObject *
1246 PySequence_List(PyObject *v)
1248 PyObject *it; /* iter(v) */
1249 PyObject *result; /* result list */
1250 int n; /* guess for result list size */
1251 int i;
1253 if (v == NULL)
1254 return null_error();
1256 /* Special-case list(a_list), for speed. */
1257 if (PyList_Check(v))
1258 return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
1260 /* Get iterator. There may be some low-level efficiency to be gained
1261 * by caching the tp_iternext slot instead of using PyIter_Next()
1262 * later, but premature optimization is the root etc.
1264 it = PyObject_GetIter(v);
1265 if (it == NULL)
1266 return NULL;
1268 /* Guess a result list size. */
1269 n = -1; /* unknown */
1270 if (PySequence_Check(v) &&
1271 v->ob_type->tp_as_sequence->sq_length) {
1272 n = PySequence_Size(v);
1273 if (n < 0)
1274 PyErr_Clear();
1276 if (n < 0)
1277 n = 8; /* arbitrary */
1278 result = PyList_New(n);
1279 if (result == NULL) {
1280 Py_DECREF(it);
1281 return NULL;
1284 /* Run iterator to exhaustion. */
1285 for (i = 0; ; i++) {
1286 PyObject *item = PyIter_Next(it);
1287 if (item == NULL) {
1288 if (PyErr_Occurred()) {
1289 Py_DECREF(result);
1290 result = NULL;
1292 break;
1294 if (i < n)
1295 PyList_SET_ITEM(result, i, item); /* steals ref */
1296 else {
1297 int status = PyList_Append(result, item);
1298 Py_DECREF(item); /* append creates a new ref */
1299 if (status < 0) {
1300 Py_DECREF(result);
1301 result = NULL;
1302 break;
1307 /* Cut back result list if initial guess was too large. */
1308 if (i < n && result != NULL) {
1309 if (PyList_SetSlice(result, i, n, (PyObject *)NULL) != 0) {
1310 Py_DECREF(result);
1311 result = NULL;
1314 Py_DECREF(it);
1315 return result;
1318 PyObject *
1319 PySequence_Fast(PyObject *v, const char *m)
1321 if (v == NULL)
1322 return null_error();
1324 if (PyList_Check(v) || PyTuple_Check(v)) {
1325 Py_INCREF(v);
1326 return v;
1329 v = PySequence_Tuple(v);
1330 if (v == NULL && PyErr_ExceptionMatches(PyExc_TypeError))
1331 return type_error(m);
1333 return v;
1336 /* Return # of times o appears in s. */
1338 PySequence_Count(PyObject *s, PyObject *o)
1340 int n; /* running count of o hits */
1341 PyObject *it; /* iter(s) */
1343 if (s == NULL || o == NULL) {
1344 null_error();
1345 return -1;
1348 it = PyObject_GetIter(s);
1349 if (it == NULL) {
1350 type_error(".count() requires iterable argument");
1351 return -1;
1354 n = 0;
1355 for (;;) {
1356 int cmp;
1357 PyObject *item = PyIter_Next(it);
1358 if (item == NULL) {
1359 if (PyErr_Occurred())
1360 goto Fail;
1361 break;
1363 cmp = PyObject_RichCompareBool(o, item, Py_EQ);
1364 Py_DECREF(item);
1365 if (cmp < 0)
1366 goto Fail;
1367 if (cmp > 0) {
1368 if (n == INT_MAX) {
1369 PyErr_SetString(PyExc_OverflowError,
1370 "count exceeds C int size");
1371 goto Fail;
1373 n++;
1376 Py_DECREF(it);
1377 return n;
1379 Fail:
1380 Py_DECREF(it);
1381 return -1;
1384 /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
1385 * Always uses the iteration protocol, and only Py_EQ comparison.
1388 _PySequence_IterContains(PyObject *seq, PyObject *ob)
1390 int result;
1391 PyObject *it = PyObject_GetIter(seq);
1392 if (it == NULL) {
1393 PyErr_SetString(PyExc_TypeError,
1394 "'in' or 'not in' needs iterable right argument");
1395 return -1;
1398 for (;;) {
1399 int cmp;
1400 PyObject *item = PyIter_Next(it);
1401 if (item == NULL) {
1402 result = PyErr_Occurred() ? -1 : 0;
1403 break;
1405 cmp = PyObject_RichCompareBool(ob, item, Py_EQ);
1406 Py_DECREF(item);
1407 if (cmp == 0)
1408 continue;
1409 result = cmp > 0 ? 1 : -1;
1410 break;
1412 Py_DECREF(it);
1413 return result;
1416 /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
1417 * Use sq_contains if possible, else defer to _PySequence_IterContains().
1420 PySequence_Contains(PyObject *seq, PyObject *ob)
1422 if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
1423 PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
1424 if (sqm != NULL && sqm->sq_contains != NULL)
1425 return (*sqm->sq_contains)(seq, ob);
1427 return _PySequence_IterContains(seq, ob);
1430 /* Backwards compatibility */
1431 #undef PySequence_In
1433 PySequence_In(PyObject *w, PyObject *v)
1435 return PySequence_Contains(w, v);
1439 PySequence_Index(PyObject *s, PyObject *o)
1441 int l, i, cmp, err;
1442 PyObject *item;
1444 if (s == NULL || o == NULL) {
1445 null_error();
1446 return -1;
1449 l = PySequence_Size(s);
1450 if (l < 0)
1451 return -1;
1453 for (i = 0; i < l; i++) {
1454 item = PySequence_GetItem(s, i);
1455 if (item == NULL)
1456 return -1;
1457 err = PyObject_Cmp(item, o, &cmp);
1458 Py_DECREF(item);
1459 if (err < 0)
1460 return err;
1461 if (cmp == 0)
1462 return i;
1465 PyErr_SetString(PyExc_ValueError, "sequence.index(x): x not in list");
1466 return -1;
1469 /* Operations on mappings */
1472 PyMapping_Check(PyObject *o)
1474 return o && o->ob_type->tp_as_mapping;
1478 PyMapping_Size(PyObject *o)
1480 PyMappingMethods *m;
1482 if (o == NULL) {
1483 null_error();
1484 return -1;
1487 m = o->ob_type->tp_as_mapping;
1488 if (m && m->mp_length)
1489 return m->mp_length(o);
1491 type_error("len() of unsized object");
1492 return -1;
1495 #undef PyMapping_Length
1497 PyMapping_Length(PyObject *o)
1499 return PyMapping_Size(o);
1501 #define PyMapping_Length PyMapping_Size
1503 PyObject *
1504 PyMapping_GetItemString(PyObject *o, char *key)
1506 PyObject *okey, *r;
1508 if (key == NULL)
1509 return null_error();
1511 okey = PyString_FromString(key);
1512 if (okey == NULL)
1513 return NULL;
1514 r = PyObject_GetItem(o, okey);
1515 Py_DECREF(okey);
1516 return r;
1520 PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
1522 PyObject *okey;
1523 int r;
1525 if (key == NULL) {
1526 null_error();
1527 return -1;
1530 okey = PyString_FromString(key);
1531 if (okey == NULL)
1532 return -1;
1533 r = PyObject_SetItem(o, okey, value);
1534 Py_DECREF(okey);
1535 return r;
1539 PyMapping_HasKeyString(PyObject *o, char *key)
1541 PyObject *v;
1543 v = PyMapping_GetItemString(o, key);
1544 if (v) {
1545 Py_DECREF(v);
1546 return 1;
1548 PyErr_Clear();
1549 return 0;
1553 PyMapping_HasKey(PyObject *o, PyObject *key)
1555 PyObject *v;
1557 v = PyObject_GetItem(o, key);
1558 if (v) {
1559 Py_DECREF(v);
1560 return 1;
1562 PyErr_Clear();
1563 return 0;
1566 /* Operations on callable objects */
1568 /* XXX PyCallable_Check() is in object.c */
1570 PyObject *
1571 PyObject_CallObject(PyObject *o, PyObject *a)
1573 PyObject *r;
1574 PyObject *args = a;
1576 if (args == NULL) {
1577 args = PyTuple_New(0);
1578 if (args == NULL)
1579 return NULL;
1582 r = PyEval_CallObject(o, args);
1584 if (args != a) {
1585 Py_DECREF(args);
1588 return r;
1591 PyObject *
1592 PyObject_CallFunction(PyObject *callable, char *format, ...)
1594 va_list va;
1595 PyObject *args, *retval;
1596 va_start(va, format);
1598 if (callable == NULL) {
1599 va_end(va);
1600 return null_error();
1603 if (format)
1604 args = Py_VaBuildValue(format, va);
1605 else
1606 args = PyTuple_New(0);
1608 va_end(va);
1610 if (args == NULL)
1611 return NULL;
1613 if (!PyTuple_Check(args)) {
1614 PyObject *a;
1616 a = PyTuple_New(1);
1617 if (a == NULL)
1618 return NULL;
1619 if (PyTuple_SetItem(a, 0, args) < 0)
1620 return NULL;
1621 args = a;
1623 retval = PyObject_CallObject(callable, args);
1625 Py_DECREF(args);
1627 return retval;
1630 PyObject *
1631 PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
1633 va_list va;
1634 PyObject *args, *func = 0, *retval;
1635 va_start(va, format);
1637 if (o == NULL || name == NULL) {
1638 va_end(va);
1639 return null_error();
1642 func = PyObject_GetAttrString(o, name);
1643 if (func == NULL) {
1644 va_end(va);
1645 PyErr_SetString(PyExc_AttributeError, name);
1646 return 0;
1649 if (!PyCallable_Check(func)) {
1650 va_end(va);
1651 return type_error("call of non-callable attribute");
1654 if (format && *format)
1655 args = Py_VaBuildValue(format, va);
1656 else
1657 args = PyTuple_New(0);
1659 va_end(va);
1661 if (!args)
1662 return NULL;
1664 if (!PyTuple_Check(args)) {
1665 PyObject *a;
1667 a = PyTuple_New(1);
1668 if (a == NULL)
1669 return NULL;
1670 if (PyTuple_SetItem(a, 0, args) < 0)
1671 return NULL;
1672 args = a;
1675 retval = PyObject_CallObject(func, args);
1677 Py_DECREF(args);
1678 Py_DECREF(func);
1680 return retval;
1684 /* isinstance(), issubclass() */
1686 static int
1687 abstract_issubclass(PyObject *derived, PyObject *cls, int first)
1689 static PyObject *__bases__ = NULL;
1690 PyObject *bases;
1691 int i, n;
1692 int r = 0;
1694 if (__bases__ == NULL) {
1695 __bases__ = PyString_FromString("__bases__");
1696 if (__bases__ == NULL)
1697 return -1;
1700 if (first) {
1701 bases = PyObject_GetAttr(cls, __bases__);
1702 if (bases == NULL || !PyTuple_Check(bases)) {
1703 Py_XDECREF(bases);
1704 PyErr_SetString(PyExc_TypeError,
1705 "issubclass() arg 2 must be a class");
1706 return -1;
1708 Py_DECREF(bases);
1711 if (derived == cls)
1712 return 1;
1714 bases = PyObject_GetAttr(derived, __bases__);
1715 if (bases == NULL || !PyTuple_Check(bases)) {
1716 Py_XDECREF(bases);
1717 PyErr_SetString(PyExc_TypeError,
1718 "issubclass() arg 1 must be a class");
1719 return -1;
1722 n = PyTuple_GET_SIZE(bases);
1723 for (i = 0; i < n; i++) {
1724 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls, 0);
1725 if (r != 0)
1726 break;
1729 Py_DECREF(bases);
1731 return r;
1735 PyObject_IsInstance(PyObject *inst, PyObject *cls)
1737 PyObject *icls;
1738 static PyObject *__class__ = NULL;
1739 int retval = 0;
1741 if (PyClass_Check(cls)) {
1742 if (PyInstance_Check(inst)) {
1743 PyObject *inclass =
1744 (PyObject*)((PyInstanceObject*)inst)->in_class;
1745 retval = PyClass_IsSubclass(inclass, cls);
1748 else if (PyType_Check(cls)) {
1749 retval = ((PyObject *)(inst->ob_type) == cls);
1751 else if (!PyInstance_Check(inst)) {
1752 if (__class__ == NULL) {
1753 __class__ = PyString_FromString("__class__");
1754 if (__class__ == NULL)
1755 return -1;
1757 icls = PyObject_GetAttr(inst, __class__);
1758 if (icls != NULL) {
1759 retval = abstract_issubclass(icls, cls, 1);
1760 Py_DECREF(icls);
1761 if (retval < 0 &&
1762 !PyErr_ExceptionMatches(PyExc_TypeError))
1763 return -1;
1765 else
1766 retval = -1;
1768 else
1769 retval = -1;
1771 if (retval < 0) {
1772 PyErr_SetString(PyExc_TypeError,
1773 "isinstance() arg 2 must be a class or type");
1775 return retval;
1779 PyObject_IsSubclass(PyObject *derived, PyObject *cls)
1781 int retval;
1783 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1784 retval = abstract_issubclass(derived, cls, 1);
1786 else {
1787 /* shortcut */
1788 if (!(retval = (derived == cls)))
1789 retval = PyClass_IsSubclass(derived, cls);
1792 return retval;
1795 PyObject *
1796 PyObject_GetIter(PyObject *o)
1798 PyTypeObject *t = o->ob_type;
1799 getiterfunc f = NULL;
1800 if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER))
1801 f = t->tp_iter;
1802 if (f == NULL) {
1803 if (PySequence_Check(o))
1804 return PySeqIter_New(o);
1805 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
1806 return NULL;
1808 else {
1809 PyObject *res = (*f)(o);
1810 if (res != NULL && !PyIter_Check(res)) {
1811 PyErr_Format(PyExc_TypeError,
1812 "iter() returned non-iterator "
1813 "of type '%.100s'",
1814 res->ob_type->tp_name);
1815 Py_DECREF(res);
1816 res = NULL;
1818 return res;
1822 /* Return next item.
1823 * If an error occurs, return NULL. PyErr_Occurred() will be true.
1824 * If the iteration terminates normally, return NULL and clear the
1825 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
1826 * will be false.
1827 * Else return the next object. PyErr_Occurred() will be false.
1829 PyObject *
1830 PyIter_Next(PyObject *iter)
1832 PyObject *result;
1833 if (!PyIter_Check(iter)) {
1834 PyErr_Format(PyExc_TypeError,
1835 "'%.100s' object is not an iterator",
1836 iter->ob_type->tp_name);
1837 return NULL;
1839 result = (*iter->ob_type->tp_iternext)(iter);
1840 if (result == NULL &&
1841 PyErr_Occurred() &&
1842 PyErr_ExceptionMatches(PyExc_StopIteration))
1843 PyErr_Clear();
1844 return result;