summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2004-06-20 02:50:16 (GMT)
committerTim Peters <tim.peters@gmail.com>2004-06-20 02:50:16 (GMT)
commit1b6f7a9057874ecd2793059f210de87837fe1911 (patch)
tree950e95d9866258870fc075706b99de3fcfbf2e72 /Modules
parent1c3fa18be76d1bcddb2de516913f46a32c5ed860 (diff)
downloadcpython-1b6f7a9057874ecd2793059f210de87837fe1911.zip
cpython-1b6f7a9057874ecd2793059f210de87837fe1911.tar.gz
cpython-1b6f7a9057874ecd2793059f210de87837fe1911.tar.bz2
Bug 975996: Add _PyTime_DoubleToTimet to C API
New include file timefuncs.h exports private API function _PyTime_DoubleToTimet() from timemodule.c. timemodule should export some other functions too (look for painful bits in datetimemodule.c). Added insane-argument checking to datetime's assorted fromtimestamp() and utcfromtimestamp() methods. Added insane-argument tests of these to test_datetime, and insane-argument tests for ctime(), localtime() and gmtime() to test_time.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/datetimemodule.c20
-rw-r--r--Modules/timemodule.c12
2 files changed, 20 insertions, 12 deletions
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index 7f38d1a..d9cf604 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -8,6 +8,7 @@
#include <time.h>
+#include "timefuncs.h"
#include "datetime.h"
/* We require that C int be at least 32 bits, and use int virtually
@@ -2226,11 +2227,15 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
/* Return new date from localtime(t). */
static PyObject *
-date_local_from_time_t(PyObject *cls, time_t t)
+date_local_from_time_t(PyObject *cls, double ts)
{
struct tm *tm;
+ time_t t;
PyObject *result = NULL;
+ t = _PyTime_DoubleToTimet(ts);
+ if (t == (time_t)-1 && PyErr_Occurred())
+ return NULL;
tm = localtime(&t);
if (tm)
result = PyObject_CallFunction(cls, "iii",
@@ -2278,7 +2283,7 @@ date_fromtimestamp(PyObject *cls, PyObject *args)
PyObject *result = NULL;
if (PyArg_ParseTuple(args, "d:fromtimestamp", &timestamp))
- result = date_local_from_time_t(cls, (time_t)timestamp);
+ result = date_local_from_time_t(cls, timestamp);
return result;
}
@@ -3654,10 +3659,15 @@ static PyObject *
datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp,
PyObject *tzinfo)
{
- time_t timet = (time_t)timestamp;
- double fraction = timestamp - (double)timet;
- int us = (int)round_to_long(fraction * 1e6);
+ time_t timet;
+ double fraction;
+ int us;
+ timet = _PyTime_DoubleToTimet(timestamp);
+ if (timet == (time_t)-1 && PyErr_Occurred())
+ return NULL;
+ fraction = timestamp - (double)timet;
+ us = (int)round_to_long(fraction * 1e6);
return datetime_from_timet_and_us(cls, f, timet, us, tzinfo);
}
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 0783f7f..f855796 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -3,6 +3,7 @@
#include "Python.h"
#include "structseq.h"
+#include "timefuncs.h"
#include <ctype.h>
@@ -84,11 +85,8 @@ static double floattime(void);
/* For Y2K check */
static PyObject *moddict;
-/* Cast double x to time_t, but raise ValueError if x is too large
- * to fit in a time_t. ValueError is set on return iff the return
- * value is (time_t)-1 and PyErr_Occurred().
- */
-static time_t
+/* Exposed in timefuncs.h. */
+time_t
_PyTime_DoubleToTimet(double x)
{
time_t result;
@@ -382,7 +380,7 @@ time_strftime(PyObject *self, PyObject *args)
/* Checks added to make sure strftime() does not crash Python by
indexing blindly into some array for a textual representation
by some bad index (fixes bug #897625).
-
+
No check for year since handled in gettmarg().
*/
if (buf.tm_mon < 0 || buf.tm_mon > 11) {
@@ -583,7 +581,7 @@ time_tzset(PyObject *self, PyObject *args)
/* Reset timezone, altzone, daylight and tzname */
inittimezone(m);
Py_DECREF(m);
-
+
Py_INCREF(Py_None);
return Py_None;
}