8000 [3.12] gh-105375: Improve error handling in _Unpickler_SetInputStream… · python/cpython@56877e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 56877e4

Browse files
[3.12] gh-105375: Improve error handling in _Unpickler_SetInputStream() (#105667) (#105720)
Prevent exceptions from possibly being overwritten in case of multiple failures. (cherry picked from commit 217589d)
1 parent 017b959 commit 56877e4

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug in :c:func:`!_Unpickler_SetInputStream` where an exception could
2+
end up being overwritten in case of failure.

Modules/_pickle.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,25 +1694,30 @@ _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
16941694
{
16951695
/* Optional file methods */
16961696
if (_PyObject_LookupAttr(file, &_Py_ID(peek), &self->peek) < 0) {
1697-
return -1;
1697+
goto error;
16981698
}
16991699
if (_PyObject_LookupAttr(file, &_Py_ID(readinto), &self->readinto) < 0) {
1700-
return -1;
1700+
goto error;
1701+
}
1702+
if (_PyObject_LookupAttr(file, &_Py_ID(read), &self->read) < 0) {
1703+
goto error;
1704+
}
1705+
if (_PyObject_LookupAttr(file, &_Py_ID(readline), &self->readline) < 0) {
1706+
goto error;
17011707
}
1702-
(void)_PyObject_LookupAttr(file, &_Py_ID(read), &self->read);
1703-
(void)_PyObject_LookupAttr(file, &_Py_ID(readline), &self->readline);
17041708
if (!self->readline || !self->read) {
1705-
if (!PyErr_Occurred()) {
1706-
PyErr_SetString(PyExc_TypeError,
1707-
"file must have 'read' and 'readline' attributes");
1708-
}
1709-
Py_CLEAR(self->read);
1710-
Py_CLEAR(self->readinto);
1711-
Py_CLEAR(self->readline);
1712-
Py_CLEAR(self->peek);
1713-
return -1;
1709+
PyErr_SetString(PyExc_TypeError,
1710+
"file must have 'read' and 'readline' attributes");
1711+
goto error;
17141712
}
17151713
return 0;
1714+
1715+
error:
1716+
Py_CLEAR(self->read);
1717+
Py_CLEAR(self->readinto);
1718+
Py_CLEAR(self->readline);
1719+
Py_CLEAR(self->peek);
1720+
return -1;
17161721
}
17171722

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

0 commit comments

Comments
 (0)
0