Attrs: cache_hash can give the wrong hash code for deserialized objects

Created on 11 Jan 2019  Â·  6Comments  Â·  Source: python-attrs/attrs

I just realized a bug in the code I wrote for hash code caching (#426). Because the hash code cache field gets serialized and deserialized by Pickle, when you deserialize a cache_hash=True attrs object, the hashcode will be the hashcode the object had at serialization-time. However, if your object has fields with hash codes which are not deterministic between interpreter runs, then on a new interpreter run your deserialized object will have a hash code which differs from a newly created identical object.

We can fix this for pickle by recomputing the hash code in __setstate__. Other serialization libraries which don't respect __setstate__ will still have a problem, but I don't think we can do anything about that. If the __setstate__ solution sounds acceptable I will implement it next week.

Bug

All 6 comments

I think it would be more elegant to clear the cache – would that be feasible?

Good point - I will just clear the cache

bump? :)

Last week was busier than expected, but I hope to complete this tomorrow

I feel slightly better that I am not the only one to screw this up. :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nicholas-Mitchell picture Nicholas-Mitchell  Â·  15Comments

hynek picture hynek  Â·  8Comments

madig picture madig  Â·  3Comments

smarie picture smarie  Â·  14Comments

ojii picture ojii  Â·  16Comments