Skip to content

Commit

Permalink
Don't use hash on floats in hashing.py (streamlit#7754)
Browse files Browse the repository at this point in the history
* Don't use hash on floats

Python's hash is salted with a random number. This will not work across restarts. Instead, use struct.pack to get the bytes of a float.

* Run autoformatter

---------

Co-authored-by: Vincent Donato <vincent@streamlit.io>
  • Loading branch information
2 people authored and zyxue committed Apr 16, 2024
1 parent caaa7fb commit 84d6dac
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/streamlit/runtime/caching/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io
import os
import pickle
import struct
import sys
import tempfile
import threading
Expand Down Expand Up @@ -224,6 +225,11 @@ def _int_to_bytes(i: int) -> bytes:
return i.to_bytes(num_bytes, "little", signed=True)


def _float_to_bytes(f: float) -> bytes:
# Floats are 64bit in Python, so we need to use the "d" format.
return struct.pack("<d", f)


def _key(obj: Optional[Any]) -> Any:
"""Return key for memoization."""

Expand Down Expand Up @@ -361,7 +367,7 @@ def _to_bytes(self, obj: Any) -> bytes:
return obj.encode()

elif isinstance(obj, float):
return self.to_bytes(hash(obj))
return _float_to_bytes(obj)

elif isinstance(obj, int):
return _int_to_bytes(obj)
Expand Down

0 comments on commit 84d6dac

Please sign in to comment.