mirror of
https://github.com/python/cpython.git
synced 2025-07-28 21:55:21 +00:00
Backport of PEP 3101, Advanced String Formatting, from py3k.
Highlights: - Adding PyObject_Format. - Adding string.Format class. - Adding __format__ for str, unicode, int, long, float, datetime. - Adding builtin format. - Adding ''.format and u''.format. - str/unicode fixups for formatters. 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).
This commit is contained in:
parent
e139688d34
commit
a9f7d62480
27 changed files with 3873 additions and 23 deletions
|
@ -2012,6 +2012,101 @@ class TestSorted(unittest.TestCase):
|
|||
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
|
||||
self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0)
|
||||
|
||||
def test_format(self):
|
||||
# Test the basic machinery of the format() builtin. Don't test
|
||||
# the specifics of the various formatters
|
||||
self.assertEqual(format(3, ''), '3')
|
||||
|
||||
# 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 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
|
||||
|
||||
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:
|
||||
# format(x, '') == str(x)
|
||||
# format(x) == str(x)
|
||||
self.assertEqual(format(value, ""), str(value))
|
||||
self.assertEqual(format(value), str(value))
|
||||
|
||||
# for builtin types, format(x, "") == str(x)
|
||||
empty_format_spec(17**13)
|
||||
empty_format_spec(1.0)
|
||||
empty_format_spec(3.1415e104)
|
||||
empty_format_spec(-3.1415e104)
|
||||
empty_format_spec(3.1415e-104)
|
||||
empty_format_spec(-3.1415e-104)
|
||||
empty_format_spec(object)
|
||||
empty_format_spec(None)
|
||||
|
||||
# TypeError because self.__format__ returns the wrong type
|
||||
class BadFormatResult:
|
||||
def __format__(self, format_spec):
|
||||
return 1.0
|
||||
self.assertRaises(TypeError, format, BadFormatResult(), "")
|
||||
|
||||
# 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
|
||||
class DerivedFromStr(str): pass
|
||||
self.assertEqual(format(0, DerivedFromStr('10')), ' 0')
|
||||
|
||||
def test_main(verbose=None):
|
||||
test_classes = (BuiltinTest, TestSorted)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue