diff options
author | Erlend E. Aasland <erlend.aasland@protonmail.com> | 2023-06-13 09:15:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-13 09:15:19 (GMT) |
commit | 56877e4560821ad00cabe6253448c1c938cfed59 (patch) | |
tree | f47e31e0234e30b4208f2acc72b2dfd08421115d /Modules | |
parent | 017b9595d4060521f93804d5dd555b6cd6585578 (diff) | |
download | cpython-56877e4560821ad00cabe6253448c1c938cfed59.zip cpython-56877e4560821ad00cabe6253448c1c938cfed59.tar.gz cpython-56877e4560821ad00cabe6253448c1c938cfed59.tar.bz2 |
[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 217589d4f3246d67c6ef0eb0be2b1c33987cf260)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_pickle.c | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 688bccb..d2f6d71 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1694,25 +1694,30 @@ _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; + } + if (_PyObject_LookupAttr(file, &_Py_ID(read), &self->read) < 0) { + goto error; + } + if (_PyObject_LookupAttr(file, &_Py_ID(readline), &self->readline) < 0) { + 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"); - } - Py_CLEAR(self->read); - Py_CLEAR(self->readinto); - Py_CLEAR(self->readline); - Py_CLEAR(self->peek); - return -1; + 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 |