Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/pickle.rst

    r2 r391  
    2525This documentation describes both the :mod:`pickle` module and the
    2626:mod:`cPickle` module.
     27
     28.. warning::
     29
     30   The :mod:`pickle` module is not intended to be secure against erroneous or
     31   maliciously constructed data.  Never unpickle data received from an untrusted
     32   or unauthenticated source.
    2733
    2834
     
    4854files.
    4955
    50 The :mod:`pickle` module differs from :mod:`marshal` several significant ways:
     56The :mod:`pickle` module differs from :mod:`marshal` in several significant ways:
    5157
    5258* The :mod:`pickle` module keeps track of the objects it has already serialized,
     
    7480  The :mod:`pickle` serialization format is guaranteed to be backwards compatible
    7581  across Python releases.
    76 
    77 .. warning::
    78 
    79    The :mod:`pickle` module is not intended to be secure against erroneous or
    80    maliciously constructed data.  Never unpickle data received from an untrusted
    81    or unauthenticated source.
    8282
    8383Note that serialization is a more primitive notion than persistence; although
     
    353353* classes that are defined at the top level of a module
    354354
    355 * instances of such classes whose :attr:`__dict__` or :meth:`__setstate__` is
    356   picklable  (see section :ref:`pickle-protocol` for details)
     355* instances of such classes whose :attr:`~object.__dict__` or the result of
     356  calling :meth:`__getstate__` is picklable  (see section :ref:`pickle-protocol`
     357  for details).
    357358
    358359Attempts to pickle unpicklable objects will raise the :exc:`PicklingError`
     
    365366Note that functions (built-in and user-defined) are pickled by "fully qualified"
    366367name reference, not by value.  This means that only the function name is
    367 pickled, along with the name of module the function is defined in.  Neither the
    368 function's code, nor any of its function attributes are pickled.  Thus the
     368pickled, along with the name of the module the function is defined in.  Neither
     369the function's code, nor any of its function attributes are pickled.  Thus the
    369370defining module must be importable in the unpickling environment, and the module
    370371must contain the named object, otherwise an exception will be raised. [#]_
     
    443444   pickled as the contents for the instance, instead of the contents of the
    444445   instance's dictionary.  If there is no :meth:`__getstate__` method, the
    445    instance's :attr:`__dict__` is pickled.
    446 
    447 .. method:: object.__setstate__()
     446   instance's :attr:`~object.__dict__` is pickled.
     447
     448.. method:: object.__setstate__(state)
    448449
    449450   Upon unpickling, if the class also defines the method :meth:`__setstate__`,
     
    511512     :meth:`__setstate__` method as described in section :ref:`pickle-inst`.  If
    512513     the object has no :meth:`__setstate__` method, then, as above, the value
    513      must be a dictionary and it will be added to the object's :attr:`__dict__`.
     514     must be a dictionary and it will be added to the object's
     515     :attr:`~object.__dict__`.
    514516
    515517   * Optionally, an iterator (and not a sequence) yielding successive list
     
    569571
    570572To define external persistent id resolution, you need to set the
    571 :attr:`persistent_id` attribute of the pickler object and the
    572 :attr:`persistent_load` attribute of the unpickler object.
     573:attr:`~Pickler.persistent_id` attribute of the pickler object and the
     574:attr:`~Unpickler.persistent_load` attribute of the unpickler object.
    573575
    574576To pickle objects that have an external persistent id, the pickler must have a
    575 custom :func:`persistent_id` method that takes an object as an argument and
    576 returns either ``None`` or the persistent id for that object.  When ``None`` is
    577 returned, the pickler simply pickles the object as normal.  When a persistent id
    578 string is returned, the pickler will pickle that string, along with a marker so
    579 that the unpickler will recognize the string as a persistent id.
     577custom :func:`~Pickler.persistent_id` method that takes an object as an
     578argument and returns either ``None`` or the persistent id for that object.
     579When ``None`` is returned, the pickler simply pickles the object as normal.
     580When a persistent id string is returned, the pickler will pickle that string,
     581along with a marker so that the unpickler will recognize the string as a
     582persistent id.
    580583
    581584To unpickle external objects, the unpickler must have a custom
    582 :func:`persistent_load` function that takes a persistent id string and returns
    583 the referenced object.
     585:func:`~Unpickler.persistent_load` function that takes a persistent id string
     586and returns the referenced object.
    584587
    585588Here's a silly example that *might* shed more light::
     
    631634   print j
    632635
    633 In the :mod:`cPickle` module, the unpickler's :attr:`persistent_load` attribute
    634 can also be set to a Python list, in which case, when the unpickler reaches a
    635 persistent id, the persistent id string will simply be appended to this list.
    636 This functionality exists so that a pickle data stream can be "sniffed" for
    637 object references without actually instantiating all the objects in a pickle.
    638 [#]_  Setting :attr:`persistent_load` to a list is usually used in conjunction
    639 with the :meth:`noload` method on the Unpickler.
     636In the :mod:`cPickle` module, the unpickler's :attr:`~Unpickler.persistent_load`
     637attribute can also be set to a Python list, in which case, when the unpickler
     638reaches a persistent id, the persistent id string will simply be appended to
     639this list.  This functionality exists so that a pickle data stream can be
     640"sniffed" for object references without actually instantiating all the objects
     641in a pickle.
     642[#]_  Setting :attr:`~Unpickler.persistent_load` to a list is usually used in
     643conjunction with the :meth:`~Unpickler.noload` method on the Unpickler.
    640644
    641645.. BAW: Both pickle and cPickle support something called inst_persistent_id()
     
    675679
    676680Things are a little cleaner with :mod:`cPickle`, but not by much. To control
    677 what gets unpickled, you can set the unpickler's :attr:`find_global` attribute
    678 to a function or ``None``.  If it is ``None`` then any attempts to unpickle
    679 instances will raise an :exc:`UnpicklingError`.  If it is a function, then it
    680 should accept a module name and a class name, and return the corresponding class
    681 object.  It is responsible for looking up the class and performing any necessary
    682 imports, and it may raise an error to prevent instances of the class from being
    683 unpickled.
     681what gets unpickled, you can set the unpickler's :attr:`~Unpickler.find_global`
     682attribute to a function or ``None``.  If it is ``None`` then any attempts to
     683unpickle instances will raise an :exc:`UnpicklingError`.  If it is a function,
     684then it should accept a module name and a class name, and return the
     685corresponding class object.  It is responsible for looking up the class and
     686performing any necessary imports, and it may raise an error to prevent
     687instances of the class from being unpickled.
    684688
    685689The moral of the story is that you should be really careful about the source of
     
    732736Here's a larger example that shows how to modify pickling behavior for a class.
    733737The :class:`TextReader` class opens a text file, and returns the line number and
    734 line contents each time its :meth:`readline` method is called. If a
     738line contents each time its :meth:`!readline` method is called. If a
    735739:class:`TextReader` instance is pickled, all attributes *except* the file object
    736740member are saved. When the instance is unpickled, the file is reopened, and
Note: See TracChangeset for help on using the changeset viewer.