Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3136,6 +3136,18 @@ def __init__(self): pass
self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
self.assertRaises(pickle.UnpicklingError, BadUnpickler().load)

def test_load_read_exception(self):
class F:
@property
def read(self):
1/0
def readinto(self):
1/0
def readline(self):
1/0

self.assertRaises(ZeroDivisionError, pickle.load, F())

def check_dumps_loads_oob_buffers(self, dumps, loads):
# No need to do the full gamut of tests here, just enough to
# check that dumps() and loads() redirect their arguments
Expand Down
33 changes: 19 additions & 14 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1609,22 +1609,27 @@ _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
return -1;
}
(void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
(void)_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto);
(void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
if (!self->readline || !self->readinto || !self->read) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"file must have 'read', 'readinto' and "
"'readline' attributes");
}
Py_CLEAR(self->read);
Py_CLEAR(self->readinto);
Py_CLEAR(self->readline);
Py_CLEAR(self->peek);
return -1;
if (_PyObject_LookupAttrId(file, &PyId_read, &self->read) <= 0) {
goto error;
}
if (_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto) <= 0) {
goto error;
}
if (_PyObject_LookupAttrId(file, &PyId_readline, &self->readline) <= 0) {
goto error;
}
return 0;
error:
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"file must have 'read', 'readinto' and "
"'readline' attributes");
}
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