88#define NEW_STYLE_NUMBER (o ) PyType_HasFeature((o)->ob_type, \
99 Py_TPFLAGS_CHECKTYPES)
1010
11+ #define HASINDEX (o ) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX)
12+
1113/* Shorthands to return certain errors */
1214
1315static PyObject *
@@ -119,10 +121,9 @@ PyObject_GetItem(PyObject *o, PyObject *key)
119121 return m -> mp_subscript (o , key );
120122
121123 if (o -> ob_type -> tp_as_sequence ) {
122- if (PyInt_Check (key ))
123- return PySequence_GetItem (o , PyInt_AsLong (key ));
124- else if (PyLong_Check (key )) {
125- long key_value = PyLong_AsLong (key );
124+ PyNumberMethods * nb = key -> ob_type -> tp_as_number ;
125+ if (nb != NULL && HASINDEX (key ) && nb -> nb_index != NULL ) {
126+ Py_ssize_t key_value = nb -> nb_index (key );
126127 if (key_value == -1 && PyErr_Occurred ())
127128 return NULL ;
128129 return PySequence_GetItem (o , key_value );
@@ -148,10 +149,9 @@ PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
148149 return m -> mp_ass_subscript (o , key , value );
149150
150151 if (o -> ob_type -> tp_as_sequence ) {
151- if (PyInt_Check (key ))
152- return PySequence_SetItem (o , PyInt_AsLong (key ), value );
153- else if (PyLong_Check (key )) {
154- long key_value = PyLong_AsLong (key );
152+ PyNumberMethods * nb = key -> ob_type -> tp_as_number ;
153+ if (nb != NULL && HASINDEX (key ) && nb -> nb_index != NULL ) {
154+ Py_ssize_t key_value = nb -> nb_index (key );
155155 if (key_value == -1 && PyErr_Occurred ())
156156 return -1 ;
157157 return PySequence_SetItem (o , key_value , value );
@@ -180,10 +180,9 @@ PyObject_DelItem(PyObject *o, PyObject *key)
180180 return m -> mp_ass_subscript (o , key , (PyObject * )NULL );
181181
182182 if (o -> ob_type -> tp_as_sequence ) {
183- if (PyInt_Check (key ))
184- return PySequence_DelItem (o , PyInt_AsLong (key ));
185- else if (PyLong_Check (key )) {
186- long key_value = PyLong_AsLong (key );
183+ PyNumberMethods * nb = key -> ob_type -> tp_as_number ;
184+ if (nb != NULL && HASINDEX (key ) && nb -> nb_index != NULL ) {
185+ Py_ssize_t key_value = nb -> nb_index (key );
187186 if (key_value == -1 && PyErr_Occurred ())
188187 return -1 ;
189188 return PySequence_DelItem (o , key_value );
@@ -647,45 +646,18 @@ PyNumber_Add(PyObject *v, PyObject *w)
647646static PyObject *
648647sequence_repeat (ssizeargfunc repeatfunc , PyObject * seq , PyObject * n )
649648{
650- long count ;
651- if (PyInt_Check (n )) {
652- count = PyInt_AsLong (n );
653- }
654- else if (PyLong_Check (n )) {
655- count = PyLong_AsLong (n );
649+ Py_ssize_t count ;
650+ PyNumberMethods * nb = n -> ob_type -> tp_as_number ;
651+ if (nb != NULL && HASINDEX (n ) && nb -> nb_index != NULL ) {
652+ count = nb -> nb_index (n );
656653 if (count == -1 && PyErr_Occurred ())
657654 return NULL ;
658655 }
659656 else {
660657 return type_error (
661658 "can't multiply sequence by non-int" );
662659 }
663- #if LONG_MAX != INT_MAX
664- if (count > INT_MAX ) {
665- PyErr_SetString (PyExc_ValueError ,
666- "sequence repeat count too large" );
667- return NULL ;
668- }
669- else if (count < INT_MIN )
670- count = INT_MIN ;
671- /* XXX Why don't I either
672-
673- - set count to -1 whenever it's negative (after all,
674- sequence repeat usually treats negative numbers
675- as zero(); or
676-
677- - raise an exception when it's less than INT_MIN?
678-
679- I'm thinking about a hypothetical use case where some
680- sequence type might use a negative value as a flag of
681- some kind. In those cases I don't want to break the
682- code by mapping all negative values to -1. But I also
683- don't want to break e.g. []*(-sys.maxint), which is
684- perfectly safe, returning []. As a compromise, I do
685- map out-of-range negative values.
686- */
687- #endif
688- return (* repeatfunc )(seq , (int )count );
660+ return (* repeatfunc )(seq , count );
689661}
690662
691663PyObject *
@@ -960,6 +932,22 @@ int_from_string(const char *s, Py_ssize_t len)
960932 return x ;
961933}
962934
935+ /* Return a Py_ssize_t integer from the object item */
936+ Py_ssize_t
937+ PyNumber_Index (PyObject * item )
938+ {
939+ Py_ssize_t value = -1 ;
940+ PyNumberMethods * nb = item -> ob_type -> tp_as_number ;
941+ if (nb != NULL && HASINDEX (item ) && nb -> nb_index != NULL ) {
942+ value = nb -> nb_index (item );
943+ }
944+ else {
945+ PyErr_SetString (PyExc_IndexError ,
946+ "object cannot be interpreted as an index" );
947+ }
948+ return value ;
949+ }
950+
963951PyObject *
964952PyNumber_Int (PyObject * o )
965953{
0 commit comments