pickle's backward compatibility

M.-A. Lemburg mal at egenix.com
Tue Oct 13 11:22:34 EDT 2009


exarkun at twistedmatrix.com wrote:
> On 02:48 pm, mal at egenix.com wrote:
>> exarkun at twistedmatrix.com wrote:
>>> On 03:17 pm, pengyu.ut at gmail.com wrote:
>>>> Hi,
>>>>
>>>> If I define my own class and use pickle to serialize the objects in
>>>> this class, will the serialized object be successfully read in later
>>>> version of python.
>>>>
>>>> What if I serialize (using pickle) an object of a class defined in
>>>> python library, will it be successfully read in later version of
>>>> python?
>>>
>>> Sometimes.  Sometimes not.  Python doesn't really offer any guarantees
>>> regarding this.
>>
>> I think this needs to be corrected: the pickle protocol versions are
>> compatible between Python releases, however, there are two things to
>> consider:
>>
>> * The default pickle version sometimes changes between minor
>>   releases.
>>
>>   This is easy to handle, though, since you can provide the pickle
>>   protocol version as parameter.
>>
>> * The pickle protocol has changed a bit between 2.x and 3.x.
>>
>>   This is mostly due to the fact that Python's native string
>>   format changed to Unicode in 3.x.
> 
> The pickle protocol isn't the only thing that determines whether an
> existing pickle can be loaded.  Consider this very simple example of a
> class which might exist in Python 2.x:
>
> [...changes to baseclasses and __init__ arguments...]
>
> So these are the kinds of things I am talking about when I say that
> there aren't really any guarantees.

True, there's no guarantee that pickle can work out your code changes.
I think that's a bit much to expect from a serialization protocol :-)

I was only talking about the data storage format itself, ie. whether
it's possible to pickle the data in Python 2.n and load it again
in 2.n+m without code changes.

Even with code changes, you can still work-around many issues by
implementing a proper .__setstate__() method in your class and
using a class .version attribute to detect older pickles.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Oct 13 2009)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-list mailing list