~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Doc/library/pickle.rst

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
396
396
The pickle protocol
397
397
-------------------
398
398
 
 
399
.. currentmodule:: None
 
400
 
399
401
This section describes the "pickling protocol" that defines the interface
400
402
between the pickler/unpickler and the objects that are being serialized.  This
401
403
protocol provides a standard way for you to define, customize, and control how
410
412
Pickling and unpickling normal class instances
411
413
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
412
414
 
413
 
.. index::
414
 
   single: __getinitargs__() (copy protocol)
415
 
   single: __init__() (instance constructor)
416
 
 
417
 
When a pickled class instance is unpickled, its :meth:`__init__` method is
418
 
normally *not* invoked.  If it is desirable that the :meth:`__init__` method be
419
 
called on unpickling, an old-style class can define a method
420
 
:meth:`__getinitargs__`, which should return a *tuple* containing the arguments
421
 
to be passed to the class constructor (:meth:`__init__` for example).  The
422
 
:meth:`__getinitargs__` method is called at pickle time; the tuple it returns is
423
 
incorporated in the pickle for the instance.
424
 
 
425
 
.. index:: single: __getnewargs__() (copy protocol)
426
 
 
427
 
New-style types can provide a :meth:`__getnewargs__` method that is used for
428
 
protocol 2.  Implementing this method is needed if the type establishes some
429
 
internal invariants when the instance is created, or if the memory allocation is
430
 
affected by the values passed to the :meth:`__new__` method for the type (as it
431
 
is for tuples and strings).  Instances of a :term:`new-style class` :class:`C`
432
 
are created using ::
433
 
 
434
 
   obj = C.__new__(C, *args)
435
 
 
436
 
 
437
 
where *args* is the result of calling :meth:`__getnewargs__` on the original
438
 
object; if there is no :meth:`__getnewargs__`, an empty tuple is assumed.
439
 
 
440
 
.. index::
441
 
   single: __getstate__() (copy protocol)
442
 
   single: __setstate__() (copy protocol)
443
 
   single: __dict__ (instance attribute)
444
 
 
445
 
Classes can further influence how their instances are pickled; if the class
446
 
defines the method :meth:`__getstate__`, it is called and the return state is
447
 
pickled as the contents for the instance, instead of the contents of the
448
 
instance's dictionary.  If there is no :meth:`__getstate__` method, the
449
 
instance's :attr:`__dict__` is pickled.
450
 
 
451
 
Upon unpickling, if the class also defines the method :meth:`__setstate__`, it
452
 
