1 /* Abstract Object Interface (many thanks to Jim Fulton) */
6 #include "structmember.h" /* we need the offsetof() macro from there */
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 return type_error("sequence index must be integer");
108 return type_error("unsubscriptable object");
112 PyObject_SetItem(PyObject
*o
, PyObject
*key
, PyObject
*value
)
116 if (o
== NULL
|| key
== NULL
|| value
== NULL
) {
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())
131 return PySequence_SetItem(o
, key_value
, value
);
133 type_error("sequence index must be integer");
137 type_error("object does not support item assignment");
142 PyObject_DelItem(PyObject
*o
, PyObject
*key
)
146 if (o
== NULL
|| key
== NULL
) {
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())
161 return PySequence_DelItem(o
, key_value
);
163 type_error("sequence index must be integer");
167 type_error("object does not support item deletion");
171 int PyObject_AsCharBuffer(PyObject
*obj
,
179 if (obj
== NULL
|| buffer
== NULL
|| buffer_len
== NULL
) {
183 pb
= obj
->ob_type
->tp_as_buffer
;
185 pb
->bf_getcharbuffer
== NULL
||
186 pb
->bf_getsegcount
== NULL
) {
187 PyErr_SetString(PyExc_TypeError
,
188 "expected a character buffer object");
191 if ( (*pb
->bf_getsegcount
)(obj
,NULL
) != 1 ) {
192 PyErr_SetString(PyExc_TypeError
,
193 "expected a single-segment buffer object");
196 len
= (*pb
->bf_getcharbuffer
)(obj
,0,&pp
);
207 int PyObject_AsReadBuffer(PyObject
*obj
,
215 if (obj
== NULL
|| buffer
== NULL
|| buffer_len
== NULL
) {
219 pb
= obj
->ob_type
->tp_as_buffer
;
221 pb
->bf_getreadbuffer
== NULL
||
222 pb
->bf_getsegcount
== NULL
) {
223 PyErr_SetString(PyExc_TypeError
,
224 "expected a readable buffer object");
227 if ( (*pb
->bf_getsegcount
)(obj
,NULL
) != 1 ) {
228 PyErr_SetString(PyExc_TypeError
,
229 "expected a single-segment buffer object");
232 len
= (*pb
->bf_getreadbuffer
)(obj
,0,&pp
);
243 int PyObject_AsWriteBuffer(PyObject
*obj
,
251 if (obj
== NULL
|| buffer
== NULL
|| buffer_len
== NULL
) {
255 pb
= obj
->ob_type
->tp_as_buffer
;
257 pb
->bf_getwritebuffer
== NULL
||
258 pb
->bf_getsegcount
== NULL
) {
259 PyErr_SetString(PyExc_TypeError
,
260 "expected a writeable buffer object");
263 if ( (*pb
->bf_getsegcount
)(obj
,NULL
) != 1 ) {
264 PyErr_SetString(PyExc_TypeError
,
265 "expected a single-segment buffer object");
268 len
= (*pb
->bf_getwritebuffer
)(obj
,0,&pp
);
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:
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)
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.
317 binary_op1(PyObject
*v
, PyObject
*w
, const int op_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
);
325 if (x
!= Py_NotImplemented
) {
328 Py_DECREF(x
); /* can't do it */
330 if (v
->ob_type
== w
->ob_type
) {
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
);
338 if (x
!= Py_NotImplemented
) {
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
);
350 PyNumberMethods
*mv
= v
->ob_type
->tp_as_number
;
352 slot
= NB_BINOP(mv
, op_slot
);
354 PyObject
*x
= (*slot
)(v
, w
);
360 /* CoerceEx incremented the reference counts */
366 Py_INCREF(Py_NotImplemented
);
367 return Py_NotImplemented
;
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
);
385 Calling scheme used for ternary operations:
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)
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.
411 ternary_op(PyObject
*v
,
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
);
426 x
= (*slot
)(v
, w
, z
);
427 if (x
!= Py_NotImplemented
)
429 /* Can't do it... fall through */
432 if (v
->ob_type
== w
->ob_type
&&
433 (z
== Py_None
|| z
->ob_type
== v
->ob_type
)) {
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
);
442 x
= (*slot
)(v
, w
, z
);
443 if (x
!= Py_NotImplemented
)
445 /* Can't do it... fall through */
448 if (NEW_STYLE_NUMBER(v
) &&
449 (z
== Py_None
|| z
->ob_type
== v
->ob_type
)) {
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
);
458 x
= (*slot
)(v
, w
, z
);
459 if (x
!= Py_NotImplemented
)
461 /* Can't do it... fall through */
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
;
472 c
= PyNumber_Coerce(&v
, &w
);
476 /* Special case: if the third argument is None, it is
477 treated as absent argument and not coerced. */
479 if (v
->ob_type
->tp_as_number
) {
480 slot
= NB_TERNOP(v
->ob_type
->tp_as_number
,
483 x
= (*slot
)(v
, w
, z
);
493 c
= PyNumber_Coerce(&v1
, &z1
);
498 c
= PyNumber_Coerce(&w2
, &z2
);
502 if (v1
->ob_type
->tp_as_number
!= NULL
) {
503 slot
= NB_TERNOP(v1
->ob_type
->tp_as_number
,
506 x
= (*slot
)(v1
, w2
, z2
);
527 PyErr_Format(PyExc_TypeError
, "unsupported operand type(s) for %s",
532 #define BINARY_FUNC(func, op, op_name) \
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()")
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
);
559 PyErr_SetString(PyExc_TypeError
,
560 "unsupported operand types for +");
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
), "%");
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)
602 binary_iop(PyObject
*v
, PyObject
*w
, const int iop_slot
, const int op_slot
,
605 PyNumberMethods
*mv
= v
->ob_type
->tp_as_number
;
606 if (mv
!= NULL
&& HASINPLACE(v
)) {
607 binaryfunc
*slot
= NB_BINOP(mv
, iop_slot
);
609 PyObject
*x
= (*slot
)(v
, w
);
610 if (x
!= Py_NotImplemented
) {
616 return binary_op(v
, w
, op_slot
, op_name
);
619 #define INPLACE_BINOP(func, iop, op, op_name) \
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
, "/=")
634 PyNumber_InPlaceAdd(PyObject
*v
, PyObject
*w
)
638 if (v
->ob_type
->tp_as_sequence
!= NULL
) {
640 f
= v
->ob_type
->tp_as_sequence
->sq_inplace_concat
;
642 f
= v
->ob_type
->tp_as_sequence
->sq_concat
;
646 return binary_iop(v
, w
, NB_SLOT(nb_inplace_add
), NB_SLOT(nb_add
), "+=");
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
)) {
656 if (PyInt_Check(w
)) {
659 else if (PyLong_Check(w
)) {
660 n
= PyLong_AsLong(w
);
661 if (n
== -1 && PyErr_Occurred())
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
), "*=");
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
);
683 return binary_iop(v
, w
, NB_SLOT(nb_inplace_remainder
),
684 NB_SLOT(nb_remainder
), "%=");
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
), "**=");
696 return ternary_op(v
, w
, z
, NB_SLOT(nb_power
), "**=");
701 /* Unary operators and functions */
704 PyNumber_Negative(PyObject
*o
)
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 -");
718 PyNumber_Positive(PyObject
*o
)
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 +");
732 PyNumber_Invert(PyObject
*o
)
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 ~");
746 PyNumber_Absolute(PyObject
*o
)
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. */
761 int_from_string(const char *s
, int len
)
766 x
= PyInt_FromString((char*)s
, &end
, 10);
769 if (end
!= s
+ len
) {
770 PyErr_SetString(PyExc_ValueError
,
771 "null byte in argument for int()");
779 PyNumber_Int(PyObject
*o
)
787 if (PyInt_Check(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
),
798 m
= o
->ob_type
->tp_as_number
;
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. */
809 long_from_string(const char *s
, int len
)
814 x
= PyLong_FromString((char*)s
, &end
, 10);
817 if (end
!= s
+ len
) {
818 PyErr_SetString(PyExc_ValueError
,
819 "null byte in argument for long()");
827 PyNumber_Long(PyObject
*o
)
835 if (PyLong_Check(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
),
851 m
= o
->ob_type
->tp_as_number
;
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");
861 PyNumber_Float(PyObject
*o
)
867 if (PyFloat_Check(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
;
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");
905 #undef PySequence_Length
907 PySequence_Length(PyObject
*s
)
909 return PySequence_Size(s
);
911 #define PySequence_Length PySequence_Size
914 PySequence_Concat(PyObject
*s
, PyObject
*o
)
916 PySequenceMethods
*m
;
918 if (s
== NULL
|| o
== NULL
)
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");
929 PySequence_Repeat(PyObject
*o
, int count
)
931 PySequenceMethods
*m
;
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");
944 PySequence_InPlaceConcat(PyObject
*s
, PyObject
*o
)
946 PySequenceMethods
*m
;
948 if (s
== NULL
|| o
== NULL
)
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");
961 PySequence_InPlaceRepeat(PyObject
*o
, int count
)
963 PySequenceMethods
*m
;
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");
978 PySequence_GetItem(PyObject
*s
, int i
)
980 PySequenceMethods
*m
;
985 m
= s
->ob_type
->tp_as_sequence
;
986 if (m
&& m
->sq_item
) {
989 int l
= (*m
->sq_length
)(s
);
995 return m
->sq_item(s
, i
);
998 return type_error("unindexable object");
1002 sliceobj_from_intint(int i
, int j
)
1004 PyObject
*start
, *end
, *slice
;
1005 start
= PyInt_FromLong((long)i
);
1008 end
= PyInt_FromLong((long)j
);
1013 slice
= PySlice_New(start
, end
, NULL
);
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) {
1031 int l
= (*m
->sq_length
)(s
);
1040 return m
->sq_slice(s
, i1
, i2
);
1041 } else if ((mp
= s
->ob_type
->tp_as_mapping
) && mp
->mp_subscript
) {
1043 PyObject
*slice
= sliceobj_from_intint(i1
, i2
);
1046 res
= mp
->mp_subscript(s
, slice
);
1051 return type_error("unsliceable object");
1055 PySequence_SetItem(PyObject
*s
, int i
, PyObject
*o
)
1057 PySequenceMethods
*m
;
1064 m
= s
->ob_type
->tp_as_sequence
;
1065 if (m
&& m
->sq_ass_item
) {
1068 int l
= (*m
->sq_length
)(s
);
1074 return m
->sq_ass_item(s
, i
, o
);
1077 type_error("object doesn't support item assignment");
1082 PySequence_DelItem(PyObject
*s
, int i
)
1084 PySequenceMethods
*m
;
1091 m
= s
->ob_type
->tp_as_sequence
;
1092 if (m
&& m
->sq_ass_item
) {
1095 int l
= (*m
->sq_length
)(s
);
1101 return m
->sq_ass_item(s
, i
, (PyObject
*)NULL
);
1104 type_error("object doesn't support item deletion");
1109 PySequence_SetSlice(PyObject
*s
, int i1
, int i2
, PyObject
*o
)
1111 PySequenceMethods
*m
;
1112 PyMappingMethods
*mp
;
1119 m
= s
->ob_type
->tp_as_sequence
;
1120 if (m
&& m
->sq_ass_slice
) {
1121 if (i1
< 0 || i2
< 0) {
1123 int l
= (*m
->sq_length
)(s
);
1132 return m
->sq_ass_slice(s
, i1
, i2
, o
);
1133 } else if ((mp
= s
->ob_type
->tp_as_mapping
) && mp
->mp_ass_subscript
) {
1135 PyObject
*slice
= sliceobj_from_intint(i1
, i2
);
1138 res
= mp
->mp_ass_subscript(s
, slice
, o
);
1143 type_error("object doesn't support slice assignment");
1148 PySequence_DelSlice(PyObject
*s
, int i1
, int i2
)
1150 PySequenceMethods
*m
;
1157 m
= s
->ob_type
->tp_as_sequence
;
1158 if (m
&& m
->sq_ass_slice
) {
1159 if (i1
< 0 || i2
< 0) {
1161 int l
= (*m
->sq_length
)(s
);
1170 return m
->sq_ass_slice(s
, i1
, i2
, (PyObject
*)NULL
);
1172 type_error("object doesn't support slice deletion");
1177 PySequence_Tuple(PyObject
*v
)
1179 PyObject
*it
; /* iter(v) */
1180 int n
; /* guess for result tuple size */
1185 return null_error();
1187 /* Special-case the common tuple and list cases, for efficiency. */
1188 if (PyTuple_Check(v
)) {
1192 if (PyList_Check(v
))
1193 return PyList_AsTuple(v
);
1196 it
= PyObject_GetIter(v
);
1198 return type_error("tuple() argument must support iteration");
1200 /* Guess result size and allocate space. */
1201 n
= PySequence_Size(v
);
1204 n
= 10; /* arbitrary */
1206 result
= PyTuple_New(n
);
1210 /* Fill the tuple. */
1211 for (j
= 0; ; ++j
) {
1212 PyObject
*item
= PyIter_Next(it
);
1214 if (PyErr_Occurred())
1223 if (_PyTuple_Resize(&result
, n
) != 0) {
1228 PyTuple_SET_ITEM(result
, j
, item
);
1231 /* Cut tuple back if guess was too large. */
1233 _PyTuple_Resize(&result
, j
) != 0)
1246 PySequence_List(PyObject
*v
)
1248 PyObject
*it
; /* iter(v) */
1249 PyObject
*result
; /* result list */
1250 int n
; /* guess for result list size */
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
);
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
);
1277 n
= 8; /* arbitrary */
1278 result
= PyList_New(n
);
1279 if (result
== NULL
) {
1284 /* Run iterator to exhaustion. */
1285 for (i
= 0; ; i
++) {
1286 PyObject
*item
= PyIter_Next(it
);
1288 if (PyErr_Occurred()) {
1295 PyList_SET_ITEM(result
, i
, item
); /* steals ref */
1297 int status
= PyList_Append(result
, item
);
1298 Py_DECREF(item
); /* append creates a new ref */
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) {
1319 PySequence_Fast(PyObject
*v
, const char *m
)
1322 return null_error();
1324 if (PyList_Check(v
) || PyTuple_Check(v
)) {
1329 v
= PySequence_Tuple(v
);
1330 if (v
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
))
1331 return type_error(m
);
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
) {
1348 it
= PyObject_GetIter(s
);
1350 type_error(".count() requires iterable argument");
1357 PyObject
*item
= PyIter_Next(it
);
1359 if (PyErr_Occurred())
1363 cmp
= PyObject_RichCompareBool(o
, item
, Py_EQ
);
1369 PyErr_SetString(PyExc_OverflowError
,
1370 "count exceeds C int size");
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
)
1391 PyObject
*it
= PyObject_GetIter(seq
);
1393 PyErr_SetString(PyExc_TypeError
,
1394 "'in' or 'not in' needs iterable right argument");
1400 PyObject
*item
= PyIter_Next(it
);
1402 result
= PyErr_Occurred() ? -1 : 0;
1405 cmp
= PyObject_RichCompareBool(ob
, item
, Py_EQ
);
1409 result
= cmp
> 0 ? 1 : -1;
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
)
1444 if (s
== NULL
|| o
== NULL
) {
1449 l
= PySequence_Size(s
);
1453 for (i
= 0; i
< l
; i
++) {
1454 item
= PySequence_GetItem(s
, i
);
1457 err
= PyObject_Cmp(item
, o
, &cmp
);
1465 PyErr_SetString(PyExc_ValueError
, "sequence.index(x): x not in list");
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
;
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");
1495 #undef PyMapping_Length
1497 PyMapping_Length(PyObject
*o
)
1499 return PyMapping_Size(o
);
1501 #define PyMapping_Length PyMapping_Size
1504 PyMapping_GetItemString(PyObject
*o
, char *key
)
1509 return null_error();
1511 okey
= PyString_FromString(key
);
1514 r
= PyObject_GetItem(o
, okey
);
1520 PyMapping_SetItemString(PyObject
*o
, char *key
, PyObject
*value
)
1530 okey
= PyString_FromString(key
);
1533 r
= PyObject_SetItem(o
, okey
, value
);
1539 PyMapping_HasKeyString(PyObject
*o
, char *key
)
1543 v
= PyMapping_GetItemString(o
, key
);
1553 PyMapping_HasKey(PyObject
*o
, PyObject
*key
)
1557 v
= PyObject_GetItem(o
, key
);
1566 /* Operations on callable objects */
1568 /* XXX PyCallable_Check() is in object.c */
1571 PyObject_CallObject(PyObject
*o
, PyObject
*a
)
1577 args
= PyTuple_New(0);
1582 r
= PyEval_CallObject(o
, args
);
1592 PyObject_CallFunction(PyObject
*callable
, char *format
, ...)
1595 PyObject
*args
, *retval
;
1596 va_start(va
, format
);
1598 if (callable
== NULL
) {
1600 return null_error();
1604 args
= Py_VaBuildValue(format
, va
);
1606 args
= PyTuple_New(0);
1613 if (!PyTuple_Check(args
)) {
1619 if (PyTuple_SetItem(a
, 0, args
) < 0)
1623 retval
= PyObject_CallObject(callable
, args
);
1631 PyObject_CallMethod(PyObject
*o
, char *name
, char *format
, ...)
1634 PyObject
*args
, *func
= 0, *retval
;
1635 va_start(va
, format
);
1637 if (o
== NULL
|| name
== NULL
) {
1639 return null_error();
1642 func
= PyObject_GetAttrString(o
, name
);
1645 PyErr_SetString(PyExc_AttributeError
, name
);
1649 if (!PyCallable_Check(func
)) {
1651 return type_error("call of non-callable attribute");
1654 if (format
&& *format
)
1655 args
= Py_VaBuildValue(format
, va
);
1657 args
= PyTuple_New(0);
1664 if (!PyTuple_Check(args
)) {
1670 if (PyTuple_SetItem(a
, 0, args
) < 0)
1675 retval
= PyObject_CallObject(func
, args
);
1684 /* isinstance(), issubclass() */
1687 abstract_issubclass(PyObject
*derived
, PyObject
*cls
, int first
)
1689 static PyObject
*__bases__
= NULL
;
1694 if (__bases__
== NULL
) {
1695 __bases__
= PyString_FromString("__bases__");
1696 if (__bases__
== NULL
)
1701 bases
= PyObject_GetAttr(cls
, __bases__
);
1702 if (bases
== NULL
|| !PyTuple_Check(bases
)) {
1704 PyErr_SetString(PyExc_TypeError
,
1705 "issubclass() arg 2 must be a class");
1714 bases
= PyObject_GetAttr(derived
, __bases__
);
1715 if (bases
== NULL
|| !PyTuple_Check(bases
)) {
1717 PyErr_SetString(PyExc_TypeError
,
1718 "issubclass() arg 1 must be a class");
1722 n
= PyTuple_GET_SIZE(bases
);
1723 for (i
= 0; i
< n
; i
++) {
1724 r
= abstract_issubclass(PyTuple_GET_ITEM(bases
, i
), cls
, 0);
1735 PyObject_IsInstance(PyObject
*inst
, PyObject
*cls
)
1738 static PyObject
*__class__
= NULL
;
1741 if (PyClass_Check(cls
)) {
1742 if (PyInstance_Check(inst
)) {
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
)
1757 icls
= PyObject_GetAttr(inst
, __class__
);
1759 retval
= abstract_issubclass(icls
, cls
, 1);
1762 !PyErr_ExceptionMatches(PyExc_TypeError
))
1772 PyErr_SetString(PyExc_TypeError
,
1773 "isinstance() arg 2 must be a class or type");
1779 PyObject_IsSubclass(PyObject
*derived
, PyObject
*cls
)
1783 if (!PyClass_Check(derived
) || !PyClass_Check(cls
)) {
1784 retval
= abstract_issubclass(derived
, cls
, 1);
1788 if (!(retval
= (derived
== cls
)))
1789 retval
= PyClass_IsSubclass(derived
, cls
);
1796 PyObject_GetIter(PyObject
*o
)
1798 PyTypeObject
*t
= o
->ob_type
;
1799 getiterfunc f
= NULL
;
1800 if (PyType_HasFeature(t
, Py_TPFLAGS_HAVE_ITER
))
1803 if (PySequence_Check(o
))
1804 return PySeqIter_New(o
);
1805 PyErr_SetString(PyExc_TypeError
, "iter() of non-sequence");
1809 PyObject
*res
= (*f
)(o
);
1810 if (res
!= NULL
&& !PyIter_Check(res
)) {
1811 PyErr_Format(PyExc_TypeError
,
1812 "iter() returned non-iterator "
1814 res
->ob_type
->tp_name
);
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()
1827 * Else return the next object. PyErr_Occurred() will be false.
1830 PyIter_Next(PyObject
*iter
)
1833 if (!PyIter_Check(iter
)) {
1834 PyErr_Format(PyExc_TypeError
,
1835 "'%.100s' object is not an iterator",
1836 iter
->ob_type
->tp_name
);
1839 result
= (*iter
->ob_type
->tp_iternext
)(iter
);
1840 if (result
== NULL
&&
1842 PyErr_ExceptionMatches(PyExc_StopIteration
))