Author: Brian Kearns <[email protected]>
Branch: 
Changeset: r61014:43e61ecb2e40
Date: 2013-02-09 20:46 -0500
http://bitbucket.org/pypy/pypy/changeset/43e61ecb2e40/

Log:    further improve datetime argument handling

diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -18,7 +18,6 @@
 
 import time as _time
 import math as _math
-import decimal as _decimal
 
 MINYEAR = 1
 MAXYEAR = 9999
@@ -272,9 +271,15 @@
     raise ValueError("%s()=%d, must be in -1439..1439" % (name, offset))
 
 def _check_int_field(value):
-    if not isinstance(value, (int, long, _decimal.Decimal)):
-        raise TypeError('integer argument expected')
-    return int(value)
+    if not isinstance(value, float):
+        try:
+            value = value.__int__()
+        except AttributeError:
+            pass
+        else:
+            if isinstance(value, int):
+                return value
+    raise TypeError('integer argument expected')
 
 def _check_date_fields(year, month, day):
     year = _check_int_field(year)
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
@@ -45,10 +45,31 @@
     l10 = 10L
     d10 = decimal.Decimal(10)
     d11 = decimal.Decimal(10.9)
+    class C10:
+        def __int__(self):
+            return 10
+    c10 = C10()
     assert datetime.datetime(i10, i10, i10, i10, i10, i10, i10) == \
            datetime.datetime(l10, l10, l10, l10, l10, l10, l10) == \
            datetime.datetime(d10, d10, d10, d10, d10, d10, d10) == \
-           datetime.datetime(d11, d11, d11, d11, d11, d11, d11)
+           datetime.datetime(d11, d11, d11, d11, d11, d11, d11) == \
+           datetime.datetime(c10, c10, c10, c10, c10, c10, c10)
+
+    with py.test.raises(TypeError):
+        datetime.datetime(10, '10', 10)
+
+    class S10(float):
+        pass
+    s10 = S10(10.9)
+    with py.test.raises(TypeError):
+        datetime.datetime(10, s10, 10)
+
+    class F10:
+        def __int__(self):
+            return 10.9
+    f10 = F10()
+    with py.test.raises(TypeError):
+        datetime.datetime(10, f10, 10)
 
     with py.test.raises(TypeError):
         datetime.datetime(10., 10, 10)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to