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

Skip to content

Commit be149d0

Browse files
committed
remove references of cPickle in the pickle docs (uhh. unlabeled footnotes)
1 parent 4fe93b2 commit be149d0

1 file changed

Lines changed: 23 additions & 117 deletions

File tree

Doc/library/pickle.rst

Lines changed: 23 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,15 @@ process whereby a Python object hierarchy is converted into a byte stream, and
2020
"unpickling" is the inverse operation, whereby a byte stream is converted back
2121
into an object hierarchy. Pickling (and unpickling) is alternatively known as
2222
"serialization", "marshalling," [#]_ or "flattening", however, to avoid
23-
confusion, the terms used here are "pickling" and "unpickling".
24-
25-
This documentation describes both the :mod:`pickle` module and the
26-
:mod:`cPickle` module.
23+
confusion, the terms used here are "pickling" and "unpickling"..
2724

2825

2926
Relationship to other Python modules
3027
------------------------------------
3128

32-
The :mod:`pickle` module has an optimized cousin called the :mod:`cPickle`
33-
module. As its name implies, :mod:`cPickle` is written in C, so it can be up to
34-
1000 times faster than :mod:`pickle`. However it does not support subclassing
35-
of the :func:`Pickler` and :func:`Unpickler` classes, because in :mod:`cPickle`
36-
these are functions, not classes. Most applications have no need for this
37-
functionality, and can benefit from the improved performance of :mod:`cPickle`.
38-
Other than that, the interfaces of the two modules are nearly identical; the
39-
common interface is described in this manual and differences are pointed out
40-
where necessary. In the following discussions, we use the term "pickle" to
41-
collectively describe the :mod:`pickle` and :mod:`cPickle` modules.
42-
43-
The data streams the two modules produce are guaranteed to be interchangeable.
29+
The :mod:`pickle` module has an transparent optimizer (:mod:`_pickle`) written
30+
in C. It is used whenever available. Otherwise the pure Python implementation is
31+
used.
4432

4533
Python has a more primitive serialization module called :mod:`marshal`, but in
4634
general :mod:`pickle` should always be the preferred way to serialize Python
@@ -229,7 +217,7 @@ The :mod:`pickle` module also defines three exceptions:
229217
necessarily limited to) :exc:`AttributeError`, :exc:`EOFError`,
230218
:exc:`ImportError`, and :exc:`IndexError`.
231219

