pickle problem:cant reload object saved with binary option

Christian Tismer tismer at appliedbiometrics.com
Sat Jan 1 13:44:30 EST 2000


Marshall wrote:
> 
> Thanks, now I have a new problem.  It still works without the binary
> option.  I think I have the organization of my data screwed up
> somehow, but I don't understand why it works without the binary, and
> breaks if I turn it on.  It doesn't matter if I use pickle or cPickle,
> I still get errors.  The object I'm pickling is self.info=crccvars()
> Thanks in advance for any insight.

No-no-no! :-)
Please understand the following:
Whenever you use the binary (c)Pickle format under Windows,
you *always* must use a binary mode for your file, *both*
for reading and writing.

The reason is: Binary pickle makes use of all 256 characters.
Since Dos/Windows has this carriage return/linefeed (CR/LF)
convention, reading a binary file as a text file turns a CR/LF
into one LF char for Python. And writing binary data in text mode
acts similar: Every single LF char is turned into a CR/LF sequence.

Here the simple rule:
Whenever you use binary pickles, open the file for writing with
mode "wb" (or w+b if you want to append). Open it for reading
with mode "rb". That's all about it.

Any combination of text and binary mode should be strictly
avoided. It can work in few rare cases but will fail quickly.
Since emission/recognition of LF is dependent from your
actual data, things which work today will break tomorrow.

>         def save(self,file='crcc.sav'):
>                 blah=open(file,'w')
@@@ look this is the bad line :-)
>                 cPickle.dump(self.info,blah,1)
>                 blah.close()
>         def load(self,file='crcc.sav'):
>                 print 'print self     :',self
>                 print 'print __name__ :',__name__
>                 print 'print self.info:',self.info
>                 print ''
>                 blah=open(file,'r+b')
@@@ better use "rb" since you don't want to append.
>                 self.info=cPickle.load(blah)
>                 blah.close()
>                 self.buildlist()

A last addition: On Unix, it is not necessary to distinguish
between text and binary mode, since no conversions take place.
But in order to write system independant applications, it is
a good idea to always use those explicit filemodes. Under
Unix, they just have no effect. With Windows or the Mac,
they prevend your above trouble.

Wishing you a trouble-free new millennium - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Düppelstr. 31                :    *Starship* http://starship.python.net
12163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list