Skip to content

Commit 9f37872

Browse files
authored
bpo-39681: Fix C pickle regression with minimal file-like objects (#18592)
Fix a regression where the C pickle module wouldn't allow unpickling from a file-like object that doesn't expose a readinto() method.
1 parent b76518d commit 9f37872

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

Lib/test/pickletester.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ def tell(self):
7373
raise io.UnsupportedOperation
7474

7575

76+
class MinimalIO(object):
77+
"""
78+
A file-like object that doesn't support readinto().
79+
"""
80+
def __init__(self, *args):
81+
self._bio = io.BytesIO(*args)
82+
self.getvalue = self._bio.getvalue
83+
self.read = self._bio.read
84+
self.readline = self._bio.readline
85+
self.write = self._bio.write
86+
87+
7688
# We can't very well test the extension registry without putting known stuff
7789
# in it, but we have to be careful to restore its original state. Code
7890
# should do this:
@@ -3363,7 +3375,7 @@ def test_reusing_unpickler_objects(self):
33633375
f.seek(0)
33643376
self.assertEqual(unpickler.load(), data2)
33653377

3366-
def _check_multiple_unpicklings(self, ioclass):
3378+
def _check_multiple_unpicklings(self, ioclass, *, seekable=True):
33673379
for proto in protocols:
33683380
with self.subTest(proto=proto):
33693381
data1 = [(x, str(x)) for x in range(2000)] + [b"abcde", len]
@@ -3376,18 +3388,23 @@ def _check_multiple_unpicklings(self, ioclass):
33763388
f = ioclass(pickled * N)
33773389
unpickler = self.unpickler_class(f)
33783390
for i in range(N):
3379-
if f.seekable():
3391+
if seekable:
33803392
pos = f.tell()
33813393
self.assertEqual(unpickler.load(), data1)
3382-
if f.seekable():
3394+
if seekable:
33833395
self.assertEqual(f.tell(), pos + len(pickled))
33843396
self.assertRaises(EOFError, unpickler.load)
33853397

33863398
def test_multiple_unpicklings_seekable(self):
33873399
self._check_multiple_unpicklings(io.BytesIO)
33883400

33893401
def test_multiple_unpicklings_unseekable(self):
3390-
self._check_multiple_unpicklings(UnseekableIO)
3402+
self._check_multiple_unpicklings(UnseekableIO, seekable=False)
3403+
3404+
def test_multiple_unpicklings_minimal(self):
3405+
# File-like object that doesn't support peek() and readinto()
3406+
# (bpo-39681)
3407+
self._check_multiple_unpicklings(MinimalIO, seekable=False)
33913408

33923409
def test_unpickling_buffering_readline(self):
33933410
# Issue #12687: the unpickler's buffering logic could fail with
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a regression where the C pickle module wouldn't allow unpickling from a
2+
file-like object that doesn't expose a readinto() method.

Modules/_pickle.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,13 +1373,42 @@ _Unpickler_ReadInto(UnpicklerObject *self, char *buf, Py_ssize_t n)
13731373
}
13741374

13751375
/* Read from file */
1376-
if (!self->readinto) {
1376+
if (!self->read) {
1377+
/* We're unpickling memory, this means the input is truncated */
13771378
return bad_readline();
13781379
}
13791380
if (_Unpickler_SkipConsumed(self) < 0) {
13801381
return -1;
13811382
}
13821383

1384+
if (!self->readinto) {
1385+
/* readinto() not supported on file-like object, fall back to read()
1386+
* and copy into destination buffer (bpo-39681) */
1387+
PyObject* len = PyLong_FromSsize_t(n);
1388+
if (len == NULL) {
1389+
return -1;
1390+
}
1391+
PyObject* data = _Pickle_FastCall(self->read, len);
1392+
if (data == NULL) {
1393+
return -1;
1394+
}
1395+
if (!PyBytes_Check(data)) {
1396+
PyErr_Format(PyExc_ValueError,
1397+
"read() returned non-bytes object (%R)",
1398+
Py_TYPE(data));
1399+
Py_DECREF(data);
1400+
return -1;
1401+
}
1402+
Py_ssize_t read_size = PyBytes_GET_SIZE(data);
1403+
if (read_size < n) {
1404+
Py_DECREF(data);
1405+
return bad_readline();
1406+
}
1407+
memcpy(buf, PyBytes_AS_STRING(data), n);
1408+
Py_DECREF(data);
1409+
return n;
1410+
}
1411+
13831412
/* Call readinto() into user buffer */
13841413
PyObject *buf_obj = PyMemoryView_FromMemory(buf, n, PyBUF_WRITE);
13851414
if (buf_obj == NULL) {
@@ -1608,17 +1637,19 @@ _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
16081637
_Py_IDENTIFIER(readinto);
16091638
_Py_IDENTIFIER(readline);
16101639

1640+
/* Optional file methods */
16111641
if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
16121642
return -1;
16131643
}
1644+
if (_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto) < 0) {
1645+
return -1;
1646+
}
16141647
(void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
1615-
(void)_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto);
16161648
(void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
1617-
if (!self->readline || !self->readinto || !self->read) {
1649+
if (!self->readline || !self->read) {
16181650
if (!PyErr_Occurred()) {
16191651
PyErr_SetString(PyExc_TypeError,
1620-
"file must have 'read', 'readinto' and "
1621-
"'readline' attributes");
1652+
"file must have 'read' and 'readline' attributes");
16221653
}
16231654
Py_CLEAR(self->read);
16241655
Py_CLEAR(self->readinto);

0 commit comments

Comments
 (0)