is called with the unpickled state. [#]_  If there is no :meth:`__setstate__`
453
 
method, the pickled state must be a dictionary and its items are assigned to the
454
 
new instance's dictionary.  If a class defines both :meth:`__getstate__` and
455
 
:meth:`__setstate__`, the state object needn't be a dictionary and these methods
456
 
can do what they want. [#]_
457
 
 
458
 
.. warning::
459
 
 
460
 
   For :term:`new-style class`\es, if :meth:`__getstate__` returns a false
461
 
   value, the :meth:`__setstate__` method will not be called.
 
415
.. method:: object.__getinitargs__()
 
416
   
 
417
   When a pickled class instance is unpickled, its :meth:`__init__` method is
 
418
   normally *not* invoked.  If it is desirable that the :meth:`__init__` method
 
419
   be called on unpickling, an old-style class can define a method
 
420
   :meth:`__getinitargs__`, which should return a *tuple* containing the
 
421
   arguments to be passed to the class constructor (:meth:`__init__` for
 
422
   example).  The :meth:`__getinitargs__` method is called at pickle time; the
 
423
   tuple it returns is incorporated in the pickle for the instance.
 
424
 
 
425
.. method:: object.__getnewargs__()
 
426
 
 
427
   New-style types can provide a :meth:`__getnewargs__` method that is used for
 
428
   protocol 2.  Implementing this method is needed if the type establishes some
 
429
   internal invariants when the instance is created, or if the memory allocation
 
430
   is affected by the values passed to the :meth:`__new__` method for the type
 
431
   (as it is for tuples and strings).  Instances of a :term:`new-style class`
 
432
   ``C`` are created using ::
 
433
    
 
434
      obj = C.__new__(C, *args)
 
435
    
 
436
   where *args* is the result of calling :meth:`__getnewargs__` on the original
 
437
   object; if there is no :meth:`__getnewargs__`, an empty tuple is assumed.
 
438
 
 
439
.. method:: object.__getstate__()
 
440
   
 
441
   Classes can further influence how their instances are pickled; if the class
 
442
   defines the method :meth:`__getstate__`, it is called and the return state is
 
443
   pickled as the contents for the instance, instead of the contents of the
 
444
   instance's dictionary.  If there is no :meth:`__getstate__` method, the
 
445
   instance's :attr:`__dict__` is pickled.
 
446
 
 
447
.. method:: object.__setstate__() 
 
448
   
 
449
   Upon unpickling, if the class also defines the method :meth:`__setstate__`,
 
450
   it is called with the unpickled state. [#]_ If there is no
 
451
   :meth:`__setstate__` method, the pickled state must be a dictionary and its
 
452
   items are assigned to the new instance's dictionary.  If a class defines both
 
453
   :meth:`__getstate__` and :meth:`__setstate__`, the state object needn't be a
 
454
   dictionary and these methods can do what they want. [#]_
 
455
    
 
456
   .. warning::
 
457
    
 
458
      For :term:`new-style class`\es, if :meth:`__getstate__` returns a false
 
459
      value, the :meth:`__setstate__` method will not be called.
462
460
 
463
461
 
464
462
Pickling and unpickling extension types
465
463
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
466
464
 
467
 
.. index::
468
 
   single: __reduce__() (pickle protocol)
469
 
   single: __reduce_ex__() (pickle protocol)
470
 
   single: __safe_for_unpickling__ (pickle protocol)
471
 
 
472
 
When the :class:`Pickler` encounters an object of a type it knows nothing about
473
 
--- such as an extension type --- it looks in two places for a hint of how to
474
 
pickle it.  One alternative is for the object to implement a :meth:`__reduce__`
475
 
method.  If provided, at pickling time :meth:`__reduce__` will be called with no
476
 
arguments, and it must return either a string or a tuple.
477
 
 
478
 
If a string is returned, it names a global variable whose contents are pickled
479
 
as normal.  The string returned by :meth:`__reduce__` should be the object's
480
 
local name relative to its module; the pickle module searches the module
481
 
namespace to determine the object's module.
482
 
 
483
 
When a tuple is returned, it must be between two and five elements long.
484
 
Optional elements can either be omitted, or ``None`` can be provided as their
485
 
value.  The contents of this tuple are pickled as normal and used to
486
 
reconstruct the object at unpickling time.  The semantics of each element are:
487
 
 
488
 
* A callable object that will be called to create the initial version of the
489
 
  object.  The next element of the tuple will provide arguments for this callable,
490
 
  and later elements provide additional state information that will subsequently
491
 
  be used to fully reconstruct the pickled data.
492
 
 
493
 
  In the unpickling environment this object must be either a class, a callable
494
 
  registered as a "safe constructor" (see below), or it must have an attribute
495
 
  :attr:`__safe_for_unpickling__` with a true value. Otherwise, an
496
 
  :exc:`UnpicklingError` will be raised in the unpickling environment.  Note that
497
 
  as usual, the callable itself is pickled by name.
498
 
 
499
 
* A tuple of arguments for the callable object.
500
 
 
501
 
  .. versionchanged:: 2.5
502
 
     Formerly, this argument could also be ``None``.
503
 
 
504
 
* Optionally, the object's state, which will be passed to the object's
505
 
  :meth:`__setstate__` method as described in section :ref:`pickle-inst`.  If the
506
 
  object has no :meth:`__setstate__` method, then, as above, the value must be a
507
 
  dictionary and it will be added to the object's :attr:`__dict__`.
508
 
 
509
 
* Optionally, an iterator (and not a sequence) yielding successive list items.
510
 
  These list items will be pickled, and appended to the object using either
511
 
  ``obj.append(item)`` or ``obj.extend(list_of_items)``.  This is primarily used
512
 
  for list subclasses, but may be used by other classes as long as they have
513
 
  :meth:`append` and :meth:`extend` methods with the appropriate signature.
514
 
  (Whether :meth:`append` or :meth:`extend` is used depends on which pickle
515
 
  protocol version is used as well as the number of items to append, so both must
516
 
  be supported.)
517
 
 
518
 
* Optionally, an iterator (not a sequence) yielding successive dictionary items,
519
 
  which should be tuples of the form ``(key, value)``.  These items will be
520
 
  pickled and stored to the object using ``obj[key] = value``. This is primarily
521
 
  used for dictionary subclasses, but may be used by other classes as long as they
522
 
  implement :meth:`__setitem__`.
523
 
 
524
 
It is sometimes useful to know the protocol version when implementing
525
 
:meth:`__reduce__`.  This can be done by implementing a method named
526
 
:meth:`__reduce_ex__` instead of :meth:`__reduce__`. :meth:`__reduce_ex__`, when
527
 
it exists, is called in preference over :meth:`__reduce__` (you may still
528
 
provide :meth:`__reduce__` for backwards compatibility).  The
529
 
:meth:`__reduce_ex__` method will be called with a single integer argument, the
530
 
protocol version.
531
 
 
532
 
The :class:`object` class implements both :meth:`__reduce__` and
533
 
:meth:`__reduce_ex__`; however, if a subclass overrides :meth:`__reduce__` but
534
 
not :meth:`__reduce_ex__`, the :meth:`__reduce_ex__` implementation detects this
535
 
and calls :meth:`__reduce__`.
 
465
.. method:: object.__reduce__()
 
466
   
 
467
   When the :class:`Pickler` encounters an object of a type it knows nothing
 
468
   about --- such as an extension type --- it looks in two places for a hint of
 
469
   how to pickle it.  One alternative is for the object to implement a
 
470
   :meth:`__reduce__` method.  If provided, at pickling time :meth:`__reduce__`
 
471
   will be called with no arguments, and it must return either a string or a
 
472
   tuple.
 
473
 
 
474
   If a string is returned, it names a global variable whose contents are
 
475
   pickled as normal.  The string returned by :meth:`__reduce__` should be the
 
476
   object's local name relative to its module; the pickle module searches the
 
477
   module namespace to determine the object's module.
 
478
 
 
479
   When a tuple is returned, it must be between two and five elements long.
 
480
   Optional elements can either be omitted, or ``None`` can be provided as their
 
481
   value.  The contents of this tuple are pickled as normal and used to
 
482
   reconstruct the object at unpickling time.  The semantics of each element
 
483
   are:
 
484
 
 
485
   * A callable object that will be called to create the initial version of the
 
486
     object.  The next element of the tuple will provide arguments for this
 
487
     callable, and later elements provide additional state information that will
 
488
     subsequently be used to fully reconstruct the pickled data.
 
489
 
 
490
     In the unpickling environment this object must be either a class, a
 
491
     callable registered as a "safe constructor" (see below), or it must have an
 
492
     attribute :attr:`__safe_for_unpickling__` with a true value. Otherwise, an
 
493
     :exc:`UnpicklingError` will be raised in the unpickling environment.  Note
 
494
     that as usual, the callable itself is pickled by name.
 
495
 
 
496
   * A tuple of arguments for the callable object.
 
497
 
 
498
     .. versionchanged:: 2.5
 
499
        Formerly, this argument could also be ``None``.
 
500
 
 
501
   * Optionally, the object's state, which will be passed to the object's
 
502
     :meth:`__setstate__` method as described in section :ref:`pickle-inst`.  If
 
503
     the object has no :meth:`__setstate__` method, then, as above, the value
 
504
     must be a dictionary and it will be added to the object's :attr:`__dict__`.
 
505
 
 
506
   * Optionally, an iterator (and not a sequence) yielding successive list
 
507
     items.  These list items will be pickled, and appended to the object using
 
508
     either ``obj.append(item)`` or ``obj.extend(list_of_items)``.  This is
 
509
     primarily used for list subclasses, but may be used by other classes as
 
510
     long as they have :meth:`append` and :meth:`extend` methods with the
 
511
     appropriate signature.  (Whether :meth:`append` or :meth:`extend` is used
 
512
     depends on which pickle protocol version is used as well as the number of
 
513
     items to append, so both must be supported.)
 
514
 
 
515
   * Optionally, an iterator (not a sequence) yielding successive dictionary
 
516
     items, which should be tuples of the form ``(key, value)``.  These items
 
517
     will be pickled and stored to the object using ``obj[key] = value``. This
 
518
     is primarily used for dictionary subclasses, but may be used by other
 
519
     classes as long as they implement :meth:`__setitem__`.
 
520
 
 
521
.. method:: object.__reduce_ex__(protocol) 
 
522
 
 
523
   It is sometimes useful to know the protocol version when implementing
 
524
   :meth:`__reduce__`.  This can be done by implementing a method named
 
525
   :meth:`__reduce_ex__` instead of :meth:`__reduce__`. :meth:`__reduce_ex__`,
 
526
   when it exists, is called in preference over :meth:`__reduce__` (you may
 
527
   still provide :meth:`__reduce__` for backwards compatibility).  The
 
528
   :meth:`__reduce_ex__` method will be called with a single integer argument,
 
529
   the protocol version.
 
530
 
 
531
   The :class:`object` class implements both :meth:`__reduce__` and
 
532
   :meth:`__reduce_ex__`; however, if a subclass overrides :meth:`__reduce__`
 
533
   but not :meth:`__reduce_ex__`, the :meth:`__reduce_ex__` implementation
 
534
   detects this and calls :meth:`__reduce__`.
536
535
 
537
536
An alternative to implementing a :meth:`__reduce__` method on the object to be
538
537
pickled, is to register the callable with the :mod:`copy_reg` module.  This