@@ -3802,6 +3802,75 @@ static PyType_Spec ziplongest_spec = {
3802
3802
};
3803
3803
3804
3804
3805
+ /* ilen function *************************************************************/
3806
+
3807
+ /*[clinic input]
3808
+ itertools.ilen
3809
+
3810
+ iterable: object
3811
+ /
3812
+
3813
+ Equivalent to len(list(iterable)).
3814
+ [clinic start generated code]*/
3815
+
3816
+ static PyObject *
3817
+ itertools_ilen (PyObject * module , PyObject * iterable )
3818
+ /*[clinic end generated code: output=99a1c852a6d09dd4 input=cf51dec933b693da]*/
3819
+ {
3820
+ PyObject * it , * item ;
3821
+ PyObject * (* iternext )(PyObject * );
3822
+ PyObject * long_cnt = NULL ;
3823
+ PyObject * new_long_cnt , * temp ;
3824
+ Py_ssize_t cnt ;
3825
+
3826
+ it = PyObject_GetIter (iterable );
3827
+ if (it == NULL ) {
3828
+ return NULL ;
3829
+ }
3830
+
3831
+ cnt = 0 ;
3832
+ iternext = * Py_TYPE (it )-> tp_iternext ;
3833
+ while ((item = iternext (it )) != NULL ) {
3834
+ Py_DECREF (item );
3835
+ cnt ++ ;
3836
+ if (cnt == PY_SSIZE_T_MAX ) {
3837
+ if (long_cnt == NULL ) {
3838
+ long_cnt = PyLong_FromSsize_t (cnt );
3839
+ }
3840
+ else {
3841
+ new_long_cnt = PyLong_FromSsize_t (cnt );
3842
+ temp = PyNumber_Add (long_cnt , new_long_cnt );
3843
+ Py_SETREF (long_cnt , temp );
3844
+ Py_DECREF (new_long_cnt );
3845
+ }
3846
+ cnt = 0 ;
3847
+ }
3848
+ }
3849
+
3850
+ if (PyErr_Occurred ()) {
3851
+ if (PyErr_ExceptionMatches (PyExc_StopIteration ))
3852
+ PyErr_Clear ();
3853
+ else {
3854
+ Py_DECREF (it );
3855
+ Py_XDECREF (long_cnt );
3856
+ return NULL ;
3857
+ }
3858
+ }
3859
+ Py_DECREF (it );
3860
+
3861
+ if (long_cnt == NULL ) {
3862
+ return PyLong_FromSsize_t (cnt );
3863
+ }
3864
+ if (cnt != 0 ) {
3865
+ new_long_cnt = PyLong_FromSsize_t (cnt );
3866
+ temp = PyNumber_Add (long_cnt , new_long_cnt );
3867
+ Py_SETREF (long_cnt , temp );
3868
+ Py_DECREF (new_long_cnt );
3869
+ }
3870
+ return long_cnt ;
3871
+ }
3872
+
3873
+
3805
3874
/* module level code ********************************************************/
3806
3875
3807
3876
PyDoc_STRVAR (module_doc ,
@@ -3951,6 +4020,7 @@ static struct PyModuleDef_Slot itertoolsmodule_slots[] = {
3951
4020
3952
4021
static PyMethodDef module_methods [] = {
3953
4022
ITERTOOLS_TEE_METHODDEF
4023
+ ITERTOOLS_ILEN_METHODDEF
3954
4024
{NULL , NULL } /* sentinel */
3955
4025
};
3956
4026
0 commit comments