[Python-checkins] python/dist/src/Lib/test test_random.py,1.11,1.12

Raymond Hettinger python@rcn.com
Tue, 24 Jun 2003 17:13:18 -0400


>     Raymond> Log Message:
>     Raymond> SF bug #759889:  Pickling of Random is broken
> 
>     Raymond> * Implement __reduce__() to support pickling.
>     Raymond> * Add a test case to prove a successful roundtrip through
>     Raymond>   pickle. 

[Skip]
> I'm confused.  I looked at this briefly this morning and as far as I could
> tell, random.Random() objects already had a __reduce__ method:
> 
>     % python
>     Python 2.3b1+ (#66, Jun 20 2003, 10:06:31) 
>     [GCC 3.1 20020420 (prerelease)] on darwin
>     Type "help", "copyright", "credits" or "license" for more information.
>     >>> import random
>     >>> r = random.Random()
>     >>> r.__reduce__
>     <built-in method __reduce__ of Random object at 0x6ddab0>
>     >>> r.__reduce__()[0]
>     <function _reconstructor at 0x427330>

The method you are seeing is inherited from object and not
only doesn't help, but will fail during pickling.  Quoth pep 307:

""" New-style classes, whether implemented in C or in Python, inherit
    a default __reduce__ implementation from the universal base class
    'object'.

    . . .

    The default __reduce__ implementation will fail at pickling time
    for built-in types not mentioned above, and for new-style classes
    implemented in C:  if they want to be picklable, they must supply
    a custom __reduce__ implementation under protocols 0 and 1.
"""

There is probably a good reason for this, but it would have been 
nice if object.__reduce__() honored existing implementations of
__getstate__.


Raymond
Raymond Hettinger

#################################################################
#################################################################
#################################################################
#####
#####
#####
#################################################################
#################################################################
#################################################################