Skip to content

Commit c2df79d

Browse files
committed
Fixes next() and previous() hanging when passed an invalid input
1 parent bfadcf4 commit c2df79d

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

pendulum/date.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,9 @@ def next(self, day_of_week=None):
848848
if day_of_week is None:
849849
day_of_week = self.day_of_week
850850

851+
if day_of_week < SUNDAY or day_of_week > SATURDAY:
852+
raise ValueError('Invalid day of week')
853+
851854
dt = self.add(days=1)
852855
while dt.day_of_week != day_of_week:
853856
dt = dt.add(days=1)
@@ -869,6 +872,9 @@ def previous(self, day_of_week=None):
869872
if day_of_week is None:
870873
day_of_week = self.day_of_week
871874

875+
if day_of_week < SUNDAY or day_of_week > SATURDAY:
876+
raise ValueError('Invalid day of week')
877+
872878
dt = self.subtract(days=1)
873879
while dt.day_of_week != day_of_week:
874880
dt = dt.subtract(days=1)

pendulum/pendulum.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
YEARS_PER_CENTURY, YEARS_PER_DECADE,
1818
MONTHS_PER_YEAR,
1919
MINUTES_PER_HOUR, SECONDS_PER_MINUTE,
20-
SECONDS_PER_DAY
20+
SECONDS_PER_DAY,
21+
SUNDAY, SATURDAY
2122
)
2223

2324

@@ -1434,6 +1435,9 @@ def next(self, day_of_week=None, keep_time=False):
14341435
if day_of_week is None:
14351436
day_of_week = self.day_of_week
14361437

1438+
if day_of_week < SUNDAY or day_of_week > SATURDAY:
1439+
raise ValueError('Invalid day of week')
1440+
14371441
if keep_time:
14381442
dt = self
14391443
else:
@@ -1463,6 +1467,9 @@ def previous(self, day_of_week=None, keep_time=False):
14631467
if day_of_week is None:
14641468
day_of_week = self.day_of_week
14651469

1470+
if day_of_week < SUNDAY or day_of_week > SATURDAY:
1471+
raise ValueError('Invalid day of week')
1472+
14661473
if keep_time:
14671474
dt = self
14681475
else:

tests/date_tests/test_day_of_week_modifiers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
import pytest
4+
35
import pendulum
46
from pendulum import Date
57
from pendulum.exceptions import PendulumException
@@ -67,6 +69,12 @@ def test_next_saturday(self):
6769
d = Date(1975, 5, 21).next(6)
6870
self.assertDate(d, 1975, 5, 24)
6971

72+
def test_next_invalid(self):
73+
dt = pendulum.date(1975, 5, 21)
74+
75+
with pytest.raises(ValueError):
76+
dt.next(7)
77+
7078
def test_previous(self):
7179
d = Date(1975, 5, 21).previous()
7280
self.assertDate(d, 1975, 5, 14)
@@ -79,6 +87,12 @@ def test_previous_saturday(self):
7987
d = Date(1975, 5, 21).previous(6)
8088
self.assertDate(d, 1975, 5, 17)
8189

90+
def test_previous_invalid(self):
91+
dt = pendulum.date(1975, 5, 21)
92+
93+
with pytest.raises(ValueError):
94+
dt.previous(7)
95+
8296
def test_first_day_of_month(self):
8397
d = Date(1975, 11, 21).first_of('month', )
8498
self.assertDate(d, 1975, 11, 1)

tests/pendulum_tests/test_day_of_week_modifiers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
import pytest
4+
35
import pendulum
46
from pendulum import Pendulum
57
from pendulum.exceptions import PendulumException
@@ -83,6 +85,12 @@ def test_next_keep_time(self):
8385
d = Pendulum.create(1975, 5, 21, 12).next(keep_time=True)
8486
self.assertPendulum(d, 1975, 5, 28, 12, 0, 0)
8587

88+
def test_next_invalid(self):
89+
dt = pendulum.create(1975, 5, 21, 12)
90+
91+
with pytest.raises(ValueError):
92+
dt.next(7)
93+
8694
def test_previous(self):
8795
d = Pendulum.create(1975, 5, 21).previous()
8896
self.assertPendulum(d, 1975, 5, 14, 0, 0, 0)
@@ -102,6 +110,12 @@ def test_previous_keep_time(self):
102110
d = Pendulum.create(1975, 5, 21, 12).previous(keep_time=True)
103111
self.assertPendulum(d, 1975, 5, 14, 12, 0, 0)
104112

113+
def test_previous_invalid(self):
114+
dt = pendulum.create(1975, 5, 21, 12)
115+
116+
with pytest.raises(ValueError):
117+
dt.previous(7)
118+
105119
def test_first_day_of_month(self):
106120
d = Pendulum.create(1975, 11, 21).first_of('month', )
107121
self.assertPendulum(d, 1975, 11, 1, 0, 0, 0)

0 commit comments

Comments
 (0)