11f70221b861a3714735251d57695b2f472d0599bZachary Wareimport unittest 2ba965deea89299611454e43be005df357b75b11dNeal Norwitzimport struct 37dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitzimport sys 40fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwaldfrom test import test_support, string_tests 50fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald 60fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald 78d30ad7c8ad5df59be8a65c8f8dab0d13be00dabSerhiy Storchakaclass StrSubclass(str): 88d30ad7c8ad5df59be8a65c8f8dab0d13be00dabSerhiy Storchaka pass 98d30ad7c8ad5df59be8a65c8f8dab0d13be00dabSerhiy Storchaka 100fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwaldclass StrTest( 110fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald string_tests.CommonTest, 120fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald string_tests.MixinStrUnicodeUserStringTest, 1357d88e5abd06cefcc8d52d62765c36214b1c1956Walter Dörwald string_tests.MixinStrUserStringTest, 1457d88e5abd06cefcc8d52d62765c36214b1c1956Walter Dörwald string_tests.MixinStrUnicodeTest, 150fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald ): 160fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald 170fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald type2test = str 180fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald 190fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald # We don't need to propagate to str 200fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald def fixtype(self, obj): 210fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald return obj 220fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald 23979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson def test_basic_creation(self): 24979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson self.assertEqual(str(''), '') 25979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson self.assertEqual(str(0), '0') 26979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson self.assertEqual(str(0L), '0') 27979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson self.assertEqual(str(()), '()') 28979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson self.assertEqual(str([]), '[]') 29979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson self.assertEqual(str({}), '{}') 30979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson a = [] 31979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson a.append(a) 32979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson self.assertEqual(str(a), '[[...]]') 33979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson a = {} 34979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson a[0] = a 35979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson self.assertEqual(str(a), '{0: {...}}') 36979395b7a8be422dfc9a081f1cd77260c6ea9aefBenjamin Peterson 3743440a621e022fbeaff24f84dcf4d5458601fedcWalter Dörwald def test_formatting(self): 3843440a621e022fbeaff24f84dcf4d5458601fedcWalter Dörwald string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) 3943440a621e022fbeaff24f84dcf4d5458601fedcWalter Dörwald self.assertRaises(OverflowError, '%c'.__mod__, 0x1234) 4043440a621e022fbeaff24f84dcf4d5458601fedcWalter Dörwald 4175d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson @test_support.cpython_only 4275d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson def test_formatting_huge_precision(self): 4375d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson from _testcapi import INT_MAX 4475d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson format_string = "%.{}f".format(INT_MAX + 1) 4575d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson with self.assertRaises(ValueError): 4675d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson result = format_string % 2.34 4775d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson 4875d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson def test_formatting_huge_width(self): 4975d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson format_string = "%{}f".format(sys.maxsize + 1) 5075d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson with self.assertRaises(ValueError): 5175d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson result = format_string % 2.34 5275d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson 53c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def test_conversion(self): 54c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon # Make sure __str__() behaves properly 55c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo0: 56c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __unicode__(self): 57c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return u"foo" 58c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 59c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo1: 60c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 61c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return "foo" 62c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 63c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo2(object): 64c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 65c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return "foo" 66c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 67c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo3(object): 68c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 69c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return u"foo" 70c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 71c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo4(unicode): 72c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 73c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return u"foo" 74c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 75c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo5(str): 76c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 77c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return u"foo" 78c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 79c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo6(str): 80c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 81c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return "foos" 82c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 83c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __unicode__(self): 84c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return u"foou" 85c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 86c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo7(unicode): 87c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 88c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return "foos" 89c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __unicode__(self): 90c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return u"foou" 91c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 92c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo8(str): 93c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __new__(cls, content=""): 94c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return str.__new__(cls, 2*content) 95c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 96c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return self 97c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 98c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon class Foo9(str): 99c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __str__(self): 100c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return "string" 101c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon def __unicode__(self): 102c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon return "not unicode" 103c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 1045c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson self.assertTrue(str(Foo0()).startswith("<")) # this is different from __unicode__ 105c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo1()), "foo") 106c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo2()), "foo") 107c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo3()), "foo") 108c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo4("bar")), "foo") 109c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo5("bar")), "foo") 110c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo6("bar")), "foos") 111c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo7("bar")), "foos") 112c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo8("foo")), "foofoo") 1138d30ad7c8ad5df59be8a65c8f8dab0d13be00dabSerhiy Storchaka self.assertIs(type(str(Foo8("foo"))), Foo8) 1148d30ad7c8ad5df59be8a65c8f8dab0d13be00dabSerhiy Storchaka self.assertEqual(StrSubclass(Foo8("foo")), "foofoo") 1158d30ad7c8ad5df59be8a65c8f8dab0d13be00dabSerhiy Storchaka self.assertIs(type(StrSubclass(Foo8("foo"))), StrSubclass) 116c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(str(Foo9("foo")), "string") 117c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon self.assertEqual(unicode(Foo9("foo")), u"not unicode") 118c3647ac93e2a38762de8a23b1d94a6380e9ad468Brett Cannon 1191f70221b861a3714735251d57695b2f472d0599bZachary Ware # This test only affects 32-bit platforms because expandtabs can only take 1201f70221b861a3714735251d57695b2f472d0599bZachary Ware # an int as the max value, not a 64-bit C long. If expandtabs is changed 1211f70221b861a3714735251d57695b2f472d0599bZachary Ware # to take a 64-bit long, this test should apply to all platforms. 1221f70221b861a3714735251d57695b2f472d0599bZachary Ware @unittest.skipIf(sys.maxint > (1 << 32) or struct.calcsize('P') != 4, 1231f70221b861a3714735251d57695b2f472d0599bZachary Ware 'only applies to 32-bit platforms') 1247dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz def test_expandtabs_overflows_gracefully(self): 1257dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxint) 1267dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz 127a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def test__format__(self): 128a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def test(value, format, expected): 129a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # test both with and without the trailing 's' 130a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual(value.__format__(format), expected) 131a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual(value.__format__(format + 's'), expected) 132a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 133a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('', '', '') 134a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('abc', '', 'abc') 135a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('abc', '.3', 'abc') 136a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('ab', '.3', 'ab') 137a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('abcdef', '.3', 'abc') 138a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('abcdef', '.0', '') 139a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('abc', '3.3', 'abc') 140a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('abc', '2.3', 'abc') 141a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('abc', '2.2', 'ab') 142a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('abc', '3.2', 'ab ') 143a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', 'x<0', 'result') 144a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', 'x<5', 'result') 145a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', 'x<6', 'result') 146a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', 'x<7', 'resultx') 147a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', 'x<8', 'resultxx') 148a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', ' <7', 'result ') 149a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', '<7', 'result ') 150a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', '>7', ' result') 151a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', '>8', ' result') 152a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', '^8', ' result ') 153a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', '^9', ' result ') 154a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('result', '^10', ' result ') 155a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('a', '10000', 'a' + ' ' * 9999) 156a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('', '10000', ' ' * 10000) 157a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith test('', '10000000', ' ' * 10000000) 158a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 159a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def test_format(self): 160a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual(''.format(), '') 161a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('a'.format(), 'a') 162a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('ab'.format(), 'ab') 163a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('a{{'.format(), 'a{') 164a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('a}}'.format(), 'a}') 165a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{{b'.format(), '{b') 166a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('}}b'.format(), '}b') 167a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('a{{b'.format(), 'a{b') 168a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 169a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # examples from the PEP: 170a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith import datetime 171a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("My name is {0}".format('Fred'), "My name is Fred") 172a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("My name is {0[name]}".format(dict(name='Fred')), 173a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith "My name is Fred") 174a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("My name is {0} :-{{}}".format('Fred'), 175a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith "My name is Fred :-{}") 176a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 177a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith d = datetime.date(2007, 8, 18) 178a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("The year is {0.year}".format(d), 179a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith "The year is 2007") 180a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 181a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # classes we'll use for testing 182a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith class C: 183a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __init__(self, x=100): 184a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self._x = x 185a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __format__(self, spec): 186a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return spec 187a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 188a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith class D: 189a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __init__(self, x): 190a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.x = x 191a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __format__(self, spec): 192a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return str(self.x) 193a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 194a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # class with __str__, but no __format__ 195a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith class E: 196a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __init__(self, x): 197a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.x = x 198a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __str__(self): 199a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return 'E(' + self.x + ')' 200a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 201a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # class with __repr__, but no __format__ or __str__ 202a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith class F: 203a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __init__(self, x): 204a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.x = x 205a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __repr__(self): 206a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return 'F(' + self.x + ')' 207a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 208a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # class with __format__ that forwards to string, for some format_spec's 209a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith class G: 210a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __init__(self, x): 211a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.x = x 212a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __str__(self): 213a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return "string is " + self.x 214a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __format__(self, format_spec): 215a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith if format_spec == 'd': 216a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return 'G(' + self.x + ')' 217a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return object.__format__(self, format_spec) 218a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 219a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # class that returns a bad type from __format__ 220a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith class H: 221a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __format__(self, format_spec): 222a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return 1.0 223a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 224a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith class I(datetime.date): 225a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __format__(self, format_spec): 226a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return self.strftime(format_spec) 227a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 228a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith class J(int): 229a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith def __format__(self, format_spec): 230a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith return int.__format__(self * 2, format_spec) 231a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 232a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 233a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual(''.format(), '') 234a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('abc'.format(), 'abc') 235a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}'.format('abc'), 'abc') 236a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:}'.format('abc'), 'abc') 237a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('X{0}'.format('abc'), 'Xabc') 238a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}X'.format('abc'), 'abcX') 239a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('X{0}Y'.format('abc'), 'XabcY') 240a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{1}'.format(1, 'abc'), 'abc') 241a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc') 242a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{1}X'.format(1, 'abc'), 'abcX') 243a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY') 244a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}'.format(-15), '-15') 245a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc') 246a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc') 247a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{{'.format(), '{') 248a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('}}'.format(), '}') 249a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{{}}'.format(), '{}') 250a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{{x}}'.format(), '{x}') 251a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{{{0}}}'.format(123), '{123}') 252a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{{{{0}}}}'.format(), '{{0}}') 253a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('}}{{'.format(), '}{') 254a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('}}x{{'.format(), '}x{') 255a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 256a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # weird field names 257a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz') 258a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz') 259a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0[ ]}".format({' ':3}), '3') 260a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 261a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{foo._x}'.format(foo=C(20)), '20') 262a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010') 263a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc') 264a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc') 265a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def') 266a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def') 267a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def') 268a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 269a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # strings 270a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:.3s}'.format('abc'), 'abc') 271a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:.3s}'.format('ab'), 'ab') 272a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:.3s}'.format('abcdef'), 'abc') 273a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:.0s}'.format('abcdef'), '') 274a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:3.3s}'.format('abc'), 'abc') 275a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:2.3s}'.format('abc'), 'abc') 276a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:2.2s}'.format('abc'), 'ab') 277a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:3.2s}'.format('abc'), 'ab ') 278a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:x<0s}'.format('result'), 'result') 279a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:x<5s}'.format('result'), 'result') 280a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:x<6s}'.format('result'), 'result') 281a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:x<7s}'.format('result'), 'resultx') 282a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:x<8s}'.format('result'), 'resultxx') 283a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0: <7s}'.format('result'), 'result ') 284a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:<7s}'.format('result'), 'result ') 285a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:>7s}'.format('result'), ' result') 286a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:>8s}'.format('result'), ' result') 287a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:^8s}'.format('result'), ' result ') 288a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:^9s}'.format('result'), ' result ') 289a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:^10s}'.format('result'), ' result ') 290a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999) 291a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:10000}'.format(''), ' ' * 10000) 292a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000) 293a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 294a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # format specifiers for user defined type 295a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:abc}'.format(C()), 'abc') 296a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 29760d512c3b01405f91872a62126d42b4bf1bea911Florent Xicluna # !r and !s coercions 298a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0!s}'.format('Hello'), 'Hello') 299a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0!s:}'.format('Hello'), 'Hello') 300a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ') 301a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ') 302a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0!r}'.format('Hello'), "'Hello'") 303a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0!r:}'.format('Hello'), "'Hello'") 304a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)') 305a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 306a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # test fallback to object.__format__ 307a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}'.format({}), '{}') 308a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}'.format([]), '[]') 309a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}'.format([1]), '[1]') 310a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0}'.format(E('data')), 'E(data)') 311a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:d}'.format(G('data')), 'G(data)') 312a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0!s}'.format(G('data')), 'string is data') 313a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 31460d512c3b01405f91872a62126d42b4bf1bea911Florent Xicluna msg = 'object.__format__ with a non-empty format string is deprecated' 31560d512c3b01405f91872a62126d42b4bf1bea911Florent Xicluna with test_support.check_warnings((msg, PendingDeprecationWarning)): 31660d512c3b01405f91872a62126d42b4bf1bea911Florent Xicluna self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ') 31760d512c3b01405f91872a62126d42b4bf1bea911Florent Xicluna self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ') 31860d512c3b01405f91872a62126d42b4bf1bea911Florent Xicluna self.assertEqual('{0:>15s}'.format(G('data')), ' string is data') 31960d512c3b01405f91872a62126d42b4bf1bea911Florent Xicluna 320a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007, 321a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith month=8, 322a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith day=27)), 323a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith "date: 2007-08-27") 324a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 325a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # test deriving from a builtin type and overriding __format__ 326a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0}".format(J(10)), "20") 327a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 328a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 329a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # string format specifiers 330a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual('{0:}'.format('a'), 'a') 331a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 332a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # computed format specifiers 333a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello') 334a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello') 335a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello') 336a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello ') 337a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hello ') 338a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 339a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # test various errors 340a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, '{'.format) 341a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, '}'.format) 342a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, 'a{'.format) 343a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, 'a}'.format) 344a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, '{a'.format) 345a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, '}a'.format) 346a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(IndexError, '{0}'.format) 347a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(IndexError, '{1}'.format, 'abc') 348a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(KeyError, '{x}'.format) 349a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "}{".format) 350a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{".format) 351a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "}".format) 352a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "abc{0:{}".format) 353a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0".format) 354a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(IndexError, "{0.}".format) 355a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0.}".format, 0) 356a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(IndexError, "{0[}".format) 357a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0[}".format, []) 358a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(KeyError, "{0]}".format) 359a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0.[]}".format, 0) 360a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0..foo}".format, 0) 361a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0[0}".format, 0) 362a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0[0:foo}".format, 0) 363a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(KeyError, "{c]}".format) 364a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{{ {{{0}}".format, 0) 365a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0}}".format, 0) 366a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(KeyError, "{foo}".format, bar=3) 367a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0!x}".format, 3) 368a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0!}".format, 0) 369a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0!rs}".format, 0) 370a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{!}".format) 3716f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertRaises(IndexError, "{:}".format) 3726f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertRaises(IndexError, "{:s}".format) 3736f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertRaises(IndexError, "{}".format) 374a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 3754b94b192ff38f37b99f04605fd6515d7fb97ffeaEric Smith # issue 6089 3764b94b192ff38f37b99f04605fd6515d7fb97ffeaEric Smith self.assertRaises(ValueError, "{0[0]x}".format, [None]) 3774b94b192ff38f37b99f04605fd6515d7fb97ffeaEric Smith self.assertRaises(ValueError, "{0[0](10)}".format, [None]) 3784b94b192ff38f37b99f04605fd6515d7fb97ffeaEric Smith 379a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # can't have a replacement on the field name portion 380a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4) 381a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 382a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # exceed maximum recursion depth 383a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '') 384a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format, 385a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 0, 1, 2, 3, 4, 5, 6, 7) 386a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 387a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith # string format spec errors 388a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0:-s}".format, '') 389a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, format, "", "-") 390a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith self.assertRaises(ValueError, "{0:=s}".format, '') 391a9f7d6248032c9572b4d2024a1be8bd2823af09fEric Smith 39275d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson def test_format_huge_precision(self): 39375d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson format_string = ".{}f".format(sys.maxsize + 1) 39475d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson with self.assertRaises(ValueError): 39575d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson result = format(2.34, format_string) 39675d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson 39775d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson def test_format_huge_width(self): 39875d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson format_string = "{}f".format(sys.maxsize + 1) 39975d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson with self.assertRaises(ValueError): 40075d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson result = format(2.34, format_string) 40175d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson 40275d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson def test_format_huge_item_number(self): 40375d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson format_string = "{{{}:.6f}}".format(sys.maxsize + 1) 40475d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson with self.assertRaises(ValueError): 40575d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson result = format_string.format(2.34) 40675d36004665a637c5d0aa868a5d0b728b3d03d39Mark Dickinson 4076f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith def test_format_auto_numbering(self): 4086f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith class C: 4096f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith def __init__(self, x=100): 4106f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self._x = x 4116f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith def __format__(self, spec): 4126f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith return spec 4136f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith 4146f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{}'.format(10), '10') 4156f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{:5}'.format('s'), 's ') 4166f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{!r}'.format('s'), "'s'") 4176f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{._x}'.format(C(10)), '10') 4186f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{[1]}'.format([1, 2]), '2') 4196f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4') 4206f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c') 4216f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith 4226f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b') 4236f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b') 4246f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith 4256f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith # can't mix and match numbering and auto-numbering 4266f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertRaises(ValueError, '{}{1}'.format, 1, 2) 4276f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertRaises(ValueError, '{1}{}'.format, 1, 2) 4286f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertRaises(ValueError, '{:{1}}'.format, 1, 2) 4296f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertRaises(ValueError, '{0:{}}'.format, 1, 2) 4306f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith 4316f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith # can mix and match auto-numbering and named 4326f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{f}{}'.format(4, f='test'), 'test4') 4336f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{}{f}'.format(4, f='test'), '4test') 4346f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3') 4356f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g') 4366f42edb6821462c9ce02c6c4e2f57731b43956adEric Smith 437e192d0bbb969bcefe4658daddd9e495153fadad5Victor Stinner def test_format_c_overflow(self): 438e192d0bbb969bcefe4658daddd9e495153fadad5Victor Stinner # issue #7267 439e192d0bbb969bcefe4658daddd9e495153fadad5Victor Stinner self.assertRaises(OverflowError, '{0:c}'.format, -1) 440e192d0bbb969bcefe4658daddd9e495153fadad5Victor Stinner self.assertRaises(OverflowError, '{0:c}'.format, 256) 441e192d0bbb969bcefe4658daddd9e495153fadad5Victor Stinner 44292a624019867885028fe0b326457b461e672eb3bAntoine Pitrou def test_buffer_is_readonly(self): 44392a624019867885028fe0b326457b461e672eb3bAntoine Pitrou self.assertRaises(TypeError, sys.stdin.readinto, b"") 44492a624019867885028fe0b326457b461e672eb3bAntoine Pitrou 445332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson def test_encode_and_decode_kwargs(self): 446332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson self.assertEqual('abcde'.encode('ascii', 'replace'), 447332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson 'abcde'.encode('ascii', errors='replace')) 448332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson self.assertEqual('abcde'.encode('ascii', 'ignore'), 449332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson 'abcde'.encode(encoding='ascii', errors='ignore')) 450332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson self.assertEqual('Andr\202 x'.decode('ascii', 'ignore'), 451332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson 'Andr\202 x'.decode('ascii', errors='ignore')) 452332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson self.assertEqual('Andr\202 x'.decode('ascii', 'replace'), 453332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson 'Andr\202 x'.decode(encoding='ascii', errors='replace')) 454332d7217509443c01ba5c3da41053786b49015faBenjamin Peterson 455e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti def test_startswith_endswith_errors(self): 456e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti with self.assertRaises(UnicodeDecodeError): 457e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti '\xff'.startswith(u'x') 458e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti with self.assertRaises(UnicodeDecodeError): 459e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti '\xff'.endswith(u'x') 460e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti for meth in ('foo'.startswith, 'foo'.endswith): 461e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti with self.assertRaises(TypeError) as cm: 462e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti meth(['f']) 463e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti exc = str(cm.exception) 464e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti self.assertIn('unicode', exc) 465e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti self.assertIn('str', exc) 466e3685f6b1b27bf089a40a12492f952dda5ff3ea2Ezio Melotti self.assertIn('tuple', exc) 4677dbd2a3720be7f54068f8a8f3fa2176dd2ab1ebeNeal Norwitz 4680fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwalddef test_main(): 46921d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald test_support.run_unittest(StrTest) 4700fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald 4710fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwaldif __name__ == "__main__": 4720fd583ce4d1500938c96c12c698a535ad0938470Walter Dörwald test_main() 473