@@ -810,7 +810,7 @@ static PyObject *
810
810
tee (PyObject * self , PyObject * args )
811
811
{
812
812
Py_ssize_t i , n = 2 ;
813
- PyObject * it , * iterable , * copyable , * result ;
813
+ PyObject * it , * iterable , * copyable , * copyfunc , * result ;
814
814
_Py_IDENTIFIER (__copy__ );
815
815
816
816
if (!PyArg_ParseTuple (args , "O|n" , & iterable , & n ))
@@ -829,25 +829,43 @@ tee(PyObject *self, PyObject *args)
829
829
Py_DECREF (result );
830
830
return NULL ;
831
831
}
832
- if (!_PyObject_HasAttrId (it , & PyId___copy__ )) {
832
+
833
+ copyfunc = _PyObject_GetAttrId (it , & PyId___copy__ );
834
+ if (copyfunc != NULL ) {
835
+ copyable = it ;
836
+ }
837
+ else if (!PyErr_ExceptionMatches (PyExc_AttributeError )) {
838
+ Py_DECREF (it );
839
+ Py_DECREF (result );
840
+ return NULL ;
841
+ }
842
+ else {
843
+ PyErr_Clear ();
833
844
copyable = tee_fromiterable (it );
834
845
Py_DECREF (it );
835
846
if (copyable == NULL ) {
836
847
Py_DECREF (result );
837
848
return NULL ;
838
849
}
839
- } else
840
- copyable = it ;
841
- PyTuple_SET_ITEM (result , 0 , copyable );
842
- for (i = 1 ; i < n ; i ++ ) {
850
+ copyfunc = _PyObject_GetAttrId (copyable , & PyId___copy__ );
851
+ if (copyfunc == NULL ) {
852
+ Py_DECREF (copyable );
853
+ Py_DECREF (result );
854
+ return NULL ;
855
+ }
856
+ }
843
857
844
- copyable = _PyObject_CallMethodId (copyable , & PyId___copy__ , NULL );
858
+ PyTuple_SET_ITEM (result , 0 , copyable );
859
+ for (i = 1 ; i < n ; i ++ ) {
860
+ copyable = _PyObject_CallNoArg (copyfunc );
845
861
if (copyable == NULL ) {
862
+ Py_DECREF (copyfunc );
846
863
Py_DECREF (result );
847
864
return NULL ;
848
865
}
849
866
PyTuple_SET_ITEM (result , i , copyable );
850
867
}
868
+ Py_DECREF (copyfunc );
851
869
return result ;
852
870
}
853
871
0 commit comments