1 /* Abstract Object Interface (many thanks to Jim Fulton) */
5 #include "structmember.h" /* we need the offsetof() macro from there */
6 #include "longintrepr.h"
8 #define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \
11 /* Shorthands to return certain errors */
14 type_error(const char *msg
)
16 PyErr_SetString(PyExc_TypeError
, msg
);
23 if (!PyErr_Occurred())
24 PyErr_SetString(PyExc_SystemError
,
25 "null argument to internal routine");
29 /* Operations on any object */
32 PyObject_Cmp(PyObject
*o1
, PyObject
*o2
, int *result
)
36 if (o1
== NULL
|| o2
== NULL
) {
40 r
= PyObject_Compare(o1
, o2
);
48 PyObject_Type(PyObject
*o
)
54 v
= (PyObject
*)o
->ob_type
;
60 PyObject_Size(PyObject
*o
)
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
78 PyObject_Length(PyObject
*o
)
80 return PyObject_Size(o
);
82 #define PyObject_Length PyObject_Size
85 PyObject_GetItem(PyObject
*o
, PyObject
*key
)
89 if (o
== NULL
|| key
== NULL
)
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
) {
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())
103 return PySequence_GetItem(o
, key_value
);
105 else if (o
->ob_type
->tp_as_sequence
->sq_item
)
106 return type_error("sequence index must be integer");
109 return type_error("unsubscriptable object");
113 PyObject_SetItem(PyObject
*o
, PyObject
*key
, PyObject
*value
)
117 if (o
== NULL
|| key
== NULL
|| value
== NULL
) {
121 m
= o
->ob_type
->tp_as_mapping
;
122 if (m
&& m
->mp_ass_subscript
)
123 return m
->mp_ass_subscript(o
, key
, value
);
125 if (o
->ob_type
->tp_as_sequence
) {
126 if (PyInt_Check(key
))
127 return PySequence_SetItem(o
, PyInt_AsLong(key
), value
);
128 else if (PyLong_Check(key
)) {
129 long key_value
= PyLong_AsLong(key
);
130 if (key_value
== -1 && PyErr_Occurred())
132 return PySequence_SetItem(o
, key_value
, value
);
134 else if (o
->ob_type
->tp_as_sequence
->sq_ass_item
) {
135 type_error("sequence index must be integer");
140 type_error("object does not support item assignment");
145 PyObject_DelItem(PyObject
*o
, PyObject
*key
)
149 if (o
== NULL
|| key
== NULL
) {
153 m
= o
->ob_type
->tp_as_mapping
;
154 if (m
&& m
->mp_ass_subscript
)
155 return m
->mp_ass_subscript(o
, key
, (PyObject
*)NULL
);
157 if (o
->ob_type
->tp_as_sequence
) {
158 if (PyInt_Check(key
))
159 return PySequence_DelItem(o
, PyInt_AsLong(key
));
160 else if (PyLong_Check(key
)) {
161 long key_value
= PyLong_AsLong(key
);
162 if (key_value
== -1 && PyErr_Occurred())
164 return PySequence_DelItem(o
, key_value
);
166 else if (o
->ob_type
->tp_as_sequence
->sq_ass_item
) {
167 type_error("sequence index must be integer");
172 type_error("object does not support item deletion");
177 PyObject_DelItemString(PyObject
*o
, char *key
)
182 if (o
== NULL
|| key
== NULL
) {
186 okey
= PyString_FromString(key
);
189 ret
= PyObject_DelItem(o
, okey
);
194 int PyObject_AsCharBuffer(PyObject
*obj
,
202 if (obj
== NULL
|| buffer
== NULL
|| buffer_len
== NULL
) {
206 pb
= obj
->ob_type
->tp_as_buffer
;
208 pb
->bf_getcharbuffer
== NULL
||
209 pb
->bf_getsegcount
== NULL
) {
210 PyErr_SetString(PyExc_TypeError
,
211 "expected a character buffer object");
214 if ((*pb
->bf_getsegcount
)(obj
,NULL
) != 1) {
215 PyErr_SetString(PyExc_TypeError
,
216 "expected a single-segment buffer object");
219 len
= (*pb
->bf_getcharbuffer
)(obj
, 0, &pp
);
228 PyObject_CheckReadBuffer(PyObject
*obj
)
230 PyBufferProcs
*pb
= obj
->ob_type
->tp_as_buffer
;
233 pb
->bf_getreadbuffer
== NULL
||
234 pb
->bf_getsegcount
== NULL
||
235 (*pb
->bf_getsegcount
)(obj
, NULL
) != 1)
240 int PyObject_AsReadBuffer(PyObject
*obj
,
248 if (obj
== NULL
|| buffer
== NULL
|| buffer_len
== NULL
) {
252 pb
= obj
->ob_type
->tp_as_buffer
;
254 pb
->bf_getreadbuffer
== NULL
||
255 pb
->bf_getsegcount
== NULL
) {
256 PyErr_SetString(PyExc_TypeError
,
257 "expected a readable buffer object");
260 if ((*pb
->bf_getsegcount
)(obj
, NULL
) != 1) {
261 PyErr_SetString(PyExc_TypeError
,
262 "expected a single-segment buffer object");
265 len
= (*pb
->bf_getreadbuffer
)(obj
, 0, &pp
);
273 int PyObject_AsWriteBuffer(PyObject
*obj
,
281 if (obj
== NULL
|| buffer
== NULL
|| buffer_len
== NULL
) {
285 pb
= obj
->ob_type
->tp_as_buffer
;
287 pb
->bf_getwritebuffer
== NULL
||
288 pb
->bf_getsegcount
== NULL
) {
289 PyErr_SetString(PyExc_TypeError
,
290 "expected a writeable buffer object");
293 if ((*pb
->bf_getsegcount
)(obj
, NULL
) != 1) {
294 PyErr_SetString(PyExc_TypeError
,
295 "expected a single-segment buffer object");
298 len
= (*pb
->bf_getwritebuffer
)(obj
,0,&pp
);
306 /* Operations on numbers */
309 PyNumber_Check(PyObject
*o
)
311 return o
&& o
->ob_type
->tp_as_number
&&
312 (o
->ob_type
->tp_as_number
->nb_int
||
313 o
->ob_type
->tp_as_number
->nb_float
);
316 /* Binary operators */
318 /* New style number protocol support */
320 #define NB_SLOT(x) offsetof(PyNumberMethods, x)
321 #define NB_BINOP(nb_methods, slot) \
322 (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
323 #define NB_TERNOP(nb_methods, slot) \
324 (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
327 Calling scheme used for binary operations:
330 -------------------------------------------------------------------
331 new new w.op(v,w)[*], v.op(v,w), w.op(v,w)
332 new old v.op(v,w), coerce(v,w), v.op(v,w)
333 old new w.op(v,w), coerce(v,w), v.op(v,w)
334 old old coerce(v,w), v.op(v,w)
336 [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
341 * new == new style number
342 * old == old style number
343 * Action indicates the order in which operations are tried until either
344 a valid result is produced or an error occurs.
349 binary_op1(PyObject
*v
, PyObject
*w
, const int op_slot
)
352 binaryfunc slotv
= NULL
;
353 binaryfunc slotw
= NULL
;
355 if (v
->ob_type
->tp_as_number
!= NULL
&& NEW_STYLE_NUMBER(v
))
356 slotv
= NB_BINOP(v
->ob_type
->tp_as_number
, op_slot
);
357 if (w
->ob_type
!= v
->ob_type
&&
358 w
->ob_type
->tp_as_number
!= NULL
&& NEW_STYLE_NUMBER(w
)) {
359 slotw
= NB_BINOP(w
->ob_type
->tp_as_number
, op_slot
);
364 if (slotw
&& PyType_IsSubtype(w
->ob_type
, v
->ob_type
)) {
366 if (x
!= Py_NotImplemented
)
368 Py_DECREF(x
); /* can't do it */
372 if (x
!= Py_NotImplemented
)
374 Py_DECREF(x
); /* can't do it */
378 if (x
!= Py_NotImplemented
)
380 Py_DECREF(x
); /* can't do it */
382 if (!NEW_STYLE_NUMBER(v
) || !NEW_STYLE_NUMBER(w
)) {
383 int err
= PyNumber_CoerceEx(&v
, &w
);
388 PyNumberMethods
*mv
= v
->ob_type
->tp_as_number
;
391 slot
= NB_BINOP(mv
, op_slot
);
393 PyObject
*x
= slot(v
, w
);
399 /* CoerceEx incremented the reference counts */
404 Py_INCREF(Py_NotImplemented
);
405 return Py_NotImplemented
;
409 binop_type_error(PyObject
*v
, PyObject
*w
, const char *op_name
)
411 PyErr_Format(PyExc_TypeError
,
412 "unsupported operand type(s) for %s: '%s' and '%s'",
415 w
->ob_type
->tp_name
);
420 binary_op(PyObject
*v
, PyObject
*w
, const int op_slot
, const char *op_name
)
422 PyObject
*result
= binary_op1(v
, w
, op_slot
);
423 if (result
== Py_NotImplemented
) {
425 return binop_type_error(v
, w
, op_name
);
432 Calling scheme used for ternary operations:
434 *** In some cases, w.op is called before v.op; see binary_op1. ***
437 -------------------------------------------------------------------
438 new new new v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
439 new old new v.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
440 old new new w.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
441 old old new z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
442 new new old v.op(v,w,z), w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
443 new old old v.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
444 old new old w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
445 old old old coerce(v,w,z), v.op(v,w,z)
449 * new == new style number
450 * old == old style number
451 * Action indicates the order in which operations are tried until either
452 a valid result is produced or an error occurs.
453 * coerce(v,w,z) actually does: coerce(v,w), coerce(v,z), coerce(w,z) and
454 only if z != Py_None; if z == Py_None, then it is treated as absent
455 variable and only coerce(v,w) is tried.
460 ternary_op(PyObject
*v
,
466 PyNumberMethods
*mv
, *mw
, *mz
;
468 ternaryfunc slotv
= NULL
;
469 ternaryfunc slotw
= NULL
;
470 ternaryfunc slotz
= NULL
;
472 mv
= v
->ob_type
->tp_as_number
;
473 mw
= w
->ob_type
->tp_as_number
;
474 if (mv
!= NULL
&& NEW_STYLE_NUMBER(v
))
475 slotv
= NB_TERNOP(mv
, op_slot
);
476 if (w
->ob_type
!= v
->ob_type
&&
477 mw
!= NULL
&& NEW_STYLE_NUMBER(w
)) {
478 slotw
= NB_TERNOP(mw
, op_slot
);
483 if (slotw
&& PyType_IsSubtype(w
->ob_type
, v
->ob_type
)) {
485 if (x
!= Py_NotImplemented
)
487 Py_DECREF(x
); /* can't do it */
491 if (x
!= Py_NotImplemented
)
493 Py_DECREF(x
); /* can't do it */
497 if (x
!= Py_NotImplemented
)
499 Py_DECREF(x
); /* can't do it */
501 mz
= z
->ob_type
->tp_as_number
;
502 if (mz
!= NULL
&& NEW_STYLE_NUMBER(z
)) {
503 slotz
= NB_TERNOP(mz
, op_slot
);
504 if (slotz
== slotv
|| slotz
== slotw
)
508 if (x
!= Py_NotImplemented
)
510 Py_DECREF(x
); /* can't do it */
514 if (!NEW_STYLE_NUMBER(v
) || !NEW_STYLE_NUMBER(w
) ||
515 (z
!= Py_None
&& !NEW_STYLE_NUMBER(z
))) {
516 /* we have an old style operand, coerce */
517 PyObject
*v1
, *z1
, *w2
, *z2
;
520 c
= PyNumber_Coerce(&v
, &w
);
524 /* Special case: if the third argument is None, it is
525 treated as absent argument and not coerced. */
527 if (v
->ob_type
->tp_as_number
) {
528 slotz
= NB_TERNOP(v
->ob_type
->tp_as_number
,
541 c
= PyNumber_Coerce(&v1
, &z1
);
546 c
= PyNumber_Coerce(&w2
, &z2
);
550 if (v1
->ob_type
->tp_as_number
!= NULL
) {
551 slotv
= NB_TERNOP(v1
->ob_type
->tp_as_number
,
554 x
= slotv(v1
, w2
, z2
);
577 "unsupported operand type(s) for ** or pow(): "
580 w
->ob_type
->tp_name
);
584 "unsupported operand type(s) for pow(): "
588 z
->ob_type
->tp_name
);
592 #define BINARY_FUNC(func, op, op_name) \
594 func(PyObject *v, PyObject *w) { \
595 return binary_op(v, w, NB_SLOT(op), op_name); \
598 BINARY_FUNC(PyNumber_Or
, nb_or
, "|")
599 BINARY_FUNC(PyNumber_Xor
, nb_xor
, "^")
600 BINARY_FUNC(PyNumber_And
, nb_and
, "&")
601 BINARY_FUNC(PyNumber_Lshift
, nb_lshift
, "<<")
602 BINARY_FUNC(PyNumber_Rshift
, nb_rshift
, ">>")
603 BINARY_FUNC(PyNumber_Subtract
, nb_subtract
, "-")
604 BINARY_FUNC(PyNumber_Divide
, nb_divide
, "/")
605 BINARY_FUNC(PyNumber_Divmod
, nb_divmod
, "divmod()")
608 PyNumber_Add(PyObject
*v
, PyObject
*w
)
610 PyObject
*result
= binary_op1(v
, w
, NB_SLOT(nb_add
));
611 if (result
== Py_NotImplemented
) {
612 PySequenceMethods
*m
= v
->ob_type
->tp_as_sequence
;
613 if (m
&& m
->sq_concat
) {
615 result
= (*m
->sq_concat
)(v
, w
);
617 if (result
== Py_NotImplemented
) {
619 return binop_type_error(v
, w
, "+");
626 sequence_repeat(intargfunc repeatfunc
, PyObject
*seq
, PyObject
*n
)
629 if (PyInt_Check(n
)) {
630 count
= PyInt_AsLong(n
);
632 else if (PyLong_Check(n
)) {
633 count
= PyLong_AsLong(n
);
634 if (count
== -1 && PyErr_Occurred())
639 "can't multiply sequence to non-int");
641 #if LONG_MAX != INT_MAX
642 if (count
> INT_MAX
) {
643 PyErr_SetString(PyExc_ValueError
,
644 "sequence repeat count too large");
647 else if (count
< INT_MIN
)
649 /* XXX Why don't I either
651 - set count to -1 whenever it's negative (after all,
652 sequence repeat usually treats negative numbers
655 - raise an exception when it's less than INT_MIN?
657 I'm thinking about a hypothetical use case where some
658 sequence type might use a negative value as a flag of
659 some kind. In those cases I don't want to break the
660 code by mapping all negative values to -1. But I also
661 don't want to break e.g. []*(-sys.maxint), which is
662 perfectly safe, returning []. As a compromise, I do
663 map out-of-range negative values.
666 return (*repeatfunc
)(seq
, (int)count
);
670 PyNumber_Multiply(PyObject
*v
, PyObject
*w
)
672 PyObject
*result
= binary_op1(v
, w
, NB_SLOT(nb_multiply
));
673 if (result
== Py_NotImplemented
) {
674 PySequenceMethods
*mv
= v
->ob_type
->tp_as_sequence
;
675 PySequenceMethods
*mw
= w
->ob_type
->tp_as_sequence
;
677 if (mv
&& mv
->sq_repeat
) {
678 return sequence_repeat(mv
->sq_repeat
, v
, w
);
680 else if (mw
&& mw
->sq_repeat
) {
681 return sequence_repeat(mw
->sq_repeat
, w
, v
);
683 result
= binop_type_error(v
, w
, "*");
689 PyNumber_FloorDivide(PyObject
*v
, PyObject
*w
)
691 /* XXX tp_flags test */
692 return binary_op(v
, w
, NB_SLOT(nb_floor_divide
), "//");
696 PyNumber_TrueDivide(PyObject
*v
, PyObject
*w
)
698 /* XXX tp_flags test */
699 return binary_op(v
, w
, NB_SLOT(nb_true_divide
), "/");
703 PyNumber_Remainder(PyObject
*v
, PyObject
*w
)
705 return binary_op(v
, w
, NB_SLOT(nb_remainder
), "%");
709 PyNumber_Power(PyObject
*v
, PyObject
*w
, PyObject
*z
)
711 return ternary_op(v
, w
, z
, NB_SLOT(nb_power
), "** or pow()");
714 /* Binary in-place operators */
716 /* The in-place operators are defined to fall back to the 'normal',
717 non in-place operations, if the in-place methods are not in place.
719 - If the left hand object has the appropriate struct members, and
720 they are filled, call the appropriate function and return the
721 result. No coercion is done on the arguments; the left-hand object
722 is the one the operation is performed on, and it's up to the
723 function to deal with the right-hand object.
725 - Otherwise, in-place modification is not supported. Handle it exactly as
726 a non in-place operation of the same kind.
730 #define HASINPLACE(t) \
731 PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS)
734 binary_iop1(PyObject
*v
, PyObject
*w
, const int iop_slot
, const int op_slot
)
736 PyNumberMethods
*mv
= v
->ob_type
->tp_as_number
;
737 if (mv
!= NULL
&& HASINPLACE(v
)) {
738 binaryfunc slot
= NB_BINOP(mv
, iop_slot
);
740 PyObject
*x
= (slot
)(v
, w
);
741 if (x
!= Py_NotImplemented
) {
747 return binary_op1(v
, w
, op_slot
);
751 binary_iop(PyObject
*v
, PyObject
*w
, const int iop_slot
, const int op_slot
,
754 PyObject
*result
= binary_iop1(v
, w
, iop_slot
, op_slot
);
755 if (result
== Py_NotImplemented
) {
757 return binop_type_error(v
, w
, op_name
);
762 #define INPLACE_BINOP(func, iop, op, op_name) \
764 func(PyObject *v, PyObject *w) { \
765 return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
768 INPLACE_BINOP(PyNumber_InPlaceOr
, nb_inplace_or
, nb_or
, "|=")
769 INPLACE_BINOP(PyNumber_InPlaceXor
, nb_inplace_xor
, nb_xor
, "^=")
770 INPLACE_BINOP(PyNumber_InPlaceAnd
, nb_inplace_and
, nb_and
, "&=")
771 INPLACE_BINOP(PyNumber_InPlaceLshift
, nb_inplace_lshift
, nb_lshift
, "<<=")
772 INPLACE_BINOP(PyNumber_InPlaceRshift
, nb_inplace_rshift
, nb_rshift
, ">>=")
773 INPLACE_BINOP(PyNumber_InPlaceSubtract
, nb_inplace_subtract
, nb_subtract
, "-=")
774 INPLACE_BINOP(PyNumber_InPlaceDivide
, nb_inplace_divide
, nb_divide
, "/=")
777 PyNumber_InPlaceFloorDivide(PyObject
*v
, PyObject
*w
)
779 /* XXX tp_flags test */
780 return binary_iop(v
, w
, NB_SLOT(nb_inplace_floor_divide
),
781 NB_SLOT(nb_floor_divide
), "//=");
785 PyNumber_InPlaceTrueDivide(PyObject
*v
, PyObject
*w
)
787 /* XXX tp_flags test */
788 return binary_iop(v
, w
, NB_SLOT(nb_inplace_true_divide
),
789 NB_SLOT(nb_true_divide
), "/=");
793 PyNumber_InPlaceAdd(PyObject
*v
, PyObject
*w
)
795 PyObject
*result
= binary_iop1(v
, w
, NB_SLOT(nb_inplace_add
),
797 if (result
== Py_NotImplemented
) {
798 PySequenceMethods
*m
= v
->ob_type
->tp_as_sequence
;
803 f
= m
->sq_inplace_concat
;
809 result
= binop_type_error(v
, w
, "+=");
815 PyNumber_InPlaceMultiply(PyObject
*v
, PyObject
*w
)
817 PyObject
*result
= binary_iop1(v
, w
, NB_SLOT(nb_inplace_multiply
),
818 NB_SLOT(nb_multiply
));
819 if (result
== Py_NotImplemented
) {
821 PySequenceMethods
*mv
= v
->ob_type
->tp_as_sequence
;
822 PySequenceMethods
*mw
= w
->ob_type
->tp_as_sequence
;
826 f
= mv
->sq_inplace_repeat
;
830 return sequence_repeat(f
, v
, w
);
832 else if (mw
!= NULL
) {
833 /* Note that the right hand operand should not be
834 * mutated in this case so sq_inplace_repeat is not
837 return sequence_repeat(mw
->sq_repeat
, w
, v
);
839 result
= binop_type_error(v
, w
, "*=");
845 PyNumber_InPlaceRemainder(PyObject
*v
, PyObject
*w
)
847 return binary_iop(v
, w
, NB_SLOT(nb_inplace_remainder
),
848 NB_SLOT(nb_remainder
), "%=");
852 PyNumber_InPlacePower(PyObject
*v
, PyObject
*w
, PyObject
*z
)
854 if (HASINPLACE(v
) && v
->ob_type
->tp_as_number
&&
855 v
->ob_type
->tp_as_number
->nb_inplace_power
!= NULL
) {
856 return ternary_op(v
, w
, z
, NB_SLOT(nb_inplace_power
), "**=");
859 return ternary_op(v
, w
, z
, NB_SLOT(nb_power
), "**=");
864 /* Unary operators and functions */
867 PyNumber_Negative(PyObject
*o
)
873 m
= o
->ob_type
->tp_as_number
;
874 if (m
&& m
->nb_negative
)
875 return (*m
->nb_negative
)(o
);
877 return type_error("bad operand type for unary -");
881 PyNumber_Positive(PyObject
*o
)
887 m
= o
->ob_type
->tp_as_number
;
888 if (m
&& m
->nb_positive
)
889 return (*m
->nb_positive
)(o
);
891 return type_error("bad operand type for unary +");
895 PyNumber_Invert(PyObject
*o
)
901 m
= o
->ob_type
->tp_as_number
;
902 if (m
&& m
->nb_invert
)
903 return (*m
->nb_invert
)(o
);
905 return type_error("bad operand type for unary ~");
909 PyNumber_Absolute(PyObject
*o
)
915 m
= o
->ob_type
->tp_as_number
;
916 if (m
&& m
->nb_absolute
)
917 return m
->nb_absolute(o
);
919 return type_error("bad operand type for abs()");
922 /* Add a check for embedded NULL-bytes in the argument. */
924 int_from_string(const char *s
, int len
)
929 x
= PyInt_FromString((char*)s
, &end
, 10);
932 if (end
!= s
+ len
) {
933 PyErr_SetString(PyExc_ValueError
,
934 "null byte in argument for int()");
942 PyNumber_Int(PyObject
*o
)
950 if (PyInt_CheckExact(o
)) {
954 if (PyInt_Check(o
)) {
955 PyIntObject
*io
= (PyIntObject
*)o
;
956 return PyInt_FromLong(io
->ob_ival
);
958 if (PyString_Check(o
))
959 return int_from_string(PyString_AS_STRING(o
),
960 PyString_GET_SIZE(o
));
961 #ifdef Py_USING_UNICODE
962 if (PyUnicode_Check(o
))
963 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o
),
964 PyUnicode_GET_SIZE(o
),
967 m
= o
->ob_type
->tp_as_number
;
970 if (!PyObject_AsCharBuffer(o
, &buffer
, &buffer_len
))
971 return int_from_string((char*)buffer
, buffer_len
);
973 return type_error("int() argument must be a string or a number");
976 /* Add a check for embedded NULL-bytes in the argument. */
978 long_from_string(const char *s
, int len
)
983 x
= PyLong_FromString((char*)s
, &end
, 10);
986 if (end
!= s
+ len
) {
987 PyErr_SetString(PyExc_ValueError
,
988 "null byte in argument for long()");
996 PyNumber_Long(PyObject
*o
)
1003 return null_error();
1004 if (PyLong_CheckExact(o
)) {
1008 if (PyLong_Check(o
))
1009 return _PyLong_Copy((PyLongObject
*)o
);
1010 if (PyString_Check(o
))
1011 /* need to do extra error checking that PyLong_FromString()
1012 * doesn't do. In particular long('9.5') must raise an
1013 * exception, not truncate the float.
1015 return long_from_string(PyString_AS_STRING(o
),
1016 PyString_GET_SIZE(o
));
1017 #ifdef Py_USING_UNICODE
1018 if (PyUnicode_Check(o
))
1019 /* The above check is done in PyLong_FromUnicode(). */
1020 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o
),
1021 PyUnicode_GET_SIZE(o
),
1024 m
= o
->ob_type
->tp_as_number
;
1025 if (m
&& m
->nb_long
)
1026 return m
->nb_long(o
);
1027 if (!PyObject_AsCharBuffer(o
, &buffer
, &buffer_len
))
1028 return long_from_string(buffer
, buffer_len
);
1030 return type_error("long() argument must be a string or a number");
1034 PyNumber_Float(PyObject
*o
)
1039 return null_error();
1040 if (PyFloat_CheckExact(o
)) {
1044 if (PyFloat_Check(o
)) {
1045 PyFloatObject
*po
= (PyFloatObject
*)o
;
1046 return PyFloat_FromDouble(po
->ob_fval
);
1048 if (!PyString_Check(o
)) {
1049 m
= o
->ob_type
->tp_as_number
;
1050 if (m
&& m
->nb_float
)
1051 return m
->nb_float(o
);
1053 return PyFloat_FromString(o
, NULL
);
1056 /* Operations on sequences */
1059 PySequence_Check(PyObject
*s
)
1061 return s
!= NULL
&& s
->ob_type
->tp_as_sequence
&&
1062 s
->ob_type
->tp_as_sequence
->sq_item
!= NULL
;
1066 PySequence_Size(PyObject
*s
)
1068 PySequenceMethods
*m
;
1075 m
= s
->ob_type
->tp_as_sequence
;
1076 if (m
&& m
->sq_length
)
1077 return m
->sq_length(s
);
1079 type_error("len() of unsized object");
1083 #undef PySequence_Length
1085 PySequence_Length(PyObject
*s
)
1087 return PySequence_Size(s
);
1089 #define PySequence_Length PySequence_Size
1092 PySequence_Concat(PyObject
*s
, PyObject
*o
)
1094 PySequenceMethods
*m
;
1096 if (s
== NULL
|| o
== NULL
)
1097 return null_error();
1099 m
= s
->ob_type
->tp_as_sequence
;
1100 if (m
&& m
->sq_concat
)
1101 return m
->sq_concat(s
, o
);
1103 return type_error("object can't be concatenated");
1107 PySequence_Repeat(PyObject
*o
, int count
)
1109 PySequenceMethods
*m
;
1112 return null_error();
1114 m
= o
->ob_type
->tp_as_sequence
;
1115 if (m
&& m
->sq_repeat
)
1116 return m
->sq_repeat(o
, count
);
1118 return type_error("object can't be repeated");
1122 PySequence_InPlaceConcat(PyObject
*s
, PyObject
*o
)
1124 PySequenceMethods
*m
;
1126 if (s
== NULL
|| o
== NULL
)
1127 return null_error();
1129 m
= s
->ob_type
->tp_as_sequence
;
1130 if (m
&& HASINPLACE(s
) && m
->sq_inplace_concat
)
1131 return m
->sq_inplace_concat(s
, o
);
1132 if (m
&& m
->sq_concat
)
1133 return m
->sq_concat(s
, o
);
1135 return type_error("object can't be concatenated");
1139 PySequence_InPlaceRepeat(PyObject
*o
, int count
)
1141 PySequenceMethods
*m
;
1144 return null_error();
1146 m
= o
->ob_type
->tp_as_sequence
;
1147 if (m
&& HASINPLACE(o
) && m
->sq_inplace_repeat
)
1148 return m
->sq_inplace_repeat(o
, count
);
1149 if (m
&& m
->sq_repeat
)
1150 return m
->sq_repeat(o
, count
);
1152 return type_error("object can't be repeated");
1156 PySequence_GetItem(PyObject
*s
, int i
)
1158 PySequenceMethods
*m
;
1161 return null_error();
1163 m
= s
->ob_type
->tp_as_sequence
;
1164 if (m
&& m
->sq_item
) {
1167 int l
= (*m
->sq_length
)(s
);
1173 return m
->sq_item(s
, i
);
1176 return type_error("unindexable object");
1180 sliceobj_from_intint(int i
, int j
)
1182 PyObject
*start
, *end
, *slice
;
1183 start
= PyInt_FromLong((long)i
);
1186 end
= PyInt_FromLong((long)j
);
1191 slice
= PySlice_New(start
, end
, NULL
);
1198 PySequence_GetSlice(PyObject
*s
, int i1
, int i2
)
1200 PySequenceMethods
*m
;
1201 PyMappingMethods
*mp
;
1203 if (!s
) return null_error();
1205 m
= s
->ob_type
->tp_as_sequence
;
1206 if (m
&& m
->sq_slice
) {
1207 if (i1
< 0 || i2
< 0) {
1209 int l
= (*m
->sq_length
)(s
);
1218 return m
->sq_slice(s
, i1
, i2
);
1219 } else if ((mp
= s
->ob_type
->tp_as_mapping
) && mp
->mp_subscript
) {
1221 PyObject
*slice
= sliceobj_from_intint(i1
, i2
);
1224 res
= mp
->mp_subscript(s
, slice
);
1229 return type_error("unsliceable object");
1233 PySequence_SetItem(PyObject
*s
, int i
, PyObject
*o
)
1235 PySequenceMethods
*m
;
1242 m
= s
->ob_type
->tp_as_sequence
;
1243 if (m
&& m
->sq_ass_item
) {
1246 int l
= (*m
->sq_length
)(s
);
1252 return m
->sq_ass_item(s
, i
, o
);
1255 type_error("object doesn't support item assignment");
1260 PySequence_DelItem(PyObject
*s
, int i
)
1262 PySequenceMethods
*m
;
1269 m
= s
->ob_type
->tp_as_sequence
;
1270 if (m
&& m
->sq_ass_item
) {
1273 int l
= (*m
->sq_length
)(s
);
1279 return m
->sq_ass_item(s
, i
, (PyObject
*)NULL
);
1282 type_error("object doesn't support item deletion");
1287 PySequence_SetSlice(PyObject
*s
, int i1
, int i2
, PyObject
*o
)
1289 PySequenceMethods
*m
;
1290 PyMappingMethods
*mp
;
1297 m
= s
->ob_type
->tp_as_sequence
;
1298 if (m
&& m
->sq_ass_slice
) {
1299 if (i1
< 0 || i2
< 0) {
1301 int l
= (*m
->sq_length
)(s
);
1310 return m
->sq_ass_slice(s
, i1
, i2
, o
);
1311 } else if ((mp
= s
->ob_type
->tp_as_mapping
) && mp
->mp_ass_subscript
) {
1313 PyObject
*slice
= sliceobj_from_intint(i1
, i2
);
1316 res
= mp
->mp_ass_subscript(s
, slice
, o
);
1321 type_error("object doesn't support slice assignment");
1326 PySequence_DelSlice(PyObject
*s
, int i1
, int i2
)
1328 PySequenceMethods
*m
;
1335 m
= s
->ob_type
->tp_as_sequence
;
1336 if (m
&& m
->sq_ass_slice
) {
1337 if (i1
< 0 || i2
< 0) {
1339 int l
= (*m
->sq_length
)(s
);
1348 return m
->sq_ass_slice(s
, i1
, i2
, (PyObject
*)NULL
);
1350 type_error("object doesn't support slice deletion");
1355 PySequence_Tuple(PyObject
*v
)
1357 PyObject
*it
; /* iter(v) */
1358 int n
; /* guess for result tuple size */
1363 return null_error();
1365 /* Special-case the common tuple and list cases, for efficiency. */
1366 if (PyTuple_CheckExact(v
)) {
1367 /* Note that we can't know whether it's safe to return
1368 a tuple *subclass* instance as-is, hence the restriction
1369 to exact tuples here. In contrast, lists always make
1370 a copy, so there's no need for exactness below. */
1374 if (PyList_Check(v
))
1375 return PyList_AsTuple(v
);
1378 it
= PyObject_GetIter(v
);
1382 /* Guess result size and allocate space. */
1383 n
= PySequence_Size(v
);
1386 n
= 10; /* arbitrary */
1388 result
= PyTuple_New(n
);
1392 /* Fill the tuple. */
1393 for (j
= 0; ; ++j
) {
1394 PyObject
*item
= PyIter_Next(it
);
1396 if (PyErr_Occurred())
1405 if (_PyTuple_Resize(&result
, n
) != 0) {
1410 PyTuple_SET_ITEM(result
, j
, item
);
1413 /* Cut tuple back if guess was too large. */
1415 _PyTuple_Resize(&result
, j
) != 0)
1428 PySequence_List(PyObject
*v
)
1430 PyObject
*it
; /* iter(v) */
1431 PyObject
*result
; /* result list */
1432 int n
; /* guess for result list size */
1436 return null_error();
1438 /* Special-case list(a_list), for speed. */
1439 if (PyList_Check(v
))
1440 return PyList_GetSlice(v
, 0, PyList_GET_SIZE(v
));
1442 /* Get iterator. There may be some low-level efficiency to be gained
1443 * by caching the tp_iternext slot instead of using PyIter_Next()
1444 * later, but premature optimization is the root etc.
1446 it
= PyObject_GetIter(v
);
1450 /* Guess a result list size. */
1451 n
= -1; /* unknown */
1452 if (PySequence_Check(v
) &&
1453 v
->ob_type
->tp_as_sequence
->sq_length
) {
1454 n
= PySequence_Size(v
);
1459 n
= 8; /* arbitrary */
1460 result
= PyList_New(n
);
1461 if (result
== NULL
) {
1466 /* Run iterator to exhaustion. */
1467 for (i
= 0; ; i
++) {
1468 PyObject
*item
= PyIter_Next(it
);
1470 if (PyErr_Occurred()) {
1477 PyList_SET_ITEM(result
, i
, item
); /* steals ref */
1479 int status
= PyList_Append(result
, item
);
1480 Py_DECREF(item
); /* append creates a new ref */
1489 /* Cut back result list if initial guess was too large. */
1490 if (i
< n
&& result
!= NULL
) {
1491 if (PyList_SetSlice(result
, i
, n
, (PyObject
*)NULL
) != 0) {
1501 PySequence_Fast(PyObject
*v
, const char *m
)
1504 return null_error();
1506 if (PyList_CheckExact(v
) || PyTuple_CheckExact(v
)) {
1511 v
= PySequence_Tuple(v
);
1512 if (v
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
))
1513 return type_error(m
);
1518 /* Iterate over seq. Result depends on the operation:
1519 PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
1520 PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq;
1521 set ValueError and return -1 if none found; also return -1 on error.
1522 Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
1525 _PySequence_IterSearch(PyObject
*seq
, PyObject
*obj
, int operation
)
1528 int wrapped
; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
1529 PyObject
*it
; /* iter(seq) */
1531 if (seq
== NULL
|| obj
== NULL
) {
1536 it
= PyObject_GetIter(seq
);
1538 type_error("iterable argument required");
1545 PyObject
*item
= PyIter_Next(it
);
1547 if (PyErr_Occurred())
1552 cmp
= PyObject_RichCompareBool(obj
, item
, Py_EQ
);
1557 switch (operation
) {
1558 case PY_ITERSEARCH_COUNT
:
1561 PyErr_SetString(PyExc_OverflowError
,
1562 "count exceeds C int size");
1567 case PY_ITERSEARCH_INDEX
:
1569 PyErr_SetString(PyExc_OverflowError
,
1570 "index exceeds C int size");
1575 case PY_ITERSEARCH_CONTAINS
:
1580 assert(!"unknown operation");
1584 if (operation
== PY_ITERSEARCH_INDEX
) {
1591 if (operation
!= PY_ITERSEARCH_INDEX
)
1594 PyErr_SetString(PyExc_ValueError
,
1595 "sequence.index(x): x not in sequence");
1596 /* fall into failure code */
1606 /* Return # of times o appears in s. */
1608 PySequence_Count(PyObject
*s
, PyObject
*o
)
1610 return _PySequence_IterSearch(s
, o
, PY_ITERSEARCH_COUNT
);
1613 /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
1614 * Use sq_contains if possible, else defer to _PySequence_IterSearch().
1617 PySequence_Contains(PyObject
*seq
, PyObject
*ob
)
1619 if (PyType_HasFeature(seq
->ob_type
, Py_TPFLAGS_HAVE_SEQUENCE_IN
)) {
1620 PySequenceMethods
*sqm
= seq
->ob_type
->tp_as_sequence
;
1621 if (sqm
!= NULL
&& sqm
->sq_contains
!= NULL
)
1622 return (*sqm
->sq_contains
)(seq
, ob
);
1624 return _PySequence_IterSearch(seq
, ob
, PY_ITERSEARCH_CONTAINS
);
1627 /* Backwards compatibility */
1628 #undef PySequence_In
1630 PySequence_In(PyObject
*w
, PyObject
*v
)
1632 return PySequence_Contains(w
, v
);
1636 PySequence_Index(PyObject
*s
, PyObject
*o
)
1638 return _PySequence_IterSearch(s
, o
, PY_ITERSEARCH_INDEX
);
1641 /* Operations on mappings */
1644 PyMapping_Check(PyObject
*o
)
1646 return o
&& o
->ob_type
->tp_as_mapping
&&
1647 o
->ob_type
->tp_as_mapping
->mp_subscript
;
1651 PyMapping_Size(PyObject
*o
)
1653 PyMappingMethods
*m
;
1660 m
= o
->ob_type
->tp_as_mapping
;
1661 if (m
&& m
->mp_length
)
1662 return m
->mp_length(o
);
1664 type_error("len() of unsized object");
1668 #undef PyMapping_Length
1670 PyMapping_Length(PyObject
*o
)
1672 return PyMapping_Size(o
);
1674 #define PyMapping_Length PyMapping_Size
1677 PyMapping_GetItemString(PyObject
*o
, char *key
)
1682 return null_error();
1684 okey
= PyString_FromString(key
);
1687 r
= PyObject_GetItem(o
, okey
);
1693 PyMapping_SetItemString(PyObject
*o
, char *key
, PyObject
*value
)
1703 okey
= PyString_FromString(key
);
1706 r
= PyObject_SetItem(o
, okey
, value
);
1712 PyMapping_HasKeyString(PyObject
*o
, char *key
)
1716 v
= PyMapping_GetItemString(o
, key
);
1726 PyMapping_HasKey(PyObject
*o
, PyObject
*key
)
1730 v
= PyObject_GetItem(o
, key
);
1739 /* Operations on callable objects */
1741 /* XXX PyCallable_Check() is in object.c */
1744 PyObject_CallObject(PyObject
*o
, PyObject
*a
)
1746 return PyEval_CallObjectWithKeywords(o
, a
, NULL
);
1750 PyObject_Call(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
1754 if ((call
= func
->ob_type
->tp_call
) != NULL
) {
1755 PyObject
*result
= (*call
)(func
, arg
, kw
);
1756 if (result
== NULL
&& !PyErr_Occurred())
1759 "NULL result without error in PyObject_Call");
1762 PyErr_Format(PyExc_TypeError
, "'%s' object is not callable",
1763 func
->ob_type
->tp_name
);
1768 PyObject_CallFunction(PyObject
*callable
, char *format
, ...)
1771 PyObject
*args
, *retval
;
1773 if (callable
== NULL
)
1774 return null_error();
1776 if (format
&& *format
) {
1777 va_start(va
, format
);
1778 args
= Py_VaBuildValue(format
, va
);
1782 args
= PyTuple_New(0);
1787 if (!PyTuple_Check(args
)) {
1793 if (PyTuple_SetItem(a
, 0, args
) < 0)
1797 retval
= PyObject_Call(callable
, args
, NULL
);
1805 PyObject_CallMethod(PyObject
*o
, char *name
, char *format
, ...)
1808 PyObject
*args
, *func
= 0, *retval
;
1810 if (o
== NULL
|| name
== NULL
)
1811 return null_error();
1813 func
= PyObject_GetAttrString(o
, name
);
1815 PyErr_SetString(PyExc_AttributeError
, name
);
1819 if (!PyCallable_Check(func
))
1820 return type_error("call of non-callable attribute");
1822 if (format
&& *format
) {
1823 va_start(va
, format
);
1824 args
= Py_VaBuildValue(format
, va
);
1828 args
= PyTuple_New(0);
1833 if (!PyTuple_Check(args
)) {
1839 if (PyTuple_SetItem(a
, 0, args
) < 0)
1844 retval
= PyObject_Call(func
, args
, NULL
);
1854 objargs_mktuple(va_list va
)
1858 PyObject
*result
, *tmp
;
1860 #ifdef VA_LIST_IS_ARRAY
1861 memcpy(countva
, va
, sizeof(va_list));
1864 __va_copy(countva
, va
);
1870 while (((PyObject
*)va_arg(countva
, PyObject
*)) != NULL
)
1872 result
= PyTuple_New(n
);
1873 if (result
!= NULL
&& n
> 0) {
1874 for (i
= 0; i
< n
; ++i
) {
1875 tmp
= (PyObject
*)va_arg(va
, PyObject
*);
1876 PyTuple_SET_ITEM(result
, i
, tmp
);
1884 PyObject_CallMethodObjArgs(PyObject
*callable
, PyObject
*name
, ...)
1886 PyObject
*args
, *tmp
;
1889 if (callable
== NULL
|| name
== NULL
)
1890 return null_error();
1892 callable
= PyObject_GetAttr(callable
, name
);
1893 if (callable
== NULL
)
1896 /* count the args */
1897 va_start(vargs
, name
);
1898 args
= objargs_mktuple(vargs
);
1901 Py_DECREF(callable
);
1904 tmp
= PyObject_Call(callable
, args
, NULL
);
1906 Py_DECREF(callable
);
1912 PyObject_CallFunctionObjArgs(PyObject
*callable
, ...)
1914 PyObject
*args
, *tmp
;
1917 if (callable
== NULL
)
1918 return null_error();
1920 /* count the args */
1921 va_start(vargs
, callable
);
1922 args
= objargs_mktuple(vargs
);
1926 tmp
= PyObject_Call(callable
, args
, NULL
);
1933 /* isinstance(), issubclass() */
1935 /* abstract_get_bases() has logically 4 return states, with a sort of 0th
1936 * state that will almost never happen.
1938 * 0. creating the __bases__ static string could get a MemoryError
1939 * 1. getattr(cls, '__bases__') could raise an AttributeError
1940 * 2. getattr(cls, '__bases__') could raise some other exception
1941 * 3. getattr(cls, '__bases__') could return a tuple
1942 * 4. getattr(cls, '__bases__') could return something other than a tuple
1944 * Only state #3 is a non-error state and only it returns a non-NULL object
1945 * (it returns the retrieved tuple).
1947 * Any raised AttributeErrors are masked by clearing the exception and
1948 * returning NULL. If an object other than a tuple comes out of __bases__,
1949 * then again, the return value is NULL. So yes, these two situations
1950 * produce exactly the same results: NULL is returned and no error is set.
1952 * If some exception other than AttributeError is raised, then NULL is also
1953 * returned, but the exception is not cleared. That's because we want the
1954 * exception to be propagated along.
1956 * Callers are expected to test for PyErr_Occurred() when the return value
1957 * is NULL to decide whether a valid exception should be propagated or not.
1958 * When there's no exception to propagate, it's customary for the caller to
1962 abstract_get_bases(PyObject
*cls
)
1964 static PyObject
*__bases__
= NULL
;
1967 if (__bases__
== NULL
) {
1968 __bases__
= PyString_FromString("__bases__");
1969 if (__bases__
== NULL
)
1972 bases
= PyObject_GetAttr(cls
, __bases__
);
1973 if (bases
== NULL
) {
1974 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
1978 if (!PyTuple_Check(bases
)) {
1987 abstract_issubclass(PyObject
*derived
, PyObject
*cls
)
1997 if (PyTuple_Check(cls
)) {
1998 /* Not a general sequence -- that opens up the road to
1999 recursion and stack overflow. */
2000 n
= PyTuple_GET_SIZE(cls
);
2001 for (i
= 0; i
< n
; i
++) {
2002 if (derived
== PyTuple_GET_ITEM(cls
, i
))
2006 bases
= abstract_get_bases(derived
);
2007 if (bases
== NULL
) {
2008 if (PyErr_Occurred())
2012 n
= PyTuple_GET_SIZE(bases
);
2013 for (i
= 0; i
< n
; i
++) {
2014 r
= abstract_issubclass(PyTuple_GET_ITEM(bases
, i
), cls
);
2025 check_class(PyObject
*cls
, const char *error
)
2027 PyObject
*bases
= abstract_get_bases(cls
);
2028 if (bases
== NULL
) {
2029 /* Do not mask errors. */
2030 if (!PyErr_Occurred())
2031 PyErr_SetString(PyExc_TypeError
, error
);
2039 PyObject_IsInstance(PyObject
*inst
, PyObject
*cls
)
2042 static PyObject
*__class__
= NULL
;
2045 if (__class__
== NULL
) {
2046 __class__
= PyString_FromString("__class__");
2047 if (__class__
== NULL
)
2051 if (PyClass_Check(cls
) && PyInstance_Check(inst
)) {
2053 (PyObject
*)((PyInstanceObject
*)inst
)->in_class
;
2054 retval
= PyClass_IsSubclass(inclass
, cls
);
2056 else if (PyType_Check(cls
)) {
2057 retval
= PyObject_TypeCheck(inst
, (PyTypeObject
*)cls
);
2059 PyObject
*c
= PyObject_GetAttr(inst
, __class__
);
2064 if (c
!= (PyObject
*)(inst
->ob_type
) &&
2066 retval
= PyType_IsSubtype(
2068 (PyTypeObject
*)cls
);
2073 else if (PyTuple_Check(cls
)) {
2074 /* Not a general sequence -- that opens up the road to
2075 recursion and stack overflow. */
2078 n
= PyTuple_GET_SIZE(cls
);
2079 for (i
= 0; i
< n
; i
++) {
2080 retval
= PyObject_IsInstance(
2081 inst
, PyTuple_GET_ITEM(cls
, i
));
2087 if (!check_class(cls
,
2088 "isinstance() arg 2 must be a class, type,"
2089 " or tuple of classes and types"))
2091 icls
= PyObject_GetAttr(inst
, __class__
);
2097 retval
= abstract_issubclass(icls
, cls
);
2106 PyObject_IsSubclass(PyObject
*derived
, PyObject
*cls
)
2110 if (!PyClass_Check(derived
) || !PyClass_Check(cls
)) {
2111 if (!check_class(derived
,
2112 "issubclass() arg 1 must be a class"))
2115 if (PyTuple_Check(cls
)) {
2117 int n
= PyTuple_GET_SIZE(cls
);
2118 for (i
= 0; i
< n
; ++i
) {
2119 retval
= PyObject_IsSubclass(
2120 derived
, PyTuple_GET_ITEM(cls
, i
));
2122 /* either found it, or got an error */
2129 if (!check_class(cls
,
2130 "issubclass() arg 2 must be a class"
2131 " or tuple of classes"))
2135 retval
= abstract_issubclass(derived
, cls
);
2139 if (!(retval
= (derived
== cls
)))
2140 retval
= PyClass_IsSubclass(derived
, cls
);
2147 PyObject_GetIter(PyObject
*o
)
2149 PyTypeObject
*t
= o
->ob_type
;
2150 getiterfunc f
= NULL
;
2151 if (PyType_HasFeature(t
, Py_TPFLAGS_HAVE_ITER
))
2154 if (PySequence_Check(o
))
2155 return PySeqIter_New(o
);
2156 PyErr_SetString(PyExc_TypeError
,
2157 "iteration over non-sequence");
2161 PyObject
*res
= (*f
)(o
);
2162 if (res
!= NULL
&& !PyIter_Check(res
)) {
2163 PyErr_Format(PyExc_TypeError
,
2164 "iter() returned non-iterator "
2166 res
->ob_type
->tp_name
);
2174 /* Return next item.
2175 * If an error occurs, return NULL. PyErr_Occurred() will be true.
2176 * If the iteration terminates normally, return NULL and clear the
2177 * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
2179 * Else return the next object. PyErr_Occurred() will be false.
2182 PyIter_Next(PyObject
*iter
)
2185 assert(PyIter_Check(iter
));
2186 result
= (*iter
->ob_type
->tp_iternext
)(iter
);
2187 if (result
== NULL
&&
2189 PyErr_ExceptionMatches(PyExc_StopIteration
))