Kaydet (Commit) 21f58935 authored tarafından Victor Stinner's avatar Victor Stinner

Issue #14180: datetime.date.fromtimestamp(), datetime.datetime.fromtimestamp()

and datetime.datetime.utcfromtimestamp() now raise an OSError instead of
ValueError if localtime() or gmtime() failed.
üst 910df329
...@@ -404,7 +404,8 @@ Other constructors, all class methods: ...@@ -404,7 +404,8 @@ Other constructors, all class methods:
.. versionchanged:: 3.3 .. versionchanged:: 3.3
Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp
is out of the range of values supported by the platform C is out of the range of values supported by the platform C
:c:func:`localtime` function. :c:func:`localtime` function. Raise :exc:`OSError` instead of
:exc:`ValueError` on :c:func:`localtime` failure.
.. classmethod:: date.fromordinal(ordinal) .. classmethod:: date.fromordinal(ordinal)
...@@ -720,7 +721,9 @@ Other constructors, all class methods: ...@@ -720,7 +721,9 @@ Other constructors, all class methods:
.. versionchanged:: 3.3 .. versionchanged:: 3.3
Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp
is out of the range of values supported by the platform C is out of the range of values supported by the platform C
:c:func:`localtime` or :c:func:`gmtime` functions :c:func:`localtime` or :c:func:`gmtime` functions. Raise :exc:`OSError`
instead of :exc:`ValueError` on :c:func:`localtime` or :c:func:`gmtime`
failure.
.. classmethod:: datetime.utcfromtimestamp(timestamp) .. classmethod:: datetime.utcfromtimestamp(timestamp)
...@@ -750,7 +753,8 @@ Other constructors, all class methods: ...@@ -750,7 +753,8 @@ Other constructors, all class methods:
.. versionchanged:: 3.3 .. versionchanged:: 3.3
Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp
is out of the range of values supported by the platform C is out of the range of values supported by the platform C
:c:func:`gmtime` function. :c:func:`gmtime` function. Raise :exc:`OSError` instead of
:exc:`ValueError` on :c:func:`gmtime` failure.
.. classmethod:: datetime.fromordinal(ordinal) .. classmethod:: datetime.fromordinal(ordinal)
......
...@@ -33,6 +33,10 @@ Library ...@@ -33,6 +33,10 @@ Library
- Issue #14184: Increase the default stack size for secondary threads on - Issue #14184: Increase the default stack size for secondary threads on
Mac OS X to avoid interpreter crashes when using threads on 10.7. Mac OS X to avoid interpreter crashes when using threads on 10.7.
- Issue #14180: datetime.date.fromtimestamp(),
datetime.datetime.fromtimestamp() and datetime.datetime.utcfromtimestamp()
now raise an OSError instead of ValueError if localtime() or gmtime() failed.
- Issue #14180: time.ctime(), gmtime(), time.localtime(), - Issue #14180: time.ctime(), gmtime(), time.localtime(),
datetime.date.fromtimestamp(), datetime.datetime.fromtimestamp() and datetime.date.fromtimestamp(), datetime.datetime.fromtimestamp() and
datetime.datetime.utcfromtimestamp() now raises an OverflowError, instead of datetime.datetime.utcfromtimestamp() now raises an OverflowError, instead of
......
...@@ -2443,22 +2443,25 @@ date_local_from_object(PyObject *cls, PyObject *obj) ...@@ -2443,22 +2443,25 @@ date_local_from_object(PyObject *cls, PyObject *obj)
{ {
struct tm *tm; struct tm *tm;
time_t t; time_t t;
PyObject *result = NULL;
if (_PyTime_ObjectToTime_t(obj, &t) == -1) if (_PyTime_ObjectToTime_t(obj, &t) == -1)
return NULL; return NULL;
tm = localtime(&t); tm = localtime(&t);
if (tm) if (tm == NULL) {
result = PyObject_CallFunction(cls, "iii", /* unconvertible time */
tm->tm_year + 1900, #ifdef EINVAL
tm->tm_mon + 1, if (errno == 0)
tm->tm_mday); errno = EINVAL;
else #endif
PyErr_SetString(PyExc_ValueError, PyErr_SetFromErrno(PyExc_OSError);
"timestamp out of range for " return NULL;
"platform localtime() function"); }
return result;
return PyObject_CallFunction(cls, "iii",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday);
} }
/* Return new date from current time. /* Return new date from current time.
...@@ -4057,33 +4060,33 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, ...@@ -4057,33 +4060,33 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us,
PyObject *tzinfo) PyObject *tzinfo)
{ {
struct tm *tm; struct tm *tm;
PyObject *result = NULL;
tm = f(&timet); tm = f(&timet);
if (tm) { if (tm == NULL) {
/* The platform localtime/gmtime may insert leap seconds, #ifdef EINVAL
* indicated by tm->tm_sec > 59. We don't care about them, if (errno == 0)
* except to the extent that passing them on to the datetime errno = EINVAL;
* constructor would raise ValueError for a reason that #endif
* made no sense to the user. return PyErr_SetFromErrno(PyExc_OSError);
*/
if (tm->tm_sec > 59)
tm->tm_sec = 59;
result = PyObject_CallFunction(cls, "iiiiiiiO",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
us,
tzinfo);
} }
else
PyErr_SetString(PyExc_ValueError, /* The platform localtime/gmtime may insert leap seconds,
"timestamp out of range for " * indicated by tm->tm_sec > 59. We don't care about them,
"platform localtime()/gmtime() function"); * except to the extent that passing them on to the datetime
return result; * constructor would raise ValueError for a reason that
* made no sense to the user.
*/
if (tm->tm_sec > 59)
tm->tm_sec = 59;
return PyObject_CallFunction(cls, "iiiiiiiO",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
us,
tzinfo);
} }
/* Internal helper. /* Internal helper.
...@@ -4102,7 +4105,7 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, ...@@ -4102,7 +4105,7 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp,
if (_PyTime_ObjectToTimeval(timestamp, &timet, &us) == -1) if (_PyTime_ObjectToTimeval(timestamp, &timet, &us) == -1)
return NULL; return NULL;
return datetime_from_timet_and_us(cls, f, timet, us, tzinfo); return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo);
} }
/* Internal helper. /* Internal helper.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment