|
|
|
@ -227,6 +227,23 @@ def set(key: int, value: bytes) -> None:
|
|
|
|
|
_SESSIONS[_active_session_idx].set(key, value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_int(key: int, value: int) -> None:
|
|
|
|
|
if key & _SESSIONLESS_FLAG:
|
|
|
|
|
length = _SESSIONLESS_CACHE.fields[key ^ _SESSIONLESS_FLAG]
|
|
|
|
|
elif _active_session_idx is None:
|
|
|
|
|
raise InvalidSessionError
|
|
|
|
|
else:
|
|
|
|
|
length = _SESSIONS[_active_session_idx].fields[key]
|
|
|
|
|
|
|
|
|
|
encoded = value.to_bytes(length, "big")
|
|
|
|
|
|
|
|
|
|
# Ensure that the value fits within the length. Micropython's int.to_bytes()
|
|
|
|
|
# doesn't raise OverflowError.
|
|
|
|
|
assert int.from_bytes(encoded, "big") == value
|
|
|
|
|
|
|
|
|
|
set(key, encoded)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
|
|
|
|
|
@overload
|
|
|
|
@ -246,6 +263,14 @@ def get(key: int, default: T | None = None) -> bytes | T | None: # noqa: F811
|
|
|
|
|
return _SESSIONS[_active_session_idx].get(key, default)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_int(key: int, default: T | None = None) -> int | T | None: # noqa: F811
|
|
|
|
|
encoded = get(key)
|
|
|
|
|
if encoded is None:
|
|
|
|
|
return default
|
|
|
|
|
else:
|
|
|
|
|
return int.from_bytes(encoded, "big")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_set(key: int) -> bool:
|
|
|
|
|
if key & _SESSIONLESS_FLAG:
|
|
|
|
|
return _SESSIONLESS_CACHE.is_set(key ^ _SESSIONLESS_FLAG)
|
|
|
|
|