[pypy-commit] pypy default: Merged in posita/pypy/posita/2193-datetime-timedelta-integrals (pull request #361)

bdkearns noreply at buildbot.pypy.org
Mon Nov 23 15:17:43 EST 2015


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r80866:3e66caad1c36
Date: 2015-11-23 12:18 -0800
http://bitbucket.org/pypy/pypy/changeset/3e66caad1c36/

Log:	Merged in posita/pypy/posita/2193-datetime-timedelta-integrals (pull
	request #361)

	Fix #2193.

diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -17,6 +17,7 @@
 """
 
 from __future__ import division
+import numbers as _numbers
 import time as _time
 import math as _math
 import struct as _struct
@@ -278,7 +279,7 @@
         except AttributeError:
             pass
         else:
-            if isinstance(value, (int, long)):
+            if isinstance(value, _numbers.Integral):
                 return value
             raise TypeError('__int__ method should return an integer')
         raise TypeError('an integer is required')
@@ -468,7 +469,7 @@
             d = days
         assert isinstance(daysecondsfrac, float)
         assert abs(daysecondsfrac) <= 1.0
-        assert isinstance(d, (int, long))
+        assert isinstance(d, _numbers.Integral)
         assert abs(s) <= 24 * 3600
         # days isn't referenced again before redefinition
 
@@ -484,11 +485,11 @@
         assert isinstance(secondsfrac, float)
         assert abs(secondsfrac) <= 2.0
 
-        assert isinstance(seconds, (int, long))
+        assert isinstance(seconds, _numbers.Integral)
         days, seconds = divmod(seconds, 24*3600)
         d += days
         s += int(seconds)    # can't overflow
-        assert isinstance(s, int)
+        assert isinstance(s, _numbers.Integral)
         assert abs(s) <= 2 * 24 * 3600
         # seconds isn't referenced again before redefinition
 
@@ -510,8 +511,8 @@
             d += days
             s += int(seconds)
             microseconds = _round(microseconds + usdouble)
-        assert isinstance(s, int)
-        assert isinstance(microseconds, int)
+        assert isinstance(s, _numbers.Integral)
+        assert isinstance(microseconds, _numbers.Integral)
         assert abs(s) <= 3 * 24 * 3600
         assert abs(microseconds) < 3.1e6
 
@@ -521,9 +522,9 @@
         days, s = divmod(s, 24*3600)
         d += days
 
-        assert isinstance(d, (int, long))
-        assert isinstance(s, int) and 0 <= s < 24*3600
-        assert isinstance(us, int) and 0 <= us < 1000000
+        assert isinstance(d, _numbers.Integral)
+        assert isinstance(s, _numbers.Integral) and 0 <= s < 24*3600
+        assert isinstance(us, _numbers.Integral) and 0 <= us < 1000000
 
         if abs(d) > 999999999:
             raise OverflowError("timedelta # of days is too large: %d" % d)
@@ -623,7 +624,7 @@
             return self
 
     def __mul__(self, other):
-        if isinstance(other, (int, long)):
+        if isinstance(other, _numbers.Integral):
             # for CPython compatibility, we cannot use
             # our __class__ here, but need a real timedelta
             return timedelta(self._days * other,
@@ -638,7 +639,7 @@
                 self._microseconds)
 
     def __div__(self, other):
-        if not isinstance(other, (int, long)):
+        if not isinstance(other, _numbers.Integral):
             return NotImplemented
         usec = self._to_microseconds()
         return timedelta(0, 0, usec // other)
@@ -1510,7 +1511,7 @@
 
         converter = _time.localtime if tz is None else _time.gmtime
 
-        if isinstance(timestamp, int):
+        if isinstance(timestamp, _numbers.Integral):
             us = 0
         else:
             t_full = timestamp
@@ -1535,7 +1536,7 @@
     @classmethod
     def utcfromtimestamp(cls, t):
         "Construct a UTC datetime from a POSIX timestamp (like time.time())."
-        if isinstance(t, int):
+        if isinstance(t, _numbers.Integral):
             us = 0
         else:
             t_full = t
diff --git a/pypy/module/test_lib_pypy/test_datetime.py b/pypy/module/test_lib_pypy/test_datetime.py
--- a/pypy/module/test_lib_pypy/test_datetime.py
+++ b/pypy/module/test_lib_pypy/test_datetime.py
@@ -242,6 +242,64 @@
             naive == aware
         assert str(e.value) == "can't compare offset-naive and offset-aware times"
 
+    def test_future_types_newint(self):
+        try:
+            from future.types.newint import newint
+        except ImportError:
+            py.test.skip('requires future')
+
+        dt_from_ints = datetime.datetime(2015, 12, 31, 12, 34, 56)
+        dt_from_newints = datetime.datetime(newint(2015), newint(12), newint(31), newint(12), newint(34), newint(56))
+        dt_from_mixed = datetime.datetime(2015, newint(12), 31, newint(12), 34, newint(56))
+        assert dt_from_ints == dt_from_newints
+        assert dt_from_newints == dt_from_mixed
+        assert dt_from_mixed == dt_from_ints
+
+        d_from_int = datetime.date.fromtimestamp(1431216000)
+        d_from_newint = datetime.date.fromtimestamp(newint(1431216000))
+        assert d_from_int == d_from_newint
+
+        dt_from_int = datetime.datetime.fromtimestamp(1431216000)
+        dt_from_newint = datetime.datetime.fromtimestamp(newint(1431216000))
+        assert dt_from_int == dt_from_newint
+
+        dtu_from_int = datetime.datetime.utcfromtimestamp(1431216000)
+        dtu_from_newint = datetime.datetime.utcfromtimestamp(newint(1431216000))
+        assert dtu_from_int == dtu_from_newint
+
+        td_from_int = datetime.timedelta(16565)
+        tds_from_int = datetime.timedelta(seconds=1431216000)
+        td_from_newint = datetime.timedelta(newint(16565))
+        tds_from_newint = datetime.timedelta(seconds=newint(1431216000))
+        assert td_from_int == tds_from_int
+        assert td_from_int == td_from_newint
+        assert td_from_int == tds_from_newint
+        assert tds_from_int == td_from_newint
+        assert tds_from_int == tds_from_newint
+        assert td_from_newint == tds_from_newint
+
+        td_mul_int_int = td_from_int * 2
+        td_mul_int_newint = td_from_int * newint(2)
+        td_mul_newint_int = td_from_newint * 2
+        td_mul_newint_newint = td_from_newint * newint(2)
+        assert td_mul_int_int == td_mul_int_newint
+        assert td_mul_int_int == td_mul_newint_int
+        assert td_mul_int_int == td_mul_newint_newint
+        assert td_mul_int_newint == td_mul_newint_int
+        assert td_mul_int_newint == td_mul_newint_newint
+        assert td_mul_newint_int == td_mul_newint_newint
+
+        td_div_int_int = td_from_int / 3600
+        td_div_int_newint = td_from_int / newint(3600)
+        td_div_newint_int = td_from_newint / 3600
+        td_div_newint_newint = td_from_newint / newint(3600)
+        assert td_div_int_int == td_div_int_newint
+        assert td_div_int_int == td_div_newint_int
+        assert td_div_int_int == td_div_newint_newint
+        assert td_div_int_newint == td_div_newint_int
+        assert td_div_int_newint == td_div_newint_newint
+        assert td_div_newint_int == td_div_newint_newint
+
 
 class TestDatetimeHost(BaseTestDatetime):
     def setup_class(cls):


More information about the pypy-commit mailing list