[Python-checkins] cpython (merge 3.2 -> default): Issue #11576: Fixed timedelta subtraction glitch on big timedelta values

alexander.belopolsky python-checkins at python.org
Wed Apr 6 02:44:01 CEST 2011


http://hg.python.org/cpython/rev/d492915cf76d
changeset:   69164:d492915cf76d
parent:      69161:7b5d09343929
parent:      69163:76180cc853b6
user:        Alexander Belopolsky <alexander.belopolsky at gmail.com>
date:        Tue Apr 05 20:43:15 2011 -0400
summary:
  Issue #11576: Fixed timedelta subtraction glitch on big timedelta values

files:
  Lib/datetime.py            |   6 +++++-
  Lib/test/datetimetester.py |   6 ++++++
  Modules/_datetimemodule.c  |  15 ++++++++-------
  3 files changed, 19 insertions(+), 8 deletions(-)


diff --git a/Lib/datetime.py b/Lib/datetime.py
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -485,7 +485,11 @@
 
     def __sub__(self, other):
         if isinstance(other, timedelta):
-            return self + -other
+            # for CPython compatibility, we cannot use
+            # our __class__ here, but need a real timedelta
+            return timedelta(self._days - other._days,
+                             self._seconds - other._seconds,
+                             self._microseconds - other._microseconds)
         return NotImplemented
 
     def __rsub__(self, other):
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -383,6 +383,12 @@
         for i in range(-10, 10):
             eq((i*us/-3)//us, round(i/-3))
 
+        # Issue #11576
+        eq(td(999999999, 86399, 999999) - td(999999999, 86399, 999998),
+           td(0, 0, 1))
+        eq(td(999999999, 1, 1) - td(999999999, 1, 0),
+           td(0, 0, 1))
+
     def test_disallowed_computations(self):
         a = timedelta(42)
 
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -1801,13 +1801,14 @@
 
     if (PyDelta_Check(left) && PyDelta_Check(right)) {
         /* delta - delta */
-        PyObject *minus_right = PyNumber_Negative(right);
-        if (minus_right) {
-            result = delta_add(left, minus_right);
-            Py_DECREF(minus_right);
-        }
-        else
-            result = NULL;
+        /* The C-level additions can't overflow because of the
+         * invariant bounds.
+         */
+        int days = GET_TD_DAYS(left) - GET_TD_DAYS(right);
+        int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right);
+        int microseconds = GET_TD_MICROSECONDS(left) -
+                           GET_TD_MICROSECONDS(right);
+        result = new_delta(days, seconds, microseconds, 1);
     }
 
     if (result == Py_NotImplemented)

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


More information about the Python-checkins mailing list