mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Merged revisions 70364 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r70364 | eric.smith | 2009-03-14 07:57:26 -0400 (Sat, 14 Mar 2009) | 17 lines Issue 5237, Allow auto-numbered replacement fields in str.format() strings. For simple uses for str.format(), this makes the typing easier. Hopfully this will help in the adoption of str.format(). For example: 'The {} is {}'.format('sky', 'blue') You can mix and matcth auto-numbering and named replacement fields: 'The {} is {color}'.format('sky', color='blue') But you can't mix and match auto-numbering and specified numbering: 'The {0} is {}'.format('sky', 'blue') ValueError: cannot switch from manual field specification to automatic field numbering Will port to 3.1. ........
This commit is contained in:
parent
350370c25f
commit
8ec90443f5
3 changed files with 166 additions and 47 deletions
|
@ -683,9 +683,9 @@ class UnicodeTest(
|
|||
self.assertRaises(ValueError, "{0!}".format, 0)
|
||||
self.assertRaises(ValueError, "{0!rs}".format, 0)
|
||||
self.assertRaises(ValueError, "{!}".format)
|
||||
self.assertRaises(ValueError, "{:}".format)
|
||||
self.assertRaises(ValueError, "{:s}".format)
|
||||
self.assertRaises(ValueError, "{}".format)
|
||||
self.assertRaises(IndexError, "{:}".format)
|
||||
self.assertRaises(IndexError, "{:s}".format)
|
||||
self.assertRaises(IndexError, "{}".format)
|
||||
|
||||
# can't have a replacement on the field name portion
|
||||
self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4)
|
||||
|
@ -704,6 +704,36 @@ class UnicodeTest(
|
|||
self.assertRaises(ValueError, format, '', '#')
|
||||
self.assertRaises(ValueError, format, '', '#20')
|
||||
|
||||
def test_format_auto_numbering(self):
|
||||
class C:
|
||||
def __init__(self, x=100):
|
||||
self._x = x
|
||||
def __format__(self, spec):
|
||||
return spec
|
||||
|
||||
self.assertEqual('{}'.format(10), '10')
|
||||
self.assertEqual('{:5}'.format('s'), 's ')
|
||||
self.assertEqual('{!r}'.format('s'), "'s'")
|
||||
self.assertEqual('{._x}'.format(C(10)), '10')
|
||||
self.assertEqual('{[1]}'.format([1, 2]), '2')
|
||||
self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4')
|
||||
self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c')
|
||||
|
||||
self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b')
|
||||
self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b')
|
||||
|
||||
# can't mix and match numbering and auto-numbering
|
||||
self.assertRaises(ValueError, '{}{1}'.format, 1, 2)
|
||||
self.assertRaises(ValueError, '{1}{}'.format, 1, 2)
|
||||
self.assertRaises(ValueError, '{:{1}}'.format, 1, 2)
|
||||
self.assertRaises(ValueError, '{0:{}}'.format, 1, 2)
|
||||
|
||||
# can mix and match auto-numbering and named
|
||||
self.assertEqual('{f}{}'.format(4, f='test'), 'test4')
|
||||
self.assertEqual('{}{f}'.format(4, f='test'), '4test')
|
||||
self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3')
|
||||
self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g')
|
||||
|
||||
def test_formatting(self):
|
||||
string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
|
||||
# Testing Unicode formatting strings...
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue