Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 1 | """ Test script for the Unicode implementation. |
| 2 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 3 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 4 | |
| 5 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 6 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 7 | """#" |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8 | import codecs |
| 9 | import struct |
| 10 | import sys |
| 11 | import unittest |
| 12 | import warnings |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 13 | from test import support, string_tests |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 14 | |
Neal Norwitz | 430f68b | 2005-11-24 22:00:56 +0000 | [diff] [blame] | 15 | # Error handling (bad decoder return) |
| 16 | def search_function(encoding): |
| 17 | def decode1(input, errors="strict"): |
| 18 | return 42 # not a tuple |
| 19 | def encode1(input, errors="strict"): |
| 20 | return 42 # not a tuple |
| 21 | def encode2(input, errors="strict"): |
| 22 | return (42, 42) # no unicode |
| 23 | def decode2(input, errors="strict"): |
| 24 | return (42, 42) # no unicode |
| 25 | if encoding=="test.unicode1": |
| 26 | return (encode1, decode1, None, None) |
| 27 | elif encoding=="test.unicode2": |
| 28 | return (encode2, decode2, None, None) |
| 29 | else: |
| 30 | return None |
| 31 | codecs.register(search_function) |
| 32 | |
Brett Cannon | 226b230 | 2010-03-20 22:22:22 +0000 | [diff] [blame] | 33 | class UnicodeTest(string_tests.CommonTest, |
| 34 | string_tests.MixinStrUnicodeUserStringTest, |
| 35 | string_tests.MixinStrUnicodeTest): |
| 36 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 37 | type2test = str |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 38 | |
| 39 | def checkequalnofix(self, result, object, methodname, *args): |
| 40 | method = getattr(object, methodname) |
| 41 | realresult = method(*args) |
| 42 | self.assertEqual(realresult, result) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 43 | self.assertTrue(type(realresult) is type(result)) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 44 | |
| 45 | # if the original is returned make sure that |
| 46 | # this doesn't happen with subclasses |
| 47 | if realresult is object: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 48 | class usub(str): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 49 | def __repr__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 50 | return 'usub(%r)' % str.__repr__(self) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 51 | object = usub(object) |
| 52 | method = getattr(object, methodname) |
| 53 | realresult = method(*args) |
| 54 | self.assertEqual(realresult, result) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 55 | self.assertTrue(object is not realresult) |
Guido van Rossum | e4874ae | 2001-09-21 15:36:41 +0000 | [diff] [blame] | 56 | |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 57 | def test_literals(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 58 | self.assertEqual('\xff', '\u00ff') |
| 59 | self.assertEqual('\uffff', '\U0000ffff') |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 60 | self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'') |
| 61 | self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'') |
| 62 | self.assertRaises(SyntaxError, eval, '\'\\U%08x\'' % 0x110000) |
Benjamin Peterson | cd76c27 | 2008-04-05 15:09:30 +0000 | [diff] [blame] | 63 | # raw strings should not have unicode escapes |
Florent Xicluna | a87b383 | 2010-09-13 02:28:18 +0000 | [diff] [blame] | 64 | self.assertNotEqual(r"\u0020", " ") |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 65 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 66 | def test_ascii(self): |
| 67 | if not sys.platform.startswith('java'): |
| 68 | # Test basic sanity of repr() |
| 69 | self.assertEqual(ascii('abc'), "'abc'") |
| 70 | self.assertEqual(ascii('ab\\c'), "'ab\\\\c'") |
| 71 | self.assertEqual(ascii('ab\\'), "'ab\\\\'") |
| 72 | self.assertEqual(ascii('\\c'), "'\\\\c'") |
| 73 | self.assertEqual(ascii('\\'), "'\\\\'") |
| 74 | self.assertEqual(ascii('\n'), "'\\n'") |
| 75 | self.assertEqual(ascii('\r'), "'\\r'") |
| 76 | self.assertEqual(ascii('\t'), "'\\t'") |
| 77 | self.assertEqual(ascii('\b'), "'\\x08'") |
| 78 | self.assertEqual(ascii("'\""), """'\\'"'""") |
| 79 | self.assertEqual(ascii("'\""), """'\\'"'""") |
| 80 | self.assertEqual(ascii("'"), '''"'"''') |
| 81 | self.assertEqual(ascii('"'), """'"'""") |
| 82 | latin1repr = ( |
| 83 | "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r" |
| 84 | "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a" |
| 85 | "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI" |
| 86 | "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f" |
| 87 | "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d" |
| 88 | "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b" |
| 89 | "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9" |
| 90 | "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7" |
| 91 | "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5" |
| 92 | "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3" |
| 93 | "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1" |
| 94 | "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef" |
| 95 | "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd" |
| 96 | "\\xfe\\xff'") |
| 97 | testrepr = ascii(''.join(map(chr, range(256)))) |
| 98 | self.assertEqual(testrepr, latin1repr) |
| 99 | # Test ascii works on wide unicode escapes without overflow. |
| 100 | self.assertEqual(ascii("\U00010000" * 39 + "\uffff" * 4096), |
| 101 | ascii("\U00010000" * 39 + "\uffff" * 4096)) |
| 102 | |
| 103 | class WrongRepr: |
| 104 | def __repr__(self): |
| 105 | return b'byte-repr' |
| 106 | self.assertRaises(TypeError, ascii, WrongRepr()) |
| 107 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 108 | def test_repr(self): |
| 109 | if not sys.platform.startswith('java'): |
| 110 | # Test basic sanity of repr() |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 111 | self.assertEqual(repr('abc'), "'abc'") |
| 112 | self.assertEqual(repr('ab\\c'), "'ab\\\\c'") |
| 113 | self.assertEqual(repr('ab\\'), "'ab\\\\'") |
| 114 | self.assertEqual(repr('\\c'), "'\\\\c'") |
| 115 | self.assertEqual(repr('\\'), "'\\\\'") |
| 116 | self.assertEqual(repr('\n'), "'\\n'") |
| 117 | self.assertEqual(repr('\r'), "'\\r'") |
| 118 | self.assertEqual(repr('\t'), "'\\t'") |
| 119 | self.assertEqual(repr('\b'), "'\\x08'") |
| 120 | self.assertEqual(repr("'\""), """'\\'"'""") |
| 121 | self.assertEqual(repr("'\""), """'\\'"'""") |
| 122 | self.assertEqual(repr("'"), '''"'"''') |
| 123 | self.assertEqual(repr('"'), """'"'""") |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 124 | latin1repr = ( |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 125 | "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r" |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 126 | "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a" |
| 127 | "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI" |
| 128 | "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f" |
| 129 | "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d" |
| 130 | "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b" |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 131 | "\\x9c\\x9d\\x9e\\x9f\\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9" |
| 132 | "\xaa\xab\xac\\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7" |
| 133 | "\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5" |
| 134 | "\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3" |
| 135 | "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1" |
| 136 | "\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" |
| 137 | "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd" |
| 138 | "\xfe\xff'") |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 139 | testrepr = repr(''.join(map(chr, range(256)))) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 140 | self.assertEqual(testrepr, latin1repr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 141 | # Test repr works on wide unicode escapes without overflow. |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 142 | self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096), |
| 143 | repr("\U00010000" * 39 + "\uffff" * 4096)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 144 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 145 | class WrongRepr: |
| 146 | def __repr__(self): |
| 147 | return b'byte-repr' |
| 148 | self.assertRaises(TypeError, repr, WrongRepr()) |
| 149 | |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 150 | def test_iterators(self): |
| 151 | # Make sure unicode objects have an __iter__ method |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 152 | it = "\u1111\u2222\u3333".__iter__() |
| 153 | self.assertEqual(next(it), "\u1111") |
| 154 | self.assertEqual(next(it), "\u2222") |
| 155 | self.assertEqual(next(it), "\u3333") |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 156 | self.assertRaises(StopIteration, next, it) |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 157 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 158 | def test_count(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 159 | string_tests.CommonTest.test_count(self) |
| 160 | # check mixed argument types |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 161 | self.checkequalnofix(3, 'aaa', 'count', 'a') |
| 162 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 163 | self.checkequalnofix(3, 'aaa', 'count', 'a') |
| 164 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 165 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 166 | self.checkequalnofix(1, 'aaa', 'count', 'a', -1) |
| 167 | self.checkequalnofix(3, 'aaa', 'count', 'a', -10) |
| 168 | self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1) |
| 169 | self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 170 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 171 | def test_find(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 172 | self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc') |
| 173 | self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1) |
| 174 | self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 176 | self.assertRaises(TypeError, 'hello'.find) |
| 177 | self.assertRaises(TypeError, 'hello'.find, 42) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 178 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 179 | def test_rfind(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 180 | string_tests.CommonTest.test_rfind(self) |
| 181 | # check mixed argument types |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 182 | self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc') |
| 183 | self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') |
| 184 | self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') |
Guido van Rossum | 8b26454 | 2000-12-19 02:22:31 +0000 | [diff] [blame] | 185 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 186 | def test_index(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 187 | string_tests.CommonTest.test_index(self) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 188 | self.checkequalnofix(0, 'abcdefghiabc', 'index', '') |
| 189 | self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def') |
| 190 | self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc') |
| 191 | self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1) |
| 192 | self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib') |
| 193 | self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1) |
| 194 | self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8) |
| 195 | self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 196 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 197 | def test_rindex(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 198 | string_tests.CommonTest.test_rindex(self) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 199 | self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '') |
| 200 | self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def') |
| 201 | self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc') |
| 202 | self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 203 | |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 204 | self.assertRaises(ValueError, 'abcdefghiabc'.rindex, 'hib') |
| 205 | self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1) |
| 206 | self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1) |
| 207 | self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8) |
| 208 | self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 209 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 210 | def test_maketrans_translate(self): |
| 211 | # these work with plain translate() |
| 212 | self.checkequalnofix('bbbc', 'abababc', 'translate', |
| 213 | {ord('a'): None}) |
| 214 | self.checkequalnofix('iiic', 'abababc', 'translate', |
| 215 | {ord('a'): None, ord('b'): ord('i')}) |
| 216 | self.checkequalnofix('iiix', 'abababc', 'translate', |
| 217 | {ord('a'): None, ord('b'): ord('i'), ord('c'): 'x'}) |
| 218 | self.checkequalnofix('c', 'abababc', 'translate', |
| 219 | {ord('a'): None, ord('b'): ''}) |
| 220 | self.checkequalnofix('xyyx', 'xzx', 'translate', |
| 221 | {ord('z'): 'yy'}) |
| 222 | # this needs maketrans() |
| 223 | self.checkequalnofix('abababc', 'abababc', 'translate', |
| 224 | {'b': '<i>'}) |
| 225 | tbl = self.type2test.maketrans({'a': None, 'b': '<i>'}) |
| 226 | self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl) |
| 227 | # test alternative way of calling maketrans() |
| 228 | tbl = self.type2test.maketrans('abc', 'xyz', 'd') |
| 229 | self.checkequalnofix('xyzzy', 'abdcdcbdddd', 'translate', tbl) |
| 230 | |
| 231 | self.assertRaises(TypeError, self.type2test.maketrans) |
| 232 | self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg') |
| 233 | self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def') |
| 234 | self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 2) |
| 235 | self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def', 2) |
| 236 | self.assertRaises(ValueError, self.type2test.maketrans, {'xy': 2}) |
| 237 | self.assertRaises(TypeError, self.type2test.maketrans, {(1,): 2}) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 239 | self.assertRaises(TypeError, 'hello'.translate) |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 240 | self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 241 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 242 | def test_split(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 243 | string_tests.CommonTest.test_split(self) |
Andrew M. Kuchling | eddd68d | 2002-03-29 16:21:44 +0000 | [diff] [blame] | 244 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 245 | # Mixed arguments |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 246 | self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
| 247 | self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
| 248 | self.checkequalnofix(['endcase ', ''], 'endcase test', 'split', 'test') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 249 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 250 | def test_join(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 251 | string_tests.MixinStrUnicodeUserStringTest.test_join(self) |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 253 | class MyWrapper: |
| 254 | def __init__(self, sval): self.sval = sval |
| 255 | def __str__(self): return self.sval |
| 256 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 257 | # mixed arguments |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 258 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 259 | self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
| 260 | self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz')) |
| 261 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 262 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 263 | self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
| 264 | self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz')) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 265 | self.checkraises(TypeError, ' ', 'join', ['1', '2', MyWrapper('foo')]) |
| 266 | self.checkraises(TypeError, ' ', 'join', ['1', '2', '3', bytes()]) |
| 267 | self.checkraises(TypeError, ' ', 'join', [1, 2, 3]) |
| 268 | self.checkraises(TypeError, ' ', 'join', ['1', '2', 3]) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 269 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 270 | def test_replace(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 271 | string_tests.CommonTest.test_replace(self) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 272 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 273 | # method call forwarded from str implementation because of unicode argument |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 274 | self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1) |
| 275 | self.assertRaises(TypeError, 'replace'.replace, "r", 42) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 277 | def test_bytes_comparison(self): |
Brett Cannon | 226b230 | 2010-03-20 22:22:22 +0000 | [diff] [blame] | 278 | with support.check_warnings(): |
| 279 | warnings.simplefilter('ignore', BytesWarning) |
| 280 | self.assertEqual('abc' == b'abc', False) |
| 281 | self.assertEqual('abc' != b'abc', True) |
| 282 | self.assertEqual('abc' == bytearray(b'abc'), False) |
| 283 | self.assertEqual('abc' != bytearray(b'abc'), True) |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 284 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 285 | def test_comparison(self): |
| 286 | # Comparisons: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 287 | self.assertEqual('abc', 'abc') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 288 | self.assertTrue('abcd' > 'abc') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 289 | self.assertTrue('abc' < 'abcd') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 290 | |
| 291 | if 0: |
| 292 | # Move these tests to a Unicode collation module test... |
| 293 | # Testing UTF-16 code point order comparisons... |
| 294 | |
| 295 | # No surrogates, no fixup required. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 296 | self.assertTrue('\u0061' < '\u20ac') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 297 | # Non surrogate below surrogate value, no fixup required |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 298 | self.assertTrue('\u0061' < '\ud800\udc02') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 299 | |
| 300 | # Non surrogate above surrogate value, fixup required |
| 301 | def test_lecmp(s, s2): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 302 | self.assertTrue(s < s2) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 303 | |
| 304 | def test_fixup(s): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 305 | s2 = '\ud800\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 306 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 307 | s2 = '\ud900\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 308 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 309 | s2 = '\uda00\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 310 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 311 | s2 = '\udb00\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 312 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 313 | s2 = '\ud800\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 314 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 315 | s2 = '\ud900\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 316 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 317 | s2 = '\uda00\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 318 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 319 | s2 = '\udb00\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 320 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 321 | s2 = '\ud800\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 322 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 323 | s2 = '\ud900\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 324 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 325 | s2 = '\uda00\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 326 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 327 | s2 = '\udb00\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 328 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 329 | s2 = '\ud800\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 330 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 331 | s2 = '\ud900\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 332 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 333 | s2 = '\uda00\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 334 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 335 | s2 = '\udb00\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 336 | test_lecmp(s, s2) |
| 337 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 338 | test_fixup('\ue000') |
| 339 | test_fixup('\uff61') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 340 | |
| 341 | # Surrogates on both sides, no fixup required |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 342 | self.assertTrue('\ud800\udc02' < '\ud84d\udc56') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 343 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 344 | def test_islower(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 345 | string_tests.MixinStrUnicodeUserStringTest.test_islower(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 346 | self.checkequalnofix(False, '\u1FFc', 'islower') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 347 | |
| 348 | def test_isupper(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 349 | string_tests.MixinStrUnicodeUserStringTest.test_isupper(self) |
| 350 | if not sys.platform.startswith('java'): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 351 | self.checkequalnofix(False, '\u1FFc', 'isupper') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 352 | |
| 353 | def test_istitle(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 354 | string_tests.MixinStrUnicodeUserStringTest.test_title(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 355 | self.checkequalnofix(True, '\u1FFc', 'istitle') |
| 356 | self.checkequalnofix(True, 'Greek \u1FFcitlecases ...', 'istitle') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 357 | |
| 358 | def test_isspace(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 359 | string_tests.MixinStrUnicodeUserStringTest.test_isspace(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 360 | self.checkequalnofix(True, '\u2000', 'isspace') |
| 361 | self.checkequalnofix(True, '\u200a', 'isspace') |
| 362 | self.checkequalnofix(False, '\u2014', 'isspace') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 363 | |
| 364 | def test_isalpha(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 365 | string_tests.MixinStrUnicodeUserStringTest.test_isalpha(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 366 | self.checkequalnofix(True, '\u1FFc', 'isalpha') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 367 | |
| 368 | def test_isdecimal(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 369 | self.checkequalnofix(False, '', 'isdecimal') |
| 370 | self.checkequalnofix(False, 'a', 'isdecimal') |
| 371 | self.checkequalnofix(True, '0', 'isdecimal') |
| 372 | self.checkequalnofix(False, '\u2460', 'isdecimal') # CIRCLED DIGIT ONE |
| 373 | self.checkequalnofix(False, '\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER |
| 374 | self.checkequalnofix(True, '\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO |
| 375 | self.checkequalnofix(True, '0123456789', 'isdecimal') |
| 376 | self.checkequalnofix(False, '0123456789a', 'isdecimal') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 377 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 378 | self.checkraises(TypeError, 'abc', 'isdecimal', 42) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 379 | |
| 380 | def test_isdigit(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 381 | string_tests.MixinStrUnicodeUserStringTest.test_isdigit(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 382 | self.checkequalnofix(True, '\u2460', 'isdigit') |
| 383 | self.checkequalnofix(False, '\xbc', 'isdigit') |
| 384 | self.checkequalnofix(True, '\u0660', 'isdigit') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 385 | |
| 386 | def test_isnumeric(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 387 | self.checkequalnofix(False, '', 'isnumeric') |
| 388 | self.checkequalnofix(False, 'a', 'isnumeric') |
| 389 | self.checkequalnofix(True, '0', 'isnumeric') |
| 390 | self.checkequalnofix(True, '\u2460', 'isnumeric') |
| 391 | self.checkequalnofix(True, '\xbc', 'isnumeric') |
| 392 | self.checkequalnofix(True, '\u0660', 'isnumeric') |
| 393 | self.checkequalnofix(True, '0123456789', 'isnumeric') |
| 394 | self.checkequalnofix(False, '0123456789a', 'isnumeric') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 395 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 396 | self.assertRaises(TypeError, "abc".isnumeric, 42) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 397 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 398 | def test_isidentifier(self): |
| 399 | self.assertTrue("a".isidentifier()) |
| 400 | self.assertTrue("Z".isidentifier()) |
| 401 | self.assertTrue("_".isidentifier()) |
| 402 | self.assertTrue("b0".isidentifier()) |
| 403 | self.assertTrue("bc".isidentifier()) |
| 404 | self.assertTrue("b_".isidentifier()) |
Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 405 | self.assertTrue("µ".isidentifier()) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 406 | |
| 407 | self.assertFalse(" ".isidentifier()) |
| 408 | self.assertFalse("[".isidentifier()) |
Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 409 | self.assertFalse("©".isidentifier()) |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 410 | self.assertFalse("0".isidentifier()) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 411 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 412 | def test_isprintable(self): |
| 413 | self.assertTrue("".isprintable()) |
Benjamin Peterson | 0983274 | 2009-03-26 17:15:46 +0000 | [diff] [blame] | 414 | self.assertTrue(" ".isprintable()) |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 415 | self.assertTrue("abcdefg".isprintable()) |
| 416 | self.assertFalse("abcdefg\n".isprintable()) |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 417 | # some defined Unicode character |
| 418 | self.assertTrue("\u0374".isprintable()) |
| 419 | # undefined character |
Amaury Forgeot d'Arc | a083f1e | 2008-09-10 23:51:42 +0000 | [diff] [blame] | 420 | self.assertFalse("\u0378".isprintable()) |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 421 | # single surrogate character |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 422 | self.assertFalse("\ud800".isprintable()) |
| 423 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 424 | def test_contains(self): |
| 425 | # Testing Unicode contains method |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 426 | self.assertIn('a', 'abdb') |
| 427 | self.assertIn('a', 'bdab') |
| 428 | self.assertIn('a', 'bdaba') |
| 429 | self.assertIn('a', 'bdba') |
| 430 | self.assertNotIn('a', 'bdb') |
| 431 | self.assertIn('a', 'bdba') |
| 432 | self.assertIn('a', ('a',1,None)) |
| 433 | self.assertIn('a', (1,None,'a')) |
| 434 | self.assertIn('a', ('a',1,None)) |
| 435 | self.assertIn('a', (1,None,'a')) |
| 436 | self.assertNotIn('a', ('x',1,'y')) |
| 437 | self.assertNotIn('a', ('x',1,None)) |
| 438 | self.assertNotIn('abcd', 'abcxxxx') |
| 439 | self.assertIn('ab', 'abcd') |
| 440 | self.assertIn('ab', 'abc') |
| 441 | self.assertIn('ab', (1,None,'ab')) |
| 442 | self.assertIn('', 'abc') |
| 443 | self.assertIn('', '') |
| 444 | self.assertIn('', 'abc') |
| 445 | self.assertNotIn('\0', 'abc') |
| 446 | self.assertIn('\0', '\0abc') |
| 447 | self.assertIn('\0', 'abc\0') |
| 448 | self.assertIn('a', '\0abc') |
| 449 | self.assertIn('asdf', 'asdf') |
| 450 | self.assertNotIn('asdf', 'asd') |
| 451 | self.assertNotIn('asdf', '') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 452 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 453 | self.assertRaises(TypeError, "abc".__contains__) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 454 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 455 | def test_format(self): |
| 456 | self.assertEqual(''.format(), '') |
| 457 | self.assertEqual('a'.format(), 'a') |
| 458 | self.assertEqual('ab'.format(), 'ab') |
| 459 | self.assertEqual('a{{'.format(), 'a{') |
| 460 | self.assertEqual('a}}'.format(), 'a}') |
| 461 | self.assertEqual('{{b'.format(), '{b') |
| 462 | self.assertEqual('}}b'.format(), '}b') |
| 463 | self.assertEqual('a{{b'.format(), 'a{b') |
| 464 | |
| 465 | # examples from the PEP: |
| 466 | import datetime |
| 467 | self.assertEqual("My name is {0}".format('Fred'), "My name is Fred") |
| 468 | self.assertEqual("My name is {0[name]}".format(dict(name='Fred')), |
| 469 | "My name is Fred") |
| 470 | self.assertEqual("My name is {0} :-{{}}".format('Fred'), |
| 471 | "My name is Fred :-{}") |
| 472 | |
| 473 | d = datetime.date(2007, 8, 18) |
| 474 | self.assertEqual("The year is {0.year}".format(d), |
| 475 | "The year is 2007") |
| 476 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 477 | # classes we'll use for testing |
| 478 | class C: |
| 479 | def __init__(self, x=100): |
| 480 | self._x = x |
| 481 | def __format__(self, spec): |
| 482 | return spec |
| 483 | |
| 484 | class D: |
| 485 | def __init__(self, x): |
| 486 | self.x = x |
| 487 | def __format__(self, spec): |
| 488 | return str(self.x) |
| 489 | |
| 490 | # class with __str__, but no __format__ |
| 491 | class E: |
| 492 | def __init__(self, x): |
| 493 | self.x = x |
| 494 | def __str__(self): |
| 495 | return 'E(' + self.x + ')' |
| 496 | |
| 497 | # class with __repr__, but no __format__ or __str__ |
| 498 | class F: |
| 499 | def __init__(self, x): |
| 500 | self.x = x |
| 501 | def __repr__(self): |
| 502 | return 'F(' + self.x + ')' |
| 503 | |
| 504 | # class with __format__ that forwards to string, for some format_spec's |
| 505 | class G: |
| 506 | def __init__(self, x): |
| 507 | self.x = x |
| 508 | def __str__(self): |
| 509 | return "string is " + self.x |
| 510 | def __format__(self, format_spec): |
| 511 | if format_spec == 'd': |
| 512 | return 'G(' + self.x + ')' |
| 513 | return object.__format__(self, format_spec) |
| 514 | |
Eric Smith | 739e2ad | 2007-08-27 19:07:22 +0000 | [diff] [blame] | 515 | class I(datetime.date): |
| 516 | def __format__(self, format_spec): |
| 517 | return self.strftime(format_spec) |
| 518 | |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 519 | class J(int): |
| 520 | def __format__(self, format_spec): |
| 521 | return int.__format__(self * 2, format_spec) |
| 522 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 523 | |
| 524 | self.assertEqual(''.format(), '') |
| 525 | self.assertEqual('abc'.format(), 'abc') |
| 526 | self.assertEqual('{0}'.format('abc'), 'abc') |
| 527 | self.assertEqual('{0:}'.format('abc'), 'abc') |
| 528 | # self.assertEqual('{ 0 }'.format('abc'), 'abc') |
| 529 | self.assertEqual('X{0}'.format('abc'), 'Xabc') |
| 530 | self.assertEqual('{0}X'.format('abc'), 'abcX') |
| 531 | self.assertEqual('X{0}Y'.format('abc'), 'XabcY') |
| 532 | self.assertEqual('{1}'.format(1, 'abc'), 'abc') |
| 533 | self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc') |
| 534 | self.assertEqual('{1}X'.format(1, 'abc'), 'abcX') |
| 535 | self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY') |
| 536 | self.assertEqual('{0}'.format(-15), '-15') |
| 537 | self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc') |
| 538 | self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc') |
| 539 | self.assertEqual('{{'.format(), '{') |
| 540 | self.assertEqual('}}'.format(), '}') |
| 541 | self.assertEqual('{{}}'.format(), '{}') |
| 542 | self.assertEqual('{{x}}'.format(), '{x}') |
| 543 | self.assertEqual('{{{0}}}'.format(123), '{123}') |
| 544 | self.assertEqual('{{{{0}}}}'.format(), '{{0}}') |
| 545 | self.assertEqual('}}{{'.format(), '}{') |
| 546 | self.assertEqual('}}x{{'.format(), '}x{') |
| 547 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 548 | # weird field names |
| 549 | self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz') |
| 550 | self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz') |
Eric Smith | 4cb4e4e | 2007-09-03 08:40:29 +0000 | [diff] [blame] | 551 | self.assertEqual("{0[ ]}".format({' ':3}), '3') |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 552 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 553 | self.assertEqual('{foo._x}'.format(foo=C(20)), '20') |
| 554 | self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010') |
| 555 | self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc') |
| 556 | self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc') |
| 557 | self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def') |
| 558 | self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def') |
| 559 | self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def') |
| 560 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 561 | # strings |
| 562 | self.assertEqual('{0:.3s}'.format('abc'), 'abc') |
| 563 | self.assertEqual('{0:.3s}'.format('ab'), 'ab') |
| 564 | self.assertEqual('{0:.3s}'.format('abcdef'), 'abc') |
| 565 | self.assertEqual('{0:.0s}'.format('abcdef'), '') |
| 566 | self.assertEqual('{0:3.3s}'.format('abc'), 'abc') |
| 567 | self.assertEqual('{0:2.3s}'.format('abc'), 'abc') |
| 568 | self.assertEqual('{0:2.2s}'.format('abc'), 'ab') |
| 569 | self.assertEqual('{0:3.2s}'.format('abc'), 'ab ') |
| 570 | self.assertEqual('{0:x<0s}'.format('result'), 'result') |
| 571 | self.assertEqual('{0:x<5s}'.format('result'), 'result') |
| 572 | self.assertEqual('{0:x<6s}'.format('result'), 'result') |
| 573 | self.assertEqual('{0:x<7s}'.format('result'), 'resultx') |
| 574 | self.assertEqual('{0:x<8s}'.format('result'), 'resultxx') |
| 575 | self.assertEqual('{0: <7s}'.format('result'), 'result ') |
| 576 | self.assertEqual('{0:<7s}'.format('result'), 'result ') |
| 577 | self.assertEqual('{0:>7s}'.format('result'), ' result') |
| 578 | self.assertEqual('{0:>8s}'.format('result'), ' result') |
| 579 | self.assertEqual('{0:^8s}'.format('result'), ' result ') |
| 580 | self.assertEqual('{0:^9s}'.format('result'), ' result ') |
| 581 | self.assertEqual('{0:^10s}'.format('result'), ' result ') |
| 582 | self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999) |
| 583 | self.assertEqual('{0:10000}'.format(''), ' ' * 10000) |
| 584 | self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000) |
| 585 | |
| 586 | # format specifiers for user defined type |
| 587 | self.assertEqual('{0:abc}'.format(C()), 'abc') |
| 588 | |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 589 | # !r, !s and !a coercions |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 590 | self.assertEqual('{0!s}'.format('Hello'), 'Hello') |
| 591 | self.assertEqual('{0!s:}'.format('Hello'), 'Hello') |
| 592 | self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ') |
| 593 | self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ') |
| 594 | self.assertEqual('{0!r}'.format('Hello'), "'Hello'") |
| 595 | self.assertEqual('{0!r:}'.format('Hello'), "'Hello'") |
| 596 | self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)') |
Amaury Forgeot d'Arc | a083f1e | 2008-09-10 23:51:42 +0000 | [diff] [blame] | 597 | self.assertEqual('{0!r}'.format('\u0378'), "'\\u0378'") # nonprintable |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 598 | self.assertEqual('{0!r}'.format('\u0374'), "'\u0374'") # printable |
| 599 | self.assertEqual('{0!r}'.format(F('\u0374')), 'F(\u0374)') |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 600 | self.assertEqual('{0!a}'.format('Hello'), "'Hello'") |
Amaury Forgeot d'Arc | a083f1e | 2008-09-10 23:51:42 +0000 | [diff] [blame] | 601 | self.assertEqual('{0!a}'.format('\u0378'), "'\\u0378'") # nonprintable |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 602 | self.assertEqual('{0!a}'.format('\u0374'), "'\\u0374'") # printable |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 603 | self.assertEqual('{0!a:}'.format('Hello'), "'Hello'") |
| 604 | self.assertEqual('{0!a}'.format(F('Hello')), 'F(Hello)') |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 605 | self.assertEqual('{0!a}'.format(F('\u0374')), 'F(\\u0374)') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 606 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 607 | # test fallback to object.__format__ |
| 608 | self.assertEqual('{0}'.format({}), '{}') |
| 609 | self.assertEqual('{0}'.format([]), '[]') |
| 610 | self.assertEqual('{0}'.format([1]), '[1]') |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 611 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 612 | self.assertEqual('{0:d}'.format(G('data')), 'G(data)') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 613 | self.assertEqual('{0!s}'.format(G('data')), 'string is data') |
| 614 | |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 615 | msg = 'object.__format__ with a non-empty format string is deprecated' |
| 616 | with support.check_warnings((msg, PendingDeprecationWarning)): |
| 617 | self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ') |
| 618 | self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ') |
| 619 | self.assertEqual('{0:>15s}'.format(G('data')), ' string is data') |
| 620 | |
Eric Smith | 739e2ad | 2007-08-27 19:07:22 +0000 | [diff] [blame] | 621 | self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007, |
| 622 | month=8, |
| 623 | day=27)), |
| 624 | "date: 2007-08-27") |
| 625 | |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 626 | # test deriving from a builtin type and overriding __format__ |
| 627 | self.assertEqual("{0}".format(J(10)), "20") |
| 628 | |
| 629 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 630 | # string format specifiers |
| 631 | self.assertEqual('{0:}'.format('a'), 'a') |
| 632 | |
| 633 | # computed format specifiers |
| 634 | self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello') |
| 635 | self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello') |
| 636 | self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello') |
| 637 | self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello ') |
| 638 | self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hello ') |
| 639 | |
| 640 | # test various errors |
| 641 | self.assertRaises(ValueError, '{'.format) |
| 642 | self.assertRaises(ValueError, '}'.format) |
| 643 | self.assertRaises(ValueError, 'a{'.format) |
| 644 | self.assertRaises(ValueError, 'a}'.format) |
| 645 | self.assertRaises(ValueError, '{a'.format) |
| 646 | self.assertRaises(ValueError, '}a'.format) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 647 | self.assertRaises(IndexError, '{0}'.format) |
| 648 | self.assertRaises(IndexError, '{1}'.format, 'abc') |
| 649 | self.assertRaises(KeyError, '{x}'.format) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 650 | self.assertRaises(ValueError, "}{".format) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 651 | self.assertRaises(ValueError, "abc{0:{}".format) |
| 652 | self.assertRaises(ValueError, "{0".format) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 653 | self.assertRaises(IndexError, "{0.}".format) |
| 654 | self.assertRaises(ValueError, "{0.}".format, 0) |
| 655 | self.assertRaises(IndexError, "{0[}".format) |
Eric Smith | 4cb4e4e | 2007-09-03 08:40:29 +0000 | [diff] [blame] | 656 | self.assertRaises(ValueError, "{0[}".format, []) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 657 | self.assertRaises(KeyError, "{0]}".format) |
| 658 | self.assertRaises(ValueError, "{0.[]}".format, 0) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 659 | self.assertRaises(ValueError, "{0..foo}".format, 0) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 660 | self.assertRaises(ValueError, "{0[0}".format, 0) |
| 661 | self.assertRaises(ValueError, "{0[0:foo}".format, 0) |
| 662 | self.assertRaises(KeyError, "{c]}".format) |
| 663 | self.assertRaises(ValueError, "{{ {{{0}}".format, 0) |
| 664 | self.assertRaises(ValueError, "{0}}".format, 0) |
| 665 | self.assertRaises(KeyError, "{foo}".format, bar=3) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 666 | self.assertRaises(ValueError, "{0!x}".format, 3) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 667 | self.assertRaises(ValueError, "{0!}".format, 0) |
| 668 | self.assertRaises(ValueError, "{0!rs}".format, 0) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 669 | self.assertRaises(ValueError, "{!}".format) |
Eric Smith | 8ec9044 | 2009-03-14 12:29:34 +0000 | [diff] [blame] | 670 | self.assertRaises(IndexError, "{:}".format) |
| 671 | self.assertRaises(IndexError, "{:s}".format) |
| 672 | self.assertRaises(IndexError, "{}".format) |
Benjamin Peterson | 59a1b2f | 2010-06-07 22:31:26 +0000 | [diff] [blame] | 673 | big = "23098475029384702983476098230754973209482573" |
| 674 | self.assertRaises(ValueError, ("{" + big + "}").format) |
| 675 | self.assertRaises(ValueError, ("{[" + big + "]}").format, [0]) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 676 | |
Eric Smith | 41669ca | 2009-05-23 14:23:22 +0000 | [diff] [blame] | 677 | # issue 6089 |
| 678 | self.assertRaises(ValueError, "{0[0]x}".format, [None]) |
| 679 | self.assertRaises(ValueError, "{0[0](10)}".format, [None]) |
| 680 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 681 | # can't have a replacement on the field name portion |
| 682 | self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4) |
| 683 | |
| 684 | # exceed maximum recursion depth |
| 685 | self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '') |
| 686 | self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format, |
| 687 | 0, 1, 2, 3, 4, 5, 6, 7) |
| 688 | |
| 689 | # string format spec errors |
| 690 | self.assertRaises(ValueError, "{0:-s}".format, '') |
| 691 | self.assertRaises(ValueError, format, "", "-") |
| 692 | self.assertRaises(ValueError, "{0:=s}".format, '') |
| 693 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 694 | # Alternate formatting is not supported |
| 695 | self.assertRaises(ValueError, format, '', '#') |
| 696 | self.assertRaises(ValueError, format, '', '#20') |
| 697 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 698 | def test_format_map(self): |
| 699 | self.assertEqual(''.format_map({}), '') |
| 700 | self.assertEqual('a'.format_map({}), 'a') |
| 701 | self.assertEqual('ab'.format_map({}), 'ab') |
| 702 | self.assertEqual('a{{'.format_map({}), 'a{') |
| 703 | self.assertEqual('a}}'.format_map({}), 'a}') |
| 704 | self.assertEqual('{{b'.format_map({}), '{b') |
| 705 | self.assertEqual('}}b'.format_map({}), '}b') |
| 706 | self.assertEqual('a{{b'.format_map({}), 'a{b') |
| 707 | |
| 708 | # using mappings |
| 709 | class Mapping(dict): |
| 710 | def __missing__(self, key): |
| 711 | return key |
| 712 | self.assertEqual('{hello}'.format_map(Mapping()), 'hello') |
| 713 | self.assertEqual('{a} {world}'.format_map(Mapping(a='hello')), 'hello world') |
| 714 | |
| 715 | class InternalMapping: |
| 716 | def __init__(self): |
| 717 | self.mapping = {'a': 'hello'} |
| 718 | def __getitem__(self, key): |
| 719 | return self.mapping[key] |
| 720 | self.assertEqual('{a}'.format_map(InternalMapping()), 'hello') |
| 721 | |
| 722 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 723 | class C: |
| 724 | def __init__(self, x=100): |
| 725 | self._x = x |
| 726 | def __format__(self, spec): |
| 727 | return spec |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 728 | self.assertEqual('{foo._x}'.format_map({'foo': C(20)}), '20') |
| 729 | |
| 730 | # test various errors |
| 731 | self.assertRaises(TypeError, '{'.format_map) |
| 732 | self.assertRaises(TypeError, '}'.format_map) |
| 733 | self.assertRaises(TypeError, 'a{'.format_map) |
| 734 | self.assertRaises(TypeError, 'a}'.format_map) |
| 735 | self.assertRaises(TypeError, '{a'.format_map) |
| 736 | self.assertRaises(TypeError, '}a'.format_map) |
| 737 | |
Eric Smith | 8ec9044 | 2009-03-14 12:29:34 +0000 | [diff] [blame] | 738 | def test_format_auto_numbering(self): |
| 739 | class C: |
| 740 | def __init__(self, x=100): |
| 741 | self._x = x |
| 742 | def __format__(self, spec): |
| 743 | return spec |
| 744 | |
| 745 | self.assertEqual('{}'.format(10), '10') |
| 746 | self.assertEqual('{:5}'.format('s'), 's ') |
| 747 | self.assertEqual('{!r}'.format('s'), "'s'") |
| 748 | self.assertEqual('{._x}'.format(C(10)), '10') |
| 749 | self.assertEqual('{[1]}'.format([1, 2]), '2') |
| 750 | self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4') |
| 751 | self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c') |
| 752 | |
| 753 | self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b') |
| 754 | self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b') |
| 755 | |
| 756 | # can't mix and match numbering and auto-numbering |
| 757 | self.assertRaises(ValueError, '{}{1}'.format, 1, 2) |
| 758 | self.assertRaises(ValueError, '{1}{}'.format, 1, 2) |
| 759 | self.assertRaises(ValueError, '{:{1}}'.format, 1, 2) |
| 760 | self.assertRaises(ValueError, '{0:{}}'.format, 1, 2) |
| 761 | |
| 762 | # can mix and match auto-numbering and named |
| 763 | self.assertEqual('{f}{}'.format(4, f='test'), 'test4') |
| 764 | self.assertEqual('{}{f}'.format(4, f='test'), '4test') |
| 765 | self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3') |
| 766 | self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g') |
| 767 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 768 | def test_formatting(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 769 | string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 770 | # Testing Unicode formatting strings... |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 771 | self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc') |
| 772 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00') |
| 773 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, -2, 3), 'abc, abc, 1, -2.000000, 3.00') |
| 774 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.5), 'abc, abc, -1, -2.000000, 3.50') |
| 775 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.57), 'abc, abc, -1, -2.000000, 3.57') |
| 776 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 1003.57), 'abc, abc, -1, -2.000000, 1003.57') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 777 | if not sys.platform.startswith('java'): |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 778 | self.assertEqual("%r, %r" % (b"abc", "abc"), "b'abc', 'abc'") |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 779 | self.assertEqual("%r" % ("\u1234",), "'\u1234'") |
| 780 | self.assertEqual("%a" % ("\u1234",), "'\\u1234'") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 781 | self.assertEqual("%(x)s, %(y)s" % {'x':"abc", 'y':"def"}, 'abc, def') |
| 782 | self.assertEqual("%(x)s, %(\xfc)s" % {'x':"abc", '\xfc':"def"}, 'abc, def') |
Walter Dörwald | 56fbcb5 | 2003-03-31 18:18:41 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 784 | self.assertEqual('%c' % 0x1234, '\u1234') |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 785 | self.assertEqual('%c' % 0x21483, '\U00021483') |
| 786 | self.assertRaises(OverflowError, "%c".__mod__, (0x110000,)) |
| 787 | self.assertEqual('%c' % '\U00021483', '\U00021483') |
| 788 | self.assertRaises(TypeError, "%c".__mod__, "aa") |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 789 | self.assertRaises(ValueError, "%.1\u1032f".__mod__, (1.0/3)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 790 | |
| 791 | # formatting jobs delegated from the string implementation: |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 792 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 793 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 794 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 795 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 796 | self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...') |
| 797 | self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...') |
| 798 | self.assertEqual('...%s...%s...%s...%s...' % (1,2,3,"abc"), '...1...2...3...abc...') |
| 799 | self.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,"abc"), '...%...%s...1...2...3...abc...') |
| 800 | self.assertEqual('...%s...' % "abc", '...abc...') |
| 801 | self.assertEqual('%*s' % (5,'abc',), ' abc') |
| 802 | self.assertEqual('%*s' % (-5,'abc',), 'abc ') |
| 803 | self.assertEqual('%*.*s' % (5,2,'abc',), ' ab') |
| 804 | self.assertEqual('%*.*s' % (5,3,'abc',), ' abc') |
| 805 | self.assertEqual('%i %*.*s' % (10, 5,3,'abc',), '10 abc') |
| 806 | self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, 'abc',), '103 abc') |
| 807 | self.assertEqual('%c' % 'a', 'a') |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 808 | class Wrapper: |
| 809 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 810 | return '\u1234' |
| 811 | self.assertEqual('%s' % Wrapper(), '\u1234') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 812 | |
Eric Smith | 741191f | 2009-05-06 13:08:15 +0000 | [diff] [blame] | 813 | # issue 3382 |
| 814 | NAN = float('nan') |
| 815 | INF = float('inf') |
| 816 | self.assertEqual('%f' % NAN, 'nan') |
| 817 | self.assertEqual('%F' % NAN, 'NAN') |
| 818 | self.assertEqual('%f' % INF, 'inf') |
| 819 | self.assertEqual('%F' % INF, 'INF') |
| 820 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 821 | @support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR') |
Georg Brandl | da6b107 | 2006-01-20 17:48:54 +0000 | [diff] [blame] | 822 | def test_format_float(self): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 823 | # should not format with a comma, but always with C locale |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 824 | self.assertEqual('1.0', '%.1f' % 1.0) |
Georg Brandl | da6b107 | 2006-01-20 17:48:54 +0000 | [diff] [blame] | 825 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 826 | def test_constructor(self): |
| 827 | # unicode(obj) tests (this maps to PyObject_Unicode() at C level) |
| 828 | |
| 829 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 830 | str('unicode remains unicode'), |
| 831 | 'unicode remains unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 832 | ) |
| 833 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 834 | class UnicodeSubclass(str): |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 835 | pass |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 836 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 837 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 838 | str(UnicodeSubclass('unicode subclass becomes unicode')), |
| 839 | 'unicode subclass becomes unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 840 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 841 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 842 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 843 | str('strings are converted to unicode'), |
| 844 | 'strings are converted to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 845 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 846 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 847 | class StringCompat: |
| 848 | def __init__(self, x): |
| 849 | self.x = x |
| 850 | def __str__(self): |
| 851 | return self.x |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 852 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 853 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 854 | str(StringCompat('__str__ compatible objects are recognized')), |
| 855 | '__str__ compatible objects are recognized' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 856 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 857 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 858 | # unicode(obj) is compatible to str(): |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 859 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 860 | o = StringCompat('unicode(obj) is compatible to str()') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 861 | self.assertEqual(str(o), 'unicode(obj) is compatible to str()') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 862 | self.assertEqual(str(o), 'unicode(obj) is compatible to str()') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 863 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 864 | for obj in (123, 123.45, 123): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 865 | self.assertEqual(str(obj), str(str(obj))) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 866 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 867 | # unicode(obj, encoding, error) tests (this maps to |
| 868 | # PyUnicode_FromEncodedObject() at C level) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 869 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 870 | if not sys.platform.startswith('java'): |
| 871 | self.assertRaises( |
| 872 | TypeError, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 873 | str, |
| 874 | 'decoding unicode is not supported', |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 875 | 'utf-8', |
| 876 | 'strict' |
| 877 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 878 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 879 | self.assertEqual( |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 880 | str(b'strings are decoded to unicode', 'utf-8', 'strict'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 881 | 'strings are decoded to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 882 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 883 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 884 | if not sys.platform.startswith('java'): |
| 885 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 886 | str( |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 887 | memoryview(b'character buffers are decoded to unicode'), |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 888 | 'utf-8', |
| 889 | 'strict' |
| 890 | ), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 891 | 'character buffers are decoded to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 892 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 893 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 894 | self.assertRaises(TypeError, str, 42, 42, 42) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 895 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 896 | def test_codecs_utf7(self): |
| 897 | utfTests = [ |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 898 | ('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example |
| 899 | ('Hi Mom -\u263a-!', b'Hi Mom -+Jjo--!'), # RFC2152 example |
| 900 | ('\u65E5\u672C\u8A9E', b'+ZeVnLIqe-'), # RFC2152 example |
| 901 | ('Item 3 is \u00a31.', b'Item 3 is +AKM-1.'), # RFC2152 example |
| 902 | ('+', b'+-'), |
| 903 | ('+-', b'+--'), |
| 904 | ('+?', b'+-?'), |
| 905 | ('\?', b'+AFw?'), |
| 906 | ('+?', b'+-?'), |
| 907 | (r'\\?', b'+AFwAXA?'), |
| 908 | (r'\\\?', b'+AFwAXABc?'), |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 909 | (r'++--', b'+-+---'), |
| 910 | ('\U000abcde', b'+2m/c3g-'), # surrogate pairs |
| 911 | ('/', b'/'), |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 912 | ] |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 913 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 914 | for (x, y) in utfTests: |
| 915 | self.assertEqual(x.encode('utf-7'), y) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 916 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 917 | # Unpaired surrogates not supported |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 918 | self.assertRaises(UnicodeError, str, b'+3ADYAA-', 'utf-7') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 920 | self.assertEqual(str(b'+3ADYAA-', 'utf-7', 'replace'), '\ufffd\ufffd') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 922 | # Issue #2242: crash on some Windows/MSVC versions |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 923 | self.assertEqual(b'+\xc1'.decode('utf-7'), '\xc1') |
| 924 | |
| 925 | # Direct encoded characters |
| 926 | set_d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?" |
| 927 | # Optional direct characters |
| 928 | set_o = '!"#$%&*;<=>@[]^_`{|}' |
| 929 | for c in set_d: |
| 930 | self.assertEqual(c.encode('utf7'), c.encode('ascii')) |
| 931 | self.assertEqual(c.encode('ascii').decode('utf7'), c) |
| 932 | for c in set_o: |
| 933 | self.assertEqual(c.encode('ascii').decode('utf7'), c) |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 934 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 935 | def test_codecs_utf8(self): |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 936 | self.assertEqual(''.encode('utf-8'), b'') |
| 937 | self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac') |
Martin v. Löwis | 74b7e44 | 2009-06-01 04:23:07 +0000 | [diff] [blame] | 938 | if sys.maxunicode == 65535: |
| 939 | self.assertEqual('\ud800\udc02'.encode('utf-8'), b'\xf0\x90\x80\x82') |
| 940 | self.assertEqual('\ud84d\udc56'.encode('utf-8'), b'\xf0\xa3\x91\x96') |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 941 | self.assertEqual('\ud800'.encode('utf-8', 'surrogatepass'), b'\xed\xa0\x80') |
| 942 | self.assertEqual('\udc00'.encode('utf-8', 'surrogatepass'), b'\xed\xb0\x80') |
Martin v. Löwis | 74b7e44 | 2009-06-01 04:23:07 +0000 | [diff] [blame] | 943 | if sys.maxunicode == 65535: |
| 944 | self.assertEqual( |
| 945 | ('\ud800\udc02'*1000).encode('utf-8'), |
| 946 | b'\xf0\x90\x80\x82'*1000) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 947 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 948 | '\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f' |
| 949 | '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00' |
| 950 | '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c' |
| 951 | '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067' |
| 952 | '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das' |
| 953 | ' Nunstuck git und'.encode('utf-8'), |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 954 | b'\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81' |
| 955 | b'\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3' |
| 956 | b'\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe' |
| 957 | b'\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83' |
| 958 | b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8' |
| 959 | b'\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81' |
| 960 | b'\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81' |
| 961 | b'\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3' |
| 962 | b'\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf' |
| 963 | b'\xe3\x80\x8cWenn ist das Nunstuck git und' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 964 | ) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 965 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 966 | # UTF-8 specific decoding tests |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 967 | self.assertEqual(str(b'\xf0\xa3\x91\x96', 'utf-8'), '\U00023456' ) |
| 968 | self.assertEqual(str(b'\xf0\x90\x80\x82', 'utf-8'), '\U00010002' ) |
| 969 | self.assertEqual(str(b'\xe2\x82\xac', 'utf-8'), '\u20ac' ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 970 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 971 | # Other possible utf-8 test cases: |
| 972 | # * strict decoding testing for all of the |
| 973 | # UTF8_ERROR cases in PyUnicode_DecodeUTF8 |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 974 | |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 975 | def test_utf8_decode_valid_sequences(self): |
| 976 | sequences = [ |
| 977 | # single byte |
| 978 | (b'\x00', '\x00'), (b'a', 'a'), (b'\x7f', '\x7f'), |
| 979 | # 2 bytes |
| 980 | (b'\xc2\x80', '\x80'), (b'\xdf\xbf', '\u07ff'), |
| 981 | # 3 bytes |
| 982 | (b'\xe0\xa0\x80', '\u0800'), (b'\xed\x9f\xbf', '\ud7ff'), |
| 983 | (b'\xee\x80\x80', '\uE000'), (b'\xef\xbf\xbf', '\uffff'), |
| 984 | # 4 bytes |
| 985 | (b'\xF0\x90\x80\x80', '\U00010000'), |
| 986 | (b'\xf4\x8f\xbf\xbf', '\U0010FFFF') |
| 987 | ] |
| 988 | for seq, res in sequences: |
| 989 | self.assertEqual(seq.decode('utf-8'), res) |
| 990 | |
| 991 | |
| 992 | def test_utf8_decode_invalid_sequences(self): |
| 993 | # continuation bytes in a sequence of 2, 3, or 4 bytes |
| 994 | continuation_bytes = [bytes([x]) for x in range(0x80, 0xC0)] |
| 995 | # start bytes of a 2-byte sequence equivalent to codepoints < 0x7F |
| 996 | invalid_2B_seq_start_bytes = [bytes([x]) for x in range(0xC0, 0xC2)] |
| 997 | # start bytes of a 4-byte sequence equivalent to codepoints > 0x10FFFF |
| 998 | invalid_4B_seq_start_bytes = [bytes([x]) for x in range(0xF5, 0xF8)] |
| 999 | invalid_start_bytes = ( |
| 1000 | continuation_bytes + invalid_2B_seq_start_bytes + |
| 1001 | invalid_4B_seq_start_bytes + [bytes([x]) for x in range(0xF7, 0x100)] |
| 1002 | ) |
| 1003 | |
| 1004 | for byte in invalid_start_bytes: |
| 1005 | self.assertRaises(UnicodeDecodeError, byte.decode, 'utf-8') |
| 1006 | |
| 1007 | for sb in invalid_2B_seq_start_bytes: |
| 1008 | for cb in continuation_bytes: |
| 1009 | self.assertRaises(UnicodeDecodeError, (sb+cb).decode, 'utf-8') |
| 1010 | |
| 1011 | for sb in invalid_4B_seq_start_bytes: |
| 1012 | for cb1 in continuation_bytes[:3]: |
| 1013 | for cb3 in continuation_bytes[:3]: |
| 1014 | self.assertRaises(UnicodeDecodeError, |
| 1015 | (sb+cb1+b'\x80'+cb3).decode, 'utf-8') |
| 1016 | |
| 1017 | for cb in [bytes([x]) for x in range(0x80, 0xA0)]: |
| 1018 | self.assertRaises(UnicodeDecodeError, |
| 1019 | (b'\xE0'+cb+b'\x80').decode, 'utf-8') |
| 1020 | self.assertRaises(UnicodeDecodeError, |
| 1021 | (b'\xE0'+cb+b'\xBF').decode, 'utf-8') |
| 1022 | # surrogates |
| 1023 | for cb in [bytes([x]) for x in range(0xA0, 0xC0)]: |
| 1024 | self.assertRaises(UnicodeDecodeError, |
| 1025 | (b'\xED'+cb+b'\x80').decode, 'utf-8') |
| 1026 | self.assertRaises(UnicodeDecodeError, |
| 1027 | (b'\xED'+cb+b'\xBF').decode, 'utf-8') |
| 1028 | for cb in [bytes([x]) for x in range(0x80, 0x90)]: |
| 1029 | self.assertRaises(UnicodeDecodeError, |
| 1030 | (b'\xF0'+cb+b'\x80\x80').decode, 'utf-8') |
| 1031 | self.assertRaises(UnicodeDecodeError, |
| 1032 | (b'\xF0'+cb+b'\xBF\xBF').decode, 'utf-8') |
| 1033 | for cb in [bytes([x]) for x in range(0x90, 0xC0)]: |
| 1034 | self.assertRaises(UnicodeDecodeError, |
| 1035 | (b'\xF4'+cb+b'\x80\x80').decode, 'utf-8') |
| 1036 | self.assertRaises(UnicodeDecodeError, |
| 1037 | (b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8') |
| 1038 | |
| 1039 | def test_issue8271(self): |
| 1040 | # Issue #8271: during the decoding of an invalid UTF-8 byte sequence, |
| 1041 | # only the start byte and the continuation byte(s) are now considered |
| 1042 | # invalid, instead of the number of bytes specified by the start byte. |
| 1043 | # See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf (page 95, |
| 1044 | # table 3-8, Row 2) for more information about the algorithm used. |
| 1045 | FFFD = '\ufffd' |
| 1046 | sequences = [ |
| 1047 | # invalid start bytes |
| 1048 | (b'\x80', FFFD), # continuation byte |
| 1049 | (b'\x80\x80', FFFD*2), # 2 continuation bytes |
| 1050 | (b'\xc0', FFFD), |
| 1051 | (b'\xc0\xc0', FFFD*2), |
| 1052 | (b'\xc1', FFFD), |
| 1053 | (b'\xc1\xc0', FFFD*2), |
| 1054 | (b'\xc0\xc1', FFFD*2), |
| 1055 | # with start byte of a 2-byte sequence |
| 1056 | (b'\xc2', FFFD), # only the start byte |
| 1057 | (b'\xc2\xc2', FFFD*2), # 2 start bytes |
| 1058 | (b'\xc2\xc2\xc2', FFFD*3), # 2 start bytes |
| 1059 | (b'\xc2\x41', FFFD+'A'), # invalid continuation byte |
| 1060 | # with start byte of a 3-byte sequence |
| 1061 | (b'\xe1', FFFD), # only the start byte |
| 1062 | (b'\xe1\xe1', FFFD*2), # 2 start bytes |
| 1063 | (b'\xe1\xe1\xe1', FFFD*3), # 3 start bytes |
| 1064 | (b'\xe1\xe1\xe1\xe1', FFFD*4), # 4 start bytes |
| 1065 | (b'\xe1\x80', FFFD), # only 1 continuation byte |
| 1066 | (b'\xe1\x41', FFFD+'A'), # invalid continuation byte |
| 1067 | (b'\xe1\x41\x80', FFFD+'A'+FFFD), # invalid cb followed by valid cb |
| 1068 | (b'\xe1\x41\x41', FFFD+'AA'), # 2 invalid continuation bytes |
| 1069 | (b'\xe1\x80\x41', FFFD+'A'), # only 1 valid continuation byte |
| 1070 | (b'\xe1\x80\xe1\x41', FFFD*2+'A'), # 1 valid and the other invalid |
| 1071 | (b'\xe1\x41\xe1\x80', FFFD+'A'+FFFD), # 1 invalid and the other valid |
| 1072 | # with start byte of a 4-byte sequence |
| 1073 | (b'\xf1', FFFD), # only the start byte |
| 1074 | (b'\xf1\xf1', FFFD*2), # 2 start bytes |
| 1075 | (b'\xf1\xf1\xf1', FFFD*3), # 3 start bytes |
| 1076 | (b'\xf1\xf1\xf1\xf1', FFFD*4), # 4 start bytes |
| 1077 | (b'\xf1\xf1\xf1\xf1\xf1', FFFD*5), # 5 start bytes |
| 1078 | (b'\xf1\x80', FFFD), # only 1 continuation bytes |
| 1079 | (b'\xf1\x80\x80', FFFD), # only 2 continuation bytes |
| 1080 | (b'\xf1\x80\x41', FFFD+'A'), # 1 valid cb and 1 invalid |
| 1081 | (b'\xf1\x80\x41\x41', FFFD+'AA'), # 1 valid cb and 1 invalid |
| 1082 | (b'\xf1\x80\x80\x41', FFFD+'A'), # 2 valid cb and 1 invalid |
| 1083 | (b'\xf1\x41\x80', FFFD+'A'+FFFD), # 1 invalid cv and 1 valid |
| 1084 | (b'\xf1\x41\x80\x80', FFFD+'A'+FFFD*2), # 1 invalid cb and 2 invalid |
| 1085 | (b'\xf1\x41\x80\x41', FFFD+'A'+FFFD+'A'), # 2 invalid cb and 1 invalid |
| 1086 | (b'\xf1\x41\x41\x80', FFFD+'AA'+FFFD), # 1 valid cb and 1 invalid |
| 1087 | (b'\xf1\x41\xf1\x80', FFFD+'A'+FFFD), |
| 1088 | (b'\xf1\x41\x80\xf1', FFFD+'A'+FFFD*2), |
| 1089 | (b'\xf1\xf1\x80\x41', FFFD*2+'A'), |
| 1090 | (b'\xf1\x41\xf1\xf1', FFFD+'A'+FFFD*2), |
| 1091 | # with invalid start byte of a 4-byte sequence (rfc2279) |
| 1092 | (b'\xf5', FFFD), # only the start byte |
| 1093 | (b'\xf5\xf5', FFFD*2), # 2 start bytes |
| 1094 | (b'\xf5\x80', FFFD*2), # only 1 continuation byte |
| 1095 | (b'\xf5\x80\x80', FFFD*3), # only 2 continuation byte |
| 1096 | (b'\xf5\x80\x80\x80', FFFD*4), # 3 continuation bytes |
| 1097 | (b'\xf5\x80\x41', FFFD*2+'A'), # 1 valid cb and 1 invalid |
| 1098 | (b'\xf5\x80\x41\xf5', FFFD*2+'A'+FFFD), |
| 1099 | (b'\xf5\x41\x80\x80\x41', FFFD+'A'+FFFD*2+'A'), |
| 1100 | # with invalid start byte of a 5-byte sequence (rfc2279) |
| 1101 | (b'\xf8', FFFD), # only the start byte |
| 1102 | (b'\xf8\xf8', FFFD*2), # 2 start bytes |
| 1103 | (b'\xf8\x80', FFFD*2), # only one continuation byte |
| 1104 | (b'\xf8\x80\x41', FFFD*2 + 'A'), # 1 valid cb and 1 invalid |
| 1105 | (b'\xf8\x80\x80\x80\x80', FFFD*5), # invalid 5 bytes seq with 5 bytes |
| 1106 | # with invalid start byte of a 6-byte sequence (rfc2279) |
| 1107 | (b'\xfc', FFFD), # only the start byte |
| 1108 | (b'\xfc\xfc', FFFD*2), # 2 start bytes |
| 1109 | (b'\xfc\x80\x80', FFFD*3), # only 2 continuation bytes |
| 1110 | (b'\xfc\x80\x80\x80\x80\x80', FFFD*6), # 6 continuation bytes |
| 1111 | # invalid start byte |
| 1112 | (b'\xfe', FFFD), |
| 1113 | (b'\xfe\x80\x80', FFFD*3), |
| 1114 | # other sequences |
| 1115 | (b'\xf1\x80\x41\x42\x43', '\ufffd\x41\x42\x43'), |
| 1116 | (b'\xf1\x80\xff\x42\x43', '\ufffd\ufffd\x42\x43'), |
| 1117 | (b'\xf1\x80\xc2\x81\x43', '\ufffd\x81\x43'), |
| 1118 | (b'\x61\xF1\x80\x80\xE1\x80\xC2\x62\x80\x63\x80\xBF\x64', |
| 1119 | '\x61\uFFFD\uFFFD\uFFFD\x62\uFFFD\x63\uFFFD\uFFFD\x64'), |
| 1120 | ] |
| 1121 | for n, (seq, res) in enumerate(sequences): |
| 1122 | self.assertRaises(UnicodeDecodeError, seq.decode, 'utf-8', 'strict') |
| 1123 | self.assertEqual(seq.decode('utf-8', 'replace'), res) |
| 1124 | self.assertEqual((seq+b'b').decode('utf-8', 'replace'), res+'b') |
| 1125 | self.assertEqual(seq.decode('utf-8', 'ignore'), |
| 1126 | res.replace('\uFFFD', '')) |
| 1127 | |
Martin v. Löwis | 0d8e16c | 2003-08-05 06:19:47 +0000 | [diff] [blame] | 1128 | def test_codecs_idna(self): |
| 1129 | # Test whether trailing dot is preserved |
Walter Dörwald | 1324c6f | 2007-05-11 19:57:05 +0000 | [diff] [blame] | 1130 | self.assertEqual("www.python.org.".encode("idna"), b"www.python.org.") |
Martin v. Löwis | 0d8e16c | 2003-08-05 06:19:47 +0000 | [diff] [blame] | 1131 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1132 | def test_codecs_errors(self): |
| 1133 | # Error handling (encoding) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1134 | self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii') |
| 1135 | self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict') |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1136 | self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x") |
| 1137 | self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x") |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 1138 | self.assertEqual('Andr\202 x'.encode('ascii', 'replace'), |
| 1139 | 'Andr\202 x'.encode('ascii', errors='replace')) |
| 1140 | self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'), |
| 1141 | 'Andr\202 x'.encode(encoding='ascii', errors='ignore')) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1142 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1143 | # Error handling (decoding) |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1144 | self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii') |
| 1145 | self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii', 'strict') |
| 1146 | self.assertEqual(str(b'Andr\202 x', 'ascii', 'ignore'), "Andr x") |
| 1147 | self.assertEqual(str(b'Andr\202 x', 'ascii', 'replace'), 'Andr\uFFFD x') |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1148 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1149 | # Error handling (unknown character names) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 1150 | self.assertEqual(b"\\N{foo}xx".decode("unicode-escape", "ignore"), "xx") |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1151 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1152 | # Error handling (truncated escape sequence) |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1153 | self.assertRaises(UnicodeError, b"\\".decode, "unicode-escape") |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 1154 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1155 | self.assertRaises(TypeError, b"hello".decode, "test.unicode1") |
| 1156 | self.assertRaises(TypeError, str, b"hello", "test.unicode2") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1157 | self.assertRaises(TypeError, "hello".encode, "test.unicode1") |
| 1158 | self.assertRaises(TypeError, "hello".encode, "test.unicode2") |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1159 | # executes PyUnicode_Encode() |
| 1160 | import imp |
| 1161 | self.assertRaises( |
| 1162 | ImportError, |
| 1163 | imp.find_module, |
| 1164 | "non-existing module", |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1165 | ["non-existing dir"] |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1166 | ) |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 1167 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1168 | # Error handling (wrong arguments) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1169 | self.assertRaises(TypeError, "hello".encode, 42, 42, 42) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1170 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1171 | # Error handling (PyUnicode_EncodeDecimal()) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1172 | self.assertRaises(UnicodeError, int, "\u0200") |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 1173 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1174 | def test_codecs(self): |
| 1175 | # Encoding |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1176 | self.assertEqual('hello'.encode('ascii'), b'hello') |
| 1177 | self.assertEqual('hello'.encode('utf-7'), b'hello') |
| 1178 | self.assertEqual('hello'.encode('utf-8'), b'hello') |
| 1179 | self.assertEqual('hello'.encode('utf8'), b'hello') |
| 1180 | self.assertEqual('hello'.encode('utf-16-le'), b'h\000e\000l\000l\000o\000') |
| 1181 | self.assertEqual('hello'.encode('utf-16-be'), b'\000h\000e\000l\000l\000o') |
| 1182 | self.assertEqual('hello'.encode('latin-1'), b'hello') |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 1183 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1184 | # Roundtrip safety for BMP (just the first 1024 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1185 | for c in range(1024): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 1186 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 1187 | for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le', |
| 1188 | 'utf-16-be', 'raw_unicode_escape', |
| 1189 | 'unicode_escape', 'unicode_internal'): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1190 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 1191 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1192 | # Roundtrip safety for BMP (just the first 256 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1193 | for c in range(256): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 1194 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 1195 | for encoding in ('latin-1',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1196 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1197 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1198 | # Roundtrip safety for BMP (just the first 128 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1199 | for c in range(128): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 1200 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 1201 | for encoding in ('ascii',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1202 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1203 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1204 | # Roundtrip safety for non-BMP (just a few chars) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1205 | u = '\U00010001\U00020002\U00030003\U00040004\U00050005' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1206 | for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be', |
| 1207 | #'raw_unicode_escape', |
| 1208 | 'unicode_escape', 'unicode_internal'): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1209 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1210 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1211 | # UTF-8 must be roundtrip safe for all UCS-2 code points |
| 1212 | # This excludes surrogates: in the full range, there would be |
| 1213 | # a surrogate pair (\udbff\udc00), which gets converted back |
| 1214 | # to a non-BMP character (\U0010fc00) |
Walter Dörwald | 1324c6f | 2007-05-11 19:57:05 +0000 | [diff] [blame] | 1215 | u = ''.join(map(chr, list(range(0,0xd800)) + |
| 1216 | list(range(0xe000,0x10000)))) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1217 | for encoding in ('utf-8',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1218 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1219 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1220 | def test_codecs_charmap(self): |
| 1221 | # 0-127 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1222 | s = bytes(range(128)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1223 | for encoding in ( |
| 1224 | 'cp037', 'cp1026', |
Benjamin Peterson | 5a6214a | 2010-06-27 22:41:29 +0000 | [diff] [blame] | 1225 | 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850', |
| 1226 | 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862', |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1227 | 'cp863', 'cp865', 'cp866', |
| 1228 | 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', |
| 1229 | 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6', |
| 1230 | 'iso8859_7', 'iso8859_9', 'koi8_r', 'latin_1', |
| 1231 | 'mac_cyrillic', 'mac_latin2', |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1232 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1233 | 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 1234 | 'cp1256', 'cp1257', 'cp1258', |
| 1235 | 'cp856', 'cp857', 'cp864', 'cp869', 'cp874', |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1236 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1237 | 'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish', |
| 1238 | 'cp1006', 'iso8859_8', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1239 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1240 | ### These have undefined mappings: |
| 1241 | #'cp424', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1242 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1243 | ### These fail the round-trip: |
| 1244 | #'cp875' |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1245 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1246 | ): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1247 | self.assertEqual(str(s, encoding).encode(encoding), s) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1248 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1249 | # 128-255 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1250 | s = bytes(range(128, 256)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1251 | for encoding in ( |
| 1252 | 'cp037', 'cp1026', |
Benjamin Peterson | 5a6214a | 2010-06-27 22:41:29 +0000 | [diff] [blame] | 1253 | 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850', |
| 1254 | 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862', |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1255 | 'cp863', 'cp865', 'cp866', |
| 1256 | 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', |
| 1257 | 'iso8859_2', 'iso8859_4', 'iso8859_5', |
| 1258 | 'iso8859_9', 'koi8_r', 'latin_1', |
| 1259 | 'mac_cyrillic', 'mac_latin2', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1260 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1261 | ### These have undefined mappings: |
| 1262 | #'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 1263 | #'cp1256', 'cp1257', 'cp1258', |
| 1264 | #'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874', |
| 1265 | #'iso8859_3', 'iso8859_6', 'iso8859_7', |
| 1266 | #'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1267 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1268 | ### These fail the round-trip: |
| 1269 | #'cp1006', 'cp875', 'iso8859_8', |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 1270 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1271 | ): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1272 | self.assertEqual(str(s, encoding).encode(encoding), s) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1273 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1274 | def test_concatenation(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1275 | self.assertEqual(("abc" "def"), "abcdef") |
| 1276 | self.assertEqual(("abc" "def"), "abcdef") |
| 1277 | self.assertEqual(("abc" "def"), "abcdef") |
| 1278 | self.assertEqual(("abc" "def" "ghi"), "abcdefghi") |
| 1279 | self.assertEqual(("abc" "def" "ghi"), "abcdefghi") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1280 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1281 | def test_printing(self): |
| 1282 | class BitBucket: |
| 1283 | def write(self, text): |
| 1284 | pass |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1285 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1286 | out = BitBucket() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1287 | print('abc', file=out) |
| 1288 | print('abc', 'def', file=out) |
| 1289 | print('abc', 'def', file=out) |
| 1290 | print('abc', 'def', file=out) |
| 1291 | print('abc\n', file=out) |
| 1292 | print('abc\n', end=' ', file=out) |
| 1293 | print('abc\n', end=' ', file=out) |
| 1294 | print('def\n', file=out) |
| 1295 | print('def\n', file=out) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1296 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 1297 | def test_ucs4(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1298 | x = '\U00100000' |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 1299 | y = x.encode("raw-unicode-escape").decode("raw-unicode-escape") |
| 1300 | self.assertEqual(x, y) |
| 1301 | |
Florent Xicluna | a87b383 | 2010-09-13 02:28:18 +0000 | [diff] [blame] | 1302 | y = br'\U00100000' |
| 1303 | x = y.decode("raw-unicode-escape").encode("raw-unicode-escape") |
| 1304 | self.assertEqual(x, y) |
| 1305 | y = br'\U00010000' |
| 1306 | x = y.decode("raw-unicode-escape").encode("raw-unicode-escape") |
| 1307 | self.assertEqual(x, y) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1308 | |
Florent Xicluna | a87b383 | 2010-09-13 02:28:18 +0000 | [diff] [blame] | 1309 | try: |
| 1310 | br'\U11111111'.decode("raw-unicode-escape") |
| 1311 | except UnicodeDecodeError as e: |
| 1312 | self.assertEqual(e.start, 0) |
| 1313 | self.assertEqual(e.end, 10) |
| 1314 | else: |
| 1315 | self.fail("Should have raised UnicodeDecodeError") |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1316 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1317 | def test_conversion(self): |
| 1318 | # Make sure __unicode__() works properly |
| 1319 | class Foo0: |
| 1320 | def __str__(self): |
| 1321 | return "foo" |
| 1322 | |
| 1323 | class Foo1: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1324 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1325 | return "foo" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1326 | |
| 1327 | class Foo2(object): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1328 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1329 | return "foo" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1330 | |
| 1331 | class Foo3(object): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1332 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1333 | return "foo" |
| 1334 | |
| 1335 | class Foo4(str): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1336 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1337 | return "foo" |
| 1338 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1339 | class Foo5(str): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1340 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1341 | return "foo" |
| 1342 | |
| 1343 | class Foo6(str): |
| 1344 | def __str__(self): |
| 1345 | return "foos" |
| 1346 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1347 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1348 | return "foou" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1349 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1350 | class Foo7(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1351 | def __str__(self): |
| 1352 | return "foos" |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1353 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1354 | return "foou" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1355 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1356 | class Foo8(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1357 | def __new__(cls, content=""): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1358 | return str.__new__(cls, 2*content) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1359 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1360 | return self |
| 1361 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1362 | class Foo9(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1363 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1364 | return "not unicode" |
| 1365 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1366 | self.assertEqual(str(Foo0()), "foo") |
| 1367 | self.assertEqual(str(Foo1()), "foo") |
| 1368 | self.assertEqual(str(Foo2()), "foo") |
| 1369 | self.assertEqual(str(Foo3()), "foo") |
| 1370 | self.assertEqual(str(Foo4("bar")), "foo") |
| 1371 | self.assertEqual(str(Foo5("bar")), "foo") |
| 1372 | self.assertEqual(str(Foo6("bar")), "foou") |
| 1373 | self.assertEqual(str(Foo7("bar")), "foou") |
| 1374 | self.assertEqual(str(Foo8("foo")), "foofoo") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1375 | self.assertEqual(str(Foo9("foo")), "not unicode") |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1376 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1377 | def test_unicode_repr(self): |
| 1378 | class s1: |
| 1379 | def __repr__(self): |
| 1380 | return '\\n' |
| 1381 | |
| 1382 | class s2: |
| 1383 | def __repr__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1384 | return '\\n' |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1385 | |
| 1386 | self.assertEqual(repr(s1()), '\\n') |
| 1387 | self.assertEqual(repr(s2()), '\\n') |
| 1388 | |
Amaury Forgeot d'Arc | 324ac65 | 2010-08-18 20:44:58 +0000 | [diff] [blame] | 1389 | def test_printable_repr(self): |
| 1390 | self.assertEqual(repr('\U00010000'), "'%c'" % (0x10000,)) # printable |
Martin v. Löwis | baecd72 | 2010-10-11 22:42:28 +0000 | [diff] [blame] | 1391 | self.assertEqual(repr('\U00014000'), "'\\U00014000'") # nonprintable |
Amaury Forgeot d'Arc | 324ac65 | 2010-08-18 20:44:58 +0000 | [diff] [blame] | 1392 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1393 | def test_expandtabs_overflows_gracefully(self): |
| 1394 | # This test only affects 32-bit platforms because expandtabs can only take |
| 1395 | # an int as the max value, not a 64-bit C long. If expandtabs is changed |
| 1396 | # to take a 64-bit long, this test should apply to all platforms. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 1397 | if sys.maxsize > (1 << 32) or struct.calcsize('P') != 4: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1398 | return |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 1399 | self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1400 | |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 1401 | def test_raiseMemError(self): |
| 1402 | # Ensure that the freelist contains a consistent object, even |
| 1403 | # when a string allocation fails with a MemoryError. |
| 1404 | # This used to crash the interpreter, |
| 1405 | # or leak references when the number was smaller. |
Antoine Pitrou | b305aeb | 2008-09-05 22:13:06 +0000 | [diff] [blame] | 1406 | charwidth = 4 if sys.maxunicode >= 0x10000 else 2 |
| 1407 | # Note: sys.maxsize is half of the actual max allocation because of |
| 1408 | # the signedness of Py_ssize_t. |
| 1409 | alloc = lambda: "a" * (sys.maxsize // charwidth * 2) |
Antoine Pitrou | 3db3e87 | 2008-08-17 17:06:51 +0000 | [diff] [blame] | 1410 | self.assertRaises(MemoryError, alloc) |
| 1411 | self.assertRaises(MemoryError, alloc) |
| 1412 | |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 1413 | def test_format_subclass(self): |
| 1414 | class S(str): |
| 1415 | def __str__(self): |
| 1416 | return '__str__ overridden' |
| 1417 | s = S('xxx') |
Florent Xicluna | a87b383 | 2010-09-13 02:28:18 +0000 | [diff] [blame] | 1418 | self.assertEqual("%s" % s, '__str__ overridden') |
| 1419 | self.assertEqual("{}".format(s), '__str__ overridden') |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 1420 | |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1421 | def test_from_format(self): |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1422 | from _testcapi import format_unicode |
| 1423 | |
| 1424 | # ascii format, non-ascii argument |
| 1425 | text = format_unicode(b'ascii\x7f=%U', 'unicode\xe9') |
| 1426 | self.assertEqual(text, 'ascii\x7f=unicode\xe9') |
| 1427 | |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1428 | # non-ascii format, ascii argument: ensure that PyUnicode_FromFormat() |
| 1429 | # raises an error for a non-ascii format string. |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1430 | self.assertRaisesRegexp(ValueError, |
| 1431 | '^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format ' |
Victor Stinner | 4c7db31 | 2010-09-12 07:51:18 +0000 | [diff] [blame] | 1432 | 'string, got a non-ASCII byte: 0xe9$', |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 1433 | format_unicode, b'unicode\xe9=%s', 'ascii') |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 1434 | |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1435 | # other tests |
| 1436 | text = format_unicode(b'%%A:%A', 'abc\xe9\uabcd\U0010ffff') |
| 1437 | self.assertEqual(text, r"%A:'abc\xe9\uabcd\U0010ffff'") |
| 1438 | |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1439 | # Test PyUnicode_AsWideChar() |
| 1440 | def test_aswidechar(self): |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1441 | from _testcapi import unicode_aswidechar |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1442 | from ctypes import c_wchar, sizeof |
| 1443 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1444 | wchar, size = unicode_aswidechar('abcdef', 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1445 | self.assertEqual(size, 2) |
| 1446 | self.assertEqual(wchar, 'ab') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1447 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1448 | wchar, size = unicode_aswidechar('abc', 3) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1449 | self.assertEqual(size, 3) |
| 1450 | self.assertEqual(wchar, 'abc') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1451 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1452 | wchar, size = unicode_aswidechar('abc', 4) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1453 | self.assertEqual(size, 3) |
| 1454 | self.assertEqual(wchar, 'abc\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1455 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1456 | wchar, size = unicode_aswidechar('abc', 10) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1457 | self.assertEqual(size, 3) |
| 1458 | self.assertEqual(wchar, 'abc\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1459 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1460 | wchar, size = unicode_aswidechar('abc\0def', 20) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1461 | self.assertEqual(size, 7) |
| 1462 | self.assertEqual(wchar, 'abc\0def\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1463 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1464 | nonbmp = chr(0x10ffff) |
| 1465 | if sizeof(c_wchar) == 2: |
| 1466 | buflen = 3 |
| 1467 | nchar = 2 |
| 1468 | else: # sizeof(c_wchar) == 4 |
| 1469 | buflen = 2 |
| 1470 | nchar = 1 |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1471 | wchar, size = unicode_aswidechar(nonbmp, buflen) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1472 | self.assertEqual(size, nchar) |
| 1473 | self.assertEqual(wchar, nonbmp + '\0') |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1474 | |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1475 | # Test PyUnicode_AsWideCharString() |
| 1476 | def test_aswidecharstring(self): |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1477 | from _testcapi import unicode_aswidecharstring |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1478 | from ctypes import c_wchar, sizeof |
| 1479 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1480 | wchar, size = unicode_aswidecharstring('abc') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1481 | self.assertEqual(size, 3) |
| 1482 | self.assertEqual(wchar, 'abc\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1483 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1484 | wchar, size = unicode_aswidecharstring('abc\0def') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1485 | self.assertEqual(size, 7) |
| 1486 | self.assertEqual(wchar, 'abc\0def\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1487 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1488 | nonbmp = chr(0x10ffff) |
| 1489 | if sizeof(c_wchar) == 2: |
| 1490 | nchar = 2 |
| 1491 | else: # sizeof(c_wchar) == 4 |
| 1492 | nchar = 1 |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1493 | wchar, size = unicode_aswidecharstring(nonbmp) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 1494 | self.assertEqual(size, nchar) |
| 1495 | self.assertEqual(wchar, nonbmp + '\0') |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 1496 | |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1497 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1498 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1499 | support.run_unittest(__name__) |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 1500 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1501 | if __name__ == "__main__": |
| 1502 | test_main() |