]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
timedelta.total_seconds() is new in python2.7, so do the math ourselves.
authorBen Darnell <ben@bendarnell.com>
Tue, 23 Aug 2011 05:45:27 +0000 (22:45 -0700)
committerBen Darnell <ben@bendarnell.com>
Tue, 23 Aug 2011 05:47:16 +0000 (22:47 -0700)
tornado/ioloop.py
tornado/test/ioloop_test.py

index f31bb5da5ceb098964527ae7d748f0e1a6c9d59d..0a97304bcd4d197c16f2d3491da07d0be9f68b2c 100644 (file)
@@ -420,11 +420,16 @@ class _Timeout(object):
         if isinstance(deadline, (int, long, float)):
             self.deadline = deadline
         elif isinstance(deadline, datetime.timedelta):
-            self.deadline = time.time() + deadline.total_seconds()
+            self.deadline = time.time() + _Timeout.timedelta_to_seconds(deadline)
         else:
             raise TypeError("Unsupported deadline %r" % deadline)
         self.callback = callback
 
+    @staticmethod
+    def timedelta_to_seconds(td):
+        """Equivalent to td.total_seconds() (introduced in python 2.7)."""
+        return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6)
+
     # Comparison methods to sort by deadline, with object id as a tiebreaker
     # to guarantee a consistent ordering.  The heapq module uses __le__
     # in python2.5, and __lt__ in 2.6+ (sort() and most other comparisons
index 2c0f5f12eee43268533481646601f1cd44a291a8..74bb60282d81c9325c2f5443b55705b793c8d1c5 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import datetime
 import unittest
 import time
 
@@ -23,5 +24,9 @@ class TestIOLoop(AsyncTestCase, LogTrapTestCase):
         self.assertAlmostEqual(time.time(), self.start_time, places=2)
         self.assertTrue(self.called)
 
+    def test_add_timeout_timedelta(self):
+        self.io_loop.add_timeout(datetime.timedelta(microseconds=1), self.stop)
+        self.wait()
+
 if __name__ == "__main__":
     unittest.main()