Changeset 391 for python/trunk/Doc/library/pickle.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/pickle.rst
r2 r391 25 25 This documentation describes both the :mod:`pickle` module and the 26 26 :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. 27 33 28 34 … … 48 54 files. 49 55 50 The :mod:`pickle` module differs from :mod:`marshal` several significant ways:56 The :mod:`pickle` module differs from :mod:`marshal` in several significant ways: 51 57 52 58 * The :mod:`pickle` module keeps track of the objects it has already serialized, … … 74 80 The :mod:`pickle` serialization format is guaranteed to be backwards compatible 75 81 across Python releases. 76 77 .. warning::78 79 The :mod:`pickle` module is not intended to be secure against erroneous or80 maliciously constructed data. Never unpickle data received from an untrusted81 or unauthenticated source.82 82 83 83 Note that serialization is a more primitive notion than persistence; although … … 353 353 * classes that are defined at the top level of a module 354 354 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). 357 358 358 359 Attempts to pickle unpicklable objects will raise the :exc:`PicklingError` … … 365 366 Note that functions (built-in and user-defined) are pickled by "fully qualified" 366 367 name 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 the368 function's code, nor any of its function attributes are pickled. Thus the368 pickled, along with the name of the module the function is defined in. Neither 369 the function's code, nor any of its function attributes are pickled. Thus the 369 370 defining module must be importable in the unpickling environment, and the module 370 371 must contain the named object, otherwise an exception will be raised. [#]_ … … 443 444 pickled as the contents for the instance, instead of the contents of the 444 445 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) 448 449 449 450 Upon unpickling, if the class also defines the method :meth:`__setstate__`, … … 511 512 :meth:`__setstate__` method as described in section :ref:`pickle-inst`. If 512 513 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__`. 514 516 515 517 * Optionally, an iterator (and not a sequence) yielding successive list … … 569 571 570 572 To define external persistent id resolution, you need to set the 571 :attr:` persistent_id` attribute of the pickler object and the572 :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. 573 575 574 576 To 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. 577 custom :func:`~Pickler.persistent_id` method that takes an object as an 578 argument and returns either ``None`` or the persistent id for that object. 579 When ``None`` is returned, the pickler simply pickles the object as normal. 580 When a persistent id string is returned, the pickler will pickle that string, 581 along with a marker so that the unpickler will recognize the string as a 582 persistent id. 580 583 581 584 To unpickle external objects, the unpickler must have a custom 582 :func:` persistent_load` function that takes a persistent id string and returns583 the referenced object.585 :func:`~Unpickler.persistent_load` function that takes a persistent id string 586 and returns the referenced object. 584 587 585 588 Here's a silly example that *might* shed more light:: … … 631 634 print j 632 635 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. 636 In the :mod:`cPickle` module, the unpickler's :attr:`~Unpickler.persistent_load` 637 attribute can also be set to a Python list, in which case, when the unpickler 638 reaches a persistent id, the persistent id string will simply be appended to 639 this list. This functionality exists so that a pickle data stream can be 640 "sniffed" for object references without actually instantiating all the objects 641 in a pickle. 642 [#]_ Setting :attr:`~Unpickler.persistent_load` to a list is usually used in 643 conjunction with the :meth:`~Unpickler.noload` method on the Unpickler. 640 644 641 645 .. BAW: Both pickle and cPickle support something called inst_persistent_id() … … 675 679 676 680 Things 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` attribute678 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.681 what gets unpickled, you can set the unpickler's :attr:`~Unpickler.find_global` 682 attribute to a function or ``None``. If it is ``None`` then any attempts to 683 unpickle instances will raise an :exc:`UnpicklingError`. If it is a function, 684 then it should accept a module name and a class name, and return the 685 corresponding class object. It is responsible for looking up the class and 686 performing any necessary imports, and it may raise an error to prevent 687 instances of the class from being unpickled. 684 688 685 689 The moral of the story is that you should be really careful about the source of … … 732 736 Here's a larger example that shows how to modify pickling behavior for a class. 733 737 The :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 a738 line contents each time its :meth:`!readline` method is called. If a 735 739 :class:`TextReader` instance is pickled, all attributes *except* the file object 736 740 member are saved. When the instance is unpickled, the file is reopened, and
Note:
See TracChangeset
for help on using the changeset viewer.