summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2009-03-14 12:29:34 (GMT)
committerEric Smith <eric@trueblade.com>2009-03-14 12:29:34 (GMT)
commit8ec90443f57c13e895a43b0f62bdb73a6b30d552 (patch)
tree1eb0a716e9e6bf041944c2c27fe111de820d9427 /Lib
parent350370c25fd1259199bb18fbce9fa447cd02ddf1 (diff)
downloadcpython-8ec90443f57c13e895a43b0f62bdb73a6b30d552.zip
cpython-8ec90443f57c13e895a43b0f62bdb73a6b30d552.tar.gz
cpython-8ec90443f57c13e895a43b0f62bdb73a6b30d552.tar.bz2
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. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_unicode.py36
1 files changed, 33 insertions, 3 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index f16a78a..b40ee84 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -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...