From: Ben Darnell Date: Tue, 23 Aug 2011 05:45:27 +0000 (-0700) Subject: timedelta.total_seconds() is new in python2.7, so do the math ourselves. X-Git-Tag: v2.1.0~44^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b7ddc91d7aa71fbd86aa8f1150dfbee74b9b196;p=thirdparty%2Ftornado.git timedelta.total_seconds() is new in python2.7, so do the math ourselves. --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index f31bb5da5..0a97304bc 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -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 diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index 2c0f5f12e..74bb60282 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -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()