[Python-checkins] cpython: Move assertion inside _PyTime_ObjectToTimeval()

victor.stinner python-checkins at python.org
Wed Sep 2 01:40:00 CEST 2015


https://hg.python.org/cpython/rev/5602d2094d2e
changeset:   97567:5602d2094d2e
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Sep 02 00:50:43 2015 +0200
summary:
  Move assertion inside _PyTime_ObjectToTimeval()

Change also _PyTime_FromSeconds() assertion to ensure that the _PyTime_t type
is used.

files:
  Modules/_datetimemodule.c |   1 -
  Python/pytime.c           |  20 ++++++++++++++------
  2 files changed, 14 insertions(+), 7 deletions(-)


diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -4100,7 +4100,6 @@
     if (_PyTime_ObjectToTimeval(timestamp,
                                 &timet, &us, _PyTime_ROUND_FLOOR) == -1)
         return NULL;
-    assert(0 <= us && us <= 999999);
 
     return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo);
 }
diff --git a/Python/pytime.c b/Python/pytime.c
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -101,7 +101,8 @@
 _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
                             double denominator, _PyTime_round_t round)
 {
-    assert(denominator <= LONG_MAX);
+    assert(denominator <= (double)LONG_MAX);
+
     if (PyFloat_Check(obj)) {
         double d = PyFloat_AsDouble(obj);
         return _PyTime_DoubleToDenominator(d, sec, numerator,
@@ -149,14 +150,20 @@
 _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
                          _PyTime_round_t round)
 {
-    return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
+    int res;
+    res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
+    assert(0 <= *nsec && *nsec < SEC_TO_NS );
+    return res;
 }
 
 int
 _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
                         _PyTime_round_t round)
 {
-    return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
+    int res;
+    res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
+    assert(0 <= *usec && *usec < SEC_TO_US );
+    return res;
 }
 
 static void
@@ -170,12 +177,13 @@
 _PyTime_FromSeconds(int seconds)
 {
     _PyTime_t t;
+    t = (_PyTime_t)seconds;
     /* ensure that integer overflow cannot happen, int type should have 32
        bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
        bits). */
-    assert((seconds >= 0 && seconds <= _PyTime_MAX / SEC_TO_NS)
-           || (seconds < 0 && seconds >= _PyTime_MIN / SEC_TO_NS));
-    t = (_PyTime_t)seconds * SEC_TO_NS;
+    assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
+           || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
+    t *= SEC_TO_NS;
     return t;
 }
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list