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)
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):
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
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
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)
* aren't, raise ValueError and return -1.
*/
static int
-check_time_args(int h, int m, int s, int us)
+check_time_args(int h, int m, int s, int us, int fold)
{
if (h < 0 || h > 23) {
PyErr_SetString(PyExc_ValueError,
"microsecond must be in 0..999999");
return -1;
}
+ if (fold != 0 && fold != 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "fold must be either 0 or 1");
+ return -1;
+ }
return 0;
}
if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws,
&hour, &minute, &second, &usecond,
&tzinfo, &fold)) {
- if (check_time_args(hour, minute, second, usecond) < 0)
+ if (check_time_args(hour, minute, second, usecond, fold) < 0)
return NULL;
if (check_tzinfo_subclass(tzinfo) < 0)
return NULL;
if (tuple == NULL)
return NULL;
clone = time_new(Py_TYPE(self), tuple, NULL);
- if (clone != NULL)
+ if (clone != NULL) {
+ if (fold != 0 && fold != 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "fold must be either 0 or 1");
+ return NULL;
+ }
TIME_SET_FOLD(clone, fold);
+ }
Py_DECREF(tuple);
return clone;
}
&second, &usecond, &tzinfo, &fold)) {
if (check_date_args(year, month, day) < 0)
return NULL;
- if (check_time_args(hour, minute, second, usecond) < 0)
+ if (check_time_args(hour, minute, second, usecond, fold) < 0)
return NULL;
if (check_tzinfo_subclass(tzinfo) < 0)
return NULL;
if (tuple == NULL)
return NULL;
clone = datetime_new(Py_TYPE(self), tuple, NULL);
- if (clone != NULL)
+
+ if (clone != NULL) {
+ if (fold != 0 && fold != 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "fold must be either 0 or 1");
+ return NULL;
+ }
DATE_SET_FOLD(clone, fold);
+ }
Py_DECREF(tuple);
return clone;
}