Author: Brian Kearns <[email protected]>
Branch: py3k
Changeset: r69774:ca4dd62f2af0
Date: 2014-03-06 21:40 -0500
http://bitbucket.org/pypy/pypy/changeset/ca4dd62f2af0/
Log: check datetime __format__ argument
diff --git a/lib-python/3/datetime.py b/lib-python/3/datetime.py
--- a/lib-python/3/datetime.py
+++ b/lib-python/3/datetime.py
@@ -747,6 +747,8 @@
return _wrap_strftime(self, fmt, self.timetuple())
def __format__(self, fmt):
+ if not isinstance(fmt, str):
+ raise TypeError("must be str, not %s" % type(fmt).__name__)
if len(fmt) != 0:
return self.strftime(fmt)
return str(self)
@@ -1211,6 +1213,8 @@
return _wrap_strftime(self, fmt, timetuple)
def __format__(self, fmt):
+ if not isinstance(fmt, str):
+ raise TypeError("must be str, not %s" % type(fmt).__name__)
if len(fmt) != 0:
return self.strftime(fmt)
return str(self)
diff --git a/lib-python/3/test/datetimetester.py
b/lib-python/3/test/datetimetester.py
--- a/lib-python/3/test/datetimetester.py
+++ b/lib-python/3/test/datetimetester.py
@@ -1131,11 +1131,13 @@
#check that this standard extension works
t.strftime("%f")
-
def test_format(self):
dt = self.theclass(2007, 9, 10)
self.assertEqual(dt.__format__(''), str(dt))
+ with self.assertRaisesRegex(TypeError, '^must be str, not int$'):
+ dt.__format__(123)
+
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
@@ -1464,6 +1466,9 @@
dt = self.theclass(2007, 9, 10, 4, 5, 1, 123)
self.assertEqual(dt.__format__(''), str(dt))
+ with self.assertRaisesRegex(TypeError, '^must be str, not int$'):
+ dt.__format__(123)
+
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
@@ -1775,6 +1780,7 @@
for insane in -1e200, 1e200:
self.assertRaises(ValueError, self.theclass.utcfromtimestamp,
insane)
+
@unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative
timestamps")
def test_negative_float_fromtimestamp(self):
# The result is tz-dependent; at least test that this doesn't
@@ -2153,6 +2159,9 @@
t = self.theclass(1, 2, 3, 4)
self.assertEqual(t.__format__(''), str(t))
+ with self.assertRaisesRegex(TypeError, '^must be str, not int$'):
+ t.__format__(123)
+
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
@@ -3728,13 +3737,15 @@
datetime(10, 10, '10')
f10 = Number(10.9)
- with self.assertRaisesRegex(TypeError, '^nb_int should return int
object$'):
+ with self.assertRaisesRegex(TypeError, '^nb_int should return int '
+ 'object$'):
datetime(10, 10, f10)
class Float(float):
pass
s10 = Float(10.9)
- with self.assertRaisesRegex(TypeError, '^integer argument expected,
got float$'):
+ with self.assertRaisesRegex(TypeError, '^integer argument expected, '
+ 'got float$'):
datetime(10, 10, s10)
with self.assertRaises(TypeError):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit