You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running the following program twice can lead to inconsistent hash values due to restoring the hash from the pickled file (which might differ from the hash value when creating the same immutabledict from scratch):
frompickleimportdump, loadfromimmutabledictimportimmutabledicti1=immutabledict({"a": "1", "b": "2", "c": "3"})
hash(i1) # Force creating a cached hash valuetry:
# Second run: load pickle filewithopen("pickle_imm.pkl", "rb") asf:
i2=load(f)
exceptFileNotFoundError:
# First run: create pickle filei2=immutabledict({"a": "1", "b": "2", "c": "3"})
withopen("pickle_imm.pkl", "wb") asf:
dump(i1, f)
asserti1==i2print(hash(i1), hash(i2))
asserthash(i1) ==hash(i2) # Fails on the second run: immutabledicts compare equal, but their hash values are different!
This mostly affects situations where strings, class types, etc. are stored in the immutabledict, which do not have stable hash values across Python invocations.
There are a few potential solutions:
Exclude the cached hash from __getstate__/__setstate__
The text was updated successfully, but these errors were encountered:
matthiasdiener
changed the title
PIckling / Unpickling leads to inconsistent hash value
Pickling / Unpickling leads to inconsistent hash value
Dec 6, 2023
Running the following program twice can lead to inconsistent hash values due to restoring the hash from the pickled file (which might differ from the hash value when creating the same
immutabledict
from scratch):This mostly affects situations where strings, class types, etc. are stored in the
immutabledict
, which do not have stable hash values across Python invocations.There are a few potential solutions:
__getstate__
/__setstate__
Usepytools.memoize_method
to cache the hash value (similar to what I did in use pytools.memoize for caching matthiasdiener/orderedsets#28)Use something like https://stackoverflow.com/a/71663059/1250282 to cache the hash valueThe text was updated successfully, but these errors were encountered: