@@ -147,7 +147,7 @@ to read the pickle produced.
147
147
earlier versions of Python.
148
148
149
149
* Protocol version 2 was introduced in Python 2.3. It provides much more
150
- efficient pickling of :term: `new-style class ` \e s . Refer to :pep: `307 ` for
150
+ efficient pickling of :term: `new-style classes <new-style class> ` . Refer to :pep: `307 ` for
151
151
information about improvements brought by protocol 2.
152
152
153
153
* Protocol version 3 was added in Python 3.0. It has explicit support for
@@ -261,7 +261,7 @@ process more convenient:
261
261
protocol argument is needed. Bytes past the pickled representation
262
262
of the object are ignored.
263
263
264
- Arguments *file *, * fix_imports *, *encoding *, *errors *, *strict * and *buffers *
264
+ Arguments *fix_imports *, *encoding *, *errors *, *strict * and *buffers *
265
265
have the same meaning as in the :class: `Unpickler ` constructor.
266
266
267
267
.. versionchanged :: 3.8
@@ -368,7 +368,7 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,
368
368
369
369
.. versionadded :: 3.3
370
370
371
- .. method :: reducer_override(self, obj)
371
+ .. method :: reducer_override(obj)
372
372
373
373
Special reducer that can be defined in :class: `Pickler ` subclasses. This
374
374
method has priority over any reducer in the :attr: `dispatch_table `. It
@@ -494,20 +494,18 @@ What can be pickled and unpickled?
494
494
495
495
The following types can be pickled:
496
496
497
- * ``None ``, ``True ``, and ``False ``
498
-
499
- * integers, floating point numbers, complex numbers
497
+ * ``None ``, ``True ``, and ``False ``;
500
498
501
- * strings, bytes, bytearrays
499
+ * integers, floating-point numbers, complex numbers;
502
500
503
- * tuples, lists, sets, and dictionaries containing only picklable objects
501
+ * strings, bytes, bytearrays;
504
502
505
- * functions defined at the top level of a module (using :keyword: `def `, not
506
- :keyword: `lambda `)
503
+ * tuples, lists, sets, and dictionaries containing only picklable objects;
507
504
508
- * built-in functions defined at the top level of a module
505
+ * functions (built-in and user-defined) defined at the top level of a module
506
+ (using :keyword: `def `, not :keyword: `lambda `);
509
507
510
- * classes that are defined at the top level of a module
508
+ * classes defined at the top level of a module;
511
509
512
510
* instances of such classes whose :attr: `~object.__dict__ ` or the result of
513
511
calling :meth: `__getstate__ ` is picklable (see section :ref: `pickle-inst ` for
@@ -520,14 +518,14 @@ structure may exceed the maximum recursion depth, a :exc:`RecursionError` will b
520
518
raised in this case. You can carefully raise this limit with
521
519
:func: `sys.setrecursionlimit `.
522
520
523
- Note that functions (built-in and user-defined) are pickled by " fully qualified"
524
- name reference , not by value. [# ]_ This means that only the function name is
521
+ Note that functions (built-in and user-defined) are pickled by fully qualified
522
+ name, not by value. [# ]_ This means that only the function name is
525
523
pickled, along with the name of the module the function is defined in. Neither
526
524
the function's code, nor any of its function attributes are pickled. Thus the
527
525
defining module must be importable in the unpickling environment, and the module
528
526
must contain the named object, otherwise an exception will be raised. [# ]_
529
527
530
- Similarly, classes are pickled by named reference , so the same restrictions in
528
+ Similarly, classes are pickled by fully qualified name , so the same restrictions in
531
529
the unpickling environment apply. Note that none of the class's code or data is
532
530
pickled, so in the following example the class attribute ``attr `` is not
533
531
restored in the unpickling environment::
@@ -537,7 +535,7 @@ restored in the unpickling environment::
537
535
538
536
picklestring = pickle.dumps(Foo)
539
537
540
- These restrictions are why picklable functions and classes must be defined in
538
+ These restrictions are why picklable functions and classes must be defined at
541
539
the top level of a module.
542
540
543
541
Similarly, when class instances are pickled, their class's code and data are not
@@ -569,7 +567,7 @@ implementation of this behaviour::
569
567
def save(obj):
570
568
return (obj.__class__, obj.__dict__)
571
569
572
- def load (cls, attributes):
570
+ def restore (cls, attributes):
573
571
obj = cls.__new__(cls)
574
572
obj.__dict__.update(attributes)
575
573
return obj
@@ -788,14 +786,15 @@ the code ::
788
786
f = io.BytesIO()
789
787
p = MyPickler(f)
790
788
791
- does the same, but all instances of ``MyPickler `` will by default
792
- share the same dispatch table. The equivalent code using the
793
- :mod: `copyreg ` module is ::
789
+ does the same but all instances of ``MyPickler `` will by default
790
+ share the private dispatch table. On the other hand, the code ::
794
791
795
792
copyreg.pickle(SomeClass, reduce_SomeClass)
796
793
f = io.BytesIO()
797
794
p = pickle.Pickler(f)
798
795
796
+ modifies the global dispatch table shared by all users of the :mod: `copyreg ` module.
797
+
799
798
.. _pickle-state :
800
799
801
800
Handling Stateful Objects
@@ -1098,7 +1097,7 @@ Here is an example of an unpickler allowing only few safe classes from the
1098
1097
"""Helper function analogous to pickle.loads()."""
1099
1098
return RestrictedUnpickler(io.BytesIO(s)).load()
1100
1099
1101
- A sample usage of our unpickler working has intended::
1100
+ A sample usage of our unpickler working as intended::
1102
1101
1103
1102
>>> restricted_loads(pickle.dumps([1, 2, range(15)]))
1104
1103
[1, 2, range(0, 15)]
@@ -1142,7 +1141,7 @@ For the simplest code, use the :func:`dump` and :func:`load` functions. ::
1142
1141
1143
1142
# An arbitrary collection of objects supported by pickle.
1144
1143
data = {
1145
- 'a': [1, 2.0, 3, 4+6j ],
1144
+ 'a': [1, 2.0, 3+4j ],
1146
1145
'b': ("character string", b"byte string"),
1147
1146
'c': {None, True, False}
1148
1147
}
@@ -1198,6 +1197,6 @@ The following example reads the resulting pickled data. ::
1198
1197
operations.
1199
1198
1200
1199
.. [# ] The limitation on alphanumeric characters is due to the fact
1201
- the persistent IDs, in protocol 0, are delimited by the newline
1200
+ that persistent IDs in protocol 0 are delimited by the newline
1202
1201
character. Therefore if any kind of newline characters occurs in
1203
- persistent IDs, the resulting pickle will become unreadable.
1202
+ persistent IDs, the resulting pickled data will become unreadable.
0 commit comments