[Python-bugs-list] [ python-Bugs-640645 ] cPickle.dumps change after for

noreply@sourceforge.net noreply@sourceforge.net
Tue, 19 Nov 2002 08:19:36 -0800


Bugs item #640645, was opened at 2002-11-19 06:42
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=640645&group_id=5470

Category: Python Library
>Group: Not a Bug
>Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Marco Beri (marcoberi)
Assigned to: Nobody/Anonymous (nobody)
Summary: cPickle.dumps change after for

Initial Comment:
try this program:

import cPickle
a=([{}])
print cPickle.dumps(a)
for x in a:
  pass
cPickle.dumps(a)

It prints:
(lp1
(da.

Why on earth it does dumps return different string?
And after just a for with a pass in it...


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-11-19 11:19

Message:
Logged In: YES 
user_id=31435

Closed as Not-A-Bug.  The internals of pickle strings aren't 
guaranteed, just that "they work" when unpickled again, and 
these do.  If you want a hash code for a dict, don't dare use 
pickle for this either, even if it appears "to work":  it doesn't.  
The order in which dict keys are enumerated isn't defined 
either, and can and does vary across releases, and even 
across program runs.

So a reliable hash code for a dict needs to be independent of 
key iteration order, and no version of pickle spends time 
trying to force that issue (waste of time -- it isn't needed for 
pickling or unpickling).

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-11-19 09:35

Message:
Logged In: YES 
user_id=6656

Well, that's asking for something you're not promised.  It
takes some imagination to categorize this as a bug, and I'm
not going to put any of my time into fixing it.

It seems using pickle in place of cPickle *may* work, fwiw.

----------------------------------------------------------------------

Comment By: Marco Beri (marcoberi)
Date: 2002-11-19 08:56

Message:
Logged In: YES 
user_id=588604

I would like to use hash(cPickle.dumps(a)) to have a unique 
hash also for dict that aren't hashable.


----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-11-19 08:03

Message:
Logged In: YES 
user_id=6656

OK, I think I know what's happening.

Look at this:

>>> a = {}       
>>> cPickle.dumps(a)
'(dp1\n.'
>>> cPickle.dumps({})
'(d.'

the only difference is in the number of references to the
object getting pickled.  I don't know why that matters, but
it seems to.

Why does this matter to you?

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-11-19 07:56

Message:
Logged In: YES 
user_id=6656

You're right, sorry.

----------------------------------------------------------------------

Comment By: Marco Beri (marcoberi)
Date: 2002-11-19 07:07

Message:
Logged In: YES 
user_id=588604

I forgot to print the second dumps but the bug remains:
Before the for:
(lp1
(da.
After the for:
(lp1
(dp2
a.


----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-11-19 07:00

Message:
Logged In: YES 
user_id=6656

Umm:

>>> a=([{}])
>>> cPickle.dumps(a)
'(lp1\n(da.'

you're only printing the first pickle...

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=640645&group_id=5470