Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug in :c:func:`!_Unpickler_SetInputStream` where an exception could
end up being overwritten in case of failure.
41 changes: 28 additions & 13 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1678,25 +1678,40 @@ _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
{
/* Optional file methods */
if (_PyObject_LookupAttr(file, &_Py_ID(peek), &self->peek) < 0) {
return -1;
goto error;
}
if (_PyObject_LookupAttr(file, &_Py_ID(readinto), &self->readinto) < 0) {
return -1;
goto error;
}
(void)_PyObject_LookupAttr(file, &_Py_ID(read), &self->read);
(void)_PyObject_LookupAttr(file, &_Py_ID(readline), &self->readline);
if (!self->readline || !self->read) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"file must have 'read' and 'readline' attributes");
int no_read_attr = 0;
if (_PyObject_LookupAttr(file, &_Py_ID(read), &self->read) < 0) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
goto error;
}
Py_CLEAR(self->read);
Py_CLEAR(self->readinto);
Py_CLEAR(self->readline);
Py_CLEAR(self->peek);
return -1;
PyErr_Clear();
no_read_attr = 1;
}
int no_readline_attr = 0;
if (_PyObject_LookupAttr(file, &_Py_ID(readline), &self->readline) < 0) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
goto error;
}
PyErr_Clear();
no_readline_attr = 1;
}
if (no_read_attr || no_readline_attr) {
PyErr_SetString(PyExc_TypeError,
"file must have 'read' and 'readline' attributes");
goto error;
}
return 0;

error:
Py_CLEAR(self->read);
Py_CLEAR(self->readinto);
Py_CLEAR(self->readline);
Py_CLEAR(self->peek);
return -1;
}

/* Returns -1 (with an exception set) on failure, 0 on success. This may
Expand Down