44
55typedef struct {
66 PyObject_HEAD
7- long en_index ; /* current index of enumeration */
7+ Py_ssize_t en_index ; /* current index of enumeration */
88 PyObject * en_sit ; /* secondary iterator of enumeration */
99 PyObject * en_result ; /* result tuple */
10- PyObject * en_longindex ; /* index for sequences >= LONG_MAX */
10+ PyObject * en_longindex ; /* index for sequences >= PY_SSIZE_T_MAX */
1111} enumobject ;
1212
1313static PyObject *
@@ -25,14 +25,21 @@ enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2525 en = (enumobject * )type -> tp_alloc (type , 0 );
2626 if (en == NULL )
2727 return NULL ;
28- if (start ) {
28+ if (start != NULL ) {
2929 start = PyNumber_Index (start );
3030 if (start == NULL ) {
3131 Py_DECREF (en );
3232 return NULL ;
3333 }
34- en -> en_index = LONG_MAX ;
35- en -> en_longindex = start ;
34+ en -> en_index = PyLong_AsSsize_t (start );
35+ if (en -> en_index == -1 && PyErr_Occurred ()) {
36+ PyErr_Clear ();
37+ en -> en_index = PY_SSIZE_T_MAX ;
38+ en -> en_longindex = start ;
39+ } else {
40+ en -> en_longindex = NULL ;
41+ Py_DECREF (start );
42+ }
3643 } else {
3744 en -> en_index = 0 ;
3845 en -> en_longindex = NULL ;
@@ -78,7 +85,7 @@ enum_next_long(enumobject *en, PyObject* next_item)
7885 PyObject * stepped_up ;
7986
8087 if (en -> en_longindex == NULL ) {
81- en -> en_longindex = PyLong_FromLong ( LONG_MAX );
88+ en -> en_longindex = PyLong_FromSsize_t ( PY_SSIZE_T_MAX );
8289 if (en -> en_longindex == NULL )
8390 return NULL ;
8491 }
@@ -123,10 +130,10 @@ enum_next(enumobject *en)
123130 if (next_item == NULL )
124131 return NULL ;
125132
126- if (en -> en_index == LONG_MAX )
133+ if (en -> en_index == PY_SSIZE_T_MAX )
127134 return enum_next_long (en , next_item );
128135
129- next_index = PyLong_FromLong (en -> en_index );
136+ next_index = PyLong_FromSsize_t (en -> en_index );
130137 if (next_index == NULL ) {
131138 Py_DECREF (next_item );
132139 return NULL ;
0 commit comments