diff --git a/news/2136.bugfix b/news/2136.bugfix new file mode 100644 index 000000000..0c5911ca6 --- /dev/null +++ b/news/2136.bugfix @@ -0,0 +1 @@ +Fixed :class:~icalendar.prop.float.vFloat and :class:~icalendar.prop.geo.vGeo writing very small and very large numbers with an exponent, such as GEO:1e-05;-2e-05. The FLOAT value type of :rfc:5545#section-3.3.7 does not allow an exponent. @fermsp diff --git a/src/icalendar/prop/float.py b/src/icalendar/prop/float.py index 18c2f9d8c..c0bf6ba0f 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,25 @@ from icalendar.parser.xcal.wrapper import from_xcal_wrapper +def _float_to_ical(value: float) -> str: + """Convert a float to the ``FLOAT`` notation of :rfc:`5545#section-3.3.7`. + + The ``FLOAT`` grammar has no exponent. + Python writes very small and very large floats with one, such as ``1e-05``. + Those values are written with all their digits instead. + + Parameters: + value: The float to convert. + + Returns: + The float as a string without an exponent. + """ + text = repr(float(value)) + if "e" in text: + text = format(Decimal(text), "f") + return text + + class vFloat(float): """Float @@ -69,7 +89,7 @@ def __new__( return self def to_ical(self) -> bytes: - return str(self).encode("utf-8") + return _float_to_ical(self).encode("utf-8") @classmethod def from_ical(cls, ical: str | float) -> Self: diff --git a/src/icalendar/prop/geo.py b/src/icalendar/prop/geo.py index e2383417c..60b2e432e 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 _float_to_ical 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"{_float_to_ical(self.latitude)};{_float_to_ical(self.longitude)}" @property def ical_value(self) -> tuple[float, float]: diff --git a/src/icalendar/tests/test_issue_2136_float_exponent.py b/src/icalendar/tests/test_issue_2136_float_exponent.py new file mode 100644 index 000000000..7db37e935 --- /dev/null +++ b/src/icalendar/tests/test_issue_2136_float_exponent.py @@ -0,0 +1,50 @@ +"""Regression tests for issue #2136: FLOAT values must not use an exponent.""" + +import re + +import pytest + +from icalendar import Event +from icalendar.prop import vFloat, vGeo + +RFC_5545_FLOAT = re.compile(r"[+-]?\d+(\.\d+)?") +"""The FLOAT grammar of RFC 5545, section 3.3.7.""" + +VALUES = [0.00001, -0.00002, 1.5e-07, 1e16, -1e22, 5e-324, 1.7976931348623157e308] +"""Floats that Python writes with an exponent.""" + + +@pytest.mark.parametrize("value", VALUES) +def test_vfloat_follows_the_float_grammar(value): + """vFloat writes no exponent.""" + assert RFC_5545_FLOAT.fullmatch(vFloat(value).to_ical().decode()) + + +@pytest.mark.parametrize("value", VALUES) +def test_vfloat_round_trip(value): + """The value is the same after writing and parsing it.""" + assert vFloat.from_ical(vFloat(value).to_ical().decode()) == value + + +def test_vfloat_examples(): + """Small and large values are written with all their digits.""" + assert vFloat(0.00001).to_ical() == b"0.00001" + assert vFloat(1e16).to_ical() == b"10000000000000000" + + +def test_vgeo_writes_no_exponent(): + """Coordinates close to zero are written with all their digits.""" + assert vGeo((0.00001, -0.00002)).to_ical() == "0.00001;-0.00002" + + +def test_geo_of_an_event(): + """The GEO property of an event has no exponent.""" + event = Event() + event.add("GEO", (0.00001, -0.00002)) + assert b"GEO:0.00001;-0.00002" in event.to_ical() + + +def test_other_values_stay_the_same(): + """Values that had no exponent are written as before.""" + assert vFloat(3.1415).to_ical() == b"3.1415" + assert vGeo((37.386013, -122.082932)).to_ical() == "37.386013;-122.082932"