[pypy-commit] pypy default: datetime shouldn't allow float arguments for various things

alex_gaynor noreply at buildbot.pypy.org
Tue Feb 14 02:00:37 CET 2012


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r52436:ea7cd308d573
Date: 2012-02-13 20:00 -0500
http://bitbucket.org/pypy/pypy/changeset/ea7cd308d573/

Log:	datetime shouldn't allow float arguments for various things

diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -271,8 +271,9 @@
     raise ValueError("%s()=%d, must be in -1439..1439" % (name, offset))
 
 def _check_date_fields(year, month, day):
-    if not isinstance(year, (int, long)):
-        raise TypeError('int expected')
+    for value in [year, day]:
+        if not isinstance(value, (int, long)):
+            raise TypeError('int expected')
     if not MINYEAR <= year <= MAXYEAR:
         raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year)
     if not 1 <= month <= 12:
@@ -282,8 +283,9 @@
         raise ValueError('day must be in 1..%d' % dim, day)
 
 def _check_time_fields(hour, minute, second, microsecond):
-    if not isinstance(hour, (int, long)):
-        raise TypeError('int expected')
+    for value in [hour, minute, second, microsecond]:
+        if not isinstance(value, (int, long)):
+            raise TypeError('int expected')
     if not 0 <= hour <= 23:
         raise ValueError('hour must be in 0..23', hour)
     if not 0 <= minute <= 59:
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
@@ -1,7 +1,10 @@
 """Additional tests for datetime."""
 
+import py
+
 import time
 import datetime
+import copy
 import os
 
 def test_utcfromtimestamp():
@@ -26,3 +29,18 @@
 def test_utcfromtimestamp_microsecond():
     dt = datetime.datetime.utcfromtimestamp(0)
     assert isinstance(dt.microsecond, int)
+
+
+def test_integer_args():
+    with py.test.raises(TypeError):
+        datetime.datetime(10, 10, 10.)
+    with py.test.raises(TypeError):
+        datetime.datetime(10, 10, 10, 10, 10.)
+    with py.test.raises(TypeError):
+        datetime.datetime(10, 10, 10, 10, 10, 10.)
+
+def test_utcnow_microsecond():
+    dt = datetime.datetime.utcnow()
+    assert type(dt.microsecond) is int
+
+    copy.copy(dt)
\ No newline at end of file


More information about the pypy-commit mailing list