Skip to content

Commit 533d284

Browse files
committed
Address review comments
1 parent 2a5a316 commit 533d284

File tree

6 files changed

+65
-30
lines changed

6 files changed

+65
-30
lines changed

Doc/library/pickle.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ process more convenient:
201201
This is equivalent to ``Pickler(file, protocol).dump(obj)``.
202202

203203
Arguments *file*, *protocol*, *fix_imports* and *buffer_callback* have
204-
the same meaning as in :class:`Pickler`.
204+
the same meaning as in the :class:`Pickler` constructor.
205205

206206
.. versionchanged:: 3.8
207207
The *buffer_callback* argument was added.
@@ -212,7 +212,7 @@ process more convenient:
212212
instead of writing it to a file.
213213

214214
Arguments *protocol*, *fix_imports* and *buffer_callback* have the same
215-
meaning as in :class:`Pickler`.
215+
meaning as in the :class:`Pickler` constructor.
216216

217217
.. versionchanged:: 3.8
218218
The *buffer_callback* argument was added.
@@ -228,7 +228,7 @@ process more convenient:
228228
representation are ignored.
229229

230230
Arguments *file*, *fix_imports*, *encoding*, *errors*, *strict* and *buffers*
231-
have the same meaning as in :class:`Unpickler`.
231+
have the same meaning as in the :class:`Unpickler` constructor.
232232

233233
.. versionchanged:: 3.8
234234
The *buffers* argument was added.
@@ -243,7 +243,7 @@ process more convenient:
243243
representation are ignored.
244244

245245
Arguments *file*, *fix_imports*, *encoding*, *errors*, *strict* and *buffers*
246-
have the same meaning as in :class:`Unpickler`.
246+
have the same meaning as in the :class:`Unpickler` constructor.
247247

248248
.. versionchanged:: 3.8
249249
The *buffers* argument was added.
@@ -300,8 +300,11 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
300300

301301
If *buffer_callback* is not None, then it can be called any number
302302
of times with a buffer view. If the callback returns a false value
303-
(such as None), the given buffer is out-of-band; otherwise the
304-
buffer is serialized in-band, i.e. inside the pickle stream.
303+
(such as None), the given buffer is :ref:`out-of-band <pickle-oob>`;
304+
otherwise the buffer is serialized in-band, i.e. inside the pickle stream.
305+
306+
It is an error if *buffer_callback* is not None and *protocol* is
307+
None or smaller than 5.
305308

306309
.. versionchanged:: 3.8
307310
The *buffer_callback* argument was added.
@@ -401,8 +404,8 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
401404

402405
If *buffers* is not None, it should be an iterable of buffer-enabled
403406
objects that is consumed each time the pickle stream references
404-
an out-of-band buffer view. Such buffers have been given in order
405-
to the *buffer_callback* of a Pickler object.
407+
an :ref:`out-of-band <pickle-oob>` buffer view. Such buffers have been
408+
given in order to the *buffer_callback* of a Pickler object.
406409

407410
.. versionchanged:: 3.8
408411
The *buffers* argument was added.
@@ -438,7 +441,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
438441

439442
.. class:: PickleBuffer(buffer)
440443

441-
A wrapper for a potentially out-of-band buffer. *buffer* must be a
444+
A wrapper for a buffer representing picklable data. *buffer* must be a
442445
:ref:`buffer-providing <bufferobjects>` object, such as a
443446
:term:`bytes-like object` or a N-dimensional array.
444447

@@ -954,7 +957,7 @@ reconstructors of the objects whose pickling produced the original
954957
:class:`PickleBuffer` objects.
955958

956959
Between the sending side and the receiving side, the communications system
957-
is free to implement its own transfer mechanisms for out-of-band buffers.
960+
is free to implement its own transfer mechanism for out-of-band buffers.
958961
Potential optimizations include the use of shared memory or datatype-dependent
959962
compression.
960963

