Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6c7a85

Browse files
committed
Issue #12687: Fix a possible buffering bug when unpickling text mode (protocol 0, mostly) pickles.
1 parent 817495a commit f6c7a85

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

Lib/test/pickletester.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,19 @@ def test_multiple_unpicklings_seekable(self):
14181418
def test_multiple_unpicklings_unseekable(self):
14191419
self._check_multiple_unpicklings(UnseekableIO)
14201420

1421+
def test_unpickling_buffering_readline(self):
1422+
# Issue #12687: the unpickler's buffering logic could fail with
1423+
# text mode opcodes.
1424+
data = list(range(10))
1425+
for proto in protocols:
1426+
for buf_size in range(1, 11):
1427+
f = io.BufferedRandom(io.BytesIO(), buffer_size=buf_size)
1428+
pickler = self.pickler_class(f, protocol=proto)
1429+
pickler.dump(data)
1430+
f.seek(0)
1431+
unpickler = self.unpickler_class(f)
1432+
self.assertEqual(unpickler.load(), data)
1433+
14211434

14221435
if __name__ == "__main__":
14231436
# Print some stuff that can be used to rewrite DATA{0,1,2}

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Core and Builtins
4141
Library
4242
-------
4343

44+
- Issue #12687: Fix a possible buffering bug when unpickling text mode
45+
(protocol 0, mostly) pickles.
46+
4447
- Issue #10087: Fix the html output format of the calendar module.
4548

4649
- Issue #12540: Prevent zombie IDLE processes on Windows due to changes

Modules/_pickle.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,8 @@ _Unpickler_Readline(UnpicklerObject *self, char **result)
10341034
num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
10351035
if (num_read < 0)
10361036
return -1;
1037-
*result = self->input_buffer;
10381037
self->next_read_idx = num_read;
1039-
return num_read;
1038+
return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
10401039
}
10411040

10421041
/* If we get here, we've run off the end of the input string. Return the

0 commit comments

Comments
 (0)