mirror of
https://github.com/python/cpython.git
synced 2024-12-04 07:15:09 +08:00
bpo-31572: Silence only AttributeError when get the __copy__ attribute in itertools.tee(). (#3724)
This commit is contained in:
parent
d4f8480dfe
commit
1707e4020f
@ -810,7 +810,7 @@ static PyObject *
|
|||||||
tee(PyObject *self, PyObject *args)
|
tee(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
Py_ssize_t i, n=2;
|
Py_ssize_t i, n=2;
|
||||||
PyObject *it, *iterable, *copyable, *result;
|
PyObject *it, *iterable, *copyable, *copyfunc, *result;
|
||||||
_Py_IDENTIFIER(__copy__);
|
_Py_IDENTIFIER(__copy__);
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O|n", &iterable, &n))
|
if (!PyArg_ParseTuple(args, "O|n", &iterable, &n))
|
||||||
@ -829,25 +829,43 @@ tee(PyObject *self, PyObject *args)
|
|||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (!_PyObject_HasAttrId(it, &PyId___copy__)) {
|
|
||||||
|
copyfunc = _PyObject_GetAttrId(it, &PyId___copy__);
|
||||||
|
if (copyfunc != NULL) {
|
||||||
|
copyable = it;
|
||||||
|
}
|
||||||
|
else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
||||||
|
Py_DECREF(it);
|
||||||
|
Py_DECREF(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_Clear();
|
||||||
copyable = tee_fromiterable(it);
|
copyable = tee_fromiterable(it);
|
||||||
Py_DECREF(it);
|
Py_DECREF(it);
|
||||||
if (copyable == NULL) {
|
if (copyable == NULL) {
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
} else
|
copyfunc = _PyObject_GetAttrId(copyable, &PyId___copy__);
|
||||||
copyable = it;
|
if (copyfunc == NULL) {
|
||||||
PyTuple_SET_ITEM(result, 0, copyable);
|
Py_DECREF(copyable);
|
||||||
for (i=1 ; i<n ; i++) {
|
Py_DECREF(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
copyable = _PyObject_CallMethodId(copyable, &PyId___copy__, NULL);
|
PyTuple_SET_ITEM(result, 0, copyable);
|
||||||
|
for (i = 1; i < n; i++) {
|
||||||
|
copyable = _PyObject_CallNoArg(copyfunc);
|
||||||
if (copyable == NULL) {
|
if (copyable == NULL) {
|
||||||
|
Py_DECREF(copyfunc);
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
PyTuple_SET_ITEM(result, i, copyable);
|
PyTuple_SET_ITEM(result, i, copyable);
|
||||||
}
|
}
|
||||||
|
Py_DECREF(copyfunc);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user