diff --git a/news/2136.bugfix b/news/2136.bugfix new file mode 100644 index 000000000..44707752d --- /dev/null +++ b/news/2136.bugfix @@ -0,0 +1 @@ +FLOAT and GEO values are written without an exponent, as RFC 5545 3.3.7 requires: a latitude of 0.00001 is written as ``GEO:0.00001;...`` instead of ``GEO:1e-05;...``. diff --git a/src/icalendar/prop/float.py b/src/icalendar/prop/float.py index 18c2f9d8c..dfb72571f 100644 --- a/src/icalendar/prop/float.py +++ b/src/icalendar/prop/float.py @@ -1,6 +1,7 @@ """FLOAT values from :rfc:`5545`.""" import math +from decimal import Decimal from typing import Any, ClassVar from xml.etree.ElementTree import Element, SubElement @@ -12,6 +13,20 @@ from icalendar.parser.xcal.wrapper import from_xcal_wrapper +def to_ical_float(value: float) -> str: + """Render a number as a FLOAT value of :rfc:`5545#section-3.3.7`. + + FLOAT has no exponent notation, but ``str()`` on a float switches to it + below ``1e-04`` and from ``1e16`` on, which other iCalendar parsers reject. + ``Decimal`` renders the shortest round-tripping decimal of the float in + plain notation, so ``1e-05`` becomes ``0.00001`` and ``1e16`` becomes + ``10000000000000000``. + """ + if not math.isfinite(value): + return str(value) + return format(Decimal(repr(float(value))), "f") + + class vFloat(float): """Float @@ -69,7 +84,7 @@ def __new__( return self def to_ical(self) -> bytes: - return str(self).encode("utf-8") + return to_ical_float(self).encode("utf-8") @classmethod def from_ical(cls, ical: str | float) -> Self: @@ -140,4 +155,4 @@ def to_xcal(self, element: Element) -> None: element.text = xsd_float_to_string(self) -__all__ = ["vFloat"] +__all__ = ["to_ical_float", "vFloat"] diff --git a/src/icalendar/prop/geo.py b/src/icalendar/prop/geo.py index e2383417c..d56be8042 100644 --- a/src/icalendar/prop/geo.py +++ b/src/icalendar/prop/geo.py @@ -10,6 +10,7 @@ from icalendar.parser import Parameters from icalendar.parser.xcal.string import xsd_float_to_string from icalendar.parser.xcal.wrapper import from_xcal_wrapper +from icalendar.prop.float import to_ical_float if TYPE_CHECKING: from icalendar.compatibility import Self @@ -108,7 +109,7 @@ def __init__( self.params = Parameters(params) def to_ical(self) -> str: - return f"{self.latitude};{self.longitude}" + return f"{to_ical_float(self.latitude)};{to_ical_float(self.longitude)}" @property def ical_value(self) -> tuple[float, float]: diff --git a/src/icalendar/tests/prop/test_vFloat.py b/src/icalendar/tests/prop/test_vFloat.py index 31b36628f..3131948bd 100644 --- a/src/icalendar/tests/prop/test_vFloat.py +++ b/src/icalendar/tests/prop/test_vFloat.py @@ -1,3 +1,5 @@ +import re + import pytest from icalendar.prop.float import vFloat @@ -29,3 +31,39 @@ def test_ical_value(): """ical_value property returns the float value.""" assert vFloat(1.0).ical_value == 1.0 assert vFloat(0.0).ical_value == 0.0 + + +RFC5545_FLOAT = re.compile(rb"^[+-]?\d+(?:\.\d+)?$") +"""FLOAT of RFC 5545 3.3.7: no exponent, optional sign, optional fraction.""" + + +@pytest.mark.parametrize( + ("value", "expected"), + [ + (1e-5, b"0.00001"), + (-2e-5, b"-0.00002"), + (1e-7, b"0.0000001"), + (1e16, b"10000000000000000"), + (-1e16, b"-10000000000000000"), + (1.333, b"1.333"), + (37.386013, b"37.386013"), + (0.0, b"0.0"), + (42.0, b"42.0"), + ], +) +def test_no_exponent(value, expected): + """FLOAT carries no exponent, see RFC 5545 3.3.7.""" + ical = vFloat(value).to_ical() + assert ical == expected + assert RFC5545_FLOAT.match(ical), ical + + +def test_small_value_keeps_its_precision(): + """A value far below 1e-04 must not be rounded away when formatted.""" + assert vFloat(1e-7).to_ical() == b"0.0000001" + assert vFloat.from_ical("0.0000001") == 1e-7 + + +def test_written_value_parses_back(): + for value in (1e-5, -2e-5, 1e16, 37.386013): + assert vFloat.from_ical(vFloat(value).to_ical().decode()) == value diff --git a/src/icalendar/tests/prop/test_vGeo.py b/src/icalendar/tests/prop/test_vGeo.py new file mode 100644 index 000000000..c13c83266 --- /dev/null +++ b/src/icalendar/tests/prop/test_vGeo.py @@ -0,0 +1,36 @@ +import re + +import pytest + +from icalendar import Event +from icalendar.prop import vGeo + +RFC5545_FLOAT = re.compile(r"^[+-]?\d+(?:\.\d+)?$") + + +@pytest.mark.parametrize( + ("geo", "expected"), + [ + ((0.00001, -0.00002), "0.00001;-0.00002"), + ((1e16, -1e16), "10000000000000000;-10000000000000000"), + ((37.386013, -122.082932), "37.386013;-122.082932"), + ((1.2, 3.0), "1.2;3.0"), + ], +) +def test_geo_has_no_exponent(geo, expected): + """FLOAT has no exponent notation, see RFC 5545 3.3.7.""" + ical = vGeo(geo).to_ical() + assert ical == expected + assert all(RFC5545_FLOAT.match(part) for part in ical.split(";")) + + +def test_event_geo_line_has_no_exponent(): + """The reported case: a GEO near the equator used to be written as 1e-05.""" + event = Event() + event.add("GEO", (0.00001, -0.00002)) + assert "GEO:0.00001;-0.00002" in event.to_ical().decode() + + +def test_written_geo_parses_back(): + geo = (1e-5, -2e-5) + assert vGeo.from_ical(vGeo(geo).to_ical()) == geo