Debian Codegraph @

Lib/test/test_str.py

raw
1 """ Test script for the Unicode implementation.
2
3 Written by Marc-Andre Lemburg ([email protected]).
4
5 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
6
7 """
8 import _string
9 import codecs
10 import datetime
11 import itertools
12 import operator
13 import pickle
14 import struct
15 import sys
16 import textwrap
17 import unicodedata
18 import unittest
19 import warnings
20 from test.support import warnings_helper
21 from test import support, string_tests
22 from test.support.script_helper import assert_python_failure
23
24 try:
25 import _testcapi
26 except ImportError:
27 _testcapi = None
28
29 # Error handling (bad decoder return)
30 def search_function(encoding):
31 def decode1(input, errors="strict"):
32 return 42 # not a tuple
33 def encode1(input, errors="strict"):
34 return 42 # not a tuple
35 def encode2(input, errors="strict"):
36 return (42, 42) # no unicode
37 def decode2(input, errors="strict"):
38 return (42, 42) # no unicode
39 if encoding=="test.unicode1":
40 return (encode1, decode1, None, None)
41 elif encoding=="test.unicode2":
42 return (encode2, decode2, None, None)
43 else:
44 return None
45
46 def duplicate_string(text):
47 """
48 Try to get a fresh clone of the specified text:
49 new object with a reference count of 1.
50
51 This is a best-effort: latin1 single letters and the empty
52 string ('') are singletons and cannot be cloned.
53 """
54 return text.encode().decode()
55
56 class StrSubclass(str):
57 pass
58
59 class OtherStrSubclass(str):
60 pass
61
62 class WithStr:
63 def __init__(self, value):
64 self.value = value
65 def __str__(self):
66 return self.value
67
68 class WithRepr:
69 def __init__(self, value):
70 self.value = value
71 def __repr__(self):
72 return self.value
73
74 class StrTest(string_tests.StringLikeTest,
75 string_tests.MixinStrUnicodeTest,
76 unittest.TestCase):
77
78 type2test = str
79
80 def setUp(self):
81 codecs.register(search_function)
82 self.addCleanup(codecs.unregister, search_function)
83
84 def checkequalnofix(self, result, object, methodname, *args):
85 method = getattr(object, methodname)
86 realresult = method(*args)
87 self.assertEqual(realresult, result)
88 self.assertTrue(type(realresult) is type(result))
89
90 # if the original is returned make sure that
91 # this doesn't happen with subclasses
92 if realresult is object:
93 class usub(str):
94 def __repr__(self):
95 return 'usub(%r)' % str.__repr__(self)
96 object = usub(object)
97 method = getattr(object, methodname)
98 realresult = method(*args)
99 self.assertEqual(realresult, result)
100 self.assertTrue(object is not realresult)
101
102 def assertTypedEqual(self, actual, expected):
103 self.assertIs(type(actual), type(expected))
104 self.assertEqual(actual, expected)
105
106 def test_literals(self):
107 self.assertEqual('\xff', '\u00ff')
108 self.assertEqual('\uffff', '\U0000ffff')
109 self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'')
110 self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'')
111 self.assertRaises(SyntaxError, eval, '\'\\U%08x\'' % 0x110000)
112 # raw strings should not have unicode escapes
113 self.assertNotEqual(r"\u0020", " ")
114
115 def test_ascii(self):
116 self.assertEqual(ascii('abc'), "'abc'")
117 self.assertEqual(ascii('ab\\c'), "'ab\\\\c'")
118 self.assertEqual(ascii('ab\\'), "'ab\\\\'")
119 self.assertEqual(ascii('\\c'), "'\\\\c'")
120 self.assertEqual(ascii('\\'), "'\\\\'")
121 self.assertEqual(ascii('\n'), "'\\n'")
122 self.assertEqual(ascii('\r'), "'\\r'")
123 self.assertEqual(ascii('\t'), "'\\t'")
124 self.assertEqual(ascii('\b'), "'\\x08'")
125 self.assertEqual(ascii("'\""), """'\\'"'""")
126 self.assertEqual(ascii("'\""), """'\\'"'""")
127 self.assertEqual(ascii("'"), '''"'"''')
128 self.assertEqual(ascii('"'), """'"'""")
129 latin1repr = (
130 "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
131 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a"
132 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
133 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
134 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d"
135 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
136 "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9"
137 "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7"
138 "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5"
139 "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3"
140 "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1"
141 "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef"
142 "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd"
143 "\\xfe\\xff'")
144 testrepr = ascii(''.join(map(chr, range(256))))
145 self.assertEqual(testrepr, latin1repr)
146 # Test ascii works on wide unicode escapes without overflow.
147 self.assertEqual(ascii("\U00010000" * 39 + "\uffff" * 4096),
148 ascii("\U00010000" * 39 + "\uffff" * 4096))
149
150 self.assertTypedEqual(ascii('\U0001f40d'), r"'\U0001f40d'")
151 self.assertTypedEqual(ascii(StrSubclass('abc')), "'abc'")
152 self.assertTypedEqual(ascii(WithRepr('<abc>')), '<abc>')
153 self.assertTypedEqual(ascii(WithRepr(StrSubclass('<abc>'))), StrSubclass('<abc>'))
154 self.assertTypedEqual(ascii(WithRepr('<\U0001f40d>')), r'<\U0001f40d>')
155 self.assertTypedEqual(ascii(WithRepr(StrSubclass('<\U0001f40d>'))), r'<\U0001f40d>')
156 self.assertRaises(TypeError, ascii, WithRepr(b'byte-repr'))
157
158 def test_repr(self):
159 # Test basic sanity of repr()
160 self.assertEqual(repr('abc'), "'abc'")
161 self.assertEqual(repr('ab\\c'), "'ab\\\\c'")
162 self.assertEqual(repr('ab\\'), "'ab\\\\'")
163 self.assertEqual(repr('\\c'), "'\\\\c'")
164 self.assertEqual(repr('\\'), "'\\\\'")
165 self.assertEqual(repr('\n'), "'\\n'")
166 self.assertEqual(repr('\r'), "'\\r'")
167 self.assertEqual(repr('\t'), "'\\t'")
168 self.assertEqual(repr('\b'), "'\\x08'")
169 self.assertEqual(repr("'\""), """'\\'"'""")
170 self.assertEqual(repr("'\""), """'\\'"'""")
171 self.assertEqual(repr("'"), '''"'"''')
172 self.assertEqual(repr('"'), """'"'""")
173 latin1repr = (
174 "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
175 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a"
176 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
177 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
178 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d"
179 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
180 "\\x9c\\x9d\\x9e\\x9f\\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9"
181 "\xaa\xab\xac\\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7"
182 "\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5"
183 "\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3"
184 "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1"
185 "\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
186 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd"
187 "\xfe\xff'")
188 testrepr = repr(''.join(map(chr, range(256))))
189 self.assertEqual(testrepr, latin1repr)
190 # Test repr works on wide unicode escapes without overflow.
191 self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096),
192 repr("\U00010000" * 39 + "\uffff" * 4096))
193
194 self.assertTypedEqual(repr('\U0001f40d'), "'\U0001f40d'")
195 self.assertTypedEqual(repr(StrSubclass('abc')), "'abc'")
196 self.assertTypedEqual(repr(WithRepr('<abc>')), '<abc>')
197 self.assertTypedEqual(repr(WithRepr(StrSubclass('<abc>'))), StrSubclass('<abc>'))
198 self.assertTypedEqual(repr(WithRepr('<\U0001f40d>')), '<\U0001f40d>')
199 self.assertTypedEqual(repr(WithRepr(StrSubclass('<\U0001f40d>'))), StrSubclass('<\U0001f40d>'))
200 self.assertRaises(TypeError, repr, WithRepr(b'byte-repr'))
201
202 def test_iterators(self):
203 # Make sure unicode objects have an __iter__ method
204 it = "\u1111\u2222\u3333".__iter__()
205 self.assertEqual(next(it), "\u1111")
206 self.assertEqual(next(it), "\u2222")
207 self.assertEqual(next(it), "\u3333")
208 self.assertRaises(StopIteration, next, it)
209
210 def test_iterators_invocation(self):
211 cases = [type(iter('abc')), type(iter('🚀'))]
212 for cls in cases:
213 with self.subTest(cls=cls):
214 self.assertRaises(TypeError, cls)
215
216 def test_iteration(self):
217 cases = ['abc', '🚀🚀🚀', "\u1111\u2222\u3333"]
218 for case in cases:
219 with self.subTest(string=case):
220 self.assertEqual(case, "".join(iter(case)))
221
222 def test_exhausted_iterator(self):
223 cases = ['abc', '🚀🚀🚀', "\u1111\u2222\u3333"]
224 for case in cases:
225 with self.subTest(case=case):
226 iterator = iter(case)
227 tuple(iterator)
228 self.assertRaises(StopIteration, next, iterator)
229
230 def test_pickle_iterator(self):
231 cases = ['abc', '🚀🚀🚀', "\u1111\u2222\u3333"]
232 for case in cases:
233 with self.subTest(case=case):
234 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
235 it = iter(case)
236 with self.subTest(proto=proto):
237 pickled = "".join(pickle.loads(pickle.dumps(it, proto)))
238 self.assertEqual(case, pickled)
239
240 def test_count(self):
241 string_tests.StringLikeTest.test_count(self)
242 # check mixed argument types
243 self.checkequalnofix(3, 'aaa', 'count', 'a')
244 self.checkequalnofix(0, 'aaa', 'count', 'b')
245 self.checkequalnofix(3, 'aaa', 'count', 'a')
246 self.checkequalnofix(0, 'aaa', 'count', 'b')
247 self.checkequalnofix(0, 'aaa', 'count', 'b')
248 self.checkequalnofix(1, 'aaa', 'count', 'a', -1)
249 self.checkequalnofix(3, 'aaa', 'count', 'a', -10)
250 self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1)
251 self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10)
252 # test mixed kinds
253 self.checkequal(10, '\u0102' + 'a' * 10, 'count', 'a')
254 self.checkequal(10, '\U00100304' + 'a' * 10, 'count', 'a')
255 self.checkequal(10, '\U00100304' + '\u0102' * 10, 'count', '\u0102')
256 self.checkequal(0, 'a' * 10, 'count', '\u0102')
257 self.checkequal(0, 'a' * 10, 'count', '\U00100304')
258 self.checkequal(0, '\u0102' * 10, 'count', '\U00100304')
259 self.checkequal(10, '\u0102' + 'a_' * 10, 'count', 'a_')
260 self.checkequal(10, '\U00100304' + 'a_' * 10, 'count', 'a_')
261 self.checkequal(10, '\U00100304' + '\u0102_' * 10, 'count', '\u0102_')
262 self.checkequal(0, 'a' * 10, 'count', 'a\u0102')
263 self.checkequal(0, 'a' * 10, 'count', 'a\U00100304')
264 self.checkequal(0, '\u0102' * 10, 'count', '\u0102\U00100304')
265 # test subclass
266 class MyStr(str):
267 pass
268 self.checkequal(3, MyStr('aaa'), 'count', 'a')
269
270 def test_find(self):
271 string_tests.StringLikeTest.test_find(self)
272 # test implementation details of the memchr fast path
273 self.checkequal(100, 'a' * 100 + '\u0102', 'find', '\u0102')
274 self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0201')
275 self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0120')
276 self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0220')
277 self.checkequal(100, 'a' * 100 + '\U00100304', 'find', '\U00100304')
278 self.checkequal(-1, 'a' * 100 + '\U00100304', 'find', '\U00100204')
279 self.checkequal(-1, 'a' * 100 + '\U00100304', 'find', '\U00102004')
280 # check mixed argument types
281 self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc')
282 self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1)
283 self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4)
284
285 # test utf-8 non-ascii char
286 self.checkequal(0, 'тест', 'find', 'т')
287 self.checkequal(3, 'тест', 'find', 'т', 1)
288 self.checkequal(-1, 'тест', 'find', 'т', 1, 3)
289 self.checkequal(-1, 'тест', 'find', 'e') # english `e`
290 # test utf-8 non-ascii slice
291 self.checkequal(1, 'тест тест', 'find', 'ес')
292 self.checkequal(1, 'тест тест', 'find', 'ес', 1)
293 self.checkequal(1, 'тест тест', 'find', 'ес', 1, 3)
294 self.checkequal(6, 'тест тест', 'find', 'ес', 2)
295 self.checkequal(-1, 'тест тест', 'find', 'ес', 6, 7)
296 self.checkequal(-1, 'тест тест', 'find', 'ес', 7)
297 self.checkequal(-1, 'тест тест', 'find', 'ec') # english `ec`
298
299 self.assertRaises(TypeError, 'hello'.find)
300 self.assertRaises(TypeError, 'hello'.find, 42)
301 # test mixed kinds
302 self.checkequal(100, '\u0102' * 100 + 'a', 'find', 'a')
303 self.checkequal(100, '\U00100304' * 100 + 'a', 'find', 'a')
304 self.checkequal(100, '\U00100304' * 100 + '\u0102', 'find', '\u0102')
305 self.checkequal(-1, 'a' * 100, 'find', '\u0102')
306 self.checkequal(-1, 'a' * 100, 'find', '\U00100304')
307 self.checkequal(-1, '\u0102' * 100, 'find', '\U00100304')
308 self.checkequal(100, '\u0102' * 100 + 'a_', 'find', 'a_')
309 self.checkequal(100, '\U00100304' * 100 + 'a_', 'find', 'a_')
310 self.checkequal(100, '\U00100304' * 100 + '\u0102_', 'find', '\u0102_')
311 self.checkequal(-1, 'a' * 100, 'find', 'a\u0102')
312 self.checkequal(-1, 'a' * 100, 'find', 'a\U00100304')
313 self.checkequal(-1, '\u0102' * 100, 'find', '\u0102\U00100304')
314
315 def test_rfind(self):
316 string_tests.StringLikeTest.test_rfind(self)
317 # test implementation details of the memrchr fast path
318 self.checkequal(0, '\u0102' + 'a' * 100 , 'rfind', '\u0102')
319 self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0201')
320 self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0120')
321 self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0220')
322 self.checkequal(0, '\U00100304' + 'a' * 100, 'rfind', '\U00100304')
323 self.checkequal(-1, '\U00100304' + 'a' * 100, 'rfind', '\U00100204')
324 self.checkequal(-1, '\U00100304' + 'a' * 100, 'rfind', '\U00102004')
325 # check mixed argument types
326 self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc')
327 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
328 self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '')
329 # test utf-8 non-ascii char
330 self.checkequal(1, 'тест', 'rfind', 'е')
331 self.checkequal(1, 'тест', 'rfind', 'е', 1)
332 self.checkequal(-1, 'тест', 'rfind', 'е', 2)
333 self.checkequal(-1, 'тест', 'rfind', 'e') # english `e`
334 # test utf-8 non-ascii slice
335 self.checkequal(6, 'тест тест', 'rfind', 'ес')
336 self.checkequal(6, 'тест тест', 'rfind', 'ес', 1)
337 self.checkequal(1, 'тест тест', 'rfind', 'ес', 1, 3)
338 self.checkequal(6, 'тест тест', 'rfind', 'ес', 2)
339 self.checkequal(-1, 'тест тест', 'rfind', 'ес', 6, 7)
340 self.checkequal(-1, 'тест тест', 'rfind', 'ес', 7)
341 self.checkequal(-1, 'тест тест', 'rfind', 'ec') # english `ec`
342 # test mixed kinds
343 self.checkequal(0, 'a' + '\u0102' * 100, 'rfind', 'a')
344 self.checkequal(0, 'a' + '\U00100304' * 100, 'rfind', 'a')
345 self.checkequal(0, '\u0102' + '\U00100304' * 100, 'rfind', '\u0102')
346 self.checkequal(-1, 'a' * 100, 'rfind', '\u0102')
347 self.checkequal(-1, 'a' * 100, 'rfind', '\U00100304')
348 self.checkequal(-1, '\u0102' * 100, 'rfind', '\U00100304')
349 self.checkequal(0, '_a' + '\u0102' * 100, 'rfind', '_a')
350 self.checkequal(0, '_a' + '\U00100304' * 100, 'rfind', '_a')
351 self.checkequal(0, '_\u0102' + '\U00100304' * 100, 'rfind', '_\u0102')
352 self.checkequal(-1, 'a' * 100, 'rfind', '\u0102a')
353 self.checkequal(-1, 'a' * 100, 'rfind', '\U00100304a')
354 self.checkequal(-1, '\u0102' * 100, 'rfind', '\U00100304\u0102')
355
356 def test_index(self):
357 string_tests.StringLikeTest.test_index(self)
358 self.checkequalnofix(0, 'abcdefghiabc', 'index', '')
359 self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def')
360 self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc')
361 self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1)
362 self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib')
363 self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1)
364 self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8)
365 self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1)
366 # test mixed kinds
367 self.checkequal(100, '\u0102' * 100 + 'a', 'index', 'a')
368 self.checkequal(100, '\U00100304' * 100 + 'a', 'index', 'a')
369 self.checkequal(100, '\U00100304' * 100 + '\u0102', 'index', '\u0102')
370 self.assertRaises(ValueError, ('a' * 100).index, '\u0102')
371 self.assertRaises(ValueError, ('a' * 100).index, '\U00100304')
372 self.assertRaises(ValueError, ('\u0102' * 100).index, '\U00100304')
373 self.checkequal(100, '\u0102' * 100 + 'a_', 'index', 'a_')
374 self.checkequal(100, '\U00100304' * 100 + 'a_', 'index', 'a_')
375 self.checkequal(100, '\U00100304' * 100 + '\u0102_', 'index', '\u0102_')
376 self.assertRaises(ValueError, ('a' * 100).index, 'a\u0102')
377 self.assertRaises(ValueError, ('a' * 100).index, 'a\U00100304')
378 self.assertRaises(ValueError, ('\u0102' * 100).index, '\u0102\U00100304')
379
380 def test_rindex(self):
381 string_tests.StringLikeTest.test_rindex(self)
382 self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '')
383 self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def')
384 self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc')
385 self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
386
387 self.assertRaises(ValueError, 'abcdefghiabc'.rindex, 'hib')
388 self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1)
389 self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1)
390 self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8)
391 self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1)
392 # test mixed kinds
393 self.checkequal(0, 'a' + '\u0102' * 100, 'rindex', 'a')
394 self.checkequal(0, 'a' + '\U00100304' * 100, 'rindex', 'a')
395 self.checkequal(0, '\u0102' + '\U00100304' * 100, 'rindex', '\u0102')
396 self.assertRaises(ValueError, ('a' * 100).rindex, '\u0102')
397 self.assertRaises(ValueError, ('a' * 100).rindex, '\U00100304')
398 self.assertRaises(ValueError, ('\u0102' * 100).rindex, '\U00100304')
399 self.checkequal(0, '_a' + '\u0102' * 100, 'rindex', '_a')
400 self.checkequal(0, '_a' + '\U00100304' * 100, 'rindex', '_a')
401 self.checkequal(0, '_\u0102' + '\U00100304' * 100, 'rindex', '_\u0102')
402 self.assertRaises(ValueError, ('a' * 100).rindex, '\u0102a')
403 self.assertRaises(ValueError, ('a' * 100).rindex, '\U00100304a')
404 self.assertRaises(ValueError, ('\u0102' * 100).rindex, '\U00100304\u0102')
405
406 def test_maketrans_translate(self):
407 # these work with plain translate()
408 self.checkequalnofix('bbbc', 'abababc', 'translate',
409 {ord('a'): None})
410 self.checkequalnofix('iiic', 'abababc', 'translate',
411 {ord('a'): None, ord('b'): ord('i')})
412 self.checkequalnofix('iiix', 'abababc', 'translate',
413 {ord('a'): None, ord('b'): ord('i'), ord('c'): 'x'})
414 self.checkequalnofix('c', 'abababc', 'translate',
415 {ord('a'): None, ord('b'): ''})
416 self.checkequalnofix('xyyx', 'xzx', 'translate',
417 {ord('z'): 'yy'})
418
419 # this needs maketrans()
420 self.checkequalnofix('abababc', 'abababc', 'translate',
421 {'b': '<i>'})
422 tbl = self.type2test.maketrans({'a': None, 'b': '<i>'})
423 self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl)
424 # test alternative way of calling maketrans()
425 tbl = self.type2test.maketrans('abc', 'xyz', 'd')
426 self.checkequalnofix('xyzzy', 'abdcdcbdddd', 'translate', tbl)
427
428 # various tests switching from ASCII to latin1 or the opposite;
429 # same length, remove a letter, or replace with a longer string.
430 self.assertEqual("[a]".translate(str.maketrans('a', 'X')),
431 "[X]")
432 self.assertEqual("[a]".translate(str.maketrans({'a': 'X'})),
433 "[X]")
434 self.assertEqual("[a]".translate(str.maketrans({'a': None})),
435 "[]")
436 self.assertEqual("[a]".translate(str.maketrans({'a': 'XXX'})),
437 "[XXX]")
438 self.assertEqual("[a]".translate(str.maketrans({'a': '\xe9'})),
439 "[\xe9]")
440 self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '123'})),
441 "x123")
442 self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '\xe9'})),
443 "x\xe9")
444
445 # test non-ASCII (don't take the fast-path)
446 self.assertEqual("[a]".translate(str.maketrans({'a': '<\xe9>'})),
447 "[<\xe9>]")
448 self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': 'a'})),
449 "[a]")
450 self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': None})),
451 "[]")
452 self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': '123'})),
453 "[123]")
454 self.assertEqual("[a\xe9]".translate(str.maketrans({'a': '<\u20ac>'})),
455 "[<\u20ac>\xe9]")
456
457 # invalid Unicode characters
458 invalid_char = 0x10ffff+1
459 for before in "a\xe9\u20ac\U0010ffff":
460 mapping = str.maketrans({before: invalid_char})
461 text = "[%s]" % before
462 self.assertRaises(ValueError, text.translate, mapping)
463
464 # errors
465 self.assertRaises(TypeError, self.type2test.maketrans)
466 self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg')
467 self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def')
468 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 2)
469 self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def', 2)
470 self.assertRaises(ValueError, self.type2test.maketrans, {'xy': 2})
471 self.assertRaises(TypeError, self.type2test.maketrans, {(1,): 2})
472
473 self.assertRaises(TypeError, 'hello'.translate)
474 self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz')
475
476 def test_split(self):
477 string_tests.StringLikeTest.test_split(self)
478
479 # test mixed kinds
480 for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
481 left *= 9
482 right *= 9
483 for delim in ('c', '\u0102', '\U00010302'):
484 self.checkequal([left + right],
485 left + right, 'split', delim)
486 self.checkequal([left, right],
487 left + delim + right, 'split', delim)
488 self.checkequal([left + right],
489 left + right, 'split', delim * 2)
490 self.checkequal([left, right],
491 left + delim * 2 + right, 'split', delim *2)
492
493 def test_rsplit(self):
494 string_tests.StringLikeTest.test_rsplit(self)
495 # test mixed kinds
496 for left, right in ('ba', 'юё', '\u0101\u0100', '\U00010301\U00010300'):
497 left *= 9
498 right *= 9
499 for delim in ('c', 'ы', '\u0102', '\U00010302'):
500 self.checkequal([left + right],
501 left + right, 'rsplit', delim)
502 self.checkequal([left, right],
503 left + delim + right, 'rsplit', delim)
504 self.checkequal([left + right],
505 left + right, 'rsplit', delim * 2)
506 self.checkequal([left, right],
507 left + delim * 2 + right, 'rsplit', delim *2)
508
509 # Check `None` as well:
510 self.checkequal([left + right],
511 left + right, 'rsplit', None)
512
513 def test_partition(self):
514 string_tests.StringLikeTest.test_partition(self)
515 # test mixed kinds
516 self.checkequal(('ABCDEFGH', '', ''), 'ABCDEFGH', 'partition', '\u4200')
517 for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
518 left *= 9
519 right *= 9
520 for delim in ('c', '\u0102', '\U00010302'):
521 self.checkequal((left + right, '', ''),
522 left + right, 'partition', delim)
523 self.checkequal((left, delim, right),
524 left + delim + right, 'partition', delim)
525 self.checkequal((left + right, '', ''),
526 left + right, 'partition', delim * 2)
527 self.checkequal((left, delim * 2, right),
528 left + delim * 2 + right, 'partition', delim * 2)
529
530 def test_rpartition(self):
531 string_tests.StringLikeTest.test_rpartition(self)
532 # test mixed kinds
533 self.checkequal(('', '', 'ABCDEFGH'), 'ABCDEFGH', 'rpartition', '\u4200')
534 for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
535 left *= 9
536 right *= 9
537 for delim in ('c', '\u0102', '\U00010302'):
538 self.checkequal(('', '', left + right),
539 left + right, 'rpartition', delim)
540 self.checkequal((left, delim, right),
541 left + delim + right, 'rpartition', delim)
542 self.checkequal(('', '', left + right),
543 left + right, 'rpartition', delim * 2)
544 self.checkequal((left, delim * 2, right),
545 left + delim * 2 + right, 'rpartition', delim * 2)
546
547 def test_join(self):
548 string_tests.StringLikeTest.test_join(self)
549
550 class MyWrapper:
551 def __init__(self, sval): self.sval = sval
552 def __str__(self): return self.sval
553
554 # mixed arguments
555 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
556 self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
557 self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
558 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
559 self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
560 self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd'))
561 self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz'))
562 self.checkraises(TypeError, ' ', 'join', ['1', '2', MyWrapper('foo')])
563 self.checkraises(TypeError, ' ', 'join', ['1', '2', '3', bytes()])
564 self.checkraises(TypeError, ' ', 'join', [1, 2, 3])
565 self.checkraises(TypeError, ' ', 'join', ['1', '2', 3])
566
567 @unittest.skipIf(sys.maxsize > 2**32,
568 'needs too much memory on a 64-bit platform')
569 def test_join_overflow(self):
570 size = int(sys.maxsize**0.5) + 1
571 seq = ('A' * size,) * size
572 self.assertRaises(OverflowError, ''.join, seq)
573
574 def test_replace(self):
575 string_tests.StringLikeTest.test_replace(self)
576
577 # method call forwarded from str implementation because of unicode argument
578 self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
579 self.assertRaises(TypeError, 'replace'.replace, "r", 42)
580 # test mixed kinds
581 for left, right in ('ba', '\u0101\u0100', '\U00010301\U00010300'):
582 left *= 9
583 right *= 9
584 for delim in ('c', '\u0102', '\U00010302'):
585 for repl in ('d', '\u0103', '\U00010303'):
586 self.checkequal(left + right,
587 left + right, 'replace', delim, repl)
588 self.checkequal(left + repl + right,
589 left + delim + right,
590 'replace', delim, repl)
591 self.checkequal(left + right,
592 left + right, 'replace', delim * 2, repl)
593 self.checkequal(left + repl + right,
594 left + delim * 2 + right,
595 'replace', delim * 2, repl)
596
597 @support.cpython_only
598 def test_replace_id(self):
599 pattern = 'abc'
600 text = 'abc def'
601 self.assertIs(text.replace(pattern, pattern), text)
602
603 def test_repeat_id_preserving(self):
604 a = '123abc1@'
605 b = '456zyx-+'
606 self.assertEqual(id(a), id(a))
607 self.assertNotEqual(id(a), id(b))
608 self.assertNotEqual(id(a), id(a * -4))
609 self.assertNotEqual(id(a), id(a * 0))
610 self.assertEqual(id(a), id(a * 1))
611 self.assertEqual(id(a), id(1 * a))
612 self.assertNotEqual(id(a), id(a * 2))
613
614 class SubStr(str):
615 pass
616
617 s = SubStr('qwerty()')
618 self.assertEqual(id(s), id(s))
619 self.assertNotEqual(id(s), id(s * -4))
620 self.assertNotEqual(id(s), id(s * 0))
621 self.assertNotEqual(id(s), id(s * 1))
622 self.assertNotEqual(id(s), id(1 * s))
623 self.assertNotEqual(id(s), id(s * 2))
624
625 def test_bytes_comparison(self):
626 with warnings_helper.check_warnings():
627 warnings.simplefilter('ignore', BytesWarning)
628 self.assertEqual('abc' == b'abc', False)
629 self.assertEqual('abc' != b'abc', True)
630 self.assertEqual('abc' == bytearray(b'abc'), False)
631 self.assertEqual('abc' != bytearray(b'abc'), True)
632
633 def test_comparison(self):
634 # Comparisons:
635 self.assertEqual('abc', 'abc')
636 self.assertTrue('abcd' > 'abc')
637 self.assertTrue('abc' < 'abcd')
638
639 if 0:
640 # Move these tests to a Unicode collation module test...
641 # Testing UTF-16 code point order comparisons...
642
643 # No surrogates, no fixup required.
644 self.assertTrue('\u0061' < '\u20ac')
645 # Non surrogate below surrogate value, no fixup required
646 self.assertTrue('\u0061' < '\ud800\udc02')
647
648 # Non surrogate above surrogate value, fixup required
649 def test_lecmp(s, s2):
650 self.assertTrue(s < s2)
651
652 def test_fixup(s):
653 s2 = '\ud800\udc01'
654 test_lecmp(s, s2)
655 s2 = '\ud900\udc01'
656 test_lecmp(s, s2)
657 s2 = '\uda00\udc01'
658 test_lecmp(s, s2)
659 s2 = '\udb00\udc01'
660 test_lecmp(s, s2)
661 s2 = '\ud800\udd01'
662 test_lecmp(s, s2)
663 s2 = '\ud900\udd01'
664 test_lecmp(s, s2)
665 s2 = '\uda00\udd01'
666 test_lecmp(s, s2)
667 s2 = '\udb00\udd01'
668 test_lecmp(s, s2)
669 s2 = '\ud800\ude01'
670 test_lecmp(s, s2)
671 s2 = '\ud900\ude01'
672 test_lecmp(s, s2)
673 s2 = '\uda00\ude01'
674 test_lecmp(s, s2)
675 s2 = '\udb00\ude01'
676 test_lecmp(s, s2)
677 s2 = '\ud800\udfff'
678 test_lecmp(s, s2)
679 s2 = '\ud900\udfff'
680 test_lecmp(s, s2)
681 s2 = '\uda00\udfff'
682 test_lecmp(s, s2)
683 s2 = '\udb00\udfff'
684 test_lecmp(s, s2)
685
686 test_fixup('\ue000')
687 test_fixup('\uff61')
688
689 # Surrogates on both sides, no fixup required
690 self.assertTrue('\ud800\udc02' < '\ud84d\udc56')
691
692 def test_islower(self):
693 super().test_islower()
694 self.checkequalnofix(False, '\u1FFc', 'islower')
695 self.assertFalse('\u2167'.islower())
696 self.assertTrue('\u2177'.islower())
697 # non-BMP, uppercase
698 self.assertFalse('\U00010401'.islower())
699 self.assertFalse('\U00010427'.islower())
700 # non-BMP, lowercase
701 self.assertTrue('\U00010429'.islower())
702 self.assertTrue('\U0001044E'.islower())
703 # non-BMP, non-cased
704 self.assertFalse('\U0001F40D'.islower())
705 self.assertFalse('\U0001F46F'.islower())
706
707 def test_isupper(self):
708 super().test_isupper()
709 self.checkequalnofix(False, '\u1FFc', 'isupper')
710 self.assertTrue('\u2167'.isupper())
711 self.assertFalse('\u2177'.isupper())
712 # non-BMP, uppercase
713 self.assertTrue('\U00010401'.isupper())
714 self.assertTrue('\U00010427'.isupper())
715 # non-BMP, lowercase
716 self.assertFalse('\U00010429'.isupper())
717 self.assertFalse('\U0001044E'.isupper())
718 # non-BMP, non-cased
719 self.assertFalse('\U0001F40D'.isupper())
720 self.assertFalse('\U0001F46F'.isupper())
721
722 def test_istitle(self):
723 super().test_istitle()
724 self.checkequalnofix(True, '\u1FFc', 'istitle')
725 self.checkequalnofix(True, 'Greek \u1FFcitlecases ...', 'istitle')
726
727 # non-BMP, uppercase + lowercase
728 self.assertTrue('\U00010401\U00010429'.istitle())
729 self.assertTrue('\U00010427\U0001044E'.istitle())
730 # apparently there are no titlecased (Lt) non-BMP chars in Unicode 6
731 for ch in ['\U00010429', '\U0001044E', '\U0001F40D', '\U0001F46F']:
732 self.assertFalse(ch.istitle(), '{!a} is not title'.format(ch))
733
734 def test_isspace(self):
735 super().test_isspace()
736 self.checkequalnofix(True, '\u2000', 'isspace')
737 self.checkequalnofix(True, '\u200a', 'isspace')
738 self.checkequalnofix(False, '\u2014', 'isspace')
739 # There are no non-BMP whitespace chars as of Unicode 12.
740 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
741 '\U0001F40D', '\U0001F46F']:
742 self.assertFalse(ch.isspace(), '{!a} is not space.'.format(ch))
743
744 @support.requires_resource('cpu')
745 def test_isspace_invariant(self):
746 for codepoint in range(sys.maxunicode + 1):
747 char = chr(codepoint)
748 bidirectional = unicodedata.bidirectional(char)
749 category = unicodedata.category(char)
750 self.assertEqual(char.isspace(),
751 (bidirectional in ('WS', 'B', 'S')
752 or category == 'Zs'))
753
754 def test_isalnum(self):
755 super().test_isalnum()
756 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
757 '\U0001D7F6', '\U00011066', '\U000104A0', '\U0001F107']:
758 self.assertTrue(ch.isalnum(), '{!a} is alnum.'.format(ch))
759
760 def test_isalpha(self):
761 super().test_isalpha()
762 self.checkequalnofix(True, '\u1FFc', 'isalpha')
763 # non-BMP, cased
764 self.assertTrue('\U00010401'.isalpha())
765 self.assertTrue('\U00010427'.isalpha())
766 self.assertTrue('\U00010429'.isalpha())
767 self.assertTrue('\U0001044E'.isalpha())
768 # non-BMP, non-cased
769 self.assertFalse('\U0001F40D'.isalpha())
770 self.assertFalse('\U0001F46F'.isalpha())
771
772 def test_isascii(self):
773 super().test_isascii()
774 self.assertFalse("\u20ac".isascii())
775 self.assertFalse("\U0010ffff".isascii())
776
777 def test_isdecimal(self):
778 self.checkequalnofix(False, '', 'isdecimal')
779 self.checkequalnofix(False, 'a', 'isdecimal')
780 self.checkequalnofix(True, '0', 'isdecimal')
781 self.checkequalnofix(False, '\u2460', 'isdecimal') # CIRCLED DIGIT ONE
782 self.checkequalnofix(False, '\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER
783 self.checkequalnofix(True, '\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO
784 self.checkequalnofix(True, '0123456789', 'isdecimal')
785 self.checkequalnofix(False, '0123456789a', 'isdecimal')
786
787 self.checkraises(TypeError, 'abc', 'isdecimal', 42)
788
789 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
790 '\U0001F40D', '\U0001F46F', '\U00011065', '\U0001F107']:
791 self.assertFalse(ch.isdecimal(), '{!a} is not decimal.'.format(ch))
792 for ch in ['\U0001D7F6', '\U00011066', '\U000104A0']:
793 self.assertTrue(ch.isdecimal(), '{!a} is decimal.'.format(ch))
794
795 def test_isdigit(self):
796 super().test_isdigit()
797 self.checkequalnofix(True, '\u2460', 'isdigit')
798 self.checkequalnofix(False, '\xbc', 'isdigit')
799 self.checkequalnofix(True, '\u0660', 'isdigit')
800
801 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
802 '\U0001F40D', '\U0001F46F', '\U00011065']:
803 self.assertFalse(ch.isdigit(), '{!a} is not a digit.'.format(ch))
804 for ch in ['\U0001D7F6', '\U00011066', '\U000104A0', '\U0001F107']:
805 self.assertTrue(ch.isdigit(), '{!a} is a digit.'.format(ch))
806
807 def test_isnumeric(self):
808 self.checkequalnofix(False, '', 'isnumeric')
809 self.checkequalnofix(False, 'a', 'isnumeric')
810 self.checkequalnofix(True, '0', 'isnumeric')
811 self.checkequalnofix(True, '\u2460', 'isnumeric')
812 self.checkequalnofix(True, '\xbc', 'isnumeric')
813 self.checkequalnofix(True, '\u0660', 'isnumeric')
814 self.checkequalnofix(True, '0123456789', 'isnumeric')
815 self.checkequalnofix(False, '0123456789a', 'isnumeric')
816
817 self.assertRaises(TypeError, "abc".isnumeric, 42)
818
819 for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E',
820 '\U0001F40D', '\U0001F46F']:
821 self.assertFalse(ch.isnumeric(), '{!a} is not numeric.'.format(ch))
822 for ch in ['\U00011065', '\U0001D7F6', '\U00011066',
823 '\U000104A0', '\U0001F107']:
824 self.assertTrue(ch.isnumeric(), '{!a} is numeric.'.format(ch))
825
826 def test_isidentifier(self):
827 self.assertTrue("a".isidentifier())
828 self.assertTrue("Z".isidentifier())
829 self.assertTrue("_".isidentifier())
830 self.assertTrue("b0".isidentifier())
831 self.assertTrue("bc".isidentifier())
832 self.assertTrue("b_".isidentifier())
833 self.assertTrue("µ".isidentifier())
834 self.assertTrue("𝔘𝔫𝔦𝔠𝔬𝔡𝔢".isidentifier())
835
836 self.assertFalse(" ".isidentifier())
837 self.assertFalse("[".isidentifier())
838 self.assertFalse("©".isidentifier())
839 self.assertFalse("0".isidentifier())
840
841 def test_isprintable(self):
842 self.assertTrue("".isprintable())
843 self.assertTrue(" ".isprintable())
844 self.assertTrue("abcdefg".isprintable())
845 self.assertFalse("abcdefg\n".isprintable())
846 # some defined Unicode character
847 self.assertTrue("\u0374".isprintable())
848 # undefined character
849 self.assertFalse("\u0378".isprintable())
850 # single surrogate character
851 self.assertFalse("\ud800".isprintable())
852
853 self.assertTrue('\U0001F46F'.isprintable())
854 self.assertFalse('\U000E0020'.isprintable())
855
856 @support.requires_resource('cpu')
857 def test_isprintable_invariant(self):
858 for codepoint in range(sys.maxunicode + 1):
859 char = chr(codepoint)
860 category = unicodedata.category(char)
861 self.assertEqual(char.isprintable(),
862 category[0] not in ('C', 'Z')
863 or char == ' ')
864
865 def test_surrogates(self):
866 for s in ('a\uD800b\uDFFF', 'a\uDFFFb\uD800',
867 'a\uD800b\uDFFFa', 'a\uDFFFb\uD800a'):
868 self.assertTrue(s.islower())
869 self.assertFalse(s.isupper())
870 self.assertFalse(s.istitle())
871 for s in ('A\uD800B\uDFFF', 'A\uDFFFB\uD800',
872 'A\uD800B\uDFFFA', 'A\uDFFFB\uD800A'):
873 self.assertFalse(s.islower())
874 self.assertTrue(s.isupper())
875 self.assertTrue(s.istitle())
876
877 for meth_name in ('islower', 'isupper', 'istitle'):
878 meth = getattr(str, meth_name)
879 for s in ('\uD800', '\uDFFF', '\uD800\uD800', '\uDFFF\uDFFF'):
880 self.assertFalse(meth(s), '%a.%s() is False' % (s, meth_name))
881
882 for meth_name in ('isalpha', 'isalnum', 'isdigit', 'isspace',
883 'isdecimal', 'isnumeric',
884 'isidentifier', 'isprintable'):
885 meth = getattr(str, meth_name)
886 for s in ('\uD800', '\uDFFF', '\uD800\uD800', '\uDFFF\uDFFF',
887 'a\uD800b\uDFFF', 'a\uDFFFb\uD800',
888 'a\uD800b\uDFFFa', 'a\uDFFFb\uD800a'):
889 self.assertFalse(meth(s), '%a.%s() is False' % (s, meth_name))
890
891
892 def test_lower(self):
893 string_tests.StringLikeTest.test_lower(self)
894 self.assertEqual('\U00010427'.lower(), '\U0001044F')
895 self.assertEqual('\U00010427\U00010427'.lower(),
896 '\U0001044F\U0001044F')
897 self.assertEqual('\U00010427\U0001044F'.lower(),
898 '\U0001044F\U0001044F')
899 self.assertEqual('X\U00010427x\U0001044F'.lower(),
900 'x\U0001044Fx\U0001044F')
901 self.assertEqual('fi'.lower(), 'fi')
902 self.assertEqual('\u0130'.lower(), '\u0069\u0307')
903 # Special case for GREEK CAPITAL LETTER SIGMA U+03A3
904 self.assertEqual('\u03a3'.lower(), '\u03c3')
905 self.assertEqual('\u0345\u03a3'.lower(), '\u0345\u03c3')
906 self.assertEqual('A\u0345\u03a3'.lower(), 'a\u0345\u03c2')
907 self.assertEqual('A\u0345\u03a3a'.lower(), 'a\u0345\u03c3a')
908 self.assertEqual('A\u0345\u03a3'.lower(), 'a\u0345\u03c2')
909 self.assertEqual('A\u03a3\u0345'.lower(), 'a\u03c2\u0345')
910 self.assertEqual('\u03a3\u0345 '.lower(), '\u03c3\u0345 ')
911 self.assertEqual('\U0008fffe'.lower(), '\U0008fffe')
912 self.assertEqual('\u2177'.lower(), '\u2177')
913
914 def test_casefold(self):
915 self.assertEqual('hello'.casefold(), 'hello')
916 self.assertEqual('hELlo'.casefold(), 'hello')
917 self.assertEqual('ß'.casefold(), 'ss')
918 self.assertEqual('fi'.casefold(), 'fi')
919 self.assertEqual('\u03a3'.casefold(), '\u03c3')
920 self.assertEqual('A\u0345\u03a3'.casefold(), 'a\u03b9\u03c3')
921 self.assertEqual('\u00b5'.casefold(), '\u03bc')
922
923 def test_upper(self):
924 string_tests.StringLikeTest.test_upper(self)
925 self.assertEqual('\U0001044F'.upper(), '\U00010427')
926 self.assertEqual('\U0001044F\U0001044F'.upper(),
927 '\U00010427\U00010427')
928 self.assertEqual('\U00010427\U0001044F'.upper(),
929 '\U00010427\U00010427')
930 self.assertEqual('X\U00010427x\U0001044F'.upper(),
931 'X\U00010427X\U00010427')
932 self.assertEqual('fi'.upper(), 'FI')
933 self.assertEqual('\u0130'.upper(), '\u0130')
934 self.assertEqual('\u03a3'.upper(), '\u03a3')
935 self.assertEqual('ß'.upper(), 'SS')
936 self.assertEqual('\u1fd2'.upper(), '\u0399\u0308\u0300')
937 self.assertEqual('\U0008fffe'.upper(), '\U0008fffe')
938 self.assertEqual('\u2177'.upper(), '\u2167')
939
940 def test_capitalize(self):
941 string_tests.StringLikeTest.test_capitalize(self)
942 self.assertEqual('\U0001044F'.capitalize(), '\U00010427')
943 self.assertEqual('\U0001044F\U0001044F'.capitalize(),
944 '\U00010427\U0001044F')
945 self.assertEqual('\U00010427\U0001044F'.capitalize(),
946 '\U00010427\U0001044F')
947 self.assertEqual('\U0001044F\U00010427'.capitalize(),
948 '\U00010427\U0001044F')
949 self.assertEqual('X\U00010427x\U0001044F'.capitalize(),
950 'X\U0001044Fx\U0001044F')
951 self.assertEqual('h\u0130'.capitalize(), 'H\u0069\u0307')
952 exp = '\u0399\u0308\u0300\u0069\u0307'
953 self.assertEqual('\u1fd2\u0130'.capitalize(), exp)
954 self.assertEqual('finnish'.capitalize(), 'Finnish')
955 self.assertEqual('A\u0345\u03a3'.capitalize(), 'A\u0345\u03c2')
956
957 def test_title(self):
958 super().test_title()
959 self.assertEqual('\U0001044F'.title(), '\U00010427')
960 self.assertEqual('\U0001044F\U0001044F'.title(),
961 '\U00010427\U0001044F')
962 self.assertEqual('\U0001044F\U0001044F \U0001044F\U0001044F'.title(),
963 '\U00010427\U0001044F \U00010427\U0001044F')
964 self.assertEqual('\U00010427\U0001044F \U00010427\U0001044F'.title(),
965 '\U00010427\U0001044F \U00010427\U0001044F')
966 self.assertEqual('\U0001044F\U00010427 \U0001044F\U00010427'.title(),
967 '\U00010427\U0001044F \U00010427\U0001044F')
968 self.assertEqual('X\U00010427x\U0001044F X\U00010427x\U0001044F'.title(),
969 'X\U0001044Fx\U0001044F X\U0001044Fx\U0001044F')
970 self.assertEqual('fiNNISH'.title(), 'Finnish')
971 self.assertEqual('A\u03a3 \u1fa1xy'.title(), 'A\u03c2 \u1fa9xy')
972 self.assertEqual('A\u03a3A'.title(), 'A\u03c3a')
973
974 def test_swapcase(self):
975 string_tests.StringLikeTest.test_swapcase(self)
976 self.assertEqual('\U0001044F'.swapcase(), '\U00010427')
977 self.assertEqual('\U00010427'.swapcase(), '\U0001044F')
978 self.assertEqual('\U0001044F\U0001044F'.swapcase(),
979 '\U00010427\U00010427')
980 self.assertEqual('\U00010427\U0001044F'.swapcase(),
981 '\U0001044F\U00010427')
982 self.assertEqual('\U0001044F\U00010427'.swapcase(),
983 '\U00010427\U0001044F')
984 self.assertEqual('X\U00010427x\U0001044F'.swapcase(),
985 'x\U0001044FX\U00010427')
986 self.assertEqual('fi'.swapcase(), 'FI')
987 self.assertEqual('\u0130'.swapcase(), '\u0069\u0307')
988 # Special case for GREEK CAPITAL LETTER SIGMA U+03A3
989 self.assertEqual('\u03a3'.swapcase(), '\u03c3')
990 self.assertEqual('\u0345\u03a3'.swapcase(), '\u0399\u03c3')
991 self.assertEqual('A\u0345\u03a3'.swapcase(), 'a\u0399\u03c2')
992 self.assertEqual('A\u0345\u03a3a'.swapcase(), 'a\u0399\u03c3A')
993 self.assertEqual('A\u0345\u03a3'.swapcase(), 'a\u0399\u03c2')
994 self.assertEqual('A\u03a3\u0345'.swapcase(), 'a\u03c2\u0399')
995 self.assertEqual('\u03a3\u0345 '.swapcase(), '\u03c3\u0399 ')
996 self.assertEqual('\u03a3'.swapcase(), '\u03c3')
997 self.assertEqual('ß'.swapcase(), 'SS')
998 self.assertEqual('\u1fd2'.swapcase(), '\u0399\u0308\u0300')
999
1000 def test_center(self):
1001 string_tests.StringLikeTest.test_center(self)
1002 self.assertEqual('x'.center(2, '\U0010FFFF'),
1003 'x\U0010FFFF')
1004 self.assertEqual('x'.center(3, '\U0010FFFF'),
1005 '\U0010FFFFx\U0010FFFF')
1006 self.assertEqual('x'.center(4, '\U0010FFFF'),
1007 '\U0010FFFFx\U0010FFFF\U0010FFFF')
1008
1009 @unittest.skipUnless(sys.maxsize == 2**31 - 1, "requires 32-bit system")
1010 @support.cpython_only
1011 def test_case_operation_overflow(self):
1012 # Issue #22643
1013 size = 2**32//12 + 1
1014 try:
1015 s = "ü" * size
1016 except MemoryError:
1017 self.skipTest('no enough memory (%.0f MiB required)' % (size / 2**20))
1018 try:
1019 self.assertRaises(OverflowError, s.upper)
1020 finally:
1021 del s
1022
1023 def test_contains(self):
1024 # Testing Unicode contains method
1025 self.assertIn('a', 'abdb')
1026 self.assertIn('a', 'bdab')
1027 self.assertIn('a', 'bdaba')
1028 self.assertIn('a', 'bdba')
1029 self.assertNotIn('a', 'bdb')
1030 self.assertIn('a', 'bdba')
1031 self.assertIn('a', ('a',1,None))
1032 self.assertIn('a', (1,None,'a'))
1033 self.assertIn('a', ('a',1,None))
1034 self.assertIn('a', (1,None,'a'))
1035 self.assertNotIn('a', ('x',1,'y'))
1036 self.assertNotIn('a', ('x',1,None))
1037 self.assertNotIn('abcd', 'abcxxxx')
1038 self.assertIn('ab', 'abcd')
1039 self.assertIn('ab', 'abc')
1040 self.assertIn('ab', (1,None,'ab'))
1041 self.assertIn('', 'abc')
1042 self.assertIn('', '')
1043 self.assertIn('', 'abc')
1044 self.assertNotIn('\0', 'abc')
1045 self.assertIn('\0', '\0abc')
1046 self.assertIn('\0', 'abc\0')
1047 self.assertIn('a', '\0abc')
1048 self.assertIn('asdf', 'asdf')
1049 self.assertNotIn('asdf', 'asd')
1050 self.assertNotIn('asdf', '')
1051
1052 self.assertRaises(TypeError, "abc".__contains__)
1053 # test mixed kinds
1054 for fill in ('a', '\u0100', '\U00010300'):
1055 fill *= 9
1056 for delim in ('c', '\u0102', '\U00010302'):
1057 self.assertNotIn(delim, fill)
1058 self.assertIn(delim, fill + delim)
1059 self.assertNotIn(delim * 2, fill)
1060 self.assertIn(delim * 2, fill + delim * 2)
1061
1062 def test_issue18183(self):
1063 '\U00010000\U00100000'.lower()
1064 '\U00010000\U00100000'.casefold()
1065 '\U00010000\U00100000'.upper()
1066 '\U00010000\U00100000'.capitalize()
1067 '\U00010000\U00100000'.title()
1068 '\U00010000\U00100000'.swapcase()
1069 '\U00100000'.center(3, '\U00010000')
1070 '\U00100000'.ljust(3, '\U00010000')
1071 '\U00100000'.rjust(3, '\U00010000')
1072
1073 def test_format(self):
1074 self.assertEqual(''.format(), '')
1075 self.assertEqual('a'.format(), 'a')
1076 self.assertEqual('ab'.format(), 'ab')
1077 self.assertEqual('a{{'.format(), 'a{')
1078 self.assertEqual('a}}'.format(), 'a}')
1079 self.assertEqual('{{b'.format(), '{b')
1080 self.assertEqual('}}b'.format(), '}b')
1081 self.assertEqual('a{{b'.format(), 'a{b')
1082
1083 # examples from the PEP:
1084 import datetime
1085 self.assertEqual("My name is {0}".format('Fred'), "My name is Fred")
1086 self.assertEqual("My name is {0[name]}".format(dict(name='Fred')),
1087 "My name is Fred")
1088 self.assertEqual("My name is {0} :-{{}}".format('Fred'),
1089 "My name is Fred :-{}")
1090
1091 d = datetime.date(2007, 8, 18)
1092 self.assertEqual("The year is {0.year}".format(d),
1093 "The year is 2007")
1094
1095 # classes we'll use for testing
1096 class C:
1097 def __init__(self, x=100):
1098 self._x = x
1099 def __format__(self, spec):
1100 return spec
1101
1102 class D:
1103 def __init__(self, x):
1104 self.x = x
1105 def __format__(self, spec):
1106 return str(self.x)
1107
1108 # class with __str__, but no __format__
1109 class E:
1110 def __init__(self, x):
1111 self.x = x
1112 def __str__(self):
1113 return 'E(' + self.x + ')'
1114
1115 # class with __repr__, but no __format__ or __str__
1116 class F:
1117 def __init__(self, x):
1118 self.x = x
1119 def __repr__(self):
1120 return 'F(' + self.x + ')'
1121
1122 # class with __format__ that forwards to string, for some format_spec's
1123 class G:
1124 def __init__(self, x):
1125 self.x = x
1126 def __str__(self):
1127 return "string is " + self.x
1128 def __format__(self, format_spec):
1129 if format_spec == 'd':
1130 return 'G(' + self.x + ')'
1131 return object.__format__(self, format_spec)
1132
1133 class I(datetime.date):
1134 def __format__(self, format_spec):
1135 return self.strftime(format_spec)
1136
1137 class J(int):
1138 def __format__(self, format_spec):
1139 return int.__format__(self * 2, format_spec)
1140
1141 class M:
1142 def __init__(self, x):
1143 self.x = x
1144 def __repr__(self):
1145 return 'M(' + self.x + ')'
1146 __str__ = None
1147
1148 class N:
1149 def __init__(self, x):
1150 self.x = x
1151 def __repr__(self):
1152 return 'N(' + self.x + ')'
1153 __format__ = None
1154
1155 self.assertEqual(''.format(), '')
1156 self.assertEqual('abc'.format(), 'abc')
1157 self.assertEqual('{0}'.format('abc'), 'abc')
1158 self.assertEqual('{0:}'.format('abc'), 'abc')
1159 # self.assertEqual('{ 0 }'.format('abc'), 'abc')
1160 self.assertEqual('X{0}'.format('abc'), 'Xabc')
1161 self.assertEqual('{0}X'.format('abc'), 'abcX')
1162 self.assertEqual('X{0}Y'.format('abc'), 'XabcY')
1163 self.assertEqual('{1}'.format(1, 'abc'), 'abc')
1164 self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc')
1165 self.assertEqual('{1}X'.format(1, 'abc'), 'abcX')
1166 self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY')
1167 self.assertEqual('{0}'.format(-15), '-15')
1168 self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc')
1169 self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc')
1170 self.assertEqual('{{'.format(), '{')
1171 self.assertEqual('}}'.format(), '}')
1172 self.assertEqual('{{}}'.format(), '{}')
1173 self.assertEqual('{{x}}'.format(), '{x}')
1174 self.assertEqual('{{{0}}}'.format(123), '{123}')
1175 self.assertEqual('{{{{0}}}}'.format(), '{{0}}')
1176 self.assertEqual('}}{{'.format(), '}{')
1177 self.assertEqual('}}x{{'.format(), '}x{')
1178
1179 # weird field names
1180 self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz')
1181 self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz')
1182 self.assertEqual("{0[ ]}".format({' ':3}), '3')
1183
1184 self.assertEqual('{foo._x}'.format(foo=C(20)), '20')
1185 self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010')
1186 self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc')
1187 self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc')
1188 self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def')
1189 self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def')
1190 self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def')
1191
1192 # strings
1193 self.assertEqual('{0:.3s}'.format('abc'), 'abc')
1194 self.assertEqual('{0:.3s}'.format('ab'), 'ab')
1195 self.assertEqual('{0:.3s}'.format('abcdef'), 'abc')
1196 self.assertEqual('{0:.0s}'.format('abcdef'), '')
1197 self.assertEqual('{0:3.3s}'.format('abc'), 'abc')
1198 self.assertEqual('{0:2.3s}'.format('abc'), 'abc')
1199 self.assertEqual('{0:2.2s}'.format('abc'), 'ab')
1200 self.assertEqual('{0:3.2s}'.format('abc'), 'ab ')
1201 self.assertEqual('{0:x<0s}'.format('result'), 'result')
1202 self.assertEqual('{0:x<5s}'.format('result'), 'result')
1203 self.assertEqual('{0:x<6s}'.format('result'), 'result')
1204 self.assertEqual('{0:x<7s}'.format('result'), 'resultx')
1205 self.assertEqual('{0:x<8s}'.format('result'), 'resultxx')
1206 self.assertEqual('{0: <7s}'.format('result'), 'result ')
1207 self.assertEqual('{0:<7s}'.format('result'), 'result ')
1208 self.assertEqual('{0:>7s}'.format('result'), ' result')
1209 self.assertEqual('{0:>8s}'.format('result'), ' result')
1210 self.assertEqual('{0:^8s}'.format('result'), ' result ')
1211 self.assertEqual('{0:^9s}'.format('result'), ' result ')
1212 self.assertEqual('{0:^10s}'.format('result'), ' result ')
1213 self.assertEqual('{0:8s}'.format('result'), 'result ')
1214 self.assertEqual('{0:0s}'.format('result'), 'result')
1215 self.assertEqual('{0:08s}'.format('result'), 'result00')
1216 self.assertEqual('{0:<08s}'.format('result'), 'result00')
1217 self.assertEqual('{0:>08s}'.format('result'), '00result')
1218 self.assertEqual('{0:^08s}'.format('result'), '0result0')
1219 self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999)
1220 self.assertEqual('{0:10000}'.format(''), ' ' * 10000)
1221 self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
1222
1223 # issue 12546: use \x00 as a fill character
1224 self.assertEqual('{0:\x00<6s}'.format('foo'), 'foo\x00\x00\x00')
1225 self.assertEqual('{0:\x01<6s}'.format('foo'), 'foo\x01\x01\x01')
1226 self.assertEqual('{0:\x00^6s}'.format('foo'), '\x00foo\x00\x00')
1227 self.assertEqual('{0:^6s}'.format('foo'), ' foo ')
1228
1229 self.assertEqual('{0:\x00<6}'.format(3), '3\x00\x00\x00\x00\x00')
1230 self.assertEqual('{0:\x01<6}'.format(3), '3\x01\x01\x01\x01\x01')
1231 self.assertEqual('{0:\x00^6}'.format(3), '\x00\x003\x00\x00\x00')
1232 self.assertEqual('{0:<6}'.format(3), '3 ')
1233
1234 self.assertEqual('{0:\x00<6}'.format(3.25), '3.25\x00\x00')
1235 self.assertEqual('{0:\x01<6}'.format(3.25), '3.25\x01\x01')
1236 self.assertEqual('{0:\x00^6}'.format(3.25), '\x003.25\x00')
1237 self.assertEqual('{0:^6}'.format(3.25), ' 3.25 ')
1238
1239 self.assertEqual('{0:\x00<12}'.format(3+2.0j), '(3+2j)\x00\x00\x00\x00\x00\x00')
1240 self.assertEqual('{0:\x01<12}'.format(3+2.0j), '(3+2j)\x01\x01\x01\x01\x01\x01')
1241 self.assertEqual('{0:\x00^12}'.format(3+2.0j), '\x00\x00\x00(3+2j)\x00\x00\x00')
1242 self.assertEqual('{0:^12}'.format(3+2.0j), ' (3+2j) ')
1243
1244 # format specifiers for user defined type
1245 self.assertEqual('{0:abc}'.format(C()), 'abc')
1246
1247 # !r, !s and !a coercions
1248 self.assertEqual('{0!s}'.format('Hello'), 'Hello')
1249 self.assertEqual('{0!s:}'.format('Hello'), 'Hello')
1250 self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ')
1251 self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ')
1252 self.assertEqual('{0!r}'.format('Hello'), "'Hello'")
1253 self.assertEqual('{0!r:}'.format('Hello'), "'Hello'")
1254 self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)')
1255 self.assertEqual('{0!r}'.format('\u0378'), "'\\u0378'") # nonprintable
1256 self.assertEqual('{0!r}'.format('\u0374'), "'\u0374'") # printable
1257 self.assertEqual('{0!r}'.format(F('\u0374')), 'F(\u0374)')
1258 self.assertEqual('{0!a}'.format('Hello'), "'Hello'")
1259 self.assertEqual('{0!a}'.format('\u0378'), "'\\u0378'") # nonprintable
1260 self.assertEqual('{0!a}'.format('\u0374'), "'\\u0374'") # printable
1261 self.assertEqual('{0!a:}'.format('Hello'), "'Hello'")
1262 self.assertEqual('{0!a}'.format(F('Hello')), 'F(Hello)')
1263 self.assertEqual('{0!a}'.format(F('\u0374')), 'F(\\u0374)')
1264
1265 # test fallback to object.__format__
1266 self.assertEqual('{0}'.format({}), '{}')
1267 self.assertEqual('{0}'.format([]), '[]')
1268 self.assertEqual('{0}'.format([1]), '[1]')
1269
1270 self.assertEqual('{0:d}'.format(G('data')), 'G(data)')
1271 self.assertEqual('{0!s}'.format(G('data')), 'string is data')
1272
1273 self.assertRaises(TypeError, '{0:^10}'.format, E('data'))
1274 self.assertRaises(TypeError, '{0:^10s}'.format, E('data'))
1275 self.assertRaises(TypeError, '{0:>15s}'.format, G('data'))
1276
1277 self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007,
1278 month=8,
1279 day=27)),
1280 "date: 2007-08-27")
1281
1282 # test deriving from a builtin type and overriding __format__
1283 self.assertEqual("{0}".format(J(10)), "20")
1284
1285
1286 # string format specifiers
1287 self.assertEqual('{0:}'.format('a'), 'a')
1288
1289 # computed format specifiers
1290 self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello')
1291 self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello')
1292 self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello')
1293 self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello ')
1294 self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hello ')
1295
1296 # test various errors
1297 self.assertRaises(ValueError, '{'.format)
1298 self.assertRaises(ValueError, '}'.format)
1299 self.assertRaises(ValueError, 'a{'.format)
1300 self.assertRaises(ValueError, 'a}'.format)
1301 self.assertRaises(ValueError, '{a'.format)
1302 self.assertRaises(ValueError, '}a'.format)
1303 self.assertRaises(IndexError, '{0}'.format)
1304 self.assertRaises(IndexError, '{1}'.format, 'abc')
1305 self.assertRaises(KeyError, '{x}'.format)
1306 self.assertRaises(ValueError, "}{".format)
1307 self.assertRaises(ValueError, "abc{0:{}".format)
1308 self.assertRaises(ValueError, "{0".format)
1309 self.assertRaises(IndexError, "{0.}".format)
1310 self.assertRaises(ValueError, "{0.}".format, 0)
1311 self.assertRaises(ValueError, "{0[}".format)
1312 self.assertRaises(ValueError, "{0[}".format, [])
1313 self.assertRaises(KeyError, "{0]}".format)
1314 self.assertRaises(ValueError, "{0.[]}".format, 0)
1315 self.assertRaises(ValueError, "{0..foo}".format, 0)
1316 self.assertRaises(ValueError, "{0[0}".format, 0)
1317 self.assertRaises(ValueError, "{0[0:foo}".format, 0)
1318 self.assertRaises(KeyError, "{c]}".format)
1319 self.assertRaises(ValueError, "{{ {{{0}}".format, 0)
1320 self.assertRaises(ValueError, "{0}}".format, 0)
1321 self.assertRaises(KeyError, "{foo}".format, bar=3)
1322 self.assertRaises(ValueError, "{0!x}".format, 3)
1323 self.assertRaises(ValueError, "{0!}".format, 0)
1324 self.assertRaises(ValueError, "{0!rs}".format, 0)
1325 self.assertRaises(ValueError, "{!}".format)
1326 self.assertRaises(IndexError, "{:}".format)
1327 self.assertRaises(IndexError, "{:s}".format)
1328 self.assertRaises(IndexError, "{}".format)
1329 big = "23098475029384702983476098230754973209482573"
1330 self.assertRaises(ValueError, ("{" + big + "}").format)
1331 self.assertRaises(ValueError, ("{[" + big + "]}").format, [0])
1332
1333 # test number formatter errors:
1334 self.assertRaises(ValueError, '{0:x}'.format, 1j)
1335 self.assertRaises(ValueError, '{0:x}'.format, 1.0)
1336 self.assertRaises(ValueError, '{0:X}'.format, 1j)
1337 self.assertRaises(ValueError, '{0:X}'.format, 1.0)
1338 self.assertRaises(ValueError, '{0:o}'.format, 1j)
1339 self.assertRaises(ValueError, '{0:o}'.format, 1.0)
1340 self.assertRaises(ValueError, '{0:u}'.format, 1j)
1341 self.assertRaises(ValueError, '{0:u}'.format, 1.0)
1342 self.assertRaises(ValueError, '{0:i}'.format, 1j)
1343 self.assertRaises(ValueError, '{0:i}'.format, 1.0)
1344 self.assertRaises(ValueError, '{0:d}'.format, 1j)
1345 self.assertRaises(ValueError, '{0:d}'.format, 1.0)
1346
1347 # issue 6089
1348 self.assertRaises(ValueError, "{0[0]x}".format, [None])
1349 self.assertRaises(ValueError, "{0[0](10)}".format, [None])
1350
1351 # can't have a replacement on the field name portion
1352 self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4)
1353
1354 # exceed maximum recursion depth
1355 self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '')
1356 self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format,
1357 0, 1, 2, 3, 4, 5, 6, 7)
1358
1359 # string format spec errors
1360 sign_msg = "Sign not allowed in string format specifier"
1361 self.assertRaisesRegex(ValueError, sign_msg, "{0:-s}".format, '')
1362 self.assertRaisesRegex(ValueError, sign_msg, format, "", "-")
1363 space_msg = "Space not allowed in string format specifier"
1364 self.assertRaisesRegex(ValueError, space_msg, "{: }".format, '')
1365 self.assertRaises(ValueError, "{0:=s}".format, '')
1366
1367 # Alternate formatting is not supported
1368 self.assertRaises(ValueError, format, '', '#')
1369 self.assertRaises(ValueError, format, '', '#20')
1370
1371 # Non-ASCII
1372 self.assertEqual("{0:s}{1:s}".format("ABC", "\u0410\u0411\u0412"),
1373 'ABC\u0410\u0411\u0412')
1374 self.assertEqual("{0:.3s}".format("ABC\u0410\u0411\u0412"),
1375 'ABC')
1376 self.assertEqual("{0:.0s}".format("ABC\u0410\u0411\u0412"),
1377 '')
1378
1379 self.assertEqual("{[{}]}".format({"{}": 5}), "5")
1380 self.assertEqual("{[{}]}".format({"{}" : "a"}), "a")
1381 self.assertEqual("{[{]}".format({"{" : "a"}), "a")
1382 self.assertEqual("{[}]}".format({"}" : "a"}), "a")
1383 self.assertEqual("{[[]}".format({"[" : "a"}), "a")
1384 self.assertEqual("{[!]}".format({"!" : "a"}), "a")
1385 self.assertRaises(ValueError, "{a{}b}".format, 42)
1386 self.assertRaises(ValueError, "{a{b}".format, 42)
1387 self.assertRaises(ValueError, "{[}".format, 42)
1388
1389 self.assertEqual("0x{:0{:d}X}".format(0x0,16), "0x0000000000000000")
1390
1391 # Blocking fallback
1392 m = M('data')
1393 self.assertEqual("{!r}".format(m), 'M(data)')
1394 self.assertRaises(TypeError, "{!s}".format, m)
1395 self.assertRaises(TypeError, "{}".format, m)
1396 n = N('data')
1397 self.assertEqual("{!r}".format(n), 'N(data)')
1398 self.assertEqual("{!s}".format(n), 'N(data)')
1399 self.assertRaises(TypeError, "{}".format, n)
1400
1401 def test_format_map(self):
1402 self.assertEqual(''.format_map({}), '')
1403 self.assertEqual('a'.format_map({}), 'a')
1404 self.assertEqual('ab'.format_map({}), 'ab')
1405 self.assertEqual('a{{'.format_map({}), 'a{')
1406 self.assertEqual('a}}'.format_map({}), 'a}')
1407 self.assertEqual('{{b'.format_map({}), '{b')
1408 self.assertEqual('}}b'.format_map({}), '}b')
1409 self.assertEqual('a{{b'.format_map({}), 'a{b')
1410
1411 # using mappings
1412 class Mapping(dict):
1413 def __missing__(self, key):
1414 return key
1415 self.assertEqual('{hello}'.format_map(Mapping()), 'hello')
1416 self.assertEqual('{a} {world}'.format_map(Mapping(a='hello')), 'hello world')
1417
1418 class InternalMapping:
1419 def __init__(self):
1420 self.mapping = {'a': 'hello'}
1421 def __getitem__(self, key):
1422 return self.mapping[key]
1423 self.assertEqual('{a}'.format_map(InternalMapping()), 'hello')
1424
1425
1426 class C:
1427 def __init__(self, x=100):
1428 self._x = x
1429 def __format__(self, spec):
1430 return spec
1431 self.assertEqual('{foo._x}'.format_map({'foo': C(20)}), '20')
1432
1433 # test various errors
1434 self.assertRaises(TypeError, ''.format_map)
1435 self.assertRaises(TypeError, 'a'.format_map)
1436
1437 self.assertRaises(ValueError, '{'.format_map, {})
1438 self.assertRaises(ValueError, '}'.format_map, {})
1439 self.assertRaises(ValueError, 'a{'.format_map, {})
1440 self.assertRaises(ValueError, 'a}'.format_map, {})
1441 self.assertRaises(ValueError, '{a'.format_map, {})
1442 self.assertRaises(ValueError, '}a'.format_map, {})
1443
1444 # issue #12579: can't supply positional params to format_map
1445 self.assertRaises(ValueError, '{}'.format_map, {'a' : 2})
1446 self.assertRaises(ValueError, '{}'.format_map, 'a')
1447 self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1})
1448
1449 class BadMapping:
1450 def __getitem__(self, key):
1451 return 1/0
1452 self.assertRaises(KeyError, '{a}'.format_map, {})
1453 self.assertRaises(TypeError, '{a}'.format_map, [])
1454 self.assertRaises(ZeroDivisionError, '{a}'.format_map, BadMapping())
1455
1456 def test_format_huge_precision(self):
1457 format_string = ".{}f".format(sys.maxsize + 1)
1458 with self.assertRaises(ValueError):
1459 result = format(2.34, format_string)
1460
1461 def test_format_huge_width(self):
1462 format_string = "{}f".format(sys.maxsize + 1)
1463 with self.assertRaises(ValueError):
1464 result = format(2.34, format_string)
1465
1466 def test_format_huge_item_number(self):
1467 format_string = "{{{}:.6f}}".format(sys.maxsize + 1)
1468 with self.assertRaises(ValueError):
1469 result = format_string.format(2.34)
1470
1471 def test_format_auto_numbering(self):
1472 class C:
1473 def __init__(self, x=100):
1474 self._x = x
1475 def __format__(self, spec):
1476 return spec
1477
1478 self.assertEqual('{}'.format(10), '10')
1479 self.assertEqual('{:5}'.format('s'), 's ')
1480 self.assertEqual('{!r}'.format('s'), "'s'")
1481 self.assertEqual('{._x}'.format(C(10)), '10')
1482 self.assertEqual('{[1]}'.format([1, 2]), '2')
1483 self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4')
1484 self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c')
1485
1486 self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b')
1487 self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b')
1488
1489 # can't mix and match numbering and auto-numbering
1490 self.assertRaises(ValueError, '{}{1}'.format, 1, 2)
1491 self.assertRaises(ValueError, '{1}{}'.format, 1, 2)
1492 self.assertRaises(ValueError, '{:{1}}'.format, 1, 2)
1493 self.assertRaises(ValueError, '{0:{}}'.format, 1, 2)
1494
1495 # can mix and match auto-numbering and named
1496 self.assertEqual('{f}{}'.format(4, f='test'), 'test4')
1497 self.assertEqual('{}{f}'.format(4, f='test'), '4test')
1498 self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3')
1499 self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g')
1500
1501 def test_formatting(self):
1502 string_tests.StringLikeTest.test_formatting(self)
1503 # Testing Unicode formatting strings...
1504 self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc')
1505 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00')
1506 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, -2, 3), 'abc, abc, 1, -2.000000, 3.00')
1507 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.5), 'abc, abc, -1, -2.000000, 3.50')
1508 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.57), 'abc, abc, -1, -2.000000, 3.57')
1509 self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 1003.57), 'abc, abc, -1, -2.000000, 1003.57')
1510 self.assertEqual("%r, %r" % (b"abc", "abc"), "b'abc', 'abc'")
1511 self.assertEqual("%r" % ("\u1234",), "'\u1234'")
1512 self.assertEqual("%a" % ("\u1234",), "'\\u1234'")
1513 self.assertEqual("%(x)s, %(y)s" % {'x':"abc", 'y':"def"}, 'abc, def')
1514 self.assertEqual("%(x)s, %(\xfc)s" % {'x':"abc", '\xfc':"def"}, 'abc, def')
1515
1516 self.assertEqual('%c' % 0x1234, '\u1234')
1517 self.assertEqual('%c' % 0x21483, '\U00021483')
1518 self.assertRaises(OverflowError, "%c".__mod__, (0x110000,))
1519 self.assertEqual('%c' % '\U00021483', '\U00021483')
1520 self.assertRaises(TypeError, "%c".__mod__, "aa")
1521 self.assertRaises(ValueError, "%.1\u1032f".__mod__, (1.0/3))
1522 self.assertRaises(TypeError, "%i".__mod__, "aa")
1523
1524 # formatting jobs delegated from the string implementation:
1525 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
1526 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
1527 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
1528 self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
1529 self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...')
1530 self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...')
1531 self.assertEqual('...%s...%s...%s...%s...' % (1,2,3,"abc"), '...1...2...3...abc...')
1532 self.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,"abc"), '...%...%s...1...2...3...abc...')
1533 self.assertEqual('...%s...' % "abc", '...abc...')
1534 self.assertEqual('%*s' % (5,'abc',), ' abc')
1535 self.assertEqual('%*s' % (-5,'abc',), 'abc ')
1536 self.assertEqual('%*.*s' % (5,2,'abc',), ' ab')
1537 self.assertEqual('%*.*s' % (5,3,'abc',), ' abc')
1538 self.assertEqual('%i %*.*s' % (10, 5,3,'abc',), '10 abc')
1539 self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, 'abc',), '103 abc')
1540 self.assertEqual('%c' % 'a', 'a')
1541 class Wrapper:
1542 def __str__(self):
1543 return '\u1234'
1544 self.assertEqual('%s' % Wrapper(), '\u1234')
1545
1546 # issue 3382
1547 NAN = float('nan')
1548 INF = float('inf')
1549 self.assertEqual('%f' % NAN, 'nan')
1550 self.assertEqual('%F' % NAN, 'NAN')
1551 self.assertEqual('%f' % INF, 'inf')
1552 self.assertEqual('%F' % INF, 'INF')
1553
1554 # PEP 393
1555 self.assertEqual('%.1s' % "a\xe9\u20ac", 'a')
1556 self.assertEqual('%.2s' % "a\xe9\u20ac", 'a\xe9')
1557
1558 #issue 19995
1559 class PseudoInt:
1560 def __init__(self, value):
1561 self.value = int(value)
1562 def __int__(self):
1563 return self.value
1564 def __index__(self):
1565 return self.value
1566 class PseudoFloat:
1567 def __init__(self, value):
1568 self.value = float(value)
1569 def __int__(self):
1570 return int(self.value)
1571 pi = PseudoFloat(3.1415)
1572 letter_m = PseudoInt(109)
1573 self.assertEqual('%x' % 42, '2a')
1574 self.assertEqual('%X' % 15, 'F')
1575 self.assertEqual('%o' % 9, '11')
1576 self.assertEqual('%c' % 109, 'm')
1577 self.assertEqual('%x' % letter_m, '6d')
1578 self.assertEqual('%X' % letter_m, '6D')
1579 self.assertEqual('%o' % letter_m, '155')
1580 self.assertEqual('%c' % letter_m, 'm')
1581 self.assertRaisesRegex(TypeError, '%x format: an integer is required, not float', operator.mod, '%x', 3.14)
1582 self.assertRaisesRegex(TypeError, '%X format: an integer is required, not float', operator.mod, '%X', 2.11)
1583 self.assertRaisesRegex(TypeError, '%o format: an integer is required, not float', operator.mod, '%o', 1.79)
1584 self.assertRaisesRegex(TypeError, '%x format: an integer is required, not PseudoFloat', operator.mod, '%x', pi)
1585 self.assertRaisesRegex(TypeError, '%x format: an integer is required, not complex', operator.mod, '%x', 3j)
1586 self.assertRaisesRegex(TypeError, '%X format: an integer is required, not complex', operator.mod, '%X', 2j)
1587 self.assertRaisesRegex(TypeError, '%o format: an integer is required, not complex', operator.mod, '%o', 1j)
1588 self.assertRaisesRegex(TypeError, '%u format: a real number is required, not complex', operator.mod, '%u', 3j)
1589 self.assertRaisesRegex(TypeError, '%i format: a real number is required, not complex', operator.mod, '%i', 2j)
1590 self.assertRaisesRegex(TypeError, '%d format: a real number is required, not complex', operator.mod, '%d', 1j)
1591 self.assertRaisesRegex(TypeError, r'%c requires an int or a unicode character, not .*\.PseudoFloat', operator.mod, '%c', pi)
1592
1593 class RaisingNumber:
1594 def __int__(self):
1595 raise RuntimeError('int') # should not be `TypeError`
1596 def __index__(self):
1597 raise RuntimeError('index') # should not be `TypeError`
1598
1599 rn = RaisingNumber()
1600 self.assertRaisesRegex(RuntimeError, 'int', operator.mod, '%d', rn)
1601 self.assertRaisesRegex(RuntimeError, 'int', operator.mod, '%i', rn)
1602 self.assertRaisesRegex(RuntimeError, 'int', operator.mod, '%u', rn)
1603 self.assertRaisesRegex(RuntimeError, 'index', operator.mod, '%x', rn)
1604 self.assertRaisesRegex(RuntimeError, 'index', operator.mod, '%X', rn)
1605 self.assertRaisesRegex(RuntimeError, 'index', operator.mod, '%o', rn)
1606
1607 def test_formatting_with_enum(self):
1608 # issue18780
1609 import enum
1610 class Float(float, enum.Enum):
1611 # a mixed-in type will use the name for %s etc.
1612 PI = 3.1415926
1613 class Int(enum.IntEnum):
1614 # IntEnum uses the value and not the name for %s etc.
1615 IDES = 15
1616 class Str(enum.StrEnum):
1617 # StrEnum uses the value and not the name for %s etc.
1618 ABC = 'abc'
1619 # Testing Unicode formatting strings...
1620 self.assertEqual("%s, %s" % (Str.ABC, Str.ABC),
1621 'abc, abc')
1622 self.assertEqual("%s, %s, %d, %i, %u, %f, %5.2f" %
1623 (Str.ABC, Str.ABC,
1624 Int.IDES, Int.IDES, Int.IDES,
1625 Float.PI, Float.PI),
1626 'abc, abc, 15, 15, 15, 3.141593, 3.14')
1627
1628 # formatting jobs delegated from the string implementation:
1629 self.assertEqual('...%(foo)s...' % {'foo':Str.ABC},
1630 '...abc...')
1631 self.assertEqual('...%(foo)r...' % {'foo':Int.IDES},
1632 '...<Int.IDES: 15>...')
1633 self.assertEqual('...%(foo)s...' % {'foo':Int.IDES},
1634 '...15...')
1635 self.assertEqual('...%(foo)i...' % {'foo':Int.IDES},
1636 '...15...')
1637 self.assertEqual('...%(foo)d...' % {'foo':Int.IDES},
1638 '...15...')
1639 self.assertEqual('...%(foo)u...' % {'foo':Int.IDES, 'def':Float.PI},
1640 '...15...')
1641 self.assertEqual('...%(foo)f...' % {'foo':Float.PI,'def':123},
1642 '...3.141593...')
1643
1644 def test_formatting_huge_precision(self):
1645 format_string = "%.{}f".format(sys.maxsize + 1)
1646 with self.assertRaises(ValueError):
1647 result = format_string % 2.34
1648
1649 def test_issue28598_strsubclass_rhs(self):
1650 # A subclass of str with an __rmod__ method should be able to hook
1651 # into the % operator
1652 class SubclassedStr(str):
1653 def __rmod__(self, other):
1654 return 'Success, self.__rmod__({!r}) was called'.format(other)
1655 self.assertEqual('lhs %% %r' % SubclassedStr('rhs'),
1656 "Success, self.__rmod__('lhs %% %r') was called")
1657
1658 @support.cpython_only
1659 @unittest.skipIf(_testcapi is None, 'need _testcapi module')
1660 def test_formatting_huge_precision_c_limits(self):
1661 format_string = "%.{}f".format(_testcapi.INT_MAX + 1)
1662 with self.assertRaises(ValueError):
1663 result = format_string % 2.34
1664
1665 def test_formatting_huge_width(self):
1666 format_string = "%{}f".format(sys.maxsize + 1)
1667 with self.assertRaises(ValueError):
1668 result = format_string % 2.34
1669
1670 def test_startswith_endswith_errors(self):
1671 for meth in ('foo'.startswith, 'foo'.endswith):
1672 with self.assertRaises(TypeError) as cm:
1673 meth(['f'])
1674 exc = str(cm.exception)
1675 self.assertIn('str', exc)
1676 self.assertIn('tuple', exc)
1677
1678 @support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR', '')
1679 def test_format_float(self):
1680 # should not format with a comma, but always with C locale
1681 self.assertEqual('1.0', '%.1f' % 1.0)
1682
1683 def test_constructor(self):
1684 # unicode(obj) tests (this maps to PyObject_Unicode() at C level)
1685
1686 self.assertEqual(
1687 str('unicode remains unicode'),
1688 'unicode remains unicode'
1689 )
1690
1691 for text in ('ascii', '\xe9', '\u20ac', '\U0010FFFF'):
1692 subclass = StrSubclass(text)
1693 self.assertEqual(str(subclass), text)
1694 self.assertEqual(len(subclass), len(text))
1695 if text == 'ascii':
1696 self.assertEqual(subclass.encode('ascii'), b'ascii')
1697 self.assertEqual(subclass.encode('utf-8'), b'ascii')
1698
1699 self.assertEqual(
1700 str('strings are converted to unicode'),
1701 'strings are converted to unicode'
1702 )
1703
1704 class StringCompat:
1705 def __init__(self, x):
1706 self.x = x
1707 def __str__(self):
1708 return self.x
1709
1710 self.assertEqual(
1711 str(StringCompat('__str__ compatible objects are recognized')),
1712 '__str__ compatible objects are recognized'
1713 )
1714
1715 # unicode(obj) is compatible to str():
1716
1717 o = StringCompat('unicode(obj) is compatible to str()')
1718 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
1719 self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
1720
1721 for obj in (123, 123.45, 123):
1722 self.assertEqual(str(obj), str(str(obj)))
1723
1724 # unicode(obj, encoding, error) tests (this maps to
1725 # PyUnicode_FromEncodedObject() at C level)
1726
1727 self.assertRaises(
1728 TypeError,
1729 str,
1730 'decoding unicode is not supported',
1731 'utf-8',
1732 'strict'
1733 )
1734
1735 self.assertEqual(
1736 str(b'strings are decoded to unicode', 'utf-8', 'strict'),
1737 'strings are decoded to unicode'
1738 )
1739
1740 self.assertEqual(
1741 str(
1742 memoryview(b'character buffers are decoded to unicode'),
1743 'utf-8',
1744 'strict'
1745 ),
1746 'character buffers are decoded to unicode'
1747 )
1748
1749 def test_constructor_keyword_args(self):
1750 """Pass various keyword argument combinations to the constructor."""
1751 # The object argument can be passed as a keyword.
1752 self.assertEqual(str(object='foo'), 'foo')
1753 self.assertEqual(str(object=b'foo', encoding='utf-8'), 'foo')
1754 # The errors argument without encoding triggers "decode" mode.
1755 self.assertEqual(str(b'foo', errors='strict'), 'foo') # not "b'foo'"
1756 self.assertEqual(str(object=b'foo', errors='strict'), 'foo')
1757
1758 def test_constructor_defaults(self):
1759 """Check the constructor argument defaults."""
1760 # The object argument defaults to '' or b''.
1761 self.assertEqual(str(), '')
1762 self.assertEqual(str(errors='strict'), '')
1763 utf8_cent = '¢'.encode('utf-8')
1764 # The encoding argument defaults to utf-8.
1765 self.assertEqual(str(utf8_cent, errors='strict'), '¢')
1766 # The errors argument defaults to strict.
1767 self.assertRaises(UnicodeDecodeError, str, utf8_cent, encoding='ascii')
1768
1769 def test_codecs_utf7(self):
1770 utfTests = [
1771 ('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example
1772 ('Hi Mom -\u263a-!', b'Hi Mom -+Jjo--!'), # RFC2152 example
1773 ('\u65E5\u672C\u8A9E', b'+ZeVnLIqe-'), # RFC2152 example
1774 ('Item 3 is \u00a31.', b'Item 3 is +AKM-1.'), # RFC2152 example
1775 ('+', b'+-'),
1776 ('+-', b'+--'),
1777 ('+?', b'+-?'),
1778 (r'\?', b'+AFw?'),
1779 ('+?', b'+-?'),
1780 (r'\\?', b'+AFwAXA?'),
1781 (r'\\\?', b'+AFwAXABc?'),
1782 (r'++--', b'+-+---'),
1783 ('\U000abcde', b'+2m/c3g-'), # surrogate pairs
1784 ('/', b'/'),
1785 ]
1786
1787 for (x, y) in utfTests:
1788 self.assertEqual(x.encode('utf-7'), y)
1789
1790 # Unpaired surrogates are passed through
1791 self.assertEqual('\uD801'.encode('utf-7'), b'+2AE-')
1792 self.assertEqual('\uD801x'.encode('utf-7'), b'+2AE-x')
1793 self.assertEqual('\uDC01'.encode('utf-7'), b'+3AE-')
1794 self.assertEqual('\uDC01x'.encode('utf-7'), b'+3AE-x')
1795 self.assertEqual(b'+2AE-'.decode('utf-7'), '\uD801')
1796 self.assertEqual(b'+2AE-x'.decode('utf-7'), '\uD801x')
1797 self.assertEqual(b'+3AE-'.decode('utf-7'), '\uDC01')
1798 self.assertEqual(b'+3AE-x'.decode('utf-7'), '\uDC01x')
1799
1800 self.assertEqual('\uD801\U000abcde'.encode('utf-7'), b'+2AHab9ze-')
1801 self.assertEqual(b'+2AHab9ze-'.decode('utf-7'), '\uD801\U000abcde')
1802
1803 # Issue #2242: crash on some Windows/MSVC versions
1804 self.assertEqual(b'+\xc1'.decode('utf-7', 'ignore'), '')
1805
1806 # Direct encoded characters
1807 set_d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?"
1808 # Optional direct characters
1809 set_o = '!"#$%&*;<=>@[]^_`{|}'
1810 for c in set_d:
1811 self.assertEqual(c.encode('utf7'), c.encode('ascii'))
1812 self.assertEqual(c.encode('ascii').decode('utf7'), c)
1813 for c in set_o:
1814 self.assertEqual(c.encode('ascii').decode('utf7'), c)
1815
1816 with self.assertRaisesRegex(UnicodeDecodeError,
1817 'ill-formed sequence'):
1818 b'+@'.decode('utf-7')
1819
1820 def test_codecs_utf8(self):
1821 self.assertEqual(''.encode('utf-8'), b'')
1822 self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac')
1823 self.assertEqual('\U00010002'.encode('utf-8'), b'\xf0\x90\x80\x82')
1824 self.assertEqual('\U00023456'.encode('utf-8'), b'\xf0\xa3\x91\x96')
1825 self.assertEqual('\ud800'.encode('utf-8', 'surrogatepass'), b'\xed\xa0\x80')
1826 self.assertEqual('\udc00'.encode('utf-8', 'surrogatepass'), b'\xed\xb0\x80')
1827 self.assertEqual(('\U00010002'*10).encode('utf-8'),
1828 b'\xf0\x90\x80\x82'*10)
1829 self.assertEqual(
1830 '\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
1831 '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
1832 '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
1833 '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
1834 '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das'
1835 ' Nunstuck git und'.encode('utf-8'),
1836 b'\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81'
1837 b'\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3'
1838 b'\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe'
1839 b'\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83'
1840 b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8'
1841 b'\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81'
1842 b'\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81'
1843 b'\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3'
1844 b'\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf'
1845 b'\xe3\x80\x8cWenn ist das Nunstuck git und'
1846 )
1847
1848 # UTF-8 specific decoding tests
1849 self.assertEqual(str(b'\xf0\xa3\x91\x96', 'utf-8'), '\U00023456' )
1850 self.assertEqual(str(b'\xf0\x90\x80\x82', 'utf-8'), '\U00010002' )
1851 self.assertEqual(str(b'\xe2\x82\xac', 'utf-8'), '\u20ac' )
1852
1853 # Other possible utf-8 test cases:
1854 # * strict decoding testing for all of the
1855 # UTF8_ERROR cases in PyUnicode_DecodeUTF8
1856
1857 def test_utf8_decode_valid_sequences(self):
1858 sequences = [
1859 # single byte
1860 (b'\x00', '\x00'), (b'a', 'a'), (b'\x7f', '\x7f'),
1861 # 2 bytes
1862 (b'\xc2\x80', '\x80'), (b'\xdf\xbf', '\u07ff'),
1863 # 3 bytes
1864 (b'\xe0\xa0\x80', '\u0800'), (b'\xed\x9f\xbf', '\ud7ff'),
1865 (b'\xee\x80\x80', '\uE000'), (b'\xef\xbf\xbf', '\uffff'),
1866 # 4 bytes
1867 (b'\xF0\x90\x80\x80', '\U00010000'),
1868 (b'\xf4\x8f\xbf\xbf', '\U0010FFFF')
1869 ]
1870 for seq, res in sequences:
1871 self.assertEqual(seq.decode('utf-8'), res)
1872
1873
1874 def test_utf8_decode_invalid_sequences(self):
1875 # continuation bytes in a sequence of 2, 3, or 4 bytes
1876 continuation_bytes = [bytes([x]) for x in range(0x80, 0xC0)]
1877 # start bytes of a 2-byte sequence equivalent to code points < 0x7F
1878 invalid_2B_seq_start_bytes = [bytes([x]) for x in range(0xC0, 0xC2)]
1879 # start bytes of a 4-byte sequence equivalent to code points > 0x10FFFF
1880 invalid_4B_seq_start_bytes = [bytes([x]) for x in range(0xF5, 0xF8)]
1881 invalid_start_bytes = (
1882 continuation_bytes + invalid_2B_seq_start_bytes +
1883 invalid_4B_seq_start_bytes + [bytes([x]) for x in range(0xF7, 0x100)]
1884 )
1885
1886 for byte in invalid_start_bytes:
1887 self.assertRaises(UnicodeDecodeError, byte.decode, 'utf-8')
1888
1889 for sb in invalid_2B_seq_start_bytes:
1890 for cb in continuation_bytes:
1891 self.assertRaises(UnicodeDecodeError, (sb+cb).decode, 'utf-8')
1892
1893 for sb in invalid_4B_seq_start_bytes:
1894 for cb1 in continuation_bytes[:3]:
1895 for cb3 in continuation_bytes[:3]:
1896 self.assertRaises(UnicodeDecodeError,
1897 (sb+cb1+b'\x80'+cb3).decode, 'utf-8')
1898
1899 for cb in [bytes([x]) for x in range(0x80, 0xA0)]:
1900 self.assertRaises(UnicodeDecodeError,
1901 (b'\xE0'+cb+b'\x80').decode, 'utf-8')
1902 self.assertRaises(UnicodeDecodeError,
1903 (b'\xE0'+cb+b'\xBF').decode, 'utf-8')
1904 # surrogates
1905 for cb in [bytes([x]) for x in range(0xA0, 0xC0)]:
1906 self.assertRaises(UnicodeDecodeError,
1907 (b'\xED'+cb+b'\x80').decode, 'utf-8')
1908 self.assertRaises(UnicodeDecodeError,
1909 (b'\xED'+cb+b'\xBF').decode, 'utf-8')
1910 for cb in [bytes([x]) for x in range(0x80, 0x90)]:
1911 self.assertRaises(UnicodeDecodeError,
1912 (b'\xF0'+cb+b'\x80\x80').decode, 'utf-8')
1913 self.assertRaises(UnicodeDecodeError,
1914 (b'\xF0'+cb+b'\xBF\xBF').decode, 'utf-8')
1915 for cb in [bytes([x]) for x in range(0x90, 0xC0)]:
1916 self.assertRaises(UnicodeDecodeError,
1917 (b'\xF4'+cb+b'\x80\x80').decode, 'utf-8')
1918 self.assertRaises(UnicodeDecodeError,
1919 (b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8')
1920
1921 def test_issue127903(self):
1922 # gh-127903: ``_copy_characters`` crashes on DEBUG builds when
1923 # there is nothing to copy.
1924 d = datetime.datetime(2013, 11, 10, 14, 20, 59)
1925 self.assertEqual(d.strftime('%z'), '')
1926
1927 def test_issue8271(self):
1928 # Issue #8271: during the decoding of an invalid UTF-8 byte sequence,
1929 # only the start byte and the continuation byte(s) are now considered
1930 # invalid, instead of the number of bytes specified by the start byte.
1931 # See https://www.unicode.org/versions/Unicode5.2.0/ch03.pdf (page 95,
1932 # table 3-8, Row 2) for more information about the algorithm used.
1933 FFFD = '\ufffd'
1934 sequences = [
1935 # invalid start bytes
1936 (b'\x80', FFFD), # continuation byte
1937 (b'\x80\x80', FFFD*2), # 2 continuation bytes
1938 (b'\xc0', FFFD),
1939 (b'\xc0\xc0', FFFD*2),
1940 (b'\xc1', FFFD),
1941 (b'\xc1\xc0', FFFD*2),
1942 (b'\xc0\xc1', FFFD*2),
1943 # with start byte of a 2-byte sequence
1944 (b'\xc2', FFFD), # only the start byte
1945 (b'\xc2\xc2', FFFD*2), # 2 start bytes
1946 (b'\xc2\xc2\xc2', FFFD*3), # 3 start bytes
1947 (b'\xc2\x41', FFFD+'A'), # invalid continuation byte
1948 # with start byte of a 3-byte sequence
1949 (b'\xe1', FFFD), # only the start byte
1950 (b'\xe1\xe1', FFFD*2), # 2 start bytes
1951 (b'\xe1\xe1\xe1', FFFD*3), # 3 start bytes
1952 (b'\xe1\xe1\xe1\xe1', FFFD*4), # 4 start bytes
1953 (b'\xe1\x80', FFFD), # only 1 continuation byte
1954 (b'\xe1\x41', FFFD+'A'), # invalid continuation byte
1955 (b'\xe1\x41\x80', FFFD+'A'+FFFD), # invalid cb followed by valid cb
1956 (b'\xe1\x41\x41', FFFD+'AA'), # 2 invalid continuation bytes
1957 (b'\xe1\x80\x41', FFFD+'A'), # only 1 valid continuation byte
1958 (b'\xe1\x80\xe1\x41', FFFD*2+'A'), # 1 valid and the other invalid
1959 (b'\xe1\x41\xe1\x80', FFFD+'A'+FFFD), # 1 invalid and the other valid
1960 # with start byte of a 4-byte sequence
1961 (b'\xf1', FFFD), # only the start byte
1962 (b'\xf1\xf1', FFFD*2), # 2 start bytes
1963 (b'\xf1\xf1\xf1', FFFD*3), # 3 start bytes
1964 (b'\xf1\xf1\xf1\xf1', FFFD*4), # 4 start bytes
1965 (b'\xf1\xf1\xf1\xf1\xf1', FFFD*5), # 5 start bytes
1966 (b'\xf1\x80', FFFD), # only 1 continuation bytes
1967 (b'\xf1\x80\x80', FFFD), # only 2 continuation bytes
1968 (b'\xf1\x80\x41', FFFD+'A'), # 1 valid cb and 1 invalid
1969 (b'\xf1\x80\x41\x41', FFFD+'AA'), # 1 valid cb and 1 invalid
1970 (b'\xf1\x80\x80\x41', FFFD+'A'), # 2 valid cb and 1 invalid
1971 (b'\xf1\x41\x80', FFFD+'A'+FFFD), # 1 invalid cv and 1 valid
1972 (b'\xf1\x41\x80\x80', FFFD+'A'+FFFD*2), # 1 invalid cb and 2 invalid
1973 (b'\xf1\x41\x80\x41', FFFD+'A'+FFFD+'A'), # 2 invalid cb and 1 invalid
1974 (b'\xf1\x41\x41\x80', FFFD+'AA'+FFFD), # 1 valid cb and 1 invalid
1975 (b'\xf1\x41\xf1\x80', FFFD+'A'+FFFD),
1976 (b'\xf1\x41\x80\xf1', FFFD+'A'+FFFD*2),
1977 (b'\xf1\xf1\x80\x41', FFFD*2+'A'),
1978 (b'\xf1\x41\xf1\xf1', FFFD+'A'+FFFD*2),
1979 # with invalid start byte of a 4-byte sequence (rfc2279)
1980 (b'\xf5', FFFD), # only the start byte
1981 (b'\xf5\xf5', FFFD*2), # 2 start bytes
1982 (b'\xf5\x80', FFFD*2), # only 1 continuation byte
1983 (b'\xf5\x80\x80', FFFD*3), # only 2 continuation byte
1984 (b'\xf5\x80\x80\x80', FFFD*4), # 3 continuation bytes
1985 (b'\xf5\x80\x41', FFFD*2+'A'), # 1 valid cb and 1 invalid
1986 (b'\xf5\x80\x41\xf5', FFFD*2+'A'+FFFD),
1987 (b'\xf5\x41\x80\x80\x41', FFFD+'A'+FFFD*2+'A'),
1988 # with invalid start byte of a 5-byte sequence (rfc2279)
1989 (b'\xf8', FFFD), # only the start byte
1990 (b'\xf8\xf8', FFFD*2), # 2 start bytes
1991 (b'\xf8\x80', FFFD*2), # only one continuation byte
1992 (b'\xf8\x80\x41', FFFD*2 + 'A'), # 1 valid cb and 1 invalid
1993 (b'\xf8\x80\x80\x80\x80', FFFD*5), # invalid 5 bytes seq with 5 bytes
1994 # with invalid start byte of a 6-byte sequence (rfc2279)
1995 (b'\xfc', FFFD), # only the start byte
1996 (b'\xfc\xfc', FFFD*2), # 2 start bytes
1997 (b'\xfc\x80\x80', FFFD*3), # only 2 continuation bytes
1998 (b'\xfc\x80\x80\x80\x80\x80', FFFD*6), # 6 continuation bytes
1999 # invalid start byte
2000 (b'\xfe', FFFD),
2001 (b'\xfe\x80\x80', FFFD*3),
2002 # other sequences
2003 (b'\xf1\x80\x41\x42\x43', '\ufffd\x41\x42\x43'),
2004 (b'\xf1\x80\xff\x42\x43', '\ufffd\ufffd\x42\x43'),
2005 (b'\xf1\x80\xc2\x81\x43', '\ufffd\x81\x43'),
2006 (b'\x61\xF1\x80\x80\xE1\x80\xC2\x62\x80\x63\x80\xBF\x64',
2007 '\x61\uFFFD\uFFFD\uFFFD\x62\uFFFD\x63\uFFFD\uFFFD\x64'),
2008 ]
2009 for n, (seq, res) in enumerate(sequences):
2010 self.assertRaises(UnicodeDecodeError, seq.decode, 'utf-8', 'strict')
2011 self.assertEqual(seq.decode('utf-8', 'replace'), res)
2012 self.assertEqual((seq+b'b').decode('utf-8', 'replace'), res+'b')
2013 self.assertEqual(seq.decode('utf-8', 'ignore'),
2014 res.replace('\uFFFD', ''))
2015
2016 def assertCorrectUTF8Decoding(self, seq, res, err):
2017 """
2018 Check that an invalid UTF-8 sequence raises a UnicodeDecodeError when
2019 'strict' is used, returns res when 'replace' is used, and that doesn't
2020 return anything when 'ignore' is used.
2021 """
2022 with self.assertRaises(UnicodeDecodeError) as cm:
2023 seq.decode('utf-8')
2024 exc = cm.exception
2025
2026 self.assertIn(err, str(exc))
2027 self.assertEqual(seq.decode('utf-8', 'replace'), res)
2028 self.assertEqual((b'aaaa' + seq + b'bbbb').decode('utf-8', 'replace'),
2029 'aaaa' + res + 'bbbb')
2030 res = res.replace('\ufffd', '')
2031 self.assertEqual(seq.decode('utf-8', 'ignore'), res)
2032 self.assertEqual((b'aaaa' + seq + b'bbbb').decode('utf-8', 'ignore'),
2033 'aaaa' + res + 'bbbb')
2034
2035 def test_invalid_start_byte(self):
2036 """
2037 Test that an 'invalid start byte' error is raised when the first byte
2038 is not in the ASCII range or is not a valid start byte of a 2-, 3-, or
2039 4-bytes sequence. The invalid start byte is replaced with a single
2040 U+FFFD when errors='replace'.
2041 E.g. <80> is a continuation byte and can appear only after a start byte.
2042 """
2043 FFFD = '\ufffd'
2044 for byte in b'\x80\xA0\x9F\xBF\xC0\xC1\xF5\xFF':
2045 self.assertCorrectUTF8Decoding(bytes([byte]), '\ufffd',
2046 'invalid start byte')
2047
2048 def test_unexpected_end_of_data(self):
2049 """
2050 Test that an 'unexpected end of data' error is raised when the string
2051 ends after a start byte of a 2-, 3-, or 4-bytes sequence without having
2052 enough continuation bytes. The incomplete sequence is replaced with a
2053 single U+FFFD when errors='replace'.
2054 E.g. in the sequence <F3 80 80>, F3 is the start byte of a 4-bytes
2055 sequence, but it's followed by only 2 valid continuation bytes and the
2056 last continuation bytes is missing.
2057 Note: the continuation bytes must be all valid, if one of them is
2058 invalid another error will be raised.
2059 """
2060 sequences = [
2061 'C2', 'DF',
2062 'E0 A0', 'E0 BF', 'E1 80', 'E1 BF', 'EC 80', 'EC BF',
2063 'ED 80', 'ED 9F', 'EE 80', 'EE BF', 'EF 80', 'EF BF',
2064 'F0 90', 'F0 BF', 'F0 90 80', 'F0 90 BF', 'F0 BF 80', 'F0 BF BF',
2065 'F1 80', 'F1 BF', 'F1 80 80', 'F1 80 BF', 'F1 BF 80', 'F1 BF BF',
2066 'F3 80', 'F3 BF', 'F3 80 80', 'F3 80 BF', 'F3 BF 80', 'F3 BF BF',
2067 'F4 80', 'F4 8F', 'F4 80 80', 'F4 80 BF', 'F4 8F 80', 'F4 8F BF'
2068 ]
2069 FFFD = '\ufffd'
2070 for seq in sequences:
2071 self.assertCorrectUTF8Decoding(bytes.fromhex(seq), '\ufffd',
2072 'unexpected end of data')
2073
2074 def test_invalid_cb_for_2bytes_seq(self):
2075 """
2076 Test that an 'invalid continuation byte' error is raised when the
2077 continuation byte of a 2-bytes sequence is invalid. The start byte
2078 is replaced by a single U+FFFD and the second byte is handled
2079 separately when errors='replace'.
2080 E.g. in the sequence <C2 41>, C2 is the start byte of a 2-bytes
2081 sequence, but 41 is not a valid continuation byte because it's the
2082 ASCII letter 'A'.
2083 """
2084 FFFD = '\ufffd'
2085 FFFDx2 = FFFD * 2
2086 sequences = [
2087 ('C2 00', FFFD+'\x00'), ('C2 7F', FFFD+'\x7f'),
2088 ('C2 C0', FFFDx2), ('C2 FF', FFFDx2),
2089 ('DF 00', FFFD+'\x00'), ('DF 7F', FFFD+'\x7f'),
2090 ('DF C0', FFFDx2), ('DF FF', FFFDx2),
2091 ]
2092 for seq, res in sequences:
2093 self.assertCorrectUTF8Decoding(bytes.fromhex(seq), res,
2094 'invalid continuation byte')
2095
2096 def test_invalid_cb_for_3bytes_seq(self):
2097 """
2098 Test that an 'invalid continuation byte' error is raised when the
2099 continuation byte(s) of a 3-bytes sequence are invalid. When
2100 errors='replace', if the first continuation byte is valid, the first
2101 two bytes (start byte + 1st cb) are replaced by a single U+FFFD and the
2102 third byte is handled separately, otherwise only the start byte is
2103 replaced with a U+FFFD and the other continuation bytes are handled
2104 separately.
2105 E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes
2106 sequence, 80 is a valid continuation byte, but 41 is not a valid cb
2107 because it's the ASCII letter 'A'.
2108 Note: when the start byte is E0 or ED, the valid ranges for the first
2109 continuation byte are limited to A0..BF and 80..9F respectively.
2110 Python 2 used to consider all the bytes in range 80..BF valid when the
2111 start byte was ED. This is fixed in Python 3.
2112 """
2113 FFFD = '\ufffd'
2114 FFFDx2 = FFFD * 2
2115 sequences = [
2116 ('E0 00', FFFD+'\x00'), ('E0 7F', FFFD+'\x7f'), ('E0 80', FFFDx2),
2117 ('E0 9F', FFFDx2), ('E0 C0', FFFDx2), ('E0 FF', FFFDx2),
2118 ('E0 A0 00', FFFD+'\x00'), ('E0 A0 7F', FFFD+'\x7f'),
2119 ('E0 A0 C0', FFFDx2), ('E0 A0 FF', FFFDx2),
2120 ('E0 BF 00', FFFD+'\x00'), ('E0 BF 7F', FFFD+'\x7f'),
2121 ('E0 BF C0', FFFDx2), ('E0 BF FF', FFFDx2), ('E1 00', FFFD+'\x00'),
2122 ('E1 7F', FFFD+'\x7f'), ('E1 C0', FFFDx2), ('E1 FF', FFFDx2),
2123 ('E1 80 00', FFFD+'\x00'), ('E1 80 7F', FFFD+'\x7f'),
2124 ('E1 80 C0', FFFDx2), ('E1 80 FF', FFFDx2),
2125 ('E1 BF 00', FFFD+'\x00'), ('E1 BF 7F', FFFD+'\x7f'),
2126 ('E1 BF C0', FFFDx2), ('E1 BF FF', FFFDx2), ('EC 00', FFFD+'\x00'),
2127 ('EC 7F', FFFD+'\x7f'), ('EC C0', FFFDx2), ('EC FF', FFFDx2),
2128 ('EC 80 00', FFFD+'\x00'), ('EC 80 7F', FFFD+'\x7f'),
2129 ('EC 80 C0', FFFDx2), ('EC 80 FF', FFFDx2),
2130 ('EC BF 00', FFFD+'\x00'), ('EC BF 7F', FFFD+'\x7f'),
2131 ('EC BF C0', FFFDx2), ('EC BF FF', FFFDx2), ('ED 00', FFFD+'\x00'),
2132 ('ED 7F', FFFD+'\x7f'),
2133 ('ED A0', FFFDx2), ('ED BF', FFFDx2), # see note ^
2134 ('ED C0', FFFDx2), ('ED FF', FFFDx2), ('ED 80 00', FFFD+'\x00'),
2135 ('ED 80 7F', FFFD+'\x7f'), ('ED 80 C0', FFFDx2),
2136 ('ED 80 FF', FFFDx2), ('ED 9F 00', FFFD+'\x00'),
2137 ('ED 9F 7F', FFFD+'\x7f'), ('ED 9F C0', FFFDx2),
2138 ('ED 9F FF', FFFDx2), ('EE 00', FFFD+'\x00'),
2139 ('EE 7F', FFFD+'\x7f'), ('EE C0', FFFDx2), ('EE FF', FFFDx2),
2140 ('EE 80 00', FFFD+'\x00'), ('EE 80 7F', FFFD+'\x7f'),
2141 ('EE 80 C0', FFFDx2), ('EE 80 FF', FFFDx2),
2142 ('EE BF 00', FFFD+'\x00'), ('EE BF 7F', FFFD+'\x7f'),
2143 ('EE BF C0', FFFDx2), ('EE BF FF', FFFDx2), ('EF 00', FFFD+'\x00'),
2144 ('EF 7F', FFFD+'\x7f'), ('EF C0', FFFDx2), ('EF FF', FFFDx2),
2145 ('EF 80 00', FFFD+'\x00'), ('EF 80 7F', FFFD+'\x7f'),
2146 ('EF 80 C0', FFFDx2), ('EF 80 FF', FFFDx2),
2147 ('EF BF 00', FFFD+'\x00'), ('EF BF 7F', FFFD+'\x7f'),
2148 ('EF BF C0', FFFDx2), ('EF BF FF', FFFDx2),
2149 ]
2150 for seq, res in sequences:
2151 self.assertCorrectUTF8Decoding(bytes.fromhex(seq), res,
2152 'invalid continuation byte')
2153
2154 def test_invalid_cb_for_4bytes_seq(self):
2155 """
2156 Test that an 'invalid continuation byte' error is raised when the
2157 continuation byte(s) of a 4-bytes sequence are invalid. When
2158 errors='replace',the start byte and all the following valid
2159 continuation bytes are replaced with a single U+FFFD, and all the bytes
2160 starting from the first invalid continuation bytes (included) are
2161 handled separately.
2162 E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes
2163 sequence, 80 is a valid continuation byte, but 41 is not a valid cb
2164 because it's the ASCII letter 'A'.
2165 Note: when the start byte is E0 or ED, the valid ranges for the first
2166 continuation byte are limited to A0..BF and 80..9F respectively.
2167 However, when the start byte is ED, Python 2 considers all the bytes
2168 in range 80..BF valid. This is fixed in Python 3.
2169 """
2170 FFFD = '\ufffd'
2171 FFFDx2 = FFFD * 2
2172 sequences = [
2173 ('F0 00', FFFD+'\x00'), ('F0 7F', FFFD+'\x7f'), ('F0 80', FFFDx2),
2174 ('F0 8F', FFFDx2), ('F0 C0', FFFDx2), ('F0 FF', FFFDx2),
2175 ('F0 90 00', FFFD+'\x00'), ('F0 90 7F', FFFD+'\x7f'),
2176 ('F0 90 C0', FFFDx2), ('F0 90 FF', FFFDx2),
2177 ('F0 BF 00', FFFD+'\x00'), ('F0 BF 7F', FFFD+'\x7f'),
2178 ('F0 BF C0', FFFDx2), ('F0 BF FF', FFFDx2),
2179 ('F0 90 80 00', FFFD+'\x00'), ('F0 90 80 7F', FFFD+'\x7f'),
2180 ('F0 90 80 C0', FFFDx2), ('F0 90 80 FF', FFFDx2),
2181 ('F0 90 BF 00', FFFD+'\x00'), ('F0 90 BF 7F', FFFD+'\x7f'),
2182 ('F0 90 BF C0', FFFDx2), ('F0 90 BF FF', FFFDx2),
2183 ('F0 BF 80 00', FFFD+'\x00'), ('F0 BF 80 7F', FFFD+'\x7f'),
2184 ('F0 BF 80 C0', FFFDx2), ('F0 BF 80 FF', FFFDx2),
2185 ('F0 BF BF 00', FFFD+'\x00'), ('F0 BF BF 7F', FFFD+'\x7f'),
2186 ('F0 BF BF C0', FFFDx2), ('F0 BF BF FF', FFFDx2),
2187 ('F1 00', FFFD+'\x00'), ('F1 7F', FFFD+'\x7f'), ('F1 C0', FFFDx2),
2188 ('F1 FF', FFFDx2), ('F1 80 00', FFFD+'\x00'),
2189 ('F1 80 7F', FFFD+'\x7f'), ('F1 80 C0', FFFDx2),
2190 ('F1 80 FF', FFFDx2), ('F1 BF 00', FFFD+'\x00'),
2191 ('F1 BF 7F', FFFD+'\x7f'), ('F1 BF C0', FFFDx2),
2192 ('F1 BF FF', FFFDx2), ('F1 80 80 00', FFFD+'\x00'),
2193 ('F1 80 80 7F', FFFD+'\x7f'), ('F1 80 80 C0', FFFDx2),
2194 ('F1 80 80 FF', FFFDx2), ('F1 80 BF 00', FFFD+'\x00'),
2195 ('F1 80 BF 7F', FFFD+'\x7f'), ('F1 80 BF C0', FFFDx2),
2196 ('F1 80 BF FF', FFFDx2), ('F1 BF 80 00', FFFD+'\x00'),
2197 ('F1 BF 80 7F', FFFD+'\x7f'), ('F1 BF 80 C0', FFFDx2),
2198 ('F1 BF 80 FF', FFFDx2), ('F1 BF BF 00', FFFD+'\x00'),
2199 ('F1 BF BF 7F', FFFD+'\x7f'), ('F1 BF BF C0', FFFDx2),
2200 ('F1 BF BF FF', FFFDx2), ('F3 00', FFFD+'\x00'),
2201 ('F3 7F', FFFD+'\x7f'), ('F3 C0', FFFDx2), ('F3 FF', FFFDx2),
2202 ('F3 80 00', FFFD+'\x00'), ('F3 80 7F', FFFD+'\x7f'),
2203 ('F3 80 C0', FFFDx2), ('F3 80 FF', FFFDx2),
2204 ('F3 BF 00', FFFD+'\x00'), ('F3 BF 7F', FFFD+'\x7f'),
2205 ('F3 BF C0', FFFDx2), ('F3 BF FF', FFFDx2),
2206 ('F3 80 80 00', FFFD+'\x00'), ('F3 80 80 7F', FFFD+'\x7f'),
2207 ('F3 80 80 C0', FFFDx2), ('F3 80 80 FF', FFFDx2),
2208 ('F3 80 BF 00', FFFD+'\x00'), ('F3 80 BF 7F', FFFD+'\x7f'),
2209 ('F3 80 BF C0', FFFDx2), ('F3 80 BF FF', FFFDx2),
2210 ('F3 BF 80 00', FFFD+'\x00'), ('F3 BF 80 7F', FFFD+'\x7f'),
2211 ('F3 BF 80 C0', FFFDx2), ('F3 BF 80 FF', FFFDx2),
2212 ('F3 BF BF 00', FFFD+'\x00'), ('F3 BF BF 7F', FFFD+'\x7f'),
2213 ('F3 BF BF C0', FFFDx2), ('F3 BF BF FF', FFFDx2),
2214 ('F4 00', FFFD+'\x00'), ('F4 7F', FFFD+'\x7f'), ('F4 90', FFFDx2),
2215 ('F4 BF', FFFDx2), ('F4 C0', FFFDx2), ('F4 FF', FFFDx2),
2216 ('F4 80 00', FFFD+'\x00'), ('F4 80 7F', FFFD+'\x7f'),
2217 ('F4 80 C0', FFFDx2), ('F4 80 FF', FFFDx2),
2218 ('F4 8F 00', FFFD+'\x00'), ('F4 8F 7F', FFFD+'\x7f'),
2219 ('F4 8F C0', FFFDx2), ('F4 8F FF', FFFDx2),
2220 ('F4 80 80 00', FFFD+'\x00'), ('F4 80 80 7F', FFFD+'\x7f'),
2221 ('F4 80 80 C0', FFFDx2), ('F4 80 80 FF', FFFDx2),
2222 ('F4 80 BF 00', FFFD+'\x00'), ('F4 80 BF 7F', FFFD+'\x7f'),
2223 ('F4 80 BF C0', FFFDx2), ('F4 80 BF FF', FFFDx2),
2224 ('F4 8F 80 00', FFFD+'\x00'), ('F4 8F 80 7F', FFFD+'\x7f'),
2225 ('F4 8F 80 C0', FFFDx2), ('F4 8F 80 FF', FFFDx2),
2226 ('F4 8F BF 00', FFFD+'\x00'), ('F4 8F BF 7F', FFFD+'\x7f'),
2227 ('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2)
2228 ]
2229 for seq, res in sequences:
2230 self.assertCorrectUTF8Decoding(bytes.fromhex(seq), res,
2231 'invalid continuation byte')
2232
2233 def test_codecs_idna(self):
2234 # Test whether trailing dot is preserved
2235 self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.")
2236
2237 def test_codecs_errors(self):
2238 # Error handling (encoding)
2239 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii')
2240 self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict')
2241 self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x")
2242 self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x")
2243 self.assertEqual('Andr\202 x'.encode('ascii', 'replace'),
2244 'Andr\202 x'.encode('ascii', errors='replace'))
2245 self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'),
2246 'Andr\202 x'.encode(encoding='ascii', errors='ignore'))
2247
2248 # Error handling (decoding)
2249 self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii')
2250 self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii', 'strict')
2251 self.assertEqual(str(b'Andr\202 x', 'ascii', 'ignore'), "Andr x")
2252 self.assertEqual(str(b'Andr\202 x', 'ascii', 'replace'), 'Andr\uFFFD x')
2253 self.assertEqual(str(b'\202 x', 'ascii', 'replace'), '\uFFFD x')
2254
2255 # Error handling (unknown character names)
2256 self.assertEqual(b"\\N{foo}xx".decode("unicode-escape", "ignore"), "xx")
2257
2258 # Error handling (truncated escape sequence)
2259 self.assertRaises(UnicodeError, b"\\".decode, "unicode-escape")
2260
2261 self.assertRaises(TypeError, b"hello".decode, "test.unicode1")
2262 self.assertRaises(TypeError, str, b"hello", "test.unicode2")
2263 self.assertRaises(TypeError, "hello".encode, "test.unicode1")
2264 self.assertRaises(TypeError, "hello".encode, "test.unicode2")
2265
2266 # Error handling (wrong arguments)
2267 self.assertRaises(TypeError, "hello".encode, 42, 42, 42)
2268
2269 # Error handling (lone surrogate in
2270 # _PyUnicode_TransformDecimalAndSpaceToASCII())
2271 self.assertRaises(ValueError, int, "\ud800")
2272 self.assertRaises(ValueError, int, "\udf00")
2273 self.assertRaises(ValueError, float, "\ud800")
2274 self.assertRaises(ValueError, float, "\udf00")
2275 self.assertRaises(ValueError, complex, "\ud800")
2276 self.assertRaises(ValueError, complex, "\udf00")
2277
2278 def test_codecs(self):
2279 # Encoding
2280 self.assertEqual('hello'.encode('ascii'), b'hello')
2281 self.assertEqual('hello'.encode('utf-7'), b'hello')
2282 self.assertEqual('hello'.encode('utf-8'), b'hello')
2283 self.assertEqual('hello'.encode('utf-8'), b'hello')
2284 self.assertEqual('hello'.encode('utf-16-le'), b'h\000e\000l\000l\000o\000')
2285 self.assertEqual('hello'.encode('utf-16-be'), b'\000h\000e\000l\000l\000o')
2286 self.assertEqual('hello'.encode('latin-1'), b'hello')
2287
2288 # Default encoding is utf-8
2289 self.assertEqual('\u2603'.encode(), b'\xe2\x98\x83')
2290
2291 # Roundtrip safety for BMP (just the first 1024 chars)
2292 for c in range(1024):
2293 u = chr(c)
2294 for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le',
2295 'utf-16-be', 'raw_unicode_escape',
2296 'unicode_escape'):
2297 self.assertEqual(str(u.encode(encoding),encoding), u)
2298
2299 # Roundtrip safety for BMP (just the first 256 chars)
2300 for c in range(256):
2301 u = chr(c)
2302 for encoding in ('latin-1',):
2303 self.assertEqual(str(u.encode(encoding),encoding), u)
2304
2305 # Roundtrip safety for BMP (just the first 128 chars)
2306 for c in range(128):
2307 u = chr(c)
2308 for encoding in ('ascii',):
2309 self.assertEqual(str(u.encode(encoding),encoding), u)
2310
2311 # Roundtrip safety for non-BMP (just a few chars)
2312 with warnings.catch_warnings():
2313 u = '\U00010001\U00020002\U00030003\U00040004\U00050005'
2314 for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be',
2315 'raw_unicode_escape', 'unicode_escape'):
2316 self.assertEqual(str(u.encode(encoding),encoding), u)
2317
2318 # UTF-8 must be roundtrip safe for all code points
2319 # (except surrogates, which are forbidden).
2320 u = ''.join(map(chr, list(range(0, 0xd800)) +
2321 list(range(0xe000, 0x110000))))
2322 for encoding in ('utf-8',):
2323 self.assertEqual(str(u.encode(encoding),encoding), u)
2324
2325 def test_codecs_charmap(self):
2326 # 0-127
2327 s = bytes(range(128))
2328 for encoding in (
2329 'cp037', 'cp1026', 'cp273',
2330 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850',
2331 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862',
2332 'cp863', 'cp865', 'cp866', 'cp1125',
2333 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
2334 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6',
2335 'iso8859_7', 'iso8859_9',
2336 'koi8_r', 'koi8_t', 'koi8_u', 'kz1048', 'latin_1',
2337 'mac_cyrillic', 'mac_latin2',
2338
2339 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
2340 'cp1256', 'cp1257', 'cp1258',
2341 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
2342
2343 'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
2344 'cp1006', 'iso8859_8',
2345
2346 ### These have undefined mappings:
2347 #'cp424',
2348
2349 ### These fail the round-trip:
2350 #'cp875'
2351
2352 ):
2353 self.assertEqual(str(s, encoding).encode(encoding), s)
2354
2355 # 128-255
2356 s = bytes(range(128, 256))
2357 for encoding in (
2358 'cp037', 'cp1026', 'cp273',
2359 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850',
2360 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862',
2361 'cp863', 'cp865', 'cp866', 'cp1125',
2362 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
2363 'iso8859_2', 'iso8859_4', 'iso8859_5',
2364 'iso8859_9', 'koi8_r', 'koi8_u', 'latin_1',
2365 'mac_cyrillic', 'mac_latin2',
2366
2367 ### These have undefined mappings:
2368 #'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
2369 #'cp1256', 'cp1257', 'cp1258',
2370 #'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
2371 #'iso8859_3', 'iso8859_6', 'iso8859_7', 'koi8_t', 'kz1048',
2372 #'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
2373
2374 ### These fail the round-trip:
2375 #'cp1006', 'cp875', 'iso8859_8',
2376
2377 ):
2378 self.assertEqual(str(s, encoding).encode(encoding), s)
2379
2380 def test_concatenation(self):
2381 self.assertEqual(("abc" "def"), "abcdef")
2382 self.assertEqual(("abc" "def"), "abcdef")
2383 self.assertEqual(("abc" "def"), "abcdef")
2384 self.assertEqual(("abc" "def" "ghi"), "abcdefghi")
2385 self.assertEqual(("abc" "def" "ghi"), "abcdefghi")
2386
2387 def test_ucs4(self):
2388 x = '\U00100000'
2389 y = x.encode("raw-unicode-escape").decode("raw-unicode-escape")
2390 self.assertEqual(x, y)
2391
2392 y = br'\U00100000'
2393 x = y.decode("raw-unicode-escape").encode("raw-unicode-escape")
2394 self.assertEqual(x, y)
2395 y = br'\U00010000'
2396 x = y.decode("raw-unicode-escape").encode("raw-unicode-escape")
2397 self.assertEqual(x, y)
2398
2399 try:
2400 br'\U11111111'.decode("raw-unicode-escape")
2401 except UnicodeDecodeError as e:
2402 self.assertEqual(e.start, 0)
2403 self.assertEqual(e.end, 10)
2404 else:
2405 self.fail("Should have raised UnicodeDecodeError")
2406
2407 def test_conversion(self):
2408 # Make sure __str__() works properly
2409 class StrWithStr(str):
2410 def __new__(cls, value):
2411 self = str.__new__(cls, "")
2412 self.value = value
2413 return self
2414 def __str__(self):
2415 return self.value
2416
2417 self.assertTypedEqual(str(WithStr('abc')), 'abc')
2418 self.assertTypedEqual(str(WithStr(StrSubclass('abc'))), StrSubclass('abc'))
2419 self.assertTypedEqual(StrSubclass(WithStr('abc')), StrSubclass('abc'))
2420 self.assertTypedEqual(StrSubclass(WithStr(StrSubclass('abc'))),
2421 StrSubclass('abc'))
2422 self.assertTypedEqual(StrSubclass(WithStr(OtherStrSubclass('abc'))),
2423 StrSubclass('abc'))
2424
2425 self.assertTypedEqual(str(StrWithStr('abc')), 'abc')
2426 self.assertTypedEqual(str(StrWithStr(StrSubclass('abc'))), StrSubclass('abc'))
2427 self.assertTypedEqual(StrSubclass(StrWithStr('abc')), StrSubclass('abc'))
2428 self.assertTypedEqual(StrSubclass(StrWithStr(StrSubclass('abc'))),
2429 StrSubclass('abc'))
2430 self.assertTypedEqual(StrSubclass(StrWithStr(OtherStrSubclass('abc'))),
2431 StrSubclass('abc'))
2432
2433 self.assertTypedEqual(str(WithRepr('<abc>')), '<abc>')
2434 self.assertTypedEqual(str(WithRepr(StrSubclass('<abc>'))), StrSubclass('<abc>'))
2435 self.assertTypedEqual(StrSubclass(WithRepr('<abc>')), StrSubclass('<abc>'))
2436 self.assertTypedEqual(StrSubclass(WithRepr(StrSubclass('<abc>'))),
2437 StrSubclass('<abc>'))
2438 self.assertTypedEqual(StrSubclass(WithRepr(OtherStrSubclass('<abc>'))),
2439 StrSubclass('<abc>'))
2440
2441 def test_unicode_repr(self):
2442 class s1:
2443 def __repr__(self):
2444 return '\\n'
2445
2446 self.assertEqual(repr(s1()), '\\n')
2447
2448 def test_printable_repr(self):
2449 # printable
2450 self.assertEqual(repr('\U00010000'), "'%c'" % (0x10000,))
2451 # nonprintable (private use area)
2452 self.assertEqual(repr('\U00100001'), "'\\U00100001'")
2453
2454 # This test only affects 32-bit platforms because expandtabs can only take
2455 # an int as the max value, not a 64-bit C long. If expandtabs is changed
2456 # to take a 64-bit long, this test should apply to all platforms.
2457 @unittest.skipIf(sys.maxsize > (1 << 32) or struct.calcsize('P') != 4,
2458 'only applies to 32-bit platforms')
2459 def test_expandtabs_overflows_gracefully(self):
2460 self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize)
2461
2462 @support.cpython_only
2463 def test_expandtabs_optimization(self):
2464 s = 'abc'
2465 self.assertIs(s.expandtabs(), s)
2466
2467 def test_raiseMemError(self):
2468 asciifields = "nnb"
2469 compactfields = asciifields + "nP"
2470 ascii_struct_size = support.calcobjsize(asciifields)
2471 compact_struct_size = support.calcobjsize(compactfields)
2472
2473 for char in ('a', '\xe9', '\u20ac', '\U0010ffff'):
2474 code = ord(char)
2475 if code < 0x80:
2476 char_size = 1 # sizeof(Py_UCS1)
2477 struct_size = ascii_struct_size
2478 elif code < 0x100:
2479 char_size = 1 # sizeof(Py_UCS1)
2480 struct_size = compact_struct_size
2481 elif code < 0x10000:
2482 char_size = 2 # sizeof(Py_UCS2)
2483 struct_size = compact_struct_size
2484 else:
2485 char_size = 4 # sizeof(Py_UCS4)
2486 struct_size = compact_struct_size
2487 # Note: sys.maxsize is half of the actual max allocation because of
2488 # the signedness of Py_ssize_t. Strings of maxlen-1 should in principle
2489 # be allocatable, given enough memory.
2490 maxlen = ((sys.maxsize - struct_size) // char_size)
2491 alloc = lambda: char * maxlen
2492 with self.subTest(
2493 char=char,
2494 struct_size=struct_size,
2495 char_size=char_size
2496 ):
2497 # self-check
2498 self.assertEqual(
2499 sys.getsizeof(char * 42),
2500 struct_size + (char_size * (42 + 1))
2501 )
2502 self.assertRaises(MemoryError, alloc)
2503 self.assertRaises(MemoryError, alloc)
2504
2505 def test_format_subclass(self):
2506 class S(str):
2507 def __str__(self):
2508 return '__str__ overridden'
2509 s = S('xxx')
2510 self.assertEqual("%s" % s, '__str__ overridden')
2511 self.assertEqual("{}".format(s), '__str__ overridden')
2512
2513 def test_subclass_add(self):
2514 class S(str):
2515 def __add__(self, o):
2516 return "3"
2517 self.assertEqual(S("4") + S("5"), "3")
2518 class S(str):
2519 def __iadd__(self, o):
2520 return "3"
2521 s = S("1")
2522 s += "4"
2523 self.assertEqual(s, "3")
2524
2525 def test_getnewargs(self):
2526 text = 'abc'
2527 args = text.__getnewargs__()
2528 self.assertIsNot(args[0], text)
2529 self.assertEqual(args[0], text)
2530 self.assertEqual(len(args), 1)
2531
2532 def test_compare(self):
2533 # Issue #17615
2534 N = 10
2535 ascii = 'a' * N
2536 ascii2 = 'z' * N
2537 latin = '\x80' * N
2538 latin2 = '\xff' * N
2539 bmp = '\u0100' * N
2540 bmp2 = '\uffff' * N
2541 astral = '\U00100000' * N
2542 astral2 = '\U0010ffff' * N
2543 strings = (
2544 ascii, ascii2,
2545 latin, latin2,
2546 bmp, bmp2,
2547 astral, astral2)
2548 for text1, text2 in itertools.combinations(strings, 2):
2549 equal = (text1 is text2)
2550 self.assertEqual(text1 == text2, equal)
2551 self.assertEqual(text1 != text2, not equal)
2552
2553 if equal:
2554 self.assertTrue(text1 <= text2)
2555 self.assertTrue(text1 >= text2)
2556
2557 # text1 is text2: duplicate strings to skip the "str1 == str2"
2558 # optimization in unicode_compare_eq() and really compare
2559 # character per character
2560 copy1 = duplicate_string(text1)
2561 copy2 = duplicate_string(text2)
2562 self.assertIsNot(copy1, copy2)
2563
2564 self.assertTrue(copy1 == copy2)
2565 self.assertFalse(copy1 != copy2)
2566
2567 self.assertTrue(copy1 <= copy2)
2568 self.assertTrue(copy2 >= copy2)
2569
2570 self.assertTrue(ascii < ascii2)
2571 self.assertTrue(ascii < latin)
2572 self.assertTrue(ascii < bmp)
2573 self.assertTrue(ascii < astral)
2574 self.assertFalse(ascii >= ascii2)
2575 self.assertFalse(ascii >= latin)
2576 self.assertFalse(ascii >= bmp)
2577 self.assertFalse(ascii >= astral)
2578
2579 self.assertFalse(latin < ascii)
2580 self.assertTrue(latin < latin2)
2581 self.assertTrue(latin < bmp)
2582 self.assertTrue(latin < astral)
2583 self.assertTrue(latin >= ascii)
2584 self.assertFalse(latin >= latin2)
2585 self.assertFalse(latin >= bmp)
2586 self.assertFalse(latin >= astral)
2587
2588 self.assertFalse(bmp < ascii)
2589 self.assertFalse(bmp < latin)
2590 self.assertTrue(bmp < bmp2)
2591 self.assertTrue(bmp < astral)
2592 self.assertTrue(bmp >= ascii)
2593 self.assertTrue(bmp >= latin)
2594 self.assertFalse(bmp >= bmp2)
2595 self.assertFalse(bmp >= astral)
2596
2597 self.assertFalse(astral < ascii)
2598 self.assertFalse(astral < latin)
2599 self.assertFalse(astral < bmp2)
2600 self.assertTrue(astral < astral2)
2601 self.assertTrue(astral >= ascii)
2602 self.assertTrue(astral >= latin)
2603 self.assertTrue(astral >= bmp2)
2604 self.assertFalse(astral >= astral2)
2605
2606 def test_free_after_iterating(self):
2607 support.check_free_after_iterating(self, iter, str)
2608 if not support.Py_GIL_DISABLED:
2609 support.check_free_after_iterating(self, reversed, str)
2610
2611 def test_check_encoding_errors(self):
2612 # bpo-37388: str(bytes) and str.decode() must check encoding and errors
2613 # arguments in dev mode
2614 encodings = ('ascii', 'utf8', 'latin1')
2615 invalid = 'Boom, Shaka Laka, Boom!'
2616 code = textwrap.dedent(f'''
2617 import sys
2618 encodings = {encodings!r}
2619
2620 for data in (b'', b'short string'):
2621 try:
2622 str(data, encoding={invalid!r})
2623 except LookupError:
2624 pass
2625 else:
2626 sys.exit(21)
2627
2628 try:
2629 str(data, errors={invalid!r})
2630 except LookupError:
2631 pass
2632 else:
2633 sys.exit(22)
2634
2635 for encoding in encodings:
2636 try:
2637 str(data, encoding, errors={invalid!r})
2638 except LookupError:
2639 pass
2640 else:
2641 sys.exit(22)
2642
2643 for data in ('', 'short string'):
2644 try:
2645 data.encode(encoding={invalid!r})
2646 except LookupError:
2647 pass
2648 else:
2649 sys.exit(23)
2650
2651 try:
2652 data.encode(errors={invalid!r})
2653 except LookupError:
2654 pass
2655 else:
2656 sys.exit(24)
2657
2658 for encoding in encodings:
2659 try:
2660 data.encode(encoding, errors={invalid!r})
2661 except LookupError:
2662 pass
2663 else:
2664 sys.exit(24)
2665
2666 sys.exit(10)
2667 ''')
2668 proc = assert_python_failure('-X', 'dev', '-c', code)
2669 self.assertEqual(proc.rc, 10, proc)
2670
2671 def test_str_invalid_call(self):
2672 # too many args
2673 with self.assertRaisesRegex(TypeError, r"str expected at most 3 arguments, got 4"):
2674 str("too", "many", "argu", "ments")
2675 with self.assertRaisesRegex(TypeError, r"str expected at most 3 arguments, got 4"):
2676 str(1, "", "", 1)
2677
2678 # no such kw arg
2679 with self.assertRaisesRegex(TypeError, r"str\(\) got an unexpected keyword argument 'test'"):
2680 str(test=1)
2681
2682 # 'encoding' must be str
2683 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
2684 str(1, 1)
2685 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
2686 str(1, encoding=1)
2687 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not bytes"):
2688 str(b"x", b"ascii")
2689 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not bytes"):
2690 str(b"x", encoding=b"ascii")
2691
2692 # 'errors' must be str
2693 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
2694 str(1, 1, 1)
2695 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not int"):
2696 str(1, errors=1)
2697 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not int"):
2698 str(1, "", errors=1)
2699 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not bytes"):
2700 str(b"x", "ascii", b"strict")
2701 with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not bytes"):
2702 str(b"x", "ascii", errors=b"strict")
2703
2704 # both positional and kwarg
2705 with self.assertRaisesRegex(TypeError, r"argument for str\(\) given by name \('encoding'\) and position \(2\)"):
2706 str(b"x", "utf-8", encoding="ascii")
2707 with self.assertRaisesRegex(TypeError, r"str\(\) takes at most 3 arguments \(4 given\)"):
2708 str(b"x", "utf-8", "ignore", encoding="ascii")
2709 with self.assertRaisesRegex(TypeError, r"str\(\) takes at most 3 arguments \(4 given\)"):
2710 str(b"x", "utf-8", "strict", errors="ignore")
2711
2712
2713 class StringModuleTest(unittest.TestCase):
2714 def test_formatter_parser(self):
2715 def parse(format):
2716 return list(_string.formatter_parser(format))
2717
2718 formatter = parse("prefix {2!s}xxx{0:^+10.3f}{obj.attr!s} {z[0]!s:10}")
2719 self.assertEqual(formatter, [
2720 ('prefix ', '2', '', 's'),
2721 ('xxx', '0', '^+10.3f', None),
2722 ('', 'obj.attr', '', 's'),
2723 (' ', 'z[0]', '10', 's'),
2724 ])
2725
2726 formatter = parse("prefix {} suffix")
2727 self.assertEqual(formatter, [
2728 ('prefix ', '', '', None),
2729 (' suffix', None, None, None),
2730 ])
2731
2732 formatter = parse("str")
2733 self.assertEqual(formatter, [
2734 ('str', None, None, None),
2735 ])
2736
2737 formatter = parse("")
2738 self.assertEqual(formatter, [])
2739
2740 formatter = parse("{0}")
2741 self.assertEqual(formatter, [
2742 ('', '0', '', None),
2743 ])
2744
2745 self.assertRaises(TypeError, _string.formatter_parser, 1)
2746
2747 def test_formatter_field_name_split(self):
2748 def split(name):
2749 items = list(_string.formatter_field_name_split(name))
2750 items[1] = list(items[1])
2751 return items
2752 self.assertEqual(split("obj"), ["obj", []])
2753 self.assertEqual(split("obj.arg"), ["obj", [(True, 'arg')]])
2754 self.assertEqual(split("obj[key]"), ["obj", [(False, 'key')]])
2755 self.assertEqual(split("obj.arg[key1][key2]"), [
2756 "obj",
2757 [(True, 'arg'),
2758 (False, 'key1'),
2759 (False, 'key2'),
2760 ]])
2761 self.assertRaises(TypeError, _string.formatter_field_name_split, 1)
2762
2763 def test_str_subclass_attr(self):
2764
2765 name = StrSubclass("name")
2766 name2 = StrSubclass("name2")
2767 class Bag:
2768 pass
2769
2770 o = Bag()
2771 with self.assertRaises(AttributeError):
2772 delattr(o, name)
2773 setattr(o, name, 1)
2774 self.assertEqual(o.name, 1)
2775 o.name = 2
2776 self.assertEqual(list(o.__dict__), [name])
2777
2778 with self.assertRaises(AttributeError):
2779 delattr(o, name2)
2780 with self.assertRaises(AttributeError):
2781 del o.name2
2782 setattr(o, name2, 3)
2783 self.assertEqual(o.name2, 3)
2784 o.name2 = 4
2785 self.assertEqual(list(o.__dict__), [name, name2])
2786
2787
2788 if __name__ == "__main__":
2789 unittest.main()