Skip to content

Commit 4e4fad4

Browse files
committed
Call tee_copy() directly
1 parent 8e2315c commit 4e4fad4

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

Modules/itertoolsmodule.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n)
10361036
/*[clinic end generated code: output=1c64519cd859c2f0 input=c99a1472c425d66d]*/
10371037
{
10381038
Py_ssize_t i;
1039-
PyObject *it, *copyable, *copyfunc, *result;
1039+
PyObject *it, *to, *result;
10401040

10411041
if (n < 0) {
10421042
PyErr_SetString(PyExc_ValueError, "n must be >= 0");
@@ -1054,30 +1054,22 @@ itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n)
10541054
}
10551055

10561056
itertools_state *state = get_module_state(module);
1057-
copyable = tee_fromiterable(state, it);
1057+
to = tee_fromiterable(state, it);
10581058
Py_DECREF(it);
1059-
if (copyable == NULL) {
1060-
Py_DECREF(result);
1061-
return NULL;
1062-
}
1063-
copyfunc = PyObject_GetAttr(copyable, &_Py_ID(__copy__));
1064-
if (copyfunc == NULL) {
1065-
Py_DECREF(copyable);
1059+
if (to == NULL) {
10661060
Py_DECREF(result);
10671061
return NULL;
10681062
}
10691063

1070-
PyTuple_SET_ITEM(result, 0, copyable);
1064+
PyTuple_SET_ITEM(result, 0, to);
10711065
for (i = 1; i < n; i++) {
1072-
copyable = _PyObject_CallNoArgs(copyfunc);
1073-
if (copyable == NULL) {
1074-
Py_DECREF(copyfunc);
1066+
to = tee_copy((teeobject *)to, NULL);
1067+
if (to == NULL) {
10751068
Py_DECREF(result);
10761069
return NULL;
10771070
}
1078-
PyTuple_SET_ITEM(result, i, copyable);
1071+
PyTuple_SET_ITEM(result, i, to);
10791072
}
1080-
Py_DECREF(copyfunc);
10811073
return result;
10821074
}
10831075

0 commit comments

Comments
 (0)