cultcache-py: one number path for the float leaves (no write/read flag); tests pin the refusal texts

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-12 01:24:35 +02:00
commit eb01c414a6
2 changed files with 33 additions and 30 deletions

View file

@ -169,41 +169,38 @@ def _int_to_float32(value: _int) -> _float:
return -rounded if value < 0 else rounded
def _number(what: str, value: Any, *, writing: _bool) -> _float:
"""A number as a double, or the float32-rounded double for `float32`; refuses what C# refuses."""
def _checked_int(what: str, value: _int) -> _int:
if not _WRITABLE_INT[0] <= value <= _WRITABLE_INT[1]:
_refuse(what, "an integer out of range")
return value
def _number(what: str, value: Any) -> _float:
"""A number as a double: a float as it is, an integer (within the 64-bit range) rounded once."""
if isinstance(value, _float):
return value
if not _is_int(value):
_refuse(what, _kind(value))
if writing and not _WRITABLE_INT[0] <= value <= _WRITABLE_INT[1]:
_refuse(what, "an integer out of range")
return _float(value)
return _float(_checked_int(what, value))
def _float32_value(value: Any) -> _float:
"""The float32 a number rounds to, in one rounding, as the reference's cast does."""
if _is_int(value):
return _int_to_float32(_checked_int("a float32", value))
return _round_to_float32(_number("a float32", value))
def _float32_encode(value: Any) -> Any:
if _is_int(value):
if not _WRITABLE_INT[0] <= value <= _WRITABLE_INT[1]:
_refuse("a float32", "an integer out of range")
return Float32(_int_to_float32(value))
return Float32(_round_to_float32(_number("a float32", value, writing=True)))
return Float32(_float32_value(value))
def _float32_decode(raw: Any) -> Any:
if _is_int(raw):
return _int_to_float32(raw)
return _round_to_float32(_number("a float32", raw, writing=False))
def _float64_check(value: Any) -> Any:
return _number("a float64", value)
def _float64_encode(value: Any) -> Any:
return _number("a float64", value, writing=True)
def _float64_decode(raw: Any) -> Any:
return _number("a float64", raw, writing=False)
float32 = SlotCodec(_float32_encode, _float32_decode, "System.Single", True)
float64 = SlotCodec(_float64_encode, _float64_decode, "System.Double", True)
float32 = SlotCodec(_float32_encode, _float32_value, "System.Single", True)
float64 = SlotCodec(_float64_check, _float64_check, "System.Double", True)
def _refuse_ext(tree: Any) -> None:

View file

@ -206,6 +206,9 @@ class LeavesReadsVectorTests(unittest.TestCase):
document.decode_payload(payload)
with self.assertRaises(ValueError):
t.any.encode({"k": [msgpack.ExtType(1, b"\x00")]})
with self.assertRaises(ValueError) as caught:
t.any.decode(msgpack.ExtType(1, b"\x00"))
self.assertEqual(str(caught.exception), "expected an object, found ext")
self.assertIsNone(t.any.decode(None))
self.assertIsNone(t.any.encode(None))
@ -452,13 +455,13 @@ class LeafRuleTests(unittest.TestCase):
self.assertEqual(t.float32.decode(1e300), math.inf)
self.assertEqual(t.float32.decode(-1e300), -math.inf)
self.assertEqual(t.float64.decode(2**53 + 1), float(2**53))
# past 2**53 a double then a float32 rounding differs from one rounding: 2**63 + 2**39 + 1 is above the tie.
value = 2**63 + 2**39 + 1
self.assertEqual(t.float32.decode(value), float(2**63 + 2**40))
self.assertEqual(t.float32.decode(-value), -float(2**63 + 2**40))
tie_even, tie_odd = 2**63 + 2**39, 2**63 + 2**40 + 2**39
self.assertEqual(t.float32.decode(tie_even), float(2**63))
self.assertEqual(t.float32.decode(tie_odd), float(2**63 + 2**41))
# past 2**53 a double then a float32 rounding can differ from one rounding: 2**62 + 2**38 + 1 is above the tie.
value = 2**62 + 2**38 + 1
self.assertEqual(t.float32.decode(value), float(2**62 + 2**39))
self.assertEqual(t.float32.decode(-value), -float(2**62 + 2**39))
tie_even, tie_odd = 2**62 + 2**38, 2**62 + 2**39 + 2**38
self.assertEqual(t.float32.decode(tie_even), float(2**62))
self.assertEqual(t.float32.decode(tie_odd), float(2**62 + 2**40))
self.assertEqual(t.float32.encode(2**64 - 1), t.Float32(float(2**64)))
self.assertEqual(t.float32.encode(1.5), t.Float32(1.5))
self.assertEqual(t.float64.encode(3), 3.0)
@ -501,7 +504,10 @@ class LeafBoundaryTests(unittest.TestCase):
for ok in (3, 0, -(2**63), 2**64 - 1):
leaf.encode(ok)
for bad in (-(2**63) - 1, 2**64):
self.assertEqual(self._refusal(lambda: leaf.encode(bad)).rsplit(", found ", 1)[1], "an integer out of range")
what = "a float32" if leaf is t.float32 else "a float64"
expected = f"expected {what}, found an integer out of range"
self.assertEqual(self._refusal(lambda: leaf.encode(bad)), expected)
self.assertEqual(self._refusal(lambda: leaf.decode(bad)), expected)
self.assertEqual(t.float32.encode(3), t.Float32(3.0))
self.assertEqual(t.float32.encode(-(2**63)), t.Float32(-float(2**63)))
self.assertEqual(t.float64.encode(-(2**63)), -float(2**63))