232-
The :mod:`pickle` module also exports two callables [#]_, :class:`Pickler` and
220+
The :mod:`pickle` module also exports two callables, :class:`Pickler` and
233221
:class:`Unpickler`:
234222

235223

@@ -305,11 +293,6 @@ instance. If the same object is pickled by multiple :meth:`dump` calls, the
305293
ids" that may be referenced in a pickle data stream. See section
306294
:ref:`pickle-protocol` below for more details.
307295

308-
**Note:** the :meth:`noload` method is currently only available on
309-
:class:`Unpickler` objects created with the :mod:`cPickle` module.
310-
:mod:`pickle` module :class:`Unpickler`\ s do not have the :meth:`noload`
311-
method.
312-
313296

314297
What can be pickled and unpickled?
315298
----------------------------------
@@ -535,7 +518,7 @@ notion of a reference to an object outside the pickled data stream. Such
535518
objects are referenced by a "persistent id", which is just an arbitrary string
536519
of printable ASCII characters. The resolution of such names is not defined by
537520
the :mod:`pickle` module; it will delegate this resolution to user defined
538-
functions on the pickler and unpickler. [#]_
521+
functions on the pickler and unpickler.
539522

540523
To define external persistent id resolution, you need to set the
541524
:attr:`persistent_id` attribute of the pickler object and the
@@ -600,15 +583,8 @@ Here's a silly example that *might* shed more light::
600583
j = up.load()
601584
print(j)
602585

603-
In the :mod:`cPickle` module, the unpickler's :attr:`persistent_load` attribute
604-
can also be set to a Python list, in which case, when the unpickler reaches a
605-
persistent id, the persistent id string will simply be appended to this list.
606-
This functionality exists so that a pickle data stream can be "sniffed" for
607-
object references without actually instantiating all the objects in a pickle.
608-
[#]_ Setting :attr:`persistent_load` to a list is usually used in conjunction
609-
with the :meth:`noload` method on the Unpickler.
610586

611-
.. BAW: Both pickle and cPickle support something called inst_persistent_id()
587+
.. BAW: pickle supports something called inst_persistent_id()
612588
which appears to give unknown types a second shot at producing a persistent
613589
id. Since Jim Fulton can't remember why it was added or what it's for, I'm
614590
leaving it undocumented.
@@ -625,32 +601,22 @@ Subclassing Unpicklers
625601

626602
By default, unpickling will import any class that it finds in the pickle data.
627603
You can control exactly what gets unpickled and what gets called by customizing
628-
your unpickler. Unfortunately, exactly how you do this is different depending
629-
on whether you're using :mod:`pickle` or :mod:`cPickle`. [#]_
630-
631-
In the :mod:`pickle` module, you need to derive a subclass from
632-
:class:`Unpickler`, overriding the :meth:`load_global` method.
633-
:meth:`load_global` should read two lines from the pickle data stream where the
634-
first line will the name of the module containing the class and the second line
635-
will be the name of the instance's class. It then looks up the class, possibly
636-
importing the module and digging out the attribute, then it appends what it
637-
finds to the unpickler's stack. Later on, this class will be assigned to the
638-
:attr:`__class__` attribute of an empty class, as a way of magically creating an
639-
instance without calling its class's :meth:`__init__`. Your job (should you
640-
choose to accept it), would be to have :meth:`load_global` push onto the
641-
unpickler's stack, a known safe version of any class you deem safe to unpickle.
642-
It is up to you to produce such a class. Or you could raise an error if you
643-
want to disallow all unpickling of instances. If this sounds like a hack,
644-
you're right. Refer to the source code to make this work.
645-
646-
Things are a little cleaner with :mod:`cPickle`, but not by much. To control
647-
what gets unpickled, you can set the unpickler's :attr:`find_global` attribute
648-
to a function or ``None``. If it is ``None`` then any attempts to unpickle
649-
instances will raise an :exc:`UnpicklingError`. If it is a function, then it
650-
should accept a module name and a class name, and return the corresponding class
651-
object. It is responsible for looking up the class and performing any necessary
652-
imports, and it may raise an error to prevent instances of the class from being
653-
unpickled.
604+
your unpickler.
605+
606+
You need to derive a subclass from :class:`Unpickler`, overriding the
607+
:meth:`load_global` method. :meth:`load_global` should read two lines from the
608+
pickle data stream where the first line will the name of the module containing
609+
the class and the second line will be the name of the instance's class. It then
610+
looks up the class, possibly importing the module and digging out the attribute,
611+
then it appends what it finds to the unpickler's stack. Later on, this class
612+
will be assigned to the :attr:`__class__` attribute of an empty class, as a way
613+
of magically creating an instance without calling its class's
614+
:meth:`__init__`. Your job (should you choose to accept it), would be to have
615+
:meth:`load_global` push onto the unpickler's stack, a known safe version of any
616+
class you deem safe to unpickle. It is up to you to produce such a class. Or
617+
you could raise an error if you want to disallow all unpickling of instances.
618+
If this sounds like a hack, you're right. Refer to the source code to make this
619+
work.
654620

655621
The moral of the story is that you should be really careful about the source of
656622
the strings your application unpickles.
@@ -777,48 +743,10 @@ the same process or a new process. ::
777743
High-performance serialization of built-in types.
778744

779745

780-
:mod:`cPickle` --- A faster :mod:`pickle`
781-
=========================================
782-
783-
.. module:: cPickle
784-
:synopsis: Faster version of pickle, but not subclassable.
785-
.. moduleauthor:: Jim Fulton <[email protected]>
786-
.. sectionauthor:: Fred L. Drake, Jr. <[email protected]>
787-
788-
789-
.. index:: module: pickle
790-
791-
The :mod:`cPickle` module supports serialization and de-serialization of Python
792-
objects, providing an interface and functionality nearly identical to the
793-
:mod:`pickle` module. There are several differences, the most important being
794-
performance and subclassability.
795-
796-
First, :mod:`cPickle` can be up to 1000 times faster than :mod:`pickle` because
797-
the former is implemented in C. Second, in the :mod:`cPickle` module the
798-
callables :func:`Pickler` and :func:`Unpickler` are functions, not classes.
799-
This means that you cannot use them to derive custom pickling and unpickling
800-
subclasses. Most applications have no need for this functionality and should
801-
benefit from the greatly improved performance of the :mod:`cPickle` module.
802-
803-
The pickle data stream produced by :mod:`pickle` and :mod:`cPickle` are
804-
identical, so it is possible to use :mod:`pickle` and :mod:`cPickle`
805-
interchangeably with existing pickles. [#]_
806-
807-
There are additional minor differences in API between :mod:`cPickle` and
808-
:mod:`pickle`, however for most applications, they are interchangeable. More
809-
documentation is provided in the :mod:`pickle` module documentation, which
810-
includes a list of the documented differences.
811-
812746
.. rubric:: Footnotes
813747

814748
.. [#] Don't confuse this with the :mod:`marshal` module
815749
816-
.. [#] In the :mod:`pickle` module these callables are classes, which you could
817-
subclass to customize the behavior. However, in the :mod:`cPickle` module these
818-
callables are factory functions and so cannot be subclassed. One common reason
819-
to subclass is to control what objects can actually be unpickled. See section
820-
:ref:`pickle-sub` for more details.
821-
822750
.. [#] *Warning*: this is intended for pickling multiple objects without intervening
823751
modifications to the objects or their parts. If you modify an object and then
824752
pickle it again using the same :class:`Pickler` instance, the object is not
@@ -834,25 +762,3 @@ includes a list of the documented differences.
834762
835763
.. [#] This protocol is also used by the shallow and deep copying operations defined in
836764
the :mod:`copy` module.
837-
838-
.. [#] The actual mechanism for associating these user defined functions is slightly
839-
different for :mod:`pickle` and :mod:`cPickle`. The description given here
840-
works the same for both implementations. Users of the :mod:`pickle` module
841-
could also use subclassing to effect the same results, overriding the
842-
:meth:`persistent_id` and :meth:`persistent_load` methods in the derived
843-
classes.
844-
845-
.. [#] We'll leave you with the image of Guido and Jim sitting around sniffing pickles
846-
in their living rooms.
847-
848-
.. [#] A word of caution: the mechanisms described here use internal attributes and
849-
methods, which are subject to change in future versions of Python. We intend to
850-
someday provide a common interface for controlling this behavior, which will
851-
work in either :mod:`pickle` or :mod:`cPickle`.
852-
853-
.. [#] Since the pickle data format is actually a tiny stack-oriented programming
854-
language, and some freedom is taken in the encodings of certain objects, it is
855-
possible that the two modules produce different data streams for the same input
856-
objects. However it is guaranteed that they will always be able to read each
857-
other's data streams.
858-

0 commit comments

Comments
 (0)