@@ -2102,7 +2102,7 @@ kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
21022102 int i = 0 ;
21032103 PyObject * otimeout = NULL ;
21042104 PyObject * ch = NULL ;
2105- PyObject * it = NULL , * ei = NULL ;
2105+ PyObject * seq = NULL , * ei = NULL ;
21062106 PyObject * result = NULL ;
21072107 struct kevent * evl = NULL ;
21082108 struct kevent * chl = NULL ;
@@ -2148,37 +2148,34 @@ kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
21482148 }
21492149
21502150 if (ch != NULL && ch != Py_None ) {
2151- it = PyObject_GetIter (ch );
2152- if (it == NULL ) {
2153- PyErr_SetString (PyExc_TypeError ,
2154- "changelist is not iterable" );
2151+ seq = PySequence_Fast (ch , "changelist is not iterable" );
2152+ if (seq == NULL ) {
21552153 return NULL ;
21562154 }
2157- nchanges = PyObject_Size (ch );
2158- if (nchanges < 0 ) {
2155+ if (PySequence_Fast_GET_SIZE (seq ) > INT_MAX ) {
2156+ PyErr_SetString (PyExc_OverflowError ,
2157+ "changelist is too long" );
21592158 goto error ;
21602159 }
2160+ nchanges = (int )PySequence_Fast_GET_SIZE (seq );
21612161
21622162 chl = PyMem_New (struct kevent , nchanges );
21632163 if (chl == NULL ) {
21642164 PyErr_NoMemory ();
21652165 goto error ;
21662166 }
2167- i = 0 ;
2168- while (( ei = PyIter_Next ( it )) != NULL ) {
2167+ for ( i = 0 ; i < nchanges ; ++ i ) {
2168+ ei = PySequence_Fast_GET_ITEM ( seq , i );
21692169 if (!kqueue_event_Check (ei )) {
2170- Py_DECREF (ei );
21712170 PyErr_SetString (PyExc_TypeError ,
21722171 "changelist must be an iterable of "
21732172 "select.kevent objects" );
21742173 goto error ;
2175- } else {
2176- chl [i ++ ] = ((kqueue_event_Object * )ei )-> e ;
21772174 }
2178- Py_DECREF ( ei );
2175+ chl [ i ] = (( kqueue_event_Object * ) ei )-> e ;
21792176 }
2177+ Py_CLEAR (seq );
21802178 }
2181- Py_CLEAR (it );
21822179
21832180 /* event list */
21842181 if (nevents ) {
@@ -2246,15 +2243,15 @@ kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
22462243 PyMem_Free (chl );
22472244 PyMem_Free (evl );
22482245 Py_XDECREF (result );
2249- Py_XDECREF (it );
2246+ Py_XDECREF (seq );
22502247 return NULL ;
22512248}
22522249
22532250PyDoc_STRVAR (kqueue_queue_control_doc ,
22542251"control(changelist, max_events[, timeout=None]) -> eventlist\n\
22552252\n\
22562253Calls the kernel kevent function.\n\
2257- - changelist must be a list of kevent objects describing the changes\n\
2254+ - changelist must be an iterable of kevent objects describing the changes\n\
22582255 to be made to the kernel's watch list or None.\n\
22592256- max_events lets you specify the maximum number of events that the\n\
22602257 kernel will return.\n\
0 commit comments