diff options
author | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2016-08-08 21:05:40 (GMT) |
---|---|---|
committer | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2016-08-08 21:05:40 (GMT) |
commit | 47649ab1f105760ba9f4d60d540b6ea63f45795a (patch) | |
tree | 52339c890933052f8ba0d5ae1362d3ba4e496b7c /Lib | |
parent | 95e0df8389c8a44c0f6c6b6be8363e602e8e8914 (diff) | |
download | cpython-47649ab1f105760ba9f4d60d540b6ea63f45795a.zip cpython-47649ab1f105760ba9f4d60d540b6ea63f45795a.tar.gz cpython-47649ab1f105760ba9f4d60d540b6ea63f45795a.tar.bz2 |
Closes #27710: Disallow fold not in [0, 1] in time and datetime constructors.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/datetime.py | 14 | ||||
-rw-r--r-- | Lib/test/datetimetester.py | 5 |
2 files changed, 13 insertions, 6 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py index 9f942a2..36374aa 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -288,7 +288,7 @@ def _check_date_fields(year, month, day): raise ValueError('day must be in 1..%d' % dim, day) return year, month, day -def _check_time_fields(hour, minute, second, microsecond): +def _check_time_fields(hour, minute, second, microsecond, fold): hour = _check_int_field(hour) minute = _check_int_field(minute) second = _check_int_field(second) @@ -301,7 +301,9 @@ def _check_time_fields(hour, minute, second, microsecond): raise ValueError('second must be in 0..59', second) if not 0 <= microsecond <= 999999: raise ValueError('microsecond must be in 0..999999', microsecond) - return hour, minute, second, microsecond + if fold not in (0, 1): + raise ValueError('fold must be either 0 or 1', fold) + return hour, minute, second, microsecond, fold def _check_tzinfo_arg(tz): if tz is not None and not isinstance(tz, tzinfo): @@ -1059,8 +1061,8 @@ class time: self.__setstate(hour, minute or None) self._hashcode = -1 return self - hour, minute, second, microsecond = _check_time_fields( - hour, minute, second, microsecond) + hour, minute, second, microsecond, fold = _check_time_fields( + hour, minute, second, microsecond, fold) _check_tzinfo_arg(tzinfo) self = object.__new__(cls) self._hour = hour @@ -1369,8 +1371,8 @@ class datetime(date): self._hashcode = -1 return self year, month, day = _check_date_fields(year, month, day) - hour, minute, second, microsecond = _check_time_fields( - hour, minute, second, microsecond) + hour, minute, second, microsecond, fold = _check_time_fields( + hour, minute, second, microsecond, fold) _check_tzinfo_arg(tzinfo) self = object.__new__(cls) self._year = year diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index e71f3aa..726b7fd 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -1724,6 +1724,11 @@ class TestDateTime(TestDate): self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, 59, 59, 1000000) + # bad fold + self.assertRaises(ValueError, self.theclass, + 2000, 1, 31, fold=-1) + self.assertRaises(ValueError, self.theclass, + 2000, 1, 31, fold=2) # Positional fold: self.assertRaises(TypeError, self.theclass, 2000, 1, 31, 23, 59, 59, 0, None, 1) |