Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions loguru/_string_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ def parse_duration(duration: str) -> Optional[datetime.timedelta]:
Raises
------
ValueError
If a value cannot be converted to float or if an invalid unit is encountered.
If a value cannot be converted to float, if an invalid unit is encountered,
or if the duration exceeds the range representable by ``datetime.timedelta``.
"""
duration = duration.strip()
reg = r"(?:([e\+\-\.\d]+)\s*([a-z]+)[\s\,]*)"
Expand Down Expand Up @@ -194,7 +195,10 @@ def parse_duration(duration: str) -> Optional[datetime.timedelta]:

seconds += value * unit

return datetime.timedelta(seconds=seconds)
try:
return datetime.timedelta(seconds=seconds)
except OverflowError as error:
raise ValueError("Duration out of range while parsing duration: '%s'" % duration) from error


def parse_frequency(frequency: str):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_filesink_retention.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,11 @@ def test_unparsable_retention(retention):
def test_invalid_value_retention_duration(retention):
with pytest.raises(ValueError, match=r"^Invalid unit value while parsing duration: '[^']+'$"):
logger.add("test.log", retention=retention)


@pytest.mark.parametrize("retention", ["1e14s", "1e20s", "1e309s"])
def test_out_of_range_retention_duration(retention):
with pytest.raises(
ValueError, match=r"^Duration out of range while parsing duration: '[^']+'$"
):
logger.add("test.log", retention=retention)
9 changes: 9 additions & 0 deletions tests/test_filesink_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def test_size_rotation(freeze_time, tmp_path, size):
("1 hour", [0.5, 1, 0.1, 100, 1000]),
("7 days", [24 * 7 - 1, 1, 48, 24 * 10, 24 * 365]),
("1h 30 minutes", [1.4, 0.2, 1, 2, 10]),
("1e6s", [240, 48, 200, 100, 300]),
("1 w, 2D", [24 * 8, 24 * 2, 24, 24 * 9, 24 * 9]),
("1.5d", [30, 10, 0.9, 48, 35]),
("1.222 hours, 3.44s", [1.222, 0.1, 1, 1.2, 2]),
Expand Down Expand Up @@ -1152,3 +1153,11 @@ def test_invalid_unit_rotation_duration(rotation):
def test_invalid_value_rotation_duration(rotation):
with pytest.raises(ValueError, match=r"^Invalid float value while parsing duration: '[^']+'$"):
logger.add("test.log", rotation=rotation)


@pytest.mark.parametrize("rotation", ["1e14s", "1e20s", "1e309s"])
def test_out_of_range_rotation_duration(rotation):
with pytest.raises(
ValueError, match=r"^Duration out of range while parsing duration: '[^']+'$"
):
logger.add("test.log", rotation=rotation)