1 # -*- coding: iso-8859-1 -*-
2 """ Test script for the Unicode implementation.
4 Written by Marc-Andre Lemburg (mal@lemburg.com).
6 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
9 import sys
, struct
, codecs
10 from test
import test_support
, string_tests
12 # Error handling (bad decoder return)
13 def search_function(encoding
):
14 def decode1(input, errors
="strict"):
15 return 42 # not a tuple
16 def encode1(input, errors
="strict"):
17 return 42 # not a tuple
18 def encode2(input, errors
="strict"):
19 return (42, 42) # no unicode
20 def decode2(input, errors
="strict"):
21 return (42, 42) # no unicode
22 if encoding
=="test.unicode1":
23 return (encode1
, decode1
, None, None)
24 elif encoding
=="test.unicode2":
25 return (encode2
, decode2
, None, None)
28 codecs
.register(search_function
)
31 string_tests
.CommonTest
,
32 string_tests
.MixinStrUnicodeUserStringTest
,
33 string_tests
.MixinStrUnicodeTest
,
37 def checkequalnofix(self
, result
, object, methodname
, *args
):
38 method
= getattr(object, methodname
)
39 realresult
= method(*args
)
40 self
.assertEqual(realresult
, result
)
41 self
.assertTrue(type(realresult
) is type(result
))
43 # if the original is returned make sure that
44 # this doesn't happen with subclasses
45 if realresult
is object:
48 return 'usub(%r)' % unicode.__repr
__(self
)
50 method
= getattr(object, methodname
)
51 realresult
= method(*args
)
52 self
.assertEqual(realresult
, result
)
53 self
.assertTrue(object is not realresult
)
55 def test_literals(self
):
56 self
.assertEqual(u
'\xff', u
'\u00ff')
57 self
.assertEqual(u
'\uffff', u
'\U0000ffff')
58 self
.assertRaises(SyntaxError, eval, 'u\'\\Ufffffffe\'')
59 self
.assertRaises(SyntaxError, eval, 'u\'\\Uffffffff\'')
60 self
.assertRaises(SyntaxError, eval, 'u\'\\U%08x\'' % 0x110000)
63 if not sys
.platform
.startswith('java'):
64 # Test basic sanity of repr()
65 self
.assertEqual(repr(u
'abc'), "u'abc'")
66 self
.assertEqual(repr(u
'ab\\c'), "u'ab\\\\c'")
67 self
.assertEqual(repr(u
'ab\\'), "u'ab\\\\'")
68 self
.assertEqual(repr(u
'\\c'), "u'\\\\c'")
69 self
.assertEqual(repr(u
'\\'), "u'\\\\'")
70 self
.assertEqual(repr(u
'\n'), "u'\\n'")
71 self
.assertEqual(repr(u
'\r'), "u'\\r'")
72 self
.assertEqual(repr(u
'\t'), "u'\\t'")
73 self
.assertEqual(repr(u
'\b'), "u'\\x08'")
74 self
.assertEqual(repr(u
"'\""), """u'\\'"'""")
75 self
.assertEqual(repr(u
"'\""), """u'\\'"'""")
76 self
.assertEqual(repr(u
"'"), '''u"'"''')
77 self
.assertEqual(repr(u
'"'), """u'"'""")
79 "u'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r"
80 "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a"
81 "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI"
82 "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f"
83 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d"
84 "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b"
85 "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9"
86 "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7"
87 "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5"
88 "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3"
89 "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1"
90 "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef"
91 "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd"
93 testrepr
= repr(u
''.join(map(unichr, xrange(256))))
94 self
.assertEqual(testrepr
, latin1repr
)
95 # Test repr works on wide unicode escapes without overflow.
96 self
.assertEqual(repr(u
"\U00010000" * 39 + u
"\uffff" * 4096),
97 repr(u
"\U00010000" * 39 + u
"\uffff" * 4096))
100 def test_count(self
):
101 string_tests
.CommonTest
.test_count(self
)
102 # check mixed argument types
103 self
.checkequalnofix(3, 'aaa', 'count', u
'a')
104 self
.checkequalnofix(0, 'aaa', 'count', u
'b')
105 self
.checkequalnofix(3, u
'aaa', 'count', 'a')
106 self
.checkequalnofix(0, u
'aaa', 'count', 'b')
107 self
.checkequalnofix(0, u
'aaa', 'count', 'b')
108 self
.checkequalnofix(1, u
'aaa', 'count', 'a', -1)
109 self
.checkequalnofix(3, u
'aaa', 'count', 'a', -10)
110 self
.checkequalnofix(2, u
'aaa', 'count', 'a', 0, -1)
111 self
.checkequalnofix(0, u
'aaa', 'count', 'a', 0, -10)
114 self
.checkequalnofix(0, u
'abcdefghiabc', 'find', u
'abc')
115 self
.checkequalnofix(9, u
'abcdefghiabc', 'find', u
'abc', 1)
116 self
.checkequalnofix(-1, u
'abcdefghiabc', 'find', u
'def', 4)
118 self
.assertRaises(TypeError, u
'hello'.find
)
119 self
.assertRaises(TypeError, u
'hello'.find
, 42)
121 def test_rfind(self
):
122 string_tests
.CommonTest
.test_rfind(self
)
123 # check mixed argument types
124 self
.checkequalnofix(9, 'abcdefghiabc', 'rfind', u
'abc')
125 self
.checkequalnofix(12, 'abcdefghiabc', 'rfind', u
'')
126 self
.checkequalnofix(12, u
'abcdefghiabc', 'rfind', '')
128 def test_index(self
):
129 string_tests
.CommonTest
.test_index(self
)
130 # check mixed argument types
131 for (t1
, t2
) in ((str, unicode), (unicode, str)):
132 self
.checkequalnofix(0, t1('abcdefghiabc'), 'index', t2(''))
133 self
.checkequalnofix(3, t1('abcdefghiabc'), 'index', t2('def'))
134 self
.checkequalnofix(0, t1('abcdefghiabc'), 'index', t2('abc'))
135 self
.checkequalnofix(9, t1('abcdefghiabc'), 'index', t2('abc'), 1)
136 self
.assertRaises(ValueError, t1('abcdefghiabc').index
, t2('hib'))
137 self
.assertRaises(ValueError, t1('abcdefghiab').index
, t2('abc'), 1)
138 self
.assertRaises(ValueError, t1('abcdefghi').index
, t2('ghi'), 8)
139 self
.assertRaises(ValueError, t1('abcdefghi').index
, t2('ghi'), -1)
141 def test_rindex(self
):
142 string_tests
.CommonTest
.test_rindex(self
)
143 # check mixed argument types
144 for (t1
, t2
) in ((str, unicode), (unicode, str)):
145 self
.checkequalnofix(12, t1('abcdefghiabc'), 'rindex', t2(''))
146 self
.checkequalnofix(3, t1('abcdefghiabc'), 'rindex', t2('def'))
147 self
.checkequalnofix(9, t1('abcdefghiabc'), 'rindex', t2('abc'))
148 self
.checkequalnofix(0, t1('abcdefghiabc'), 'rindex', t2('abc'), 0, -1)
150 self
.assertRaises(ValueError, t1('abcdefghiabc').rindex
, t2('hib'))
151 self
.assertRaises(ValueError, t1('defghiabc').rindex
, t2('def'), 1)
152 self
.assertRaises(ValueError, t1('defghiabc').rindex
, t2('abc'), 0, -1)
153 self
.assertRaises(ValueError, t1('abcdefghi').rindex
, t2('ghi'), 0, 8)
154 self
.assertRaises(ValueError, t1('abcdefghi').rindex
, t2('ghi'), 0, -1)
156 def test_translate(self
):
157 self
.checkequalnofix(u
'bbbc', u
'abababc', 'translate', {ord('a'):None})
158 self
.checkequalnofix(u
'iiic', u
'abababc', 'translate', {ord('a'):None, ord('b'):ord('i')})
159 self
.checkequalnofix(u
'iiix', u
'abababc', 'translate', {ord('a'):None, ord('b'):ord('i'), ord('c'):u
'x'})
160 self
.checkequalnofix(u
'<i><i><i>c', u
'abababc', 'translate', {ord('a'):None, ord('b'):u
'<i>'})
161 self
.checkequalnofix(u
'c', u
'abababc', 'translate', {ord('a'):None, ord('b'):u
''})
162 self
.checkequalnofix(u
'xyyx', u
'xzx', 'translate', {ord('z'):u
'yy'})
164 self
.assertRaises(TypeError, u
'hello'.translate
)
165 self
.assertRaises(TypeError, u
'abababc'.translate
, {ord('a'):''})
167 def test_split(self
):
168 string_tests
.CommonTest
.test_split(self
)
171 self
.checkequalnofix([u
'a', u
'b', u
'c', u
'd'], u
'a//b//c//d', 'split', '//')
172 self
.checkequalnofix([u
'a', u
'b', u
'c', u
'd'], 'a//b//c//d', 'split', u
'//')
173 self
.checkequalnofix([u
'endcase ', u
''], u
'endcase test', 'split', 'test')
176 string_tests
.MixinStrUnicodeUserStringTest
.test_join(self
)
179 self
.checkequalnofix(u
'a b c d', u
' ', 'join', ['a', 'b', u
'c', u
'd'])
180 self
.checkequalnofix(u
'abcd', u
'', 'join', (u
'a', u
'b', u
'c', u
'd'))
181 self
.checkequalnofix(u
'w x y z', u
' ', 'join', string_tests
.Sequence('wxyz'))
182 self
.checkequalnofix(u
'a b c d', ' ', 'join', [u
'a', u
'b', u
'c', u
'd'])
183 self
.checkequalnofix(u
'a b c d', ' ', 'join', ['a', 'b', u
'c', u
'd'])
184 self
.checkequalnofix(u
'abcd', '', 'join', (u
'a', u
'b', u
'c', u
'd'))
185 self
.checkequalnofix(u
'w x y z', ' ', 'join', string_tests
.Sequence(u
'wxyz'))
187 def test_strip(self
):
188 string_tests
.CommonTest
.test_strip(self
)
189 self
.assertRaises(UnicodeError, u
"hello".strip
, "\xff")
191 def test_replace(self
):
192 string_tests
.CommonTest
.test_replace(self
)
194 # method call forwarded from str implementation because of unicode argument
195 self
.checkequalnofix(u
'one@two!three!', 'one!two!three!', 'replace', u
'!', u
'@', 1)
196 self
.assertRaises(TypeError, 'replace'.replace
, u
"r", 42)
198 def test_comparison(self
):
200 self
.assertEqual(u
'abc', 'abc')
201 self
.assertEqual('abc', u
'abc')
202 self
.assertEqual(u
'abc', u
'abc')
203 self
.assertTrue(u
'abcd' > 'abc')
204 self
.assertTrue('abcd' > u
'abc')
205 self
.assertTrue(u
'abcd' > u
'abc')
206 self
.assertTrue(u
'abc' < 'abcd')
207 self
.assertTrue('abc' < u
'abcd')
208 self
.assertTrue(u
'abc' < u
'abcd')
211 # Move these tests to a Unicode collation module test...
212 # Testing UTF-16 code point order comparisons...
214 # No surrogates, no fixup required.
215 self
.assertTrue(u
'\u0061' < u
'\u20ac')
216 # Non surrogate below surrogate value, no fixup required
217 self
.assertTrue(u
'\u0061' < u
'\ud800\udc02')
219 # Non surrogate above surrogate value, fixup required
220 def test_lecmp(s
, s2
):
221 self
.assertTrue(s
< s2
)
257 test_fixup(u
'\ue000')
258 test_fixup(u
'\uff61')
260 # Surrogates on both sides, no fixup required
261 self
.assertTrue(u
'\ud800\udc02' < u
'\ud84d\udc56')
263 def test_islower(self
):
264 string_tests
.MixinStrUnicodeUserStringTest
.test_islower(self
)
265 self
.checkequalnofix(False, u
'\u1FFc', 'islower')
267 def test_isupper(self
):
268 string_tests
.MixinStrUnicodeUserStringTest
.test_isupper(self
)
269 if not sys
.platform
.startswith('java'):
270 self
.checkequalnofix(False, u
'\u1FFc', 'isupper')
272 def test_istitle(self
):
273 string_tests
.MixinStrUnicodeUserStringTest
.test_title(self
)
274 self
.checkequalnofix(True, u
'\u1FFc', 'istitle')
275 self
.checkequalnofix(True, u
'Greek \u1FFcitlecases ...', 'istitle')
277 def test_isspace(self
):
278 string_tests
.MixinStrUnicodeUserStringTest
.test_isspace(self
)
279 self
.checkequalnofix(True, u
'\u2000', 'isspace')
280 self
.checkequalnofix(True, u
'\u200a', 'isspace')
281 self
.checkequalnofix(False, u
'\u2014', 'isspace')
283 def test_isalpha(self
):
284 string_tests
.MixinStrUnicodeUserStringTest
.test_isalpha(self
)
285 self
.checkequalnofix(True, u
'\u1FFc', 'isalpha')
287 def test_isdecimal(self
):
288 self
.checkequalnofix(False, u
'', 'isdecimal')
289 self
.checkequalnofix(False, u
'a', 'isdecimal')
290 self
.checkequalnofix(True, u
'0', 'isdecimal')
291 self
.checkequalnofix(False, u
'\u2460', 'isdecimal') # CIRCLED DIGIT ONE
292 self
.checkequalnofix(False, u
'\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER
293 self
.checkequalnofix(True, u
'\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO
294 self
.checkequalnofix(True, u
'0123456789', 'isdecimal')
295 self
.checkequalnofix(False, u
'0123456789a', 'isdecimal')
297 self
.checkraises(TypeError, 'abc', 'isdecimal', 42)
299 def test_isdigit(self
):
300 string_tests
.MixinStrUnicodeUserStringTest
.test_isdigit(self
)
301 self
.checkequalnofix(True, u
'\u2460', 'isdigit')
302 self
.checkequalnofix(False, u
'\xbc', 'isdigit')
303 self
.checkequalnofix(True, u
'\u0660', 'isdigit')
305 def test_isnumeric(self
):
306 self
.checkequalnofix(False, u
'', 'isnumeric')
307 self
.checkequalnofix(False, u
'a', 'isnumeric')
308 self
.checkequalnofix(True, u
'0', 'isnumeric')
309 self
.checkequalnofix(True, u
'\u2460', 'isnumeric')
310 self
.checkequalnofix(True, u
'\xbc', 'isnumeric')
311 self
.checkequalnofix(True, u
'\u0660', 'isnumeric')
312 self
.checkequalnofix(True, u
'0123456789', 'isnumeric')
313 self
.checkequalnofix(False, u
'0123456789a', 'isnumeric')
315 self
.assertRaises(TypeError, u
"abc".isnumeric
, 42)
317 def test_contains(self
):
318 # Testing Unicode contains method
319 self
.assertTrue('a' in u
'abdb')
320 self
.assertTrue('a' in u
'bdab')
321 self
.assertTrue('a' in u
'bdaba')
322 self
.assertTrue('a' in u
'bdba')
323 self
.assertTrue('a' in u
'bdba')
324 self
.assertTrue(u
'a' in u
'bdba')
325 self
.assertTrue(u
'a' not in u
'bdb')
326 self
.assertTrue(u
'a' not in 'bdb')
327 self
.assertTrue(u
'a' in 'bdba')
328 self
.assertTrue(u
'a' in ('a',1,None))
329 self
.assertTrue(u
'a' in (1,None,'a'))
330 self
.assertTrue(u
'a' in (1,None,u
'a'))
331 self
.assertTrue('a' in ('a',1,None))
332 self
.assertTrue('a' in (1,None,'a'))
333 self
.assertTrue('a' in (1,None,u
'a'))
334 self
.assertTrue('a' not in ('x',1,u
'y'))
335 self
.assertTrue('a' not in ('x',1,None))
336 self
.assertTrue(u
'abcd' not in u
'abcxxxx')
337 self
.assertTrue(u
'ab' in u
'abcd')
338 self
.assertTrue('ab' in u
'abc')
339 self
.assertTrue(u
'ab' in 'abc')
340 self
.assertTrue(u
'ab' in (1,None,u
'ab'))
341 self
.assertTrue(u
'' in u
'abc')
342 self
.assertTrue('' in u
'abc')
344 # If the following fails either
345 # the contains operator does not propagate UnicodeErrors or
346 # someone has changed the default encoding
347 self
.assertRaises(UnicodeError, 'g\xe2teau'.__contains
__, u
'\xe2')
349 self
.assertTrue(u
'' in '')
350 self
.assertTrue('' in u
'')
351 self
.assertTrue(u
'' in u
'')
352 self
.assertTrue(u
'' in 'abc')
353 self
.assertTrue('' in u
'abc')
354 self
.assertTrue(u
'' in u
'abc')
355 self
.assertTrue(u
'\0' not in 'abc')
356 self
.assertTrue('\0' not in u
'abc')
357 self
.assertTrue(u
'\0' not in u
'abc')
358 self
.assertTrue(u
'\0' in '\0abc')
359 self
.assertTrue('\0' in u
'\0abc')
360 self
.assertTrue(u
'\0' in u
'\0abc')
361 self
.assertTrue(u
'\0' in 'abc\0')
362 self
.assertTrue('\0' in u
'abc\0')
363 self
.assertTrue(u
'\0' in u
'abc\0')
364 self
.assertTrue(u
'a' in '\0abc')
365 self
.assertTrue('a' in u
'\0abc')
366 self
.assertTrue(u
'a' in u
'\0abc')
367 self
.assertTrue(u
'asdf' in 'asdf')
368 self
.assertTrue('asdf' in u
'asdf')
369 self
.assertTrue(u
'asdf' in u
'asdf')
370 self
.assertTrue(u
'asdf' not in 'asd')
371 self
.assertTrue('asdf' not in u
'asd')
372 self
.assertTrue(u
'asdf' not in u
'asd')
373 self
.assertTrue(u
'asdf' not in '')
374 self
.assertTrue('asdf' not in u
'')
375 self
.assertTrue(u
'asdf' not in u
'')
377 self
.assertRaises(TypeError, u
"abc".__contains
__)
379 def test_formatting(self
):
380 string_tests
.MixinStrUnicodeUserStringTest
.test_formatting(self
)
381 # Testing Unicode formatting strings...
382 self
.assertEqual(u
"%s, %s" % (u
"abc", "abc"), u
'abc, abc')
383 self
.assertEqual(u
"%s, %s, %i, %f, %5.2f" % (u
"abc", "abc", 1, 2, 3), u
'abc, abc, 1, 2.000000, 3.00')
384 self
.assertEqual(u
"%s, %s, %i, %f, %5.2f" % (u
"abc", "abc", 1, -2, 3), u
'abc, abc, 1, -2.000000, 3.00')
385 self
.assertEqual(u
"%s, %s, %i, %f, %5.2f" % (u
"abc", "abc", -1, -2, 3.5), u
'abc, abc, -1, -2.000000, 3.50')
386 self
.assertEqual(u
"%s, %s, %i, %f, %5.2f" % (u
"abc", "abc", -1, -2, 3.57), u
'abc, abc, -1, -2.000000, 3.57')
387 self
.assertEqual(u
"%s, %s, %i, %f, %5.2f" % (u
"abc", "abc", -1, -2, 1003.57), u
'abc, abc, -1, -2.000000, 1003.57')
388 if not sys
.platform
.startswith('java'):
389 self
.assertEqual(u
"%r, %r" % (u
"abc", "abc"), u
"u'abc', 'abc'")
390 self
.assertEqual(u
"%(x)s, %(y)s" % {'x':u
"abc", 'y':"def"}, u
'abc, def')
391 self
.assertEqual(u
"%(x)s, %(\xfc)s" % {'x':u
"abc", u
'\xfc':"def"}, u
'abc, def')
393 self
.assertEqual(u
'%c' % 0x1234, u
'\u1234')
394 self
.assertRaises(OverflowError, u
"%c".__mod
__, (sys
.maxunicode
+1,))
396 # formatting jobs delegated from the string implementation:
397 self
.assertEqual('...%(foo)s...' % {'foo':u
"abc"}, u
'...abc...')
398 self
.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...')
399 self
.assertEqual('...%(foo)s...' % {u
'foo':"abc"}, '...abc...')
400 self
.assertEqual('...%(foo)s...' % {u
'foo':u
"abc"}, u
'...abc...')
401 self
.assertEqual('...%(foo)s...' % {u
'foo':u
"abc",'def':123}, u
'...abc...')
402 self
.assertEqual('...%(foo)s...' % {u
'foo':u
"abc",u
'def':123}, u
'...abc...')
403 self
.assertEqual('...%s...%s...%s...%s...' % (1,2,3,u
"abc"), u
'...1...2...3...abc...')
404 self
.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,u
"abc"), u
'...%...%s...1...2...3...abc...')
405 self
.assertEqual('...%s...' % u
"abc", u
'...abc...')
406 self
.assertEqual('%*s' % (5,u
'abc',), u
' abc')
407 self
.assertEqual('%*s' % (-5,u
'abc',), u
'abc ')
408 self
.assertEqual('%*.*s' % (5,2,u
'abc',), u
' ab')
409 self
.assertEqual('%*.*s' % (5,3,u
'abc',), u
' abc')
410 self
.assertEqual('%i %*.*s' % (10, 5,3,u
'abc',), u
'10 abc')
411 self
.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, u
'abc',), u
'103 abc')
412 self
.assertEqual('%c' % u
'a', u
'a')
416 self
.assertEqual('%s' % Wrapper(), u
'\u1234')
418 @test_support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
419 def test_format_float(self
):
420 # should not format with a comma, but always with C locale
421 self
.assertEqual(u
'1.0', u
'%.1f' % 1.0)
423 def test_constructor(self
):
424 # unicode(obj) tests (this maps to PyObject_Unicode() at C level)
427 unicode(u
'unicode remains unicode'),
428 u
'unicode remains unicode'
431 class UnicodeSubclass(unicode):
435 unicode(UnicodeSubclass('unicode subclass becomes unicode')),
436 u
'unicode subclass becomes unicode'
440 unicode('strings are converted to unicode'),
441 u
'strings are converted to unicode'
445 def __init__(self
, x
):
447 def __unicode__(self
):
451 unicode(UnicodeCompat('__unicode__ compatible objects are recognized')),
452 u
'__unicode__ compatible objects are recognized')
455 def __init__(self
, x
):
461 unicode(StringCompat('__str__ compatible objects are recognized')),
462 u
'__str__ compatible objects are recognized'
465 # unicode(obj) is compatible to str():
467 o
= StringCompat('unicode(obj) is compatible to str()')
468 self
.assertEqual(unicode(o
), u
'unicode(obj) is compatible to str()')
469 self
.assertEqual(str(o
), 'unicode(obj) is compatible to str()')
471 # %-formatting and .__unicode__()
472 self
.assertEqual(u
'%s' %
473 UnicodeCompat(u
"u'%s' % obj uses obj.__unicode__()"),
474 u
"u'%s' % obj uses obj.__unicode__()")
475 self
.assertEqual(u
'%s' %
476 UnicodeCompat(u
"u'%s' % obj falls back to obj.__str__()"),
477 u
"u'%s' % obj falls back to obj.__str__()")
479 for obj
in (123, 123.45, 123L):
480 self
.assertEqual(unicode(obj
), unicode(str(obj
)))
482 # unicode(obj, encoding, error) tests (this maps to
483 # PyUnicode_FromEncodedObject() at C level)
485 if not sys
.platform
.startswith('java'):
489 u
'decoding unicode is not supported',
495 unicode('strings are decoded to unicode', 'utf-8', 'strict'),
496 u
'strings are decoded to unicode'
499 if not sys
.platform
.startswith('java'):
502 buffer('character buffers are decoded to unicode'),
506 u
'character buffers are decoded to unicode'
509 self
.assertRaises(TypeError, unicode, 42, 42, 42)
511 def test_codecs_utf7(self
):
513 (u
'A\u2262\u0391.', 'A+ImIDkQ.'), # RFC2152 example
514 (u
'Hi Mom -\u263a-!', 'Hi Mom -+Jjo--!'), # RFC2152 example
515 (u
'\u65E5\u672C\u8A9E', '+ZeVnLIqe-'), # RFC2152 example
516 (u
'Item 3 is \u00a31.', 'Item 3 is +AKM-1.'), # RFC2152 example
522 (ur
'\\?', '+AFwAXA?'),
523 (ur
'\\\?', '+AFwAXABc?'),
524 (ur
'++--', '+-+---'),
525 (u
'\U000abcde', '+2m/c3g-'), # surrogate pairs
529 for (x
, y
) in utfTests
:
530 self
.assertEqual(x
.encode('utf-7'), y
)
532 # Unpaired surrogates not supported
533 self
.assertRaises(UnicodeError, unicode, '+3ADYAA-', 'utf-7')
535 self
.assertEqual(unicode('+3ADYAA-', 'utf-7', 'replace'), u
'\ufffd\ufffd')
537 # Direct encoded characters
538 set_d
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?"
539 # Optional direct characters
540 set_o
= '!"#$%&*;<=>@[]^_`{|}'
542 self
.assertEqual(c
.encode('utf7'), c
.encode('ascii'))
543 self
.assertEqual(c
.encode('ascii').decode('utf7'), c
)
545 self
.assertEqual(c
.encode('ascii').decode('utf7'), c
)
547 def test_codecs_utf8(self
):
548 self
.assertEqual(u
''.encode('utf-8'), '')
549 self
.assertEqual(u
'\u20ac'.encode('utf-8'), '\xe2\x82\xac')
550 self
.assertEqual(u
'\ud800\udc02'.encode('utf-8'), '\xf0\x90\x80\x82')
551 self
.assertEqual(u
'\ud84d\udc56'.encode('utf-8'), '\xf0\xa3\x91\x96')
552 self
.assertEqual(u
'\ud800'.encode('utf-8'), '\xed\xa0\x80')
553 self
.assertEqual(u
'\udc00'.encode('utf-8'), '\xed\xb0\x80')
555 (u
'\ud800\udc02'*1000).encode('utf-8'),
556 '\xf0\x90\x80\x82'*1000
559 u
'\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
560 u
'\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
561 u
'\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
562 u
'\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
563 u
'\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das'
564 u
' Nunstuck git und'.encode('utf-8'),
565 '\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81'
566 '\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3'
567 '\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe'
568 '\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83'
569 '\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8'
570 '\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81'
571 '\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81'
572 '\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3'
573 '\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf'
574 '\xe3\x80\x8cWenn ist das Nunstuck git und'
577 # UTF-8 specific decoding tests
578 self
.assertEqual(unicode('\xf0\xa3\x91\x96', 'utf-8'), u
'\U00023456' )
579 self
.assertEqual(unicode('\xf0\x90\x80\x82', 'utf-8'), u
'\U00010002' )
580 self
.assertEqual(unicode('\xe2\x82\xac', 'utf-8'), u
'\u20ac' )
582 # Other possible utf-8 test cases:
583 # * strict decoding testing for all of the
584 # UTF8_ERROR cases in PyUnicode_DecodeUTF8
586 def test_codecs_idna(self
):
587 # Test whether trailing dot is preserved
588 self
.assertEqual(u
"www.python.org.".encode("idna"), "www.python.org.")
590 def test_codecs_errors(self
):
591 # Error handling (encoding)
592 self
.assertRaises(UnicodeError, u
'Andr\202 x'.encode
, 'ascii')
593 self
.assertRaises(UnicodeError, u
'Andr\202 x'.encode
, 'ascii','strict')
594 self
.assertEqual(u
'Andr\202 x'.encode('ascii','ignore'), "Andr x")
595 self
.assertEqual(u
'Andr\202 x'.encode('ascii','replace'), "Andr? x")
596 self
.assertEqual(u
'Andr\202 x'.encode('ascii', 'replace'),
597 u
'Andr\202 x'.encode('ascii', errors
='replace'))
598 self
.assertEqual(u
'Andr\202 x'.encode('ascii', 'ignore'),
599 u
'Andr\202 x'.encode(encoding
='ascii', errors
='ignore'))
601 # Error handling (decoding)
602 self
.assertRaises(UnicodeError, unicode, 'Andr\202 x', 'ascii')
603 self
.assertRaises(UnicodeError, unicode, 'Andr\202 x', 'ascii','strict')
604 self
.assertEqual(unicode('Andr\202 x','ascii','ignore'), u
"Andr x")
605 self
.assertEqual(unicode('Andr\202 x','ascii','replace'), u
'Andr\uFFFD x')
606 self
.assertEqual(u
'abcde'.decode('ascii', 'ignore'),
607 u
'abcde'.decode('ascii', errors
='ignore'))
608 self
.assertEqual(u
'abcde'.decode('ascii', 'replace'),
609 u
'abcde'.decode(encoding
='ascii', errors
='replace'))
611 # Error handling (unknown character names)
612 self
.assertEqual("\\N{foo}xx".decode("unicode-escape", "ignore"), u
"xx")
614 # Error handling (truncated escape sequence)
615 self
.assertRaises(UnicodeError, "\\".decode
, "unicode-escape")
617 self
.assertRaises(TypeError, "hello".decode
, "test.unicode1")
618 self
.assertRaises(TypeError, unicode, "hello", "test.unicode2")
619 self
.assertRaises(TypeError, u
"hello".encode
, "test.unicode1")
620 self
.assertRaises(TypeError, u
"hello".encode
, "test.unicode2")
621 # executes PyUnicode_Encode()
626 "non-existing module",
627 [u
"non-existing dir"]
630 # Error handling (wrong arguments)
631 self
.assertRaises(TypeError, u
"hello".encode
, 42, 42, 42)
633 # Error handling (PyUnicode_EncodeDecimal())
634 self
.assertRaises(UnicodeError, int, u
"\u0200")
636 def test_codecs(self
):
638 self
.assertEqual(u
'hello'.encode('ascii'), 'hello')
639 self
.assertEqual(u
'hello'.encode('utf-7'), 'hello')
640 self
.assertEqual(u
'hello'.encode('utf-8'), 'hello')
641 self
.assertEqual(u
'hello'.encode('utf8'), 'hello')
642 self
.assertEqual(u
'hello'.encode('utf-16-le'), 'h\000e\000l\000l\000o\000')
643 self
.assertEqual(u
'hello'.encode('utf-16-be'), '\000h\000e\000l\000l\000o')
644 self
.assertEqual(u
'hello'.encode('latin-1'), 'hello')
646 # Roundtrip safety for BMP (just the first 1024 chars)
647 for c
in xrange(1024):
649 for encoding
in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le',
650 'utf-16-be', 'raw_unicode_escape',
651 'unicode_escape', 'unicode_internal'):
652 self
.assertEqual(unicode(u
.encode(encoding
),encoding
), u
)
654 # Roundtrip safety for BMP (just the first 256 chars)
655 for c
in xrange(256):
657 for encoding
in ('latin-1',):
658 self
.assertEqual(unicode(u
.encode(encoding
),encoding
), u
)
660 # Roundtrip safety for BMP (just the first 128 chars)
661 for c
in xrange(128):
663 for encoding
in ('ascii',):
664 self
.assertEqual(unicode(u
.encode(encoding
),encoding
), u
)
666 # Roundtrip safety for non-BMP (just a few chars)
667 u
= u
'\U00010001\U00020002\U00030003\U00040004\U00050005'
668 for encoding
in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be',
669 #'raw_unicode_escape',
670 'unicode_escape', 'unicode_internal'):
671 self
.assertEqual(unicode(u
.encode(encoding
),encoding
), u
)
673 # UTF-8 must be roundtrip safe for all UCS-2 code points
674 # This excludes surrogates: in the full range, there would be
675 # a surrogate pair (\udbff\udc00), which gets converted back
676 # to a non-BMP character (\U0010fc00)
677 u
= u
''.join(map(unichr, range(0,0xd800)+range(0xe000,0x10000)))
678 for encoding
in ('utf-8',):
679 self
.assertEqual(unicode(u
.encode(encoding
),encoding
), u
)
681 def test_codecs_charmap(self
):
683 s
= ''.join(map(chr, xrange(128)))
686 'cp437', 'cp500', 'cp737', 'cp775', 'cp850',
687 'cp852', 'cp855', 'cp860', 'cp861', 'cp862',
688 'cp863', 'cp865', 'cp866',
689 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
690 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6',
691 'iso8859_7', 'iso8859_9', 'koi8_r', 'latin_1',
692 'mac_cyrillic', 'mac_latin2',
694 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
695 'cp1256', 'cp1257', 'cp1258',
696 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
698 'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
699 'cp1006', 'iso8859_8',
701 ### These have undefined mappings:
704 ### These fail the round-trip:
708 self
.assertEqual(unicode(s
, encoding
).encode(encoding
), s
)
711 s
= ''.join(map(chr, xrange(128, 256)))
714 'cp437', 'cp500', 'cp737', 'cp775', 'cp850',
715 'cp852', 'cp855', 'cp860', 'cp861', 'cp862',
716 'cp863', 'cp865', 'cp866',
717 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15',
718 'iso8859_2', 'iso8859_4', 'iso8859_5',
719 'iso8859_9', 'koi8_r', 'latin_1',
720 'mac_cyrillic', 'mac_latin2',
722 ### These have undefined mappings:
723 #'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255',
724 #'cp1256', 'cp1257', 'cp1258',
725 #'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874',
726 #'iso8859_3', 'iso8859_6', 'iso8859_7',
727 #'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish',
729 ### These fail the round-trip:
730 #'cp1006', 'cp875', 'iso8859_8',
733 self
.assertEqual(unicode(s
, encoding
).encode(encoding
), s
)
735 def test_concatenation(self
):
736 self
.assertEqual((u
"abc" u
"def"), u
"abcdef")
737 self
.assertEqual(("abc" u
"def"), u
"abcdef")
738 self
.assertEqual((u
"abc" "def"), u
"abcdef")
739 self
.assertEqual((u
"abc" u
"def" "ghi"), u
"abcdefghi")
740 self
.assertEqual(("abc" "def" u
"ghi"), u
"abcdefghi")
742 def test_printing(self
):
744 def write(self
, text
):
749 print >>out
, u
'abc', u
'def'
750 print >>out
, u
'abc', 'def'
751 print >>out
, 'abc', u
'def'
752 print >>out
, u
'abc\n'
753 print >>out
, u
'abc\n',
754 print >>out
, u
'abc\n',
755 print >>out
, u
'def\n'
756 print >>out
, u
'def\n'
760 y
= x
.encode("raw-unicode-escape").decode("raw-unicode-escape")
761 self
.assertEqual(x
, y
)
764 x
= y
.decode("raw-unicode-escape").encode("raw-unicode-escape")
765 self
.assertEqual(x
, y
)
767 x
= y
.decode("raw-unicode-escape").encode("raw-unicode-escape")
768 self
.assertEqual(x
, y
)
771 '\U11111111'.decode("raw-unicode-escape")
772 except UnicodeDecodeError as e
:
773 self
.assertEqual(e
.start
, 0)
774 self
.assertEqual(e
.end
, 10)
776 self
.fail("Should have raised UnicodeDecodeError")
778 def test_conversion(self
):
779 # Make sure __unicode__() works properly
785 def __unicode__(self
):
789 def __unicode__(self
):
793 def __unicode__(self
):
797 def __unicode__(self
):
801 def __unicode__(self
):
808 def __unicode__(self
):
814 def __unicode__(self
):
818 def __new__(cls
, content
=""):
819 return unicode.__new
__(cls
, 2*content
)
820 def __unicode__(self
):
826 def __unicode__(self
):
829 self
.assertEqual(unicode(Foo0()), u
"foo")
830 self
.assertEqual(unicode(Foo1()), u
"foo")
831 self
.assertEqual(unicode(Foo2()), u
"foo")
832 self
.assertEqual(unicode(Foo3()), u
"foo")
833 self
.assertEqual(unicode(Foo4("bar")), u
"foo")
834 self
.assertEqual(unicode(Foo5("bar")), u
"foo")
835 self
.assertEqual(unicode(Foo6("bar")), u
"foou")
836 self
.assertEqual(unicode(Foo7("bar")), u
"foou")
837 self
.assertEqual(unicode(Foo8("foo")), u
"foofoo")
838 self
.assertEqual(str(Foo9("foo")), "string")
839 self
.assertEqual(unicode(Foo9("foo")), u
"not unicode")
841 def test_unicode_repr(self
):
850 self
.assertEqual(repr(s1()), '\\n')
851 self
.assertEqual(repr(s2()), '\\n')
853 def test_expandtabs_overflows_gracefully(self
):
854 # This test only affects 32-bit platforms because expandtabs can only take
855 # an int as the max value, not a 64-bit C long. If expandtabs is changed
856 # to take a 64-bit long, this test should apply to all platforms.
857 if sys
.maxint
> (1 << 32) or struct
.calcsize('P') != 4:
859 self
.assertRaises(OverflowError, u
't\tt\t'.expandtabs
, sys
.maxint
)
861 def test__format__(self
):
862 def test(value
, format
, expected
):
863 # test both with and without the trailing 's'
864 self
.assertEqual(value
.__format
__(format
), expected
)
865 self
.assertEqual(value
.__format
__(format
+ u
's'), expected
)
868 test(u
'abc', u
'', u
'abc')
869 test(u
'abc', u
'.3', u
'abc')
870 test(u
'ab', u
'.3', u
'ab')
871 test(u
'abcdef', u
'.3', u
'abc')
872 test(u
'abcdef', u
'.0', u
'')
873 test(u
'abc', u
'3.3', u
'abc')
874 test(u
'abc', u
'2.3', u
'abc')
875 test(u
'abc', u
'2.2', u
'ab')
876 test(u
'abc', u
'3.2', u
'ab ')
877 test(u
'result', u
'x<0', u
'result')
878 test(u
'result', u
'x<5', u
'result')
879 test(u
'result', u
'x<6', u
'result')
880 test(u
'result', u
'x<7', u
'resultx')
881 test(u
'result', u
'x<8', u
'resultxx')
882 test(u
'result', u
' <7', u
'result ')
883 test(u
'result', u
'<7', u
'result ')
884 test(u
'result', u
'>7', u
' result')
885 test(u
'result', u
'>8', u
' result')
886 test(u
'result', u
'^8', u
' result ')
887 test(u
'result', u
'^9', u
' result ')
888 test(u
'result', u
'^10', u
' result ')
889 test(u
'a', u
'10000', u
'a' + u
' ' * 9999)
890 test(u
'', u
'10000', u
' ' * 10000)
891 test(u
'', u
'10000000', u
' ' * 10000000)
893 # test mixing unicode and str
894 self
.assertEqual(u
'abc'.__format
__('s'), u
'abc')
895 self
.assertEqual(u
'abc'.__format
__('->10s'), u
'-------abc')
897 def test_format(self
):
898 self
.assertEqual(u
''.format(), u
'')
899 self
.assertEqual(u
'a'.format(), u
'a')
900 self
.assertEqual(u
'ab'.format(), u
'ab')
901 self
.assertEqual(u
'a{{'.format(), u
'a{')
902 self
.assertEqual(u
'a}}'.format(), u
'a}')
903 self
.assertEqual(u
'{{b'.format(), u
'{b')
904 self
.assertEqual(u
'}}b'.format(), u
'}b')
905 self
.assertEqual(u
'a{{b'.format(), u
'a{b')
907 # examples from the PEP:
909 self
.assertEqual(u
"My name is {0}".format(u
'Fred'), u
"My name is Fred")
910 self
.assertEqual(u
"My name is {0[name]}".format(dict(name
=u
'Fred')),
912 self
.assertEqual(u
"My name is {0} :-{{}}".format(u
'Fred'),
913 u
"My name is Fred :-{}")
915 # datetime.__format__ doesn't work with unicode
916 #d = datetime.date(2007, 8, 18)
917 #self.assertEqual("The year is {0.year}".format(d),
918 # "The year is 2007")
920 # classes we'll use for testing
922 def __init__(self
, x
=100):
924 def __format__(self
, spec
):
928 def __init__(self
, x
):
930 def __format__(self
, spec
):
933 # class with __str__, but no __format__
935 def __init__(self
, x
):
938 return u
'E(' + self
.x
+ u
')'
940 # class with __repr__, but no __format__ or __str__
942 def __init__(self
, x
):
945 return u
'F(' + self
.x
+ u
')'
947 # class with __format__ that forwards to string, for some format_spec's
949 def __init__(self
, x
):
952 return u
"string is " + self
.x
953 def __format__(self
, format_spec
):
954 if format_spec
== 'd':
955 return u
'G(' + self
.x
+ u
')'
956 return object.__format
__(self
, format_spec
)
958 # class that returns a bad type from __format__
960 def __format__(self
, format_spec
):
963 class I(datetime
.date
):
964 def __format__(self
, format_spec
):
965 return self
.strftime(format_spec
)
968 def __format__(self
, format_spec
):
969 return int.__format
__(self
* 2, format_spec
)
972 self
.assertEqual(u
''.format(), u
'')
973 self
.assertEqual(u
'abc'.format(), u
'abc')
974 self
.assertEqual(u
'{0}'.format(u
'abc'), u
'abc')
975 self
.assertEqual(u
'{0:}'.format(u
'abc'), u
'abc')
976 self
.assertEqual(u
'X{0}'.format(u
'abc'), u
'Xabc')
977 self
.assertEqual(u
'{0}X'.format(u
'abc'), u
'abcX')
978 self
.assertEqual(u
'X{0}Y'.format(u
'abc'), u
'XabcY')
979 self
.assertEqual(u
'{1}'.format(1, u
'abc'), u
'abc')
980 self
.assertEqual(u
'X{1}'.format(1, u
'abc'), u
'Xabc')
981 self
.assertEqual(u
'{1}X'.format(1, u
'abc'), u
'abcX')
982 self
.assertEqual(u
'X{1}Y'.format(1, u
'abc'), u
'XabcY')
983 self
.assertEqual(u
'{0}'.format(-15), u
'-15')
984 self
.assertEqual(u
'{0}{1}'.format(-15, u
'abc'), u
'-15abc')
985 self
.assertEqual(u
'{0}X{1}'.format(-15, u
'abc'), u
'-15Xabc')
986 self
.assertEqual(u
'{{'.format(), u
'{')
987 self
.assertEqual(u
'}}'.format(), u
'}')
988 self
.assertEqual(u
'{{}}'.format(), u
'{}')
989 self
.assertEqual(u
'{{x}}'.format(), u
'{x}')
990 self
.assertEqual(u
'{{{0}}}'.format(123), u
'{123}')
991 self
.assertEqual(u
'{{{{0}}}}'.format(), u
'{{0}}')
992 self
.assertEqual(u
'}}{{'.format(), u
'}{')
993 self
.assertEqual(u
'}}x{{'.format(), u
'}x{')
996 self
.assertEqual(u
"{0[foo-bar]}".format({u
'foo-bar':u
'baz'}), u
'baz')
997 self
.assertEqual(u
"{0[foo bar]}".format({u
'foo bar':u
'baz'}), u
'baz')
998 self
.assertEqual(u
"{0[ ]}".format({u
' ':3}), u
'3')
1000 self
.assertEqual(u
'{foo._x}'.format(foo
=C(20)), u
'20')
1001 self
.assertEqual(u
'{1}{0}'.format(D(10), D(20)), u
'2010')
1002 self
.assertEqual(u
'{0._x.x}'.format(C(D(u
'abc'))), u
'abc')
1003 self
.assertEqual(u
'{0[0]}'.format([u
'abc', u
'def']), u
'abc')
1004 self
.assertEqual(u
'{0[1]}'.format([u
'abc', u
'def']), u
'def')
1005 self
.assertEqual(u
'{0[1][0]}'.format([u
'abc', [u
'def']]), u
'def')
1006 self
.assertEqual(u
'{0[1][0].x}'.format(['abc', [D(u
'def')]]), u
'def')
1009 self
.assertEqual(u
'{0:.3s}'.format(u
'abc'), u
'abc')
1010 self
.assertEqual(u
'{0:.3s}'.format(u
'ab'), u
'ab')
1011 self
.assertEqual(u
'{0:.3s}'.format(u
'abcdef'), u
'abc')
1012 self
.assertEqual(u
'{0:.0s}'.format(u
'abcdef'), u
'')
1013 self
.assertEqual(u
'{0:3.3s}'.format(u
'abc'), u
'abc')
1014 self
.assertEqual(u
'{0:2.3s}'.format(u
'abc'), u
'abc')
1015 self
.assertEqual(u
'{0:2.2s}'.format(u
'abc'), u
'ab')
1016 self
.assertEqual(u
'{0:3.2s}'.format(u
'abc'), u
'ab ')
1017 self
.assertEqual(u
'{0:x<0s}'.format(u
'result'), u
'result')
1018 self
.assertEqual(u
'{0:x<5s}'.format(u
'result'), u
'result')
1019 self
.assertEqual(u
'{0:x<6s}'.format(u
'result'), u
'result')
1020 self
.assertEqual(u
'{0:x<7s}'.format(u
'result'), u
'resultx')
1021 self
.assertEqual(u
'{0:x<8s}'.format(u
'result'), u
'resultxx')
1022 self
.assertEqual(u
'{0: <7s}'.format(u
'result'), u
'result ')
1023 self
.assertEqual(u
'{0:<7s}'.format(u
'result'), u
'result ')
1024 self
.assertEqual(u
'{0:>7s}'.format(u
'result'), u
' result')
1025 self
.assertEqual(u
'{0:>8s}'.format(u
'result'), u
' result')
1026 self
.assertEqual(u
'{0:^8s}'.format(u
'result'), u
' result ')
1027 self
.assertEqual(u
'{0:^9s}'.format(u
'result'), u
' result ')
1028 self
.assertEqual(u
'{0:^10s}'.format(u
'result'), u
' result ')
1029 self
.assertEqual(u
'{0:10000}'.format(u
'a'), u
'a' + u
' ' * 9999)
1030 self
.assertEqual(u
'{0:10000}'.format(u
''), u
' ' * 10000)
1031 self
.assertEqual(u
'{0:10000000}'.format(u
''), u
' ' * 10000000)
1033 # format specifiers for user defined type
1034 self
.assertEqual(u
'{0:abc}'.format(C()), u
'abc')
1036 # !r and !s coersions
1037 self
.assertEqual(u
'{0!s}'.format(u
'Hello'), u
'Hello')
1038 self
.assertEqual(u
'{0!s:}'.format(u
'Hello'), u
'Hello')
1039 self
.assertEqual(u
'{0!s:15}'.format(u
'Hello'), u
'Hello ')
1040 self
.assertEqual(u
'{0!s:15s}'.format(u
'Hello'), u
'Hello ')
1041 self
.assertEqual(u
'{0!r}'.format(u
'Hello'), u
"u'Hello'")
1042 self
.assertEqual(u
'{0!r:}'.format(u
'Hello'), u
"u'Hello'")
1043 self
.assertEqual(u
'{0!r}'.format(F(u
'Hello')), u
'F(Hello)')
1045 # test fallback to object.__format__
1046 self
.assertEqual(u
'{0}'.format({}), u
'{}')
1047 self
.assertEqual(u
'{0}'.format([]), u
'[]')
1048 self
.assertEqual(u
'{0}'.format([1]), u
'[1]')
1049 self
.assertEqual(u
'{0}'.format(E(u
'data')), u
'E(data)')
1050 self
.assertEqual(u
'{0:^10}'.format(E(u
'data')), u
' E(data) ')
1051 self
.assertEqual(u
'{0:^10s}'.format(E(u
'data')), u
' E(data) ')
1052 self
.assertEqual(u
'{0:d}'.format(G(u
'data')), u
'G(data)')
1053 self
.assertEqual(u
'{0:>15s}'.format(G(u
'data')), u
' string is data')
1054 self
.assertEqual(u
'{0!s}'.format(G(u
'data')), u
'string is data')
1056 self
.assertEqual(u
"{0:date: %Y-%m-%d}".format(I(year
=2007,
1059 u
"date: 2007-08-27")
1061 # test deriving from a builtin type and overriding __format__
1062 self
.assertEqual(u
"{0}".format(J(10)), u
"20")
1065 # string format specifiers
1066 self
.assertEqual(u
'{0:}'.format('a'), u
'a')
1068 # computed format specifiers
1069 self
.assertEqual(u
"{0:.{1}}".format(u
'hello world', 5), u
'hello')
1070 self
.assertEqual(u
"{0:.{1}s}".format(u
'hello world', 5), u
'hello')
1071 self
.assertEqual(u
"{0:.{precision}s}".format('hello world', precision
=5), u
'hello')
1072 self
.assertEqual(u
"{0:{width}.{precision}s}".format('hello world', width
=10, precision
=5), u
'hello ')
1073 self
.assertEqual(u
"{0:{width}.{precision}s}".format('hello world', width
='10', precision
='5'), u
'hello ')
1075 # test various errors
1076 self
.assertRaises(ValueError, u
'{'.format
)
1077 self
.assertRaises(ValueError, u
'}'.format
)
1078 self
.assertRaises(ValueError, u
'a{'.format
)
1079 self
.assertRaises(ValueError, u
'a}'.format
)
1080 self
.assertRaises(ValueError, u
'{a'.format
)
1081 self
.assertRaises(ValueError, u
'}a'.format
)
1082 self
.assertRaises(IndexError, u
'{0}'.format
)
1083 self
.assertRaises(IndexError, u
'{1}'.format
, u
'abc')
1084 self
.assertRaises(KeyError, u
'{x}'.format
)
1085 self
.assertRaises(ValueError, u
"}{".format
)
1086 self
.assertRaises(ValueError, u
"{".format
)
1087 self
.assertRaises(ValueError, u
"}".format
)
1088 self
.assertRaises(ValueError, u
"abc{0:{}".format
)
1089 self
.assertRaises(ValueError, u
"{0".format
)
1090 self
.assertRaises(IndexError, u
"{0.}".format
)
1091 self
.assertRaises(ValueError, u
"{0.}".format
, 0)
1092 self
.assertRaises(IndexError, u
"{0[}".format
)
1093 self
.assertRaises(ValueError, u
"{0[}".format
, [])
1094 self
.assertRaises(KeyError, u
"{0]}".format
)
1095 self
.assertRaises(ValueError, u
"{0.[]}".format
, 0)
1096 self
.assertRaises(ValueError, u
"{0..foo}".format
, 0)
1097 self
.assertRaises(ValueError, u
"{0[0}".format
, 0)
1098 self
.assertRaises(ValueError, u
"{0[0:foo}".format
, 0)
1099 self
.assertRaises(KeyError, u
"{c]}".format
)
1100 self
.assertRaises(ValueError, u
"{{ {{{0}}".format
, 0)
1101 self
.assertRaises(ValueError, u
"{0}}".format
, 0)
1102 self
.assertRaises(KeyError, u
"{foo}".format
, bar
=3)
1103 self
.assertRaises(ValueError, u
"{0!x}".format
, 3)
1104 self
.assertRaises(ValueError, u
"{0!}".format
, 0)
1105 self
.assertRaises(ValueError, u
"{0!rs}".format
, 0)
1106 self
.assertRaises(ValueError, u
"{!}".format
)
1107 self
.assertRaises(IndexError, u
"{:}".format
)
1108 self
.assertRaises(IndexError, u
"{:s}".format
)
1109 self
.assertRaises(IndexError, u
"{}".format
)
1112 self
.assertRaises(ValueError, u
"{0[0]x}".format
, [None])
1113 self
.assertRaises(ValueError, u
"{0[0](10)}".format
, [None])
1115 # can't have a replacement on the field name portion
1116 self
.assertRaises(TypeError, u
'{0[{1}]}'.format
, u
'abcdefg', 4)
1118 # exceed maximum recursion depth
1119 self
.assertRaises(ValueError, u
"{0:{1:{2}}}".format
, u
'abc', u
's', u
'')
1120 self
.assertRaises(ValueError, u
"{0:{1:{2:{3:{4:{5:{6}}}}}}}".format
,
1121 0, 1, 2, 3, 4, 5, 6, 7)
1123 # string format spec errors
1124 self
.assertRaises(ValueError, u
"{0:-s}".format
, u
'')
1125 self
.assertRaises(ValueError, format
, u
"", u
"-")
1126 self
.assertRaises(ValueError, u
"{0:=s}".format
, u
'')
1128 # test combining string and unicode
1129 self
.assertEqual(u
"foo{0}".format('bar'), u
'foobar')
1130 # This will try to convert the argument from unicode to str, which
1132 self
.assertEqual("foo{0}".format(u
'bar'), 'foobar')
1133 # This will try to convert the argument from unicode to str, which
1135 self
.assertRaises(UnicodeEncodeError, "foo{0}".format
, u
'\u1000bar')
1137 def test_format_auto_numbering(self
):
1139 def __init__(self
, x
=100):
1141 def __format__(self
, spec
):
1144 self
.assertEqual(u
'{}'.format(10), u
'10')
1145 self
.assertEqual(u
'{:5}'.format('s'), u
's ')
1146 self
.assertEqual(u
'{!r}'.format('s'), u
"'s'")
1147 self
.assertEqual(u
'{._x}'.format(C(10)), u
'10')
1148 self
.assertEqual(u
'{[1]}'.format([1, 2]), u
'2')
1149 self
.assertEqual(u
'{[a]}'.format({'a':4, 'b':2}), u
'4')
1150 self
.assertEqual(u
'a{}b{}c'.format(0, 1), u
'a0b1c')
1152 self
.assertEqual(u
'a{:{}}b'.format('x', '^10'), u
'a x b')
1153 self
.assertEqual(u
'a{:{}x}b'.format(20, '#'), u
'a0x14b')
1155 # can't mix and match numbering and auto-numbering
1156 self
.assertRaises(ValueError, u
'{}{1}'.format
, 1, 2)
1157 self
.assertRaises(ValueError, u
'{1}{}'.format
, 1, 2)
1158 self
.assertRaises(ValueError, u
'{:{1}}'.format
, 1, 2)
1159 self
.assertRaises(ValueError, u
'{0:{}}'.format
, 1, 2)
1161 # can mix and match auto-numbering and named
1162 self
.assertEqual(u
'{f}{}'.format(4, f
='test'), u
'test4')
1163 self
.assertEqual(u
'{}{f}'.format(4, f
='test'), u
'4test')
1164 self
.assertEqual(u
'{:{f}}{g}{}'.format(1, 3, g
='g', f
=2), u
' 1g3')
1165 self
.assertEqual(u
'{f:{}}{}{g}'.format(2, 4, f
=1, g
='g'), u
' 14g')
1167 def test_raiseMemError(self
):
1168 # Ensure that the freelist contains a consistent object, even
1169 # when a string allocation fails with a MemoryError.
1170 # This used to crash the interpreter,
1171 # or leak references when the number was smaller.
1172 charwidth
= 4 if sys
.maxunicode
>= 0x10000 else 2
1173 # Note: sys.maxsize is half of the actual max allocation because of
1174 # the signedness of Py_ssize_t.
1175 alloc
= lambda: u
"a" * (sys
.maxsize
// charwidth
* 2)
1176 self
.assertRaises(MemoryError, alloc
)
1177 self
.assertRaises(MemoryError, alloc
)
1180 test_support
.run_unittest(__name__
)
1182 if __name__
== "__main__":