summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2008-02-17 19:48:00 (GMT)
committerEric Smith <eric@trueblade.com>2008-02-17 19:48:00 (GMT)
commit8fd3eba0501a77eec463179f529f56025ff4b40b (patch)
treed459220bd52a30ce3de89b89ed1db077e7b5d4a4 /Lib/test
parent18c66898b0a14761786161c07d89d65c8f088601 (diff)
downloadcpython-8fd3eba0501a77eec463179f529f56025ff4b40b.zip
cpython-8fd3eba0501a77eec463179f529f56025ff4b40b.tar.gz
cpython-8fd3eba0501a77eec463179f529f56025ff4b40b.tar.bz2
Fixes for shared 2.6 code that implements PEP 3101, advanced string
formatting. Includes: - Modifying tests for basic types to use __format__ methods, instead of builtin "format". - Adding PyObject_Format. - General str/unicode cleanup discovered when backporting to 2.6. - Removing datetimemodule.c's time_format, since it was identical to date_format. The files in Objects/stringlib that implement PEP 3101 (stringdefs.h, unicodedefs.h, formatter.h, string_format.h) are identical in trunk and py3k. Any changes from here on should be made to trunk, and changes will propogate to py3k).
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_builtin.py79
-rw-r--r--Lib/test/test_datetime.py38
2 files changed, 79 insertions, 38 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 762afad..42c55cc 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -541,24 +541,58 @@ class BuiltinTest(unittest.TestCase):
self.assertRaises(TypeError, float, Foo4(42))
def test_format(self):
- class A:
- def __init__(self, x):
- self.x = x
- def __format__(self, format_spec):
- return str(self.x) + format_spec
+ # Test the basic machinery of the format() builtin. Don't test
+ # the specifics of the various formatters
+ self.assertEqual(format(3, ''), '3')
- # class that returns a bad type from __format__
- class B:
- def __format__(self, format_spec):
- return 1.0
+ # Returns some classes to use for various tests. There's
+ # an old-style version, and a new-style version
+ def classes_new():
+ class A(object):
+ def __init__(self, x):
+ self.x = x
+ def __format__(self, format_spec):
+ return str(self.x) + format_spec
+ class DerivedFromA(A):
+ pass
- # class that is derived from string, used
- # as a format spec
- class C(str):
- pass
+ class Simple(object): pass
+ class DerivedFromSimple(Simple):
+ def __init__(self, x):
+ self.x = x
+ def __format__(self, format_spec):
+ return str(self.x) + format_spec
+ class DerivedFromSimple2(DerivedFromSimple): pass
+ return A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2
+
+ # In 3.0, classes_classic has the same meaning as classes_new
+ def classes_classic():
+ class A:
+ def __init__(self, x):
+ self.x = x
+ def __format__(self, format_spec):
+ return str(self.x) + format_spec
+ class DerivedFromA(A):
+ pass
- self.assertEqual(format(3, ''), '3')
- self.assertEqual(format(A(3), 'spec'), '3spec')
+ class Simple: pass
+ class DerivedFromSimple(Simple):
+ def __init__(self, x):
+ self.x = x
+ def __format__(self, format_spec):
+ return str(self.x) + format_spec
+ class DerivedFromSimple2(DerivedFromSimple): pass
+ return A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2
+
+ def class_test(A, DerivedFromA, DerivedFromSimple, DerivedFromSimple2):
+ self.assertEqual(format(A(3), 'spec'), '3spec')
+ self.assertEqual(format(DerivedFromA(4), 'spec'), '4spec')
+ self.assertEqual(format(DerivedFromSimple(5), 'abc'), '5abc')
+ self.assertEqual(format(DerivedFromSimple2(10), 'abcdef'),
+ '10abcdef')
+
+ class_test(*classes_new())
+ class_test(*classes_classic())
def empty_format_spec(value):
# test that:
@@ -578,19 +612,28 @@ class BuiltinTest(unittest.TestCase):
empty_format_spec(None)
# TypeError because self.__format__ returns the wrong type
- self.assertRaises(TypeError, format, B(), "")
+ class BadFormatResult:
+ def __format__(self, format_spec):
+ return 1.0
+ self.assertRaises(TypeError, format, BadFormatResult(), "")
- # TypeError because format_spec is not unicode
+ # TypeError because format_spec is not unicode or str
self.assertRaises(TypeError, format, object(), 4)
self.assertRaises(TypeError, format, object(), object())
+ # tests for object.__format__ really belong elsewhere, but
+ # there's no good place to put them
+ x = object().__format__('')
+ self.assert_(x.startswith('<object object at'))
+
# first argument to object.__format__ must be string
self.assertRaises(TypeError, object().__format__, 3)
self.assertRaises(TypeError, object().__format__, object())
self.assertRaises(TypeError, object().__format__, None)
# make sure we can take a subclass of str as a format spec
- self.assertEqual(format(0, C('10')), ' 0')
+ class DerivedFromStr(str): pass
+ self.assertEqual(format(0, DerivedFromStr('10')), ' 0')
def test_floatasratio(self):
for f, ratio in [
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index 7c08f9e..8ac7160 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -851,29 +851,29 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
def test_format(self):
dt = self.theclass(2007, 9, 10)
- self.assertEqual(format(dt, ''), str(dt))
+ self.assertEqual(dt.__format__(''), str(dt))
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
return 'A'
a = A(2007, 9, 10)
- self.assertEqual(format(a, ''), 'A')
+ self.assertEqual(a.__format__(''), 'A')
# check that a derived class's strftime gets called
class B(self.theclass):
def strftime(self, format_spec):
return 'B'
b = B(2007, 9, 10)
- self.assertEqual(format(b, ''), str(dt))
+ self.assertEqual(b.__format__(''), str(dt))
for fmt in ["m:%m d:%d y:%y",
"m:%m d:%d y:%y H:%H M:%M S:%S",
"%z %Z",
]:
- self.assertEqual(format(dt, fmt), dt.strftime(fmt))
- self.assertEqual(format(a, fmt), dt.strftime(fmt))
- self.assertEqual(format(b, fmt), 'B')
+ self.assertEqual(dt.__format__(fmt), dt.strftime(fmt))
+ self.assertEqual(a.__format__(fmt), dt.strftime(fmt))
+ self.assertEqual(b.__format__(fmt), 'B')
def test_resolution_info(self):
self.assert_(isinstance(self.theclass.min, self.theclass))
@@ -1178,31 +1178,29 @@ class TestDateTime(TestDate):
def test_format(self):
dt = self.theclass(2007, 9, 10, 4, 5, 1, 123)
- self.assertEqual(format(dt, ''), str(dt))
+ self.assertEqual(dt.__format__(''), str(dt))
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
return 'A'
a = A(2007, 9, 10, 4, 5, 1, 123)
- self.assertEqual(format(a, ''), 'A')
+ self.assertEqual(a.__format__(''), 'A')
# check that a derived class's strftime gets called
class B(self.theclass):
def strftime(self, format_spec):
return 'B'
b = B(2007, 9, 10, 4, 5, 1, 123)
- self.assertEqual(format(b, ''), str(dt))
+ self.assertEqual(b.__format__(''), str(dt))
for fmt in ["m:%m d:%d y:%y",
"m:%m d:%d y:%y H:%H M:%M S:%S",
"%z %Z",
]:
- self.assertEqual(format(dt, fmt), dt.strftime(fmt))
- self.assertEqual(format(a, fmt), dt.strftime(fmt))
- self.assertEqual(format(b, fmt), 'B')
-
-
+ self.assertEqual(dt.__format__(fmt), dt.strftime(fmt))
+ self.assertEqual(a.__format__(fmt), dt.strftime(fmt))
+ self.assertEqual(b.__format__(fmt), 'B')
def test_more_ctime(self):
# Test fields that TestDate doesn't touch.
@@ -1837,27 +1835,27 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
def test_format(self):
t = self.theclass(1, 2, 3, 4)
- self.assertEqual(format(t, ''), str(t))
+ self.assertEqual(t.__format__(''), str(t))
# check that a derived class's __str__() gets called
class A(self.theclass):
def __str__(self):
return 'A'
a = A(1, 2, 3, 4)
- self.assertEqual(format(a, ''), 'A')
+ self.assertEqual(a.__format__(''), 'A')
# check that a derived class's strftime gets called
class B(self.theclass):
def strftime(self, format_spec):
return 'B'
b = B(1, 2, 3, 4)
- self.assertEqual(format(b, ''), str(t))
+ self.assertEqual(b.__format__(''), str(t))
for fmt in ['%H %M %S',
]:
- self.assertEqual(format(t, fmt), t.strftime(fmt))
- self.assertEqual(format(a, fmt), t.strftime(fmt))
- self.assertEqual(format(b, fmt), 'B')
+ self.assertEqual(t.__format__(fmt), t.strftime(fmt))
+ self.assertEqual(a.__format__(fmt), t.strftime(fmt))
+ self.assertEqual(b.__format__(fmt), 'B')
def test_str(self):
self.assertEqual(str(self.theclass(1, 2, 3, 4)), "01:02:03.000004")