Lib/pickle.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,15 @@ def __init__(self, file, protocol=None, *, fix_imports=True,
423423
with Python 2.
424424
425425
If *buffer_callback* is None (the default), buffer views are
426-
serialized into *file* as part of the pickle stream. It is
427-
an error if *buffer_callback* is not None and *protocol* is
428-
None or smaller than 5.
426+
serialized into *file* as part of the pickle stream.
427+
428+
If *buffer_callback* is not None, then it can be called any number
429+
of times with a buffer view. If the callback returns a false value
430+
(such as None), the given buffer is out-of-band; otherwise the
431+
buffer is serialized in-band, i.e. inside the pickle stream.
432+
433+
It is an error if *buffer_callback* is not None and *protocol*
434+
is None or smaller than 5.
429435
"""
430436
if protocol is None:
431437
protocol = DEFAULT_PROTOCOL
@@ -819,7 +825,7 @@ def save_picklebuffer(self, obj):
819825
in_band = bool(self._buffer_callback(obj))
820826
if in_band:
821827
# Write data in-band
822-
# XXX we could avoid a copy here
828+
# XXX The C implementation avoids a copy here
823829
if m.readonly:
824830
self.save_bytes(m.tobytes())
825831
else:

Modules/_pickle.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,10 +1371,12 @@ _Unpickler_ReadInto(UnpicklerObject *self, char *buf, Py_ssize_t n)
13711371
}
13721372

13731373
/* Read from file */
1374-
if (!self->readinto)
1374+
if (!self->readinto) {
13751375
return bad_readline();
1376-
if (_Unpickler_SkipConsumed(self) < 0)
1376+
}
1377+
if (_Unpickler_SkipConsumed(self) < 0) {
13771378
return -1;
1379+
}
13781380

13791381
/* Call readinto() into user buffer */
13801382
PyObject *buf_obj = PyMemoryView_FromMemory(buf, n, PyBUF_WRITE);
@@ -1395,8 +1397,9 @@ _Unpickler_ReadInto(UnpicklerObject *self, char *buf, Py_ssize_t n)
13951397
}
13961398
return -1;
13971399
}
1398-
if (read_size < n)
1400+
if (read_size < n) {
13991401
return bad_readline();
1402+
}
14001403
return n;
14011404
}
14021405

@@ -2349,7 +2352,8 @@ _save_bytes_data(PicklerObject *self, PyObject *obj, const char *data,
23492352
}
23502353
else {
23512354
PyErr_SetString(PyExc_OverflowError,
2352-
"cannot serialize a bytes object larger than 4 GiB");
2355+
"serializing a bytes object larger than 4 GiB "
2356+
"requires pickle protocol 4 or higher");
23532357
return -1;
23542358
}
23552359

@@ -2655,7 +2659,8 @@ write_unicode_binary(PicklerObject *self, PyObject *obj)
26552659
}
26562660
else {
26572661
PyErr_SetString(PyExc_OverflowError,
2658-
"cannot serialize a string larger than 4GiB");
2662+
"serializing a string larger than 4 GiB "
2663+
"requires pickle protocol 4 or higher");
26592664
Py_XDECREF(encoded);
26602665
return -1;
26612666
}
@@ -4655,17 +4660,24 @@ If *fix_imports* is True and protocol is less than 3, pickle will try
46554660
to map the new Python 3 names to the old module names used in Python
46564661
2, so that the pickle data stream is readable with Python 2.
46574662
4658-
If *buffer_callback* is None (the default), buffer views are serialized
4659-
into *file* as part of the pickle stream. It is an error if
4660-
*buffer_callback* is not None and *protocol* is None or smaller than 5.
4663+
If *buffer_callback* is None (the default), buffer views are
4664+
serialized into *file* as part of the pickle stream.
4665+
4666+
If *buffer_callback* is not None, then it can be called any number
4667+
of times with a buffer view. If the callback returns a false value
4668+
(such as None), the given buffer is out-of-band; otherwise the
4669+
buffer is serialized in-band, i.e. inside the pickle stream.
4670+
4671+
It is an error if *buffer_callback* is not None and *protocol*
4672+
is None or smaller than 5.
46614673
46624674
[clinic start generated code]*/
46634675

46644676
static int
46654677
_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
46664678
PyObject *protocol, int fix_imports,
46674679
PyObject *buffer_callback)
4668-
/*[clinic end generated code: output=0abedc50590d259b input=10e101f062872d3c]*/
4680+
/*[clinic end generated code: output=0abedc50590d259b input=9a43a1c50ab91652]*/
46694681
{
46704682
_Py_IDENTIFIER(persistent_id);
46714683
_Py_IDENTIFIER(dispatch_table);
@@ -5490,8 +5502,9 @@ load_counted_bytearray(UnpicklerObject *self)
54905502
Py_ssize_t size;
54915503
char *s;
54925504

5493-
if (_Unpickler_Read(self, &s, 8) < 0)
5505+
if (_Unpickler_Read(self, &s, 8) < 0) {
54945506
return -1;
5507+
}
54955508

54965509
size = calc_binsize(s, 8);
54975510
if (size < 0) {
@@ -5502,8 +5515,9 @@ load_counted_bytearray(UnpicklerObject *self)
55025515
}
55035516

55045517
bytearray = PyByteArray_FromStringAndSize(NULL, size);
5505-
if (bytearray == NULL)
5518+
if (bytearray == NULL) {
55065519
return -1;
5520+
}
55075521
if (_Unpickler_ReadInto(self, PyByteArray_AS_STRING(bytearray), size) < 0) {
55085522
Py_DECREF(bytearray);
55095523
return -1;
@@ -5541,8 +5555,9 @@ static int
55415555
load_readonly_buffer(UnpicklerObject *self)
55425556
{
55435557
Py_ssize_t len = Py_SIZE(self->stack);
5544-
if (len <= self->stack->fence)
5558+
if (len <= self->stack->fence) {
55455559
return Pdata_stack_underflow(self->stack);
5560+
}
55465561

55475562
PyObject *obj = self->stack->data[len - 1];
55485563
PyObject *view = PyMemoryView_FromObject(obj);

Modules/clinic/_pickle.c.h

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
<ClInclude Include="..\Include\osmodule.h" />
198198
<ClInclude Include="..\Include\parsetok.h" />
199199
<ClInclude Include="..\Include\patchlevel.h" />
200+
<ClInclude Include="..\Include\picklebufobject.h" />
200201
<ClInclude Include="..\Include\pyhash.h" />
201202
<ClInclude Include="..\Include\py_curses.h" />
202203
<ClInclude Include="..\Include\pyarena.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@
285285
<ClInclude Include="..\Include\patchlevel.h">
286286
<Filter>Include</Filter>
287287
</ClInclude>
288+
<ClInclude Include="..\Include\picklebufobject.h">
289+
<Filter>Include</Filter>
290+
</ClInclude>
288291
<ClInclude Include="..\Include\py_curses.h">
289292
<Filter>Include</Filter>
290293
</ClInclude>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy