Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid infinite loop in Datetime.bulk_create_datetimes by checking for positive timedelta #1229

Merged
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
2 changes: 2 additions & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Patches and Suggestions
- Oleg Höfling `(hoefling)`_
- Nick Pope `(ngnpope)`_
- Arul Prabakaran `(arulprabakaran)`_
- Florian Kroiß `(Wooza)`_


.. _(lk-geimfari): https://github.com/lk-geimfari
Expand Down Expand Up @@ -158,3 +159,4 @@ Patches and Suggestions
.. _(hoefling): https://github.com/hoefling
.. _(ngnpope): https://github.com/ngnpope
.. _(arulprabakaran): https://github.com/arulprabakaran
.. _(Wooza): https://github.com/Wooza
8 changes: 6 additions & 2 deletions mimesis/providers/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def bulk_create_datetimes(
:param date_end: End of the range.
:param kwargs: Keyword arguments for :py:class:`datetime.timedelta`
:return: List of datetime objects
:raises: ValueError: When ``date_start``/``date_end`` not passed and
when ``date_start`` larger than ``date_end``.
:raises: ValueError: When ``date_start``/``date_end`` not passed,
when ``date_start`` larger than ``date_end`` or when the given
keywords for `datetime.timedelta` represent a non-positive timedelta.
"""
dt_objects = []

Expand All @@ -66,6 +67,9 @@ def bulk_create_datetimes(
if date_end < date_start:
raise ValueError("date_start can not be larger than date_end")

if timedelta(**kwargs) <= timedelta():
raise ValueError("timedelta must be positive")

while date_start <= date_end:
date_start += timedelta(**kwargs)
dt_objects.append(date_start)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_providers/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def test_bulk_create_datetimes_error(self, _datetime):
with pytest.raises(ValueError):
_datetime.bulk_create_datetimes(None, None)

with pytest.raises(ValueError):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it. Why error should raise here if timedelta is positive in this particular case?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I get it. It just incrementing date_start by timedelta(0) that make no sense.

_datetime.bulk_create_datetimes(
date_start, date_start + datetime.timedelta(days=1)
)

def test_year(self, _datetime):
result = _datetime.year(minimum=2000, maximum=_datetime.CURRENT_YEAR)
assert result >= 2000
Expand Down