blob: 66368f4e2329fd5d0a19a636af6bc7470737fde1 [file] [log] [blame]
Martin v. Löwisa729daf2002-08-04 17:28:33 +00001# -*- coding: iso-8859-1 -*-
Guido van Rossuma831cac2000-03-10 23:23:21 +00002""" Test script for the Unicode implementation.
3
Guido van Rossuma831cac2000-03-10 23:23:21 +00004Written by Marc-Andre Lemburg (mal@lemburg.com).
5
6(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
7
Marc-André Lemburg36619082001-01-17 19:11:13 +00008"""#"
Guido van Rossum98297ee2007-11-06 21:34:58 +00009import codecs
10import struct
11import sys
12import unittest
13import warnings
Benjamin Petersonee8712c2008-05-20 21:35:26 +000014from test import support, string_tests
Guido van Rossuma831cac2000-03-10 23:23:21 +000015
Neal Norwitz430f68b2005-11-24 22:00:56 +000016# Error handling (bad decoder return)
17def search_function(encoding):
18 def decode1(input, errors="strict"):
19 return 42 # not a tuple
20 def encode1(input, errors="strict"):
21 return 42 # not a tuple
22 def encode2(input, errors="strict"):
23 return (42, 42) # no unicode
24 def decode2(input, errors="strict"):
25 return (42, 42) # no unicode
26 if encoding=="test.unicode1":
27 return (encode1, decode1, None, None)
28 elif encoding=="test.unicode2":
29 return (encode2, decode2, None, None)
30 else:
31 return None
32codecs.register(search_function)
33
Walter Dörwald0fd583c2003-02-21 12:53:50 +000034class UnicodeTest(
35 string_tests.CommonTest,
Walter Dörwald57d88e52004-08-26 16:53:04 +000036 string_tests.MixinStrUnicodeUserStringTest,
37 string_tests.MixinStrUnicodeTest,
Walter Dörwald0fd583c2003-02-21 12:53:50 +000038 ):
Guido van Rossumef87d6e2007-05-02 19:09:54 +000039 type2test = str
Walter Dörwald0fd583c2003-02-21 12:53:50 +000040
Guido van Rossum98297ee2007-11-06 21:34:58 +000041 def setUp(self):
42 self.warning_filters = warnings.filters[:]
43
44 def tearDown(self):
45 warnings.filters = self.warning_filters
46
Walter Dörwald0fd583c2003-02-21 12:53:50 +000047 def checkequalnofix(self, result, object, methodname, *args):
48 method = getattr(object, methodname)
49 realresult = method(*args)
50 self.assertEqual(realresult, result)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000051 self.assertTrue(type(realresult) is type(result))
Walter Dörwald0fd583c2003-02-21 12:53:50 +000052
53 # if the original is returned make sure that
54 # this doesn't happen with subclasses
55 if realresult is object:
Guido van Rossumef87d6e2007-05-02 19:09:54 +000056 class usub(str):
Walter Dörwald0fd583c2003-02-21 12:53:50 +000057 def __repr__(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +000058 return 'usub(%r)' % str.__repr__(self)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000059 object = usub(object)
60 method = getattr(object, methodname)
61 realresult = method(*args)
62 self.assertEqual(realresult, result)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000063 self.assertTrue(object is not realresult)
Guido van Rossume4874ae2001-09-21 15:36:41 +000064
Jeremy Hylton504de6b2003-10-06 05:08:26 +000065 def test_literals(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +000066 self.assertEqual('\xff', '\u00ff')
67 self.assertEqual('\uffff', '\U0000ffff')
Guido van Rossum36e0a922007-07-20 04:05:57 +000068 self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'')
69 self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'')
70 self.assertRaises(SyntaxError, eval, '\'\\U%08x\'' % 0x110000)
Benjamin Petersoncd76c272008-04-05 15:09:30 +000071 # raw strings should not have unicode escapes
72 self.assertNotEquals(r"\u0020", " ")
Jeremy Hylton504de6b2003-10-06 05:08:26 +000073
Georg Brandl559e5d72008-06-11 18:37:52 +000074 def test_ascii(self):
75 if not sys.platform.startswith('java'):
76 # Test basic sanity of repr()
77 self.assertEqual(ascii('abc'), "'abc'")
78 self.assertEqual(ascii('ab\\c'), "'ab\\\\c'")
79 self.assertEqual(ascii('ab\\'), "'ab\\\\'")
80 self.assertEqual(ascii('\\c'), "'\\\\c'")
81 self.assertEqual(ascii('\\'), "'\\\\'")
82 self.assertEqual(ascii('\n'), "'\\n'")
83 self.assertEqual(ascii('\r'), "'\\r'")
84 self.assertEqual(ascii('\t'), "'\\t'")
85 self.assertEqual(ascii('\b'), "'\\x08'")
86 self.assertEqual(ascii("'\""), """'\\'"'""")
87 self.assertEqual(ascii("'\""), """'\\'"'""")
88 self.assertEqual(ascii("'"), '''"'"''')
89 self.assertEqual(ascii('"'), """'"'""")
90 latin1repr = (
91 "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
92 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a"
93 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
94 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
95 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d"
96 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
97 "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9"
98 "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7"
99 "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5"
100 "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3"
101 "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1"
102 "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef"
103 "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd"
104 "\\xfe\\xff'")
105 testrepr = ascii(''.join(map(chr, range(256))))
106 self.assertEqual(testrepr, latin1repr)
107 # Test ascii works on wide unicode escapes without overflow.
108 self.assertEqual(ascii("\U00010000" * 39 + "\uffff" * 4096),
109 ascii("\U00010000" * 39 + "\uffff" * 4096))
110
111 class WrongRepr:
112 def __repr__(self):
113 return b'byte-repr'
114 self.assertRaises(TypeError, ascii, WrongRepr())
115
Walter Dörwald28256f22003-01-19 16:59:20 +0000116 def test_repr(self):
117 if not sys.platform.startswith('java'):
118 # Test basic sanity of repr()
Walter Dörwald67e83882007-05-05 12:26:27 +0000119 self.assertEqual(repr('abc'), "'abc'")
120 self.assertEqual(repr('ab\\c'), "'ab\\\\c'")
121 self.assertEqual(repr('ab\\'), "'ab\\\\'")
122 self.assertEqual(repr('\\c'), "'\\\\c'")
123 self.assertEqual(repr('\\'), "'\\\\'")
124 self.assertEqual(repr('\n'), "'\\n'")
125 self.assertEqual(repr('\r'), "'\\r'")
126 self.assertEqual(repr('\t'), "'\\t'")
127 self.assertEqual(repr('\b'), "'\\x08'")
128 self.assertEqual(repr("'\""), """'\\'"'""")
129 self.assertEqual(repr("'\""), """'\\'"'""")
130 self.assertEqual(repr("'"), '''"'"''')
131 self.assertEqual(repr('"'), """'"'""")
Walter Dörwald28256f22003-01-19 16:59:20 +0000132 latin1repr = (
Walter Dörwald67e83882007-05-05 12:26:27 +0000133 "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
Walter Dörwald28256f22003-01-19 16:59:20 +0000134 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a"
135 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
136 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
137 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d"
138 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
Georg Brandl559e5d72008-06-11 18:37:52 +0000139 "\\x9c\\x9d\\x9e\\x9f\\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9"
140 "\xaa\xab\xac\\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7"
141 "\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5"
142 "\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3"
143 "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1"
144 "\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
145 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd"
146 "\xfe\xff'")
Guido van Rossum805365e2007-05-07 22:24:25 +0000147 testrepr = repr(''.join(map(chr, range(256))))
Walter Dörwald28256f22003-01-19 16:59:20 +0000148 self.assertEqual(testrepr, latin1repr)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000149 # Test repr works on wide unicode escapes without overflow.
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000150 self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096),
151 repr("\U00010000" * 39 + "\uffff" * 4096))
Walter Dörwald28256f22003-01-19 16:59:20 +0000152
Georg Brandl559e5d72008-06-11 18:37:52 +0000153 class WrongRepr:
154 def __repr__(self):
155 return b'byte-repr'
156 self.assertRaises(TypeError, repr, WrongRepr())
157
Guido van Rossum49d6b072006-08-17 21:11:47 +0000158 def test_iterators(self):
159 # Make sure unicode objects have an __iter__ method
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000160 it = "\u1111\u2222\u3333".__iter__()
161 self.assertEqual(next(it), "\u1111")
162 self.assertEqual(next(it), "\u2222")
163 self.assertEqual(next(it), "\u3333")
Georg Brandla18af4e2007-04-21 15:47:16 +0000164 self.assertRaises(StopIteration, next, it)
Guido van Rossum49d6b072006-08-17 21:11:47 +0000165
Walter Dörwald28256f22003-01-19 16:59:20 +0000166 def test_count(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000167 string_tests.CommonTest.test_count(self)
168 # check mixed argument types
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000169 self.checkequalnofix(3, 'aaa', 'count', 'a')
170 self.checkequalnofix(0, 'aaa', 'count', 'b')
171 self.checkequalnofix(3, 'aaa', 'count', 'a')
172 self.checkequalnofix(0, 'aaa', 'count', 'b')
173 self.checkequalnofix(0, 'aaa', 'count', 'b')
174 self.checkequalnofix(1, 'aaa', 'count', 'a', -1)
175 self.checkequalnofix(3, 'aaa', 'count', 'a', -10)
176 self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1)
177 self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10)
Guido van Rossuma831cac2000-03-10 23:23:21 +0000178
Walter Dörwald28256f22003-01-19 16:59:20 +0000179 def test_find(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000180 self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc')
181 self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1)
182 self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4)
Guido van Rossuma831cac2000-03-10 23:23:21 +0000183
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000184 self.assertRaises(TypeError, 'hello'.find)
185 self.assertRaises(TypeError, 'hello'.find, 42)
Guido van Rossuma831cac2000-03-10 23:23:21 +0000186
Walter Dörwald28256f22003-01-19 16:59:20 +0000187 def test_rfind(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000188 string_tests.CommonTest.test_rfind(self)
189 # check mixed argument types
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000190 self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc')
191 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
192 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
Guido van Rossum8b264542000-12-19 02:22:31 +0000193
Walter Dörwald28256f22003-01-19 16:59:20 +0000194 def test_index(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000195 string_tests.CommonTest.test_index(self)
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000196 self.checkequalnofix(0, 'abcdefghiabc', 'index', '')
197 self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def')
198 self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc')
199 self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1)
200 self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib')
201 self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1)
202 self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8)
203 self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1)
Guido van Rossuma831cac2000-03-10 23:23:21 +0000204
Walter Dörwald28256f22003-01-19 16:59:20 +0000205 def test_rindex(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000206 string_tests.CommonTest.test_rindex(self)
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000207 self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '')
208 self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def')
209 self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc')
210 self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
Guido van Rossuma831cac2000-03-10 23:23:21 +0000211
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000212 self.assertRaises(ValueError, 'abcdefghiabc'.rindex, 'hib')
213 self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1)
214 self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1)
215 self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8)
216 self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1)
Guido van Rossuma831cac2000-03-10 23:23:21 +0000217
Georg Brandlceee0772007-11-27 23:48:05 +0000218 def test_maketrans_translate(self):
219 # these work with plain translate()
220 self.checkequalnofix('bbbc', 'abababc', 'translate',
221 {ord('a'): None})
222 self.checkequalnofix('iiic', 'abababc', 'translate',
223 {ord('a'): None, ord('b'): ord('i')})
224 self.checkequalnofix('iiix', 'abababc', 'translate',
225 {ord('a'): None, ord('b'): ord('i'), ord('c'): 'x'})
226 self.checkequalnofix('c', 'abababc', 'translate',
227 {ord('a'): None, ord('b'): ''})
228 self.checkequalnofix('xyyx', 'xzx', 'translate',
229 {ord('z'): 'yy'})
230 # this needs maketrans()
231 self.checkequalnofix('abababc', 'abababc', 'translate',
232 {'b': '<i>'})
233 tbl = self.type2test.maketrans({'a': None, 'b': '<i>'})
234 self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl)
235 # test alternative way of calling maketrans()
236 tbl = self.type2test.maketrans('abc', 'xyz', 'd')
237 self.checkequalnofix('xyzzy', 'abdcdcbdddd', 'translate', tbl)
238
239 self.assertRaises(TypeError, self.type2test.maketrans)
240 self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg')
241 self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def')
242 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 2)
243 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def', 2)
244 self.assertRaises(ValueError, self.type2test.maketrans, {'xy': 2})
245 self.assertRaises(TypeError, self.type2test.maketrans, {(1,): 2})
Guido van Rossuma831cac2000-03-10 23:23:21 +0000246
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000247 self.assertRaises(TypeError, 'hello'.translate)
Walter Dörwald67e83882007-05-05 12:26:27 +0000248 self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz')
Guido van Rossuma831cac2000-03-10 23:23:21 +0000249
Walter Dörwald28256f22003-01-19 16:59:20 +0000250 def test_split(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000251 string_tests.CommonTest.test_split(self)
Andrew M. Kuchlingeddd68d2002-03-29 16:21:44 +0000252
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000253 # Mixed arguments
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000254 self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
255 self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
256 self.checkequalnofix(['endcase ', ''], 'endcase test', 'split', 'test')
Guido van Rossuma831cac2000-03-10 23:23:21 +0000257
Walter Dörwald28256f22003-01-19 16:59:20 +0000258 def test_join(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000259 string_tests.MixinStrUnicodeUserStringTest.test_join(self)
Marc-André Lemburgd6d06ad2000-07-07 17:48:52 +0000260
Guido van Rossumf1044292007-09-27 18:01:22 +0000261 class MyWrapper:
262 def __init__(self, sval): self.sval = sval
263 def __str__(self): return self.sval
264
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000265 # mixed arguments
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000266 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
267 self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
268 self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
269 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
270 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
271 self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
272 self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
Guido van Rossum98297ee2007-11-06 21:34:58 +0000273 self.checkraises(TypeError, ' ', 'join', ['1', '2', MyWrapper('foo')])
274 self.checkraises(TypeError, ' ', 'join', ['1', '2', '3', bytes()])
275 self.checkraises(TypeError, ' ', 'join', [1, 2, 3])
276 self.checkraises(TypeError, ' ', 'join', ['1', '2', 3])
Marc-André Lemburge5034372000-08-08 08:04:29 +0000277
Walter Dörwald28256f22003-01-19 16:59:20 +0000278 def test_replace(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000279 string_tests.CommonTest.test_replace(self)
Guido van Rossuma831cac2000-03-10 23:23:21 +0000280
Walter Dörwald28256f22003-01-19 16:59:20 +0000281 # method call forwarded from str implementation because of unicode argument
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000282 self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
283 self.assertRaises(TypeError, 'replace'.replace, "r", 42)
Guido van Rossuma831cac2000-03-10 23:23:21 +0000284
Guido van Rossum98297ee2007-11-06 21:34:58 +0000285 def test_bytes_comparison(self):
286 warnings.simplefilter('ignore', BytesWarning)
287 self.assertEqual('abc' == b'abc', False)
288 self.assertEqual('abc' != b'abc', True)
Guido van Rossum254348e2007-11-21 19:29:53 +0000289 self.assertEqual('abc' == bytearray(b'abc'), False)
290 self.assertEqual('abc' != bytearray(b'abc'), True)
Brett Cannon40430012007-10-22 20:24:51 +0000291
Walter Dörwald28256f22003-01-19 16:59:20 +0000292 def test_comparison(self):
293 # Comparisons:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000294 self.assertEqual('abc', 'abc')
295 self.assertEqual('abc', 'abc')
296 self.assertEqual('abc', 'abc')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000297 self.assertTrue('abcd' > 'abc')
298 self.assertTrue('abcd' > 'abc')
299 self.assertTrue('abcd' > 'abc')
300 self.assertTrue('abc' < 'abcd')
301 self.assertTrue('abc' < 'abcd')
302 self.assertTrue('abc' < 'abcd')
Walter Dörwald28256f22003-01-19 16:59:20 +0000303
304 if 0:
305 # Move these tests to a Unicode collation module test...
306 # Testing UTF-16 code point order comparisons...
307
308 # No surrogates, no fixup required.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000309 self.assertTrue('\u0061' < '\u20ac')
Walter Dörwald28256f22003-01-19 16:59:20 +0000310 # Non surrogate below surrogate value, no fixup required
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000311 self.assertTrue('\u0061' < '\ud800\udc02')
Walter Dörwald28256f22003-01-19 16:59:20 +0000312
313 # Non surrogate above surrogate value, fixup required
314 def test_lecmp(s, s2):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000315 self.assertTrue(s < s2)
Walter Dörwald28256f22003-01-19 16:59:20 +0000316
317 def test_fixup(s):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000318 s2 = '\ud800\udc01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000319 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000320 s2 = '\ud900\udc01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000321 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000322 s2 = '\uda00\udc01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000323 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000324 s2 = '\udb00\udc01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000325 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000326 s2 = '\ud800\udd01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000327 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000328 s2 = '\ud900\udd01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000329 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000330 s2 = '\uda00\udd01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000331 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000332 s2 = '\udb00\udd01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000333 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000334 s2 = '\ud800\ude01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000335 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000336 s2 = '\ud900\ude01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000337 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000338 s2 = '\uda00\ude01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000339 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000340 s2 = '\udb00\ude01'
Walter Dörwald28256f22003-01-19 16:59:20 +0000341 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000342 s2 = '\ud800\udfff'
Walter Dörwald28256f22003-01-19 16:59:20 +0000343 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000344 s2 = '\ud900\udfff'
Walter Dörwald28256f22003-01-19 16:59:20 +0000345 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000346 s2 = '\uda00\udfff'
Walter Dörwald28256f22003-01-19 16:59:20 +0000347 test_lecmp(s, s2)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000348 s2 = '\udb00\udfff'
Walter Dörwald28256f22003-01-19 16:59:20 +0000349 test_lecmp(s, s2)
350
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000351 test_fixup('\ue000')
352 test_fixup('\uff61')
Walter Dörwald28256f22003-01-19 16:59:20 +0000353
354 # Surrogates on both sides, no fixup required
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000355 self.assertTrue('\ud800\udc02' < '\ud84d\udc56')
Walter Dörwald28256f22003-01-19 16:59:20 +0000356
Walter Dörwald28256f22003-01-19 16:59:20 +0000357 def test_islower(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000358 string_tests.MixinStrUnicodeUserStringTest.test_islower(self)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000359 self.checkequalnofix(False, '\u1FFc', 'islower')
Walter Dörwald28256f22003-01-19 16:59:20 +0000360
361 def test_isupper(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000362 string_tests.MixinStrUnicodeUserStringTest.test_isupper(self)
363 if not sys.platform.startswith('java'):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000364 self.checkequalnofix(False, '\u1FFc', 'isupper')
Walter Dörwald28256f22003-01-19 16:59:20 +0000365
366 def test_istitle(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000367 string_tests.MixinStrUnicodeUserStringTest.test_title(self)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000368 self.checkequalnofix(True, '\u1FFc', 'istitle')
369 self.checkequalnofix(True, 'Greek \u1FFcitlecases ...', 'istitle')
Walter Dörwald28256f22003-01-19 16:59:20 +0000370
371 def test_isspace(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000372 string_tests.MixinStrUnicodeUserStringTest.test_isspace(self)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000373 self.checkequalnofix(True, '\u2000', 'isspace')
374 self.checkequalnofix(True, '\u200a', 'isspace')
375 self.checkequalnofix(False, '\u2014', 'isspace')
Walter Dörwald28256f22003-01-19 16:59:20 +0000376
377 def test_isalpha(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000378 string_tests.MixinStrUnicodeUserStringTest.test_isalpha(self)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000379 self.checkequalnofix(True, '\u1FFc', 'isalpha')
Walter Dörwald28256f22003-01-19 16:59:20 +0000380
381 def test_isdecimal(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000382 self.checkequalnofix(False, '', 'isdecimal')
383 self.checkequalnofix(False, 'a', 'isdecimal')
384 self.checkequalnofix(True, '0', 'isdecimal')
385 self.checkequalnofix(False, '\u2460', 'isdecimal') # CIRCLED DIGIT ONE
386 self.checkequalnofix(False, '\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER
387 self.checkequalnofix(True, '\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO
388 self.checkequalnofix(True, '0123456789', 'isdecimal')
389 self.checkequalnofix(False, '0123456789a', 'isdecimal')
Walter Dörwald28256f22003-01-19 16:59:20 +0000390
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000391 self.checkraises(TypeError, 'abc', 'isdecimal', 42)
Walter Dörwald28256f22003-01-19 16:59:20 +0000392
393 def test_isdigit(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000394 string_tests.MixinStrUnicodeUserStringTest.test_isdigit(self)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000395 self.checkequalnofix(True, '\u2460', 'isdigit')
396 self.checkequalnofix(False, '\xbc', 'isdigit')
397 self.checkequalnofix(True, '\u0660', 'isdigit')
Walter Dörwald28256f22003-01-19 16:59:20 +0000398
399 def test_isnumeric(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000400 self.checkequalnofix(False, '', 'isnumeric')
401 self.checkequalnofix(False, 'a', 'isnumeric')
402 self.checkequalnofix(True, '0', 'isnumeric')
403 self.checkequalnofix(True, '\u2460', 'isnumeric')
404 self.checkequalnofix(True, '\xbc', 'isnumeric')
405 self.checkequalnofix(True, '\u0660', 'isnumeric')
406 self.checkequalnofix(True, '0123456789', 'isnumeric')
407 self.checkequalnofix(False, '0123456789a', 'isnumeric')
Walter Dörwald28256f22003-01-19 16:59:20 +0000408
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000409 self.assertRaises(TypeError, "abc".isnumeric, 42)
Walter Dörwald28256f22003-01-19 16:59:20 +0000410
Martin v. Löwis47383402007-08-15 07:32:56 +0000411 def test_isidentifier(self):
412 self.assertTrue("a".isidentifier())
413 self.assertTrue("Z".isidentifier())
414 self.assertTrue("_".isidentifier())
415 self.assertTrue("b0".isidentifier())
416 self.assertTrue("bc".isidentifier())
417 self.assertTrue("b_".isidentifier())
418 self.assertTrue("µ".isidentifier())
419
420 self.assertFalse(" ".isidentifier())
421 self.assertFalse("[".isidentifier())
422 self.assertFalse("©".isidentifier())
Georg Brandld52429f2008-07-04 15:55:02 +0000423 self.assertFalse("0".isidentifier())
Martin v. Löwis47383402007-08-15 07:32:56 +0000424
Georg Brandl559e5d72008-06-11 18:37:52 +0000425 def test_isprintable(self):
426 self.assertTrue("".isprintable())
Benjamin Peterson09832742009-03-26 17:15:46 +0000427 self.assertTrue(" ".isprintable())
Georg Brandl559e5d72008-06-11 18:37:52 +0000428 self.assertTrue("abcdefg".isprintable())
429 self.assertFalse("abcdefg\n".isprintable())
Georg Brandld52429f2008-07-04 15:55:02 +0000430 # some defined Unicode character
431 self.assertTrue("\u0374".isprintable())
432 # undefined character
Amaury Forgeot d'Arca083f1e2008-09-10 23:51:42 +0000433 self.assertFalse("\u0378".isprintable())
Georg Brandld52429f2008-07-04 15:55:02 +0000434 # single surrogate character
Georg Brandl559e5d72008-06-11 18:37:52 +0000435 self.assertFalse("\ud800".isprintable())
436
Walter Dörwald28256f22003-01-19 16:59:20 +0000437 def test_contains(self):
438 # Testing Unicode contains method
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000439 self.assertTrue('a' in 'abdb')
440 self.assertTrue('a' in 'bdab')
441 self.assertTrue('a' in 'bdaba')
442 self.assertTrue('a' in 'bdba')
443 self.assertTrue('a' not in 'bdb')
444 self.assertTrue('a' in 'bdba')
445 self.assertTrue('a' in ('a',1,None))
446 self.assertTrue('a' in (1,None,'a'))
447 self.assertTrue('a' in ('a',1,None))
448 self.assertTrue('a' in (1,None,'a'))
449 self.assertTrue('a' not in ('x',1,'y'))
450 self.assertTrue('a' not in ('x',1,None))
451 self.assertTrue('abcd' not in 'abcxxxx')
452 self.assertTrue('ab' in 'abcd')
453 self.assertTrue('ab' in 'abc')
454 self.assertTrue('ab' in (1,None,'ab'))
455 self.assertTrue('' in 'abc')
456 self.assertTrue('' in '')
457 self.assertTrue('' in 'abc')
458 self.assertTrue('\0' not in 'abc')
459 self.assertTrue('\0' in '\0abc')
460 self.assertTrue('\0' in 'abc\0')
461 self.assertTrue('a' in '\0abc')
462 self.assertTrue('asdf' in 'asdf')
463 self.assertTrue('asdf' not in 'asd')
464 self.assertTrue('asdf' not in '')
Walter Dörwald28256f22003-01-19 16:59:20 +0000465
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000466 self.assertRaises(TypeError, "abc".__contains__)
Walter Dörwald28256f22003-01-19 16:59:20 +0000467
Eric Smith8c663262007-08-25 02:26:07 +0000468 def test_format(self):
469 self.assertEqual(''.format(), '')
470 self.assertEqual('a'.format(), 'a')
471 self.assertEqual('ab'.format(), 'ab')
472 self.assertEqual('a{{'.format(), 'a{')
473 self.assertEqual('a}}'.format(), 'a}')
474 self.assertEqual('{{b'.format(), '{b')
475 self.assertEqual('}}b'.format(), '}b')
476 self.assertEqual('a{{b'.format(), 'a{b')
477
478 # examples from the PEP:
479 import datetime
480 self.assertEqual("My name is {0}".format('Fred'), "My name is Fred")
481 self.assertEqual("My name is {0[name]}".format(dict(name='Fred')),
482 "My name is Fred")
483 self.assertEqual("My name is {0} :-{{}}".format('Fred'),
484 "My name is Fred :-{}")
485
486 d = datetime.date(2007, 8, 18)
487 self.assertEqual("The year is {0.year}".format(d),
488 "The year is 2007")
489
Eric Smith8c663262007-08-25 02:26:07 +0000490 # classes we'll use for testing
491 class C:
492 def __init__(self, x=100):
493 self._x = x
494 def __format__(self, spec):
495 return spec
496
497 class D:
498 def __init__(self, x):
499 self.x = x
500 def __format__(self, spec):
501 return str(self.x)
502
503 # class with __str__, but no __format__
504 class E:
505 def __init__(self, x):
506 self.x = x
507 def __str__(self):
508 return 'E(' + self.x + ')'
509
510 # class with __repr__, but no __format__ or __str__
511 class F:
512 def __init__(self, x):
513 self.x = x
514 def __repr__(self):
515 return 'F(' + self.x + ')'
516
517 # class with __format__ that forwards to string, for some format_spec's
518 class G:
519 def __init__(self, x):
520 self.x = x
521 def __str__(self):
522 return "string is " + self.x
523 def __format__(self, format_spec):
524 if format_spec == 'd':
525 return 'G(' + self.x + ')'
526 return object.__format__(self, format_spec)
527
528 # class that returns a bad type from __format__
529 class H:
530 def __format__(self, format_spec):
531 return 1.0
532
Eric Smith739e2ad2007-08-27 19:07:22 +0000533 class I(datetime.date):
534 def __format__(self, format_spec):
535 return self.strftime(format_spec)
536
Eric Smith185e30c2007-08-30 22:23:08 +0000537 class J(int):
538 def __format__(self, format_spec):
539 return int.__format__(self * 2, format_spec)
540
Eric Smith8c663262007-08-25 02:26:07 +0000541
542 self.assertEqual(''.format(), '')
543 self.assertEqual('abc'.format(), 'abc')
544 self.assertEqual('{0}'.format('abc'), 'abc')
545 self.assertEqual('{0:}'.format('abc'), 'abc')
546# self.assertEqual('{ 0 }'.format('abc'), 'abc')
547 self.assertEqual('X{0}'.format('abc'), 'Xabc')
548 self.assertEqual('{0}X'.format('abc'), 'abcX')
549 self.assertEqual('X{0}Y'.format('abc'), 'XabcY')
550 self.assertEqual('{1}'.format(1, 'abc'), 'abc')
551 self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc')
552 self.assertEqual('{1}X'.format(1, 'abc'), 'abcX')
553 self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY')
554 self.assertEqual('{0}'.format(-15), '-15')
555 self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc')
556 self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc')
557 self.assertEqual('{{'.format(), '{')
558 self.assertEqual('}}'.format(), '}')
559 self.assertEqual('{{}}'.format(), '{}')
560 self.assertEqual('{{x}}'.format(), '{x}')
561 self.assertEqual('{{{0}}}'.format(123), '{123}')
562 self.assertEqual('{{{{0}}}}'.format(), '{{0}}')
563 self.assertEqual('}}{{'.format(), '}{')
564 self.assertEqual('}}x{{'.format(), '}x{')
565
Eric Smith7ade6482007-08-26 22:27:13 +0000566 # weird field names
567 self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz')
568 self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz')
Eric Smith4cb4e4e2007-09-03 08:40:29 +0000569 self.assertEqual("{0[ ]}".format({' ':3}), '3')
Eric Smith7ade6482007-08-26 22:27:13 +0000570
Eric Smith8c663262007-08-25 02:26:07 +0000571 self.assertEqual('{foo._x}'.format(foo=C(20)), '20')
572 self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010')
573 self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc')
574 self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc')
575 self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def')
576 self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def')
577 self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def')
578
Eric Smith8c663262007-08-25 02:26:07 +0000579 # strings
580 self.assertEqual('{0:.3s}'.format('abc'), 'abc')
581 self.assertEqual('{0:.3s}'.format('ab'), 'ab')
582 self.assertEqual('{0:.3s}'.format('abcdef'), 'abc')
583 self.assertEqual('{0:.0s}'.format('abcdef'), '')
584 self.assertEqual('{0:3.3s}'.format('abc'), 'abc')
585 self.assertEqual('{0:2.3s}'.format('abc'), 'abc')
586 self.assertEqual('{0:2.2s}'.format('abc'), 'ab')
587 self.assertEqual('{0:3.2s}'.format('abc'), 'ab ')
588 self.assertEqual('{0:x<0s}'.format('result'), 'result')
589 self.assertEqual('{0:x<5s}'.format('result'), 'result')
590 self.assertEqual('{0:x<6s}'.format('result'), 'result')
591 self.assertEqual('{0:x<7s}'.format('result'), 'resultx')
592 self.assertEqual('{0:x<8s}'.format('result'), 'resultxx')
593 self.assertEqual('{0: <7s}'.format('result'), 'result ')
594 self.assertEqual('{0:<7s}'.format('result'), 'result ')
595 self.assertEqual('{0:>7s}'.format('result'), ' result')
596 self.assertEqual('{0:>8s}'.format('result'), ' result')
597 self.assertEqual('{0:^8s}'.format('result'), ' result ')
598 self.assertEqual('{0:^9s}'.format('result'), ' result ')
599 self.assertEqual('{0:^10s}'.format('result'), ' result ')
600 self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999)
601 self.assertEqual('{0:10000}'.format(''), ' ' * 10000)
602 self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
603
604 # format specifiers for user defined type
605 self.assertEqual('{0:abc}'.format(C()), 'abc')
606
Georg Brandld52429f2008-07-04 15:55:02 +0000607 # !r, !s and !a coercions
Eric Smith8c663262007-08-25 02:26:07 +0000608 self.assertEqual('{0!s}'.format('Hello'), 'Hello')
609 self.assertEqual('{0!s:}'.format('Hello'), 'Hello')
610 self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ')
611 self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ')
612 self.assertEqual('{0!r}'.format('Hello'), "'Hello'")
613 self.assertEqual('{0!r:}'.format('Hello'), "'Hello'")
614 self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)')
Amaury Forgeot d'Arca083f1e2008-09-10 23:51:42 +0000615 self.assertEqual('{0!r}'.format('\u0378'), "'\\u0378'") # nonprintable
Georg Brandld52429f2008-07-04 15:55:02 +0000616 self.assertEqual('{0!r}'.format('\u0374'), "'\u0374'") # printable
617 self.assertEqual('{0!r}'.format(F('\u0374')), 'F(\u0374)')
Georg Brandl559e5d72008-06-11 18:37:52 +0000618 self.assertEqual('{0!a}'.format('Hello'), "'Hello'")
Amaury Forgeot d'Arca083f1e2008-09-10 23:51:42 +0000619 self.assertEqual('{0!a}'.format('\u0378'), "'\\u0378'") # nonprintable
Georg Brandld52429f2008-07-04 15:55:02 +0000620 self.assertEqual('{0!a}'.format('\u0374'), "'\\u0374'") # printable
Georg Brandl559e5d72008-06-11 18:37:52 +0000621 self.assertEqual('{0!a:}'.format('Hello'), "'Hello'")
622 self.assertEqual('{0!a}'.format(F('Hello')), 'F(Hello)')
Georg Brandld52429f2008-07-04 15:55:02 +0000623 self.assertEqual('{0!a}'.format(F('\u0374')), 'F(\\u0374)')
Eric Smith8c663262007-08-25 02:26:07 +0000624
Eric Smith8c663262007-08-25 02:26:07 +0000625 # test fallback to object.__format__
626 self.assertEqual('{0}'.format({}), '{}')
627 self.assertEqual('{0}'.format([]), '[]')
628 self.assertEqual('{0}'.format([1]), '[1]')
629 self.assertEqual('{0}'.format(E('data')), 'E(data)')
630 self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ')
631 self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ')
632 self.assertEqual('{0:d}'.format(G('data')), 'G(data)')
633 self.assertEqual('{0:>15s}'.format(G('data')), ' string is data')
634 self.assertEqual('{0!s}'.format(G('data')), 'string is data')
635
Eric Smith739e2ad2007-08-27 19:07:22 +0000636 self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007,
637 month=8,
638 day=27)),
639 "date: 2007-08-27")
640
Eric Smith185e30c2007-08-30 22:23:08 +0000641 # test deriving from a builtin type and overriding __format__
642 self.assertEqual("{0}".format(J(10)), "20")
643
644
Eric Smith8c663262007-08-25 02:26:07 +0000645 # string format specifiers
646 self.assertEqual('{0:}'.format('a'), 'a')
647
648 # computed format specifiers
649 self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello')
650 self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello')
651 self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello')
652 self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello ')
653 self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hello ')
654
655 # test various errors
656 self.assertRaises(ValueError, '{'.format)
657 self.assertRaises(ValueError, '}'.format)
658 self.assertRaises(ValueError, 'a{'.format)
659 self.assertRaises(ValueError, 'a}'.format)
660 self.assertRaises(ValueError, '{a'.format)
661 self.assertRaises(ValueError, '}a'.format)
Eric Smith11529192007-09-04 23:04:22 +0000662 self.assertRaises(IndexError, '{0}'.format)
663 self.assertRaises(IndexError, '{1}'.format, 'abc')
664 self.assertRaises(KeyError, '{x}'.format)
Eric Smith8c663262007-08-25 02:26:07 +0000665 self.assertRaises(ValueError, "}{".format)
666 self.assertRaises(ValueError, "{".format)
667 self.assertRaises(ValueError, "}".format)
668 self.assertRaises(ValueError, "abc{0:{}".format)
669 self.assertRaises(ValueError, "{0".format)
Eric Smith11529192007-09-04 23:04:22 +0000670 self.assertRaises(IndexError, "{0.}".format)
671 self.assertRaises(ValueError, "{0.}".format, 0)
672 self.assertRaises(IndexError, "{0[}".format)
Eric Smith4cb4e4e2007-09-03 08:40:29 +0000673 self.assertRaises(ValueError, "{0[}".format, [])
Eric Smith11529192007-09-04 23:04:22 +0000674 self.assertRaises(KeyError, "{0]}".format)
675 self.assertRaises(ValueError, "{0.[]}".format, 0)
Eric Smith7ade6482007-08-26 22:27:13 +0000676 self.assertRaises(ValueError, "{0..foo}".format, 0)
Eric Smith11529192007-09-04 23:04:22 +0000677 self.assertRaises(ValueError, "{0[0}".format, 0)
678 self.assertRaises(ValueError, "{0[0:foo}".format, 0)
679 self.assertRaises(KeyError, "{c]}".format)
680 self.assertRaises(ValueError, "{{ {{{0}}".format, 0)
681 self.assertRaises(ValueError, "{0}}".format, 0)
682 self.assertRaises(KeyError, "{foo}".format, bar=3)
Eric Smith8c663262007-08-25 02:26:07 +0000683 self.assertRaises(ValueError, "{0!x}".format, 3)
Eric Smith11529192007-09-04 23:04:22 +0000684 self.assertRaises(ValueError, "{0!}".format, 0)
685 self.assertRaises(ValueError, "{0!rs}".format, 0)
Eric Smith8c663262007-08-25 02:26:07 +0000686 self.assertRaises(ValueError, "{!}".format)
Eric Smith8ec90442009-03-14 12:29:34 +0000687 self.assertRaises(IndexError, "{:}".format)
688 self.assertRaises(IndexError, "{:s}".format)
689 self.assertRaises(IndexError, "{}".format)
Eric Smith8c663262007-08-25 02:26:07 +0000690
Eric Smith41669ca2009-05-23 14:23:22 +0000691 # issue 6089
692 self.assertRaises(ValueError, "{0[0]x}".format, [None])
693 self.assertRaises(ValueError, "{0[0](10)}".format, [None])
694
Eric Smith8c663262007-08-25 02:26:07 +0000695 # can't have a replacement on the field name portion
696 self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4)
697
698 # exceed maximum recursion depth
699 self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '')
700 self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format,
701 0, 1, 2, 3, 4, 5, 6, 7)
702
703 # string format spec errors
704 self.assertRaises(ValueError, "{0:-s}".format, '')
705 self.assertRaises(ValueError, format, "", "-")
706 self.assertRaises(ValueError, "{0:=s}".format, '')
707
Eric Smithb1ebcc62008-07-15 13:02:41 +0000708 # Alternate formatting is not supported
709 self.assertRaises(ValueError, format, '', '#')
710 self.assertRaises(ValueError, format, '', '#20')
711
Eric Smith8ec90442009-03-14 12:29:34 +0000712 def test_format_auto_numbering(self):
713 class C:
714 def __init__(self, x=100):
715 self._x = x
716 def __format__(self, spec):
717 return spec
718
719 self.assertEqual('{}'.format(10), '10')
720 self.assertEqual('{:5}'.format('s'), 's ')
721 self.assertEqual('{!r}'.format('s'), "'s'")
722 self.assertEqual('{._x}'.format(C(10)), '10')
723 self.assertEqual('{[1]}'.format([1, 2]), '2')
724 self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4')
725 self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c')
726
727 self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b')
728 self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b')
729
730 # can't mix and match numbering and auto-numbering
731 self.assertRaises(ValueError, '{}{1}'.format, 1, 2)
732 self.assertRaises(ValueError, '{1}{}'.format, 1, 2)
733 self.assertRaises(ValueError, '{:{1}}'.format, 1, 2)
734 self.assertRaises(ValueError, '{0:{}}'.format, 1, 2)
735
736 # can mix and match auto-numbering and named
737 self.assertEqual('{f}{}'.format(4, f='test'), 'test4')
738 self.assertEqual('{}{f}'.format(4, f='test'), '4test')
739 self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3')
740 self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g')
741
Walter Dörwald28256f22003-01-19 16:59:20 +0000742 def test_formatting(self):
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000743 string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
Walter Dörwald28256f22003-01-19 16:59:20 +0000744 # Testing Unicode formatting strings...
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000745 self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc')
746 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00')
747 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, -2, 3), 'abc, abc, 1, -2.000000, 3.00')
748 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.5), 'abc, abc, -1, -2.000000, 3.50')
749 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.57), 'abc, abc, -1, -2.000000, 3.57')
750 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 1003.57), 'abc, abc, -1, -2.000000, 1003.57')
Walter Dörwald28256f22003-01-19 16:59:20 +0000751 if not sys.platform.startswith('java'):
Walter Dörwald67e83882007-05-05 12:26:27 +0000752 self.assertEqual("%r, %r" % (b"abc", "abc"), "b'abc', 'abc'")
Georg Brandl559e5d72008-06-11 18:37:52 +0000753 self.assertEqual("%r" % ("\u1234",), "'\u1234'")
754 self.assertEqual("%a" % ("\u1234",), "'\\u1234'")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000755 self.assertEqual("%(x)s, %(y)s" % {'x':"abc", 'y':"def"}, 'abc, def')
756 self.assertEqual("%(x)s, %(\xfc)s" % {'x':"abc", '\xfc':"def"}, 'abc, def')
Walter Dörwald56fbcb52003-03-31 18:18:41 +0000757
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000758 self.assertEqual('%c' % 0x1234, '\u1234')
Amaury Forgeot d'Arca4db6862008-07-04 21:26:43 +0000759 self.assertEqual('%c' % 0x21483, '\U00021483')
760 self.assertRaises(OverflowError, "%c".__mod__, (0x110000,))
761 self.assertEqual('%c' % '\U00021483', '\U00021483')
762 self.assertRaises(TypeError, "%c".__mod__, "aa")
Walter Dörwald28256f22003-01-19 16:59:20 +0000763
764 # formatting jobs delegated from the string implementation:
Walter Dörwald28256f22003-01-19 16:59:20 +0000765 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000766 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
767 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
768 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
769 self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...')
770 self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...')
771 self.assertEqual('...%s...%s...%s...%s...' % (1,2,3,"abc"), '...1...2...3...abc...')
772 self.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,"abc"), '...%...%s...1...2...3...abc...')
773 self.assertEqual('...%s...' % "abc", '...abc...')
774 self.assertEqual('%*s' % (5,'abc',), ' abc')
775 self.assertEqual('%*s' % (-5,'abc',), 'abc ')
776 self.assertEqual('%*.*s' % (5,2,'abc',), ' ab')
777 self.assertEqual('%*.*s' % (5,3,'abc',), ' abc')
778 self.assertEqual('%i %*.*s' % (10, 5,3,'abc',), '10 abc')
779 self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, 'abc',), '103 abc')
780 self.assertEqual('%c' % 'a', 'a')
Neil Schemenauercf52c072005-08-12 17:34:58 +0000781 class Wrapper:
782 def __str__(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000783 return '\u1234'
784 self.assertEqual('%s' % Wrapper(), '\u1234')
Walter Dörwald28256f22003-01-19 16:59:20 +0000785
Eric Smith741191f2009-05-06 13:08:15 +0000786 # issue 3382
787 NAN = float('nan')
788 INF = float('inf')
789 self.assertEqual('%f' % NAN, 'nan')
790 self.assertEqual('%F' % NAN, 'NAN')
791 self.assertEqual('%f' % INF, 'inf')
792 self.assertEqual('%F' % INF, 'INF')
793
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000794 @support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
Georg Brandlda6b1072006-01-20 17:48:54 +0000795 def test_format_float(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000796 # should not format with a comma, but always with C locale
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000797 self.assertEqual('1.0', '%.1f' % 1.0)
Georg Brandlda6b1072006-01-20 17:48:54 +0000798
Walter Dörwald28256f22003-01-19 16:59:20 +0000799 def test_constructor(self):
800 # unicode(obj) tests (this maps to PyObject_Unicode() at C level)
801
802 self.assertEqual(
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000803 str('unicode remains unicode'),
804 'unicode remains unicode'
Walter Dörwald28256f22003-01-19 16:59:20 +0000805 )
806
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000807 class UnicodeSubclass(str):
Marc-André Lemburg79f57832002-12-29 19:44:06 +0000808 pass
Guido van Rossuma831cac2000-03-10 23:23:21 +0000809
Walter Dörwald28256f22003-01-19 16:59:20 +0000810 self.assertEqual(
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000811 str(UnicodeSubclass('unicode subclass becomes unicode')),
812 'unicode subclass becomes unicode'
Walter Dörwald28256f22003-01-19 16:59:20 +0000813 )
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000814
Walter Dörwald28256f22003-01-19 16:59:20 +0000815 self.assertEqual(
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000816 str('strings are converted to unicode'),
817 'strings are converted to unicode'
Walter Dörwald28256f22003-01-19 16:59:20 +0000818 )
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000819
Walter Dörwald28256f22003-01-19 16:59:20 +0000820 class StringCompat:
821 def __init__(self, x):
822 self.x = x
823 def __str__(self):
824 return self.x
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000825
Walter Dörwald28256f22003-01-19 16:59:20 +0000826 self.assertEqual(
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000827 str(StringCompat('__str__ compatible objects are recognized')),
828 '__str__ compatible objects are recognized'
Walter Dörwald28256f22003-01-19 16:59:20 +0000829 )
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000830
Walter Dörwald28256f22003-01-19 16:59:20 +0000831 # unicode(obj) is compatible to str():
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000832
Walter Dörwald28256f22003-01-19 16:59:20 +0000833 o = StringCompat('unicode(obj) is compatible to str()')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000834 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
Walter Dörwald28256f22003-01-19 16:59:20 +0000835 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000836
Guido van Rossume2a383d2007-01-15 16:59:06 +0000837 for obj in (123, 123.45, 123):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000838 self.assertEqual(str(obj), str(str(obj)))
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000839
Walter Dörwald28256f22003-01-19 16:59:20 +0000840 # unicode(obj, encoding, error) tests (this maps to
841 # PyUnicode_FromEncodedObject() at C level)
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000842
Walter Dörwald28256f22003-01-19 16:59:20 +0000843 if not sys.platform.startswith('java'):
844 self.assertRaises(
845 TypeError,
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000846 str,
847 'decoding unicode is not supported',
Walter Dörwald28256f22003-01-19 16:59:20 +0000848 'utf-8',
849 'strict'
850 )
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000851
Walter Dörwald28256f22003-01-19 16:59:20 +0000852 self.assertEqual(
Walter Dörwald67e83882007-05-05 12:26:27 +0000853 str(b'strings are decoded to unicode', 'utf-8', 'strict'),
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000854 'strings are decoded to unicode'
Walter Dörwald28256f22003-01-19 16:59:20 +0000855 )
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000856
Walter Dörwald28256f22003-01-19 16:59:20 +0000857 if not sys.platform.startswith('java'):
858 self.assertEqual(
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000859 str(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000860 memoryview(b'character buffers are decoded to unicode'),
Walter Dörwald28256f22003-01-19 16:59:20 +0000861 'utf-8',
862 'strict'
863 ),
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000864 'character buffers are decoded to unicode'
Walter Dörwald28256f22003-01-19 16:59:20 +0000865 )
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000866
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000867 self.assertRaises(TypeError, str, 42, 42, 42)
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000868
Walter Dörwald28256f22003-01-19 16:59:20 +0000869 def test_codecs_utf7(self):
870 utfTests = [
Walter Dörwald67e83882007-05-05 12:26:27 +0000871 ('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example
872 ('Hi Mom -\u263a-!', b'Hi Mom -+Jjo--!'), # RFC2152 example
873 ('\u65E5\u672C\u8A9E', b'+ZeVnLIqe-'), # RFC2152 example
874 ('Item 3 is \u00a31.', b'Item 3 is +AKM-1.'), # RFC2152 example
875 ('+', b'+-'),
876 ('+-', b'+--'),
877 ('+?', b'+-?'),
878 ('\?', b'+AFw?'),
879 ('+?', b'+-?'),
880 (r'\\?', b'+AFwAXA?'),
881 (r'\\\?', b'+AFwAXABc?'),
Antoine Pitrou244651a2009-05-04 18:56:13 +0000882 (r'++--', b'+-+---'),
883 ('\U000abcde', b'+2m/c3g-'), # surrogate pairs
884 ('/', b'/'),
Walter Dörwald28256f22003-01-19 16:59:20 +0000885 ]
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000886
Walter Dörwald28256f22003-01-19 16:59:20 +0000887 for (x, y) in utfTests:
888 self.assertEqual(x.encode('utf-7'), y)
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000889
Antoine Pitrou244651a2009-05-04 18:56:13 +0000890 # Unpaired surrogates not supported
Walter Dörwald67e83882007-05-05 12:26:27 +0000891 self.assertRaises(UnicodeError, str, b'+3ADYAA-', 'utf-7')
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000892
Antoine Pitrou244651a2009-05-04 18:56:13 +0000893 self.assertEqual(str(b'+3ADYAA-', 'utf-7', 'replace'), '\ufffd\ufffd')
Marc-André Lemburgb5507ec2001-10-19 12:02:29 +0000894
Antoine Pitrou5ffd9e92008-07-25 18:05:24 +0000895 # Issue #2242: crash on some Windows/MSVC versions
Antoine Pitrou244651a2009-05-04 18:56:13 +0000896 self.assertEqual(b'+\xc1'.decode('utf-7'), '\xc1')
897
898 # Direct encoded characters
899 set_d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?"
900 # Optional direct characters
901 set_o = '!"#$%&*;<=>@[]^_`{|}'
902 for c in set_d:
903 self.assertEqual(c.encode('utf7'), c.encode('ascii'))
904 self.assertEqual(c.encode('ascii').decode('utf7'), c)
905 for c in set_o:
906 self.assertEqual(c.encode('ascii').decode('utf7'), c)
Antoine Pitrou5ffd9e92008-07-25 18:05:24 +0000907
Walter Dörwald28256f22003-01-19 16:59:20 +0000908 def test_codecs_utf8(self):
Walter Dörwald67e83882007-05-05 12:26:27 +0000909 self.assertEqual(''.encode('utf-8'), b'')
910 self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac')
Martin v. Löwis74b7e442009-06-01 04:23:07 +0000911 if sys.maxunicode == 65535:
912 self.assertEqual('\ud800\udc02'.encode('utf-8'), b'\xf0\x90\x80\x82')
913 self.assertEqual('\ud84d\udc56'.encode('utf-8'), b'\xf0\xa3\x91\x96')
Martin v. Löwise0a2b722009-05-10 08:08:56 +0000914 self.assertEqual('\ud800'.encode('utf-8', 'surrogatepass'), b'\xed\xa0\x80')
915 self.assertEqual('\udc00'.encode('utf-8', 'surrogatepass'), b'\xed\xb0\x80')
Martin v. Löwis74b7e442009-06-01 04:23:07 +0000916 if sys.maxunicode == 65535:
917 self.assertEqual(
918 ('\ud800\udc02'*1000).encode('utf-8'),
919 b'\xf0\x90\x80\x82'*1000)
Walter Dörwald28256f22003-01-19 16:59:20 +0000920 self.assertEqual(
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000921 '\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
922 '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
923 '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
924 '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
925 '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das'
926 ' Nunstuck git und'.encode('utf-8'),
Walter Dörwald67e83882007-05-05 12:26:27 +0000927 b'\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81'
928 b'\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3'
929 b'\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe'
930 b'\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83'
931 b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8'
932 b'\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81'
933 b'\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81'
934 b'\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3'
935 b'\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf'
936 b'\xe3\x80\x8cWenn ist das Nunstuck git und'
Walter Dörwald28256f22003-01-19 16:59:20 +0000937 )
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000938
Walter Dörwald28256f22003-01-19 16:59:20 +0000939 # UTF-8 specific decoding tests
Walter Dörwald67e83882007-05-05 12:26:27 +0000940 self.assertEqual(str(b'\xf0\xa3\x91\x96', 'utf-8'), '\U00023456' )
941 self.assertEqual(str(b'\xf0\x90\x80\x82', 'utf-8'), '\U00010002' )
942 self.assertEqual(str(b'\xe2\x82\xac', 'utf-8'), '\u20ac' )
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000943
Walter Dörwald28256f22003-01-19 16:59:20 +0000944 # Other possible utf-8 test cases:
945 # * strict decoding testing for all of the
946 # UTF8_ERROR cases in PyUnicode_DecodeUTF8
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000947
Martin v. Löwis0d8e16c2003-08-05 06:19:47 +0000948 def test_codecs_idna(self):
949 # Test whether trailing dot is preserved
Walter Dörwald1324c6f2007-05-11 19:57:05 +0000950 self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.")
Martin v. Löwis0d8e16c2003-08-05 06:19:47 +0000951
Walter Dörwald28256f22003-01-19 16:59:20 +0000952 def test_codecs_errors(self):
953 # Error handling (encoding)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000954 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii')
955 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict')
Walter Dörwald67e83882007-05-05 12:26:27 +0000956 self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x")
957 self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x")
Benjamin Peterson308d6372009-09-18 21:42:35 +0000958 self.assertEqual('Andr\202 x'.encode('ascii', 'replace'),
959 'Andr\202 x'.encode('ascii', errors='replace'))
960 self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'),
961 'Andr\202 x'.encode(encoding='ascii', errors='ignore'))
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000962
Walter Dörwald28256f22003-01-19 16:59:20 +0000963 # Error handling (decoding)
Walter Dörwald67e83882007-05-05 12:26:27 +0000964 self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii')
965 self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii', 'strict')
966 self.assertEqual(str(b'Andr\202 x', 'ascii', 'ignore'), "Andr x")
967 self.assertEqual(str(b'Andr\202 x', 'ascii', 'replace'), 'Andr\uFFFD x')
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000968
Walter Dörwald28256f22003-01-19 16:59:20 +0000969 # Error handling (unknown character names)
Guido van Rossum39478e82007-08-27 17:23:59 +0000970 self.assertEqual(b"\\N{foo}xx".decode("unicode-escape", "ignore"), "xx")
Marc-André Lemburg3688a882002-02-06 18:09:02 +0000971
Walter Dörwald28256f22003-01-19 16:59:20 +0000972 # Error handling (truncated escape sequence)
Guido van Rossum9c627722007-08-27 18:31:48 +0000973 self.assertRaises(UnicodeError, b"\\".decode, "unicode-escape")
Marc-André Lemburgd6d06ad2000-07-07 17:48:52 +0000974
Guido van Rossum9c627722007-08-27 18:31:48 +0000975 self.assertRaises(TypeError, b"hello".decode, "test.unicode1")
976 self.assertRaises(TypeError, str, b"hello", "test.unicode2")
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000977 self.assertRaises(TypeError, "hello".encode, "test.unicode1")
978 self.assertRaises(TypeError, "hello".encode, "test.unicode2")
Walter Dörwald28256f22003-01-19 16:59:20 +0000979 # executes PyUnicode_Encode()
980 import imp
981 self.assertRaises(
982 ImportError,
983 imp.find_module,
984 "non-existing module",
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000985 ["non-existing dir"]
Walter Dörwald28256f22003-01-19 16:59:20 +0000986 )
Marc-André Lemburgd6d06ad2000-07-07 17:48:52 +0000987
Walter Dörwald28256f22003-01-19 16:59:20 +0000988 # Error handling (wrong arguments)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000989 self.assertRaises(TypeError, "hello".encode, 42, 42, 42)
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000990
Walter Dörwald28256f22003-01-19 16:59:20 +0000991 # Error handling (PyUnicode_EncodeDecimal())
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000992 self.assertRaises(UnicodeError, int, "\u0200")
Guido van Rossum97064862000-04-10 13:52:48 +0000993
Walter Dörwald28256f22003-01-19 16:59:20 +0000994 def test_codecs(self):
995 # Encoding
Walter Dörwald67e83882007-05-05 12:26:27 +0000996 self.assertEqual('hello'.encode('ascii'), b'hello')
997 self.assertEqual('hello'.encode('utf-7'), b'hello')
998 self.assertEqual('hello'.encode('utf-8'), b'hello')
999 self.assertEqual('hello'.encode('utf8'), b'hello')
1000 self.assertEqual('hello'.encode('utf-16-le'), b'h\000e\000l\000l\000o\000')
1001 self.assertEqual('hello'.encode('utf-16-be'), b'\000h\000e\000l\000l\000o')
1002 self.assertEqual('hello'.encode('latin-1'), b'hello')
Guido van Rossum97064862000-04-10 13:52:48 +00001003
Walter Dörwald28256f22003-01-19 16:59:20 +00001004 # Roundtrip safety for BMP (just the first 1024 chars)
Guido van Rossum805365e2007-05-07 22:24:25 +00001005 for c in range(1024):
Guido van Rossum84fc66d2007-05-03 17:18:26 +00001006 u = chr(c)
Hye-Shik Chang835b2432005-12-17 04:38:31 +00001007 for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le',
1008 'utf-16-be', 'raw_unicode_escape',
1009 'unicode_escape', 'unicode_internal'):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001010 self.assertEqual(str(u.encode(encoding),encoding), u)
Martin v. Löwis047c05e2002-03-21 08:55:28 +00001011
Walter Dörwald28256f22003-01-19 16:59:20 +00001012 # Roundtrip safety for BMP (just the first 256 chars)
Guido van Rossum805365e2007-05-07 22:24:25 +00001013 for c in range(256):
Guido van Rossum84fc66d2007-05-03 17:18:26 +00001014 u = chr(c)
Hye-Shik Chang835b2432005-12-17 04:38:31 +00001015 for encoding in ('latin-1',):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001016 self.assertEqual(str(u.encode(encoding),encoding), u)
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001017
Walter Dörwald28256f22003-01-19 16:59:20 +00001018 # Roundtrip safety for BMP (just the first 128 chars)
Guido van Rossum805365e2007-05-07 22:24:25 +00001019 for c in range(128):
Guido van Rossum84fc66d2007-05-03 17:18:26 +00001020 u = chr(c)
Hye-Shik Chang835b2432005-12-17 04:38:31 +00001021 for encoding in ('ascii',):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001022 self.assertEqual(str(u.encode(encoding),encoding), u)
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001023
Walter Dörwald28256f22003-01-19 16:59:20 +00001024 # Roundtrip safety for non-BMP (just a few chars)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001025 u = '\U00010001\U00020002\U00030003\U00040004\U00050005'
Walter Dörwald28256f22003-01-19 16:59:20 +00001026 for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be',
1027 #'raw_unicode_escape',
1028 'unicode_escape', 'unicode_internal'):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001029 self.assertEqual(str(u.encode(encoding),encoding), u)
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001030
Walter Dörwald28256f22003-01-19 16:59:20 +00001031 # UTF-8 must be roundtrip safe for all UCS-2 code points
1032 # This excludes surrogates: in the full range, there would be
1033 # a surrogate pair (\udbff\udc00), which gets converted back
1034 # to a non-BMP character (\U0010fc00)
Walter Dörwald1324c6f2007-05-11 19:57:05 +00001035 u = ''.join(map(chr, list(range(0,0xd800)) +
1036 list(range(0xe000,0x10000))))
Walter Dörwald28256f22003-01-19 16:59:20 +00001037 for encoding in ('utf-8',):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001038 self.assertEqual(str(u.encode(encoding),encoding), u)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001039
Walter Dörwald28256f22003-01-19 16:59:20 +00001040 def test_codecs_charmap(self):
1041 # 0-127
Guido van Rossum805365e2007-05-07 22:24:25 +00001042 s = bytes(range(128))
Walter Dörwald28256f22003-01-19 16:59:20 +00001043 for encoding in (
1044 'cp037', 'cp1026',
1045 'cp437', 'cp500', 'cp737', 'cp775', 'cp850',
1046 'cp852', 'cp855', 'cp860', 'cp861', 'cp862',
1047 'cp863', 'cp865', 'cp866',
1048 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
1049 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6',
1050 'iso8859_7', 'iso8859_9', 'koi8_r', 'latin_1',
1051 'mac_cyrillic', 'mac_latin2',
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +00001052
Walter Dörwald28256f22003-01-19 16:59:20 +00001053 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
1054 'cp1256', 'cp1257', 'cp1258',
1055 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
Marc-André Lemburgbd3be8f2002-02-07 11:33:49 +00001056
Walter Dörwald28256f22003-01-19 16:59:20 +00001057 'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
1058 'cp1006', 'iso8859_8',
Guido van Rossum9e896b32000-04-05 20:11:21 +00001059
Walter Dörwald28256f22003-01-19 16:59:20 +00001060 ### These have undefined mappings:
1061 #'cp424',
Guido van Rossum9e896b32000-04-05 20:11:21 +00001062
Walter Dörwald28256f22003-01-19 16:59:20 +00001063 ### These fail the round-trip:
1064 #'cp875'
Guido van Rossum9e896b32000-04-05 20:11:21 +00001065
Walter Dörwald28256f22003-01-19 16:59:20 +00001066 ):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001067 self.assertEqual(str(s, encoding).encode(encoding), s)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001068
Walter Dörwald28256f22003-01-19 16:59:20 +00001069 # 128-255
Guido van Rossum805365e2007-05-07 22:24:25 +00001070 s = bytes(range(128, 256))
Walter Dörwald28256f22003-01-19 16:59:20 +00001071 for encoding in (
1072 'cp037', 'cp1026',
1073 'cp437', 'cp500', 'cp737', 'cp775', 'cp850',
1074 'cp852', 'cp855', 'cp860', 'cp861', 'cp862',
1075 'cp863', 'cp865', 'cp866',
1076 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
1077 'iso8859_2', 'iso8859_4', 'iso8859_5',
1078 'iso8859_9', 'koi8_r', 'latin_1',
1079 'mac_cyrillic', 'mac_latin2',
Fred Drake004d5e62000-10-23 17:22:08 +00001080
Walter Dörwald28256f22003-01-19 16:59:20 +00001081 ### These have undefined mappings:
1082 #'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
1083 #'cp1256', 'cp1257', 'cp1258',
1084 #'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
1085 #'iso8859_3', 'iso8859_6', 'iso8859_7',
1086 #'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
Fred Drake004d5e62000-10-23 17:22:08 +00001087
Walter Dörwald28256f22003-01-19 16:59:20 +00001088 ### These fail the round-trip:
1089 #'cp1006', 'cp875', 'iso8859_8',
Tim Peters2f228e72001-05-13 00:19:31 +00001090
Walter Dörwald28256f22003-01-19 16:59:20 +00001091 ):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001092 self.assertEqual(str(s, encoding).encode(encoding), s)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001093
Walter Dörwald28256f22003-01-19 16:59:20 +00001094 def test_concatenation(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001095 self.assertEqual(("abc" "def"), "abcdef")
1096 self.assertEqual(("abc" "def"), "abcdef")
1097 self.assertEqual(("abc" "def"), "abcdef")
1098 self.assertEqual(("abc" "def" "ghi"), "abcdefghi")
1099 self.assertEqual(("abc" "def" "ghi"), "abcdefghi")
Fred Drake004d5e62000-10-23 17:22:08 +00001100
Walter Dörwald28256f22003-01-19 16:59:20 +00001101 def test_printing(self):
1102 class BitBucket:
1103 def write(self, text):
1104 pass
Fred Drake004d5e62000-10-23 17:22:08 +00001105
Walter Dörwald28256f22003-01-19 16:59:20 +00001106 out = BitBucket()
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001107 print('abc', file=out)
1108 print('abc', 'def', file=out)
1109 print('abc', 'def', file=out)
1110 print('abc', 'def', file=out)
1111 print('abc\n', file=out)
1112 print('abc\n', end=' ', file=out)
1113 print('abc\n', end=' ', file=out)
1114 print('def\n', file=out)
1115 print('def\n', file=out)
Fred Drake004d5e62000-10-23 17:22:08 +00001116
Martin v. Löwis9a3a9f72003-05-18 12:31:09 +00001117 def test_ucs4(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001118 x = '\U00100000'
Martin v. Löwis9a3a9f72003-05-18 12:31:09 +00001119 y = x.encode("raw-unicode-escape").decode("raw-unicode-escape")
1120 self.assertEqual(x, y)
1121
Christian Heimesfe337bf2008-03-23 21:54:12 +00001122 # FIXME
1123 #y = r'\U00100000'
1124 #x = y.encode("raw-unicode-escape").decode("raw-unicode-escape")
1125 #self.assertEqual(x, y)
1126 #y = r'\U00010000'
1127 #x = y.encode("raw-unicode-escape").decode("raw-unicode-escape")
1128 #self.assertEqual(x, y)
1129
1130 #try:
1131 # '\U11111111'.decode("raw-unicode-escape")
1132 #except UnicodeDecodeError as e:
1133 # self.assertEqual(e.start, 0)
1134 # self.assertEqual(e.end, 10)
1135 #else:
1136 # self.fail("Should have raised UnicodeDecodeError")
1137
Brett Cannonc3647ac2005-04-26 03:45:26 +00001138 def test_conversion(self):
1139 # Make sure __unicode__() works properly
1140 class Foo0:
1141 def __str__(self):
1142 return "foo"
1143
1144 class Foo1:
Guido van Rossum98297ee2007-11-06 21:34:58 +00001145 def __str__(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001146 return "foo"
Brett Cannonc3647ac2005-04-26 03:45:26 +00001147
1148 class Foo2(object):
Guido van Rossum98297ee2007-11-06 21:34:58 +00001149 def __str__(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001150 return "foo"
Brett Cannonc3647ac2005-04-26 03:45:26 +00001151
1152 class Foo3(object):
Guido van Rossum98297ee2007-11-06 21:34:58 +00001153 def __str__(self):
Brett Cannonc3647ac2005-04-26 03:45:26 +00001154 return "foo"
1155
1156 class Foo4(str):
Guido van Rossum98297ee2007-11-06 21:34:58 +00001157 def __str__(self):
Brett Cannonc3647ac2005-04-26 03:45:26 +00001158 return "foo"
1159
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001160 class Foo5(str):
Guido van Rossum98297ee2007-11-06 21:34:58 +00001161 def __str__(self):
Brett Cannonc3647ac2005-04-26 03:45:26 +00001162 return "foo"
1163
1164 class Foo6(str):
1165 def __str__(self):
1166 return "foos"
1167
Guido van Rossum98297ee2007-11-06 21:34:58 +00001168 def __str__(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001169 return "foou"
Brett Cannonc3647ac2005-04-26 03:45:26 +00001170
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001171 class Foo7(str):
Brett Cannonc3647ac2005-04-26 03:45:26 +00001172 def __str__(self):
1173 return "foos"
Guido van Rossum98297ee2007-11-06 21:34:58 +00001174 def __str__(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001175 return "foou"
Brett Cannonc3647ac2005-04-26 03:45:26 +00001176
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001177 class Foo8(str):
Brett Cannonc3647ac2005-04-26 03:45:26 +00001178 def __new__(cls, content=""):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001179 return str.__new__(cls, 2*content)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001180 def __str__(self):
Brett Cannonc3647ac2005-04-26 03:45:26 +00001181 return self
1182
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001183 class Foo9(str):
Brett Cannonc3647ac2005-04-26 03:45:26 +00001184 def __str__(self):
Brett Cannonc3647ac2005-04-26 03:45:26 +00001185 return "not unicode"
1186
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001187 self.assertEqual(str(Foo0()), "foo")
1188 self.assertEqual(str(Foo1()), "foo")
1189 self.assertEqual(str(Foo2()), "foo")
1190 self.assertEqual(str(Foo3()), "foo")
1191 self.assertEqual(str(Foo4("bar")), "foo")
1192 self.assertEqual(str(Foo5("bar")), "foo")
1193 self.assertEqual(str(Foo6("bar")), "foou")
1194 self.assertEqual(str(Foo7("bar")), "foou")
1195 self.assertEqual(str(Foo8("foo")), "foofoo")
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001196 self.assertEqual(str(Foo9("foo")), "not unicode")
Brett Cannonc3647ac2005-04-26 03:45:26 +00001197
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001198 def test_unicode_repr(self):
1199 class s1:
1200 def __repr__(self):
1201 return '\\n'
1202
1203 class s2:
1204 def __repr__(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001205 return '\\n'
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001206
1207 self.assertEqual(repr(s1()), '\\n')
1208 self.assertEqual(repr(s2()), '\\n')
1209
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001210 def test_expandtabs_overflows_gracefully(self):
1211 # This test only affects 32-bit platforms because expandtabs can only take
1212 # an int as the max value, not a 64-bit C long. If expandtabs is changed
1213 # to take a 64-bit long, this test should apply to all platforms.
Christian Heimesa37d4c62007-12-04 23:02:19 +00001214 if sys.maxsize > (1 << 32) or struct.calcsize('P') != 4:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001215 return
Christian Heimesa37d4c62007-12-04 23:02:19 +00001216 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001217
Amaury Forgeot d'Arc7888d082008-08-01 01:06:32 +00001218 def test_raiseMemError(self):
1219 # Ensure that the freelist contains a consistent object, even
1220 # when a string allocation fails with a MemoryError.
1221 # This used to crash the interpreter,
1222 # or leak references when the number was smaller.
Antoine Pitroub305aeb2008-09-05 22:13:06 +00001223 charwidth = 4 if sys.maxunicode >= 0x10000 else 2
1224 # Note: sys.maxsize is half of the actual max allocation because of
1225 # the signedness of Py_ssize_t.
1226 alloc = lambda: "a" * (sys.maxsize // charwidth * 2)
Antoine Pitrou3db3e872008-08-17 17:06:51 +00001227 self.assertRaises(MemoryError, alloc)
1228 self.assertRaises(MemoryError, alloc)
1229
Amaury Forgeot d'Arc7888d082008-08-01 01:06:32 +00001230
Walter Dörwald28256f22003-01-19 16:59:20 +00001231def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001232 support.run_unittest(__name__)
Barry Warsaw817918c2002-08-06 16:58:21 +00001233
Walter Dörwald28256f22003-01-19 16:59:20 +00001234if __name__ == "__main__":
1235 test_main()