Skip to content

Commit 79699bb

Browse files
committed
refactor
1 parent e19457e commit 79699bb

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

Modules/itertoolsmodule.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,11 +1881,12 @@ chain_traverse(PyObject *op, visitproc visit, void *arg)
18811881
}
18821882

18831883
static inline PyObject *
1884-
chain_next_lock_held(PyObject *op)
1884+
chain_next(PyObject *op)
18851885
{
18861886
chainobject *lz = chainobject_CAST(op);
1887-
PyObject *item;
1887+
PyObject *item = NULL;
18881888

1889+
Py_BEGIN_CRITICAL_SECTION(op);
18891890
/* lz->source is the iterator of iterables. If it's NULL, we've already
18901891
* consumed them all. lz->active is the current iterator. If it's NULL,
18911892
* we should grab a new one from lz->source. */
@@ -1894,41 +1895,38 @@ chain_next_lock_held(PyObject *op)
18941895
PyObject *iterable = PyIter_Next(lz->source);
18951896
if (iterable == NULL) {
18961897
Py_CLEAR(lz->source);
1897-
return NULL; /* no more input sources */
1898+
goto exit; /* no more input sources */
18981899
}
18991900
lz->active = PyObject_GetIter(iterable);
19001901
Py_DECREF(iterable);
19011902
if (lz->active == NULL) {
19021903
Py_CLEAR(lz->source);
1903-
return NULL; /* input not iterable */
1904+
goto exit; /* input not iterable */
19041905
}
19051906
}
19061907
item = (*Py_TYPE(lz->active)->tp_iternext)(lz->active);
1907-
if (item != NULL)
1908-
return item;
1908+
if (item != NULL) {
1909+
goto exit;
1910+
}
19091911
if (PyErr_Occurred()) {
19101912
if (PyErr_ExceptionMatches(PyExc_StopIteration))
19111913
PyErr_Clear();
1912-
else
1913-
return NULL; /* input raised an exception */
1914+
else {
1915+
goto exit; /* input raised an exception */
1916+
}
19141917
}
19151918
/* lz->active is consumed, try with the next iterable. */
19161919
Py_CLEAR(lz->active);
19171920
}
19181921
/* Everything had been consumed already. */
1919-
return NULL;
1920-
}
19211922

1922-
static PyObject *
1923-
chain_next(PyObject *op)
1924-
{
1925-
PyObject *result;
1926-
Py_BEGIN_CRITICAL_SECTION(op);
1927-
result = chain_next_lock_held(op);
1923+
exit:
19281924
Py_END_CRITICAL_SECTION()
1929-
return result;
1925+
1926+
return item;
19301927
}
19311928

1929+
19321930
PyDoc_STRVAR(chain_doc,
19331931
"chain(*iterables)\n\
19341932
--\n\

0 commit comments

Comments
 (0)