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 | """#" |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 8 | import _string |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 9 | import codecs |
| 10 | import struct |
| 11 | import sys |
| 12 | import unittest |
| 13 | import warnings |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 14 | from test import support, string_tests |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 15 | |
Neal Norwitz | 430f68b | 2005-11-24 22:00:56 +0000 | [diff] [blame] | 16 | # Error handling (bad decoder return) |
| 17 | def search_function(encoding): |
| 18 | def decode1(input, errors="strict"): |
| 19 | return 42 # not a tuple |
| 20 | def encode1(input, errors="strict"): |
| 21 | return 42 # not a tuple |
| 22 | def encode2(input, errors="strict"): |
| 23 | return (42, 42) # no unicode |
| 24 | def decode2(input, errors="strict"): |
| 25 | return (42, 42) # no unicode |
| 26 | if encoding=="test.unicode1": |
| 27 | return (encode1, decode1, None, None) |
| 28 | elif encoding=="test.unicode2": |
| 29 | return (encode2, decode2, None, None) |
| 30 | else: |
| 31 | return None |
| 32 | codecs.register(search_function) |
| 33 | |
Brett Cannon | 226b230 | 2010-03-20 22:22:22 +0000 | [diff] [blame] | 34 | class UnicodeTest(string_tests.CommonTest, |
| 35 | string_tests.MixinStrUnicodeUserStringTest, |
Ezio Melotti | 0dceb56 | 2013-01-10 07:43:26 +0200 | [diff] [blame] | 36 | string_tests.MixinStrUnicodeTest, |
| 37 | unittest.TestCase): |
Brett Cannon | 226b230 | 2010-03-20 22:22:22 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 39 | type2test = str |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 40 | |
| 41 | def checkequalnofix(self, result, object, methodname, *args): |
| 42 | method = getattr(object, methodname) |
| 43 | realresult = method(*args) |
| 44 | self.assertEqual(realresult, result) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 45 | self.assertTrue(type(realresult) is type(result)) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 46 | |
| 47 | # if the original is returned make sure that |
| 48 | # this doesn't happen with subclasses |
| 49 | if realresult is object: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 50 | class usub(str): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 51 | def __repr__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 52 | return 'usub(%r)' % str.__repr__(self) |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 53 | object = usub(object) |
| 54 | method = getattr(object, methodname) |
| 55 | realresult = method(*args) |
| 56 | self.assertEqual(realresult, result) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 57 | self.assertTrue(object is not realresult) |
Guido van Rossum | e4874ae | 2001-09-21 15:36:41 +0000 | [diff] [blame] | 58 | |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 59 | def test_literals(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 60 | self.assertEqual('\xff', '\u00ff') |
| 61 | self.assertEqual('\uffff', '\U0000ffff') |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 62 | self.assertRaises(SyntaxError, eval, '\'\\Ufffffffe\'') |
| 63 | self.assertRaises(SyntaxError, eval, '\'\\Uffffffff\'') |
| 64 | self.assertRaises(SyntaxError, eval, '\'\\U%08x\'' % 0x110000) |
Benjamin Peterson | cd76c27 | 2008-04-05 15:09:30 +0000 | [diff] [blame] | 65 | # raw strings should not have unicode escapes |
Florent Xicluna | a87b383 | 2010-09-13 02:28:18 +0000 | [diff] [blame] | 66 | self.assertNotEqual(r"\u0020", " ") |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 67 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 68 | def test_ascii(self): |
| 69 | if not sys.platform.startswith('java'): |
| 70 | # Test basic sanity of repr() |
| 71 | self.assertEqual(ascii('abc'), "'abc'") |
| 72 | self.assertEqual(ascii('ab\\c'), "'ab\\\\c'") |
| 73 | self.assertEqual(ascii('ab\\'), "'ab\\\\'") |
| 74 | self.assertEqual(ascii('\\c'), "'\\\\c'") |
| 75 | self.assertEqual(ascii('\\'), "'\\\\'") |
| 76 | self.assertEqual(ascii('\n'), "'\\n'") |
| 77 | self.assertEqual(ascii('\r'), "'\\r'") |
| 78 | self.assertEqual(ascii('\t'), "'\\t'") |
| 79 | self.assertEqual(ascii('\b'), "'\\x08'") |
| 80 | self.assertEqual(ascii("'\""), """'\\'"'""") |
| 81 | self.assertEqual(ascii("'\""), """'\\'"'""") |
| 82 | self.assertEqual(ascii("'"), '''"'"''') |
| 83 | self.assertEqual(ascii('"'), """'"'""") |
| 84 | latin1repr = ( |
| 85 | "'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r" |
| 86 | "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a" |
| 87 | "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI" |
| 88 | "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f" |
| 89 | "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d" |
| 90 | "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b" |
| 91 | "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9" |
| 92 | "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7" |
| 93 | "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5" |
| 94 | "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3" |
| 95 | "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1" |
| 96 | "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef" |
| 97 | "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd" |
| 98 | "\\xfe\\xff'") |
| 99 | testrepr = ascii(''.join(map(chr, range(256)))) |
| 100 | self.assertEqual(testrepr, latin1repr) |
| 101 | # Test ascii works on wide unicode escapes without overflow. |
| 102 | self.assertEqual(ascii("\U00010000" * 39 + "\uffff" * 4096), |
| 103 | ascii("\U00010000" * 39 + "\uffff" * 4096)) |
| 104 | |
| 105 | class WrongRepr: |
| 106 | def __repr__(self): |
| 107 | return b'byte-repr' |
| 108 | self.assertRaises(TypeError, ascii, WrongRepr()) |
| 109 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 110 | def test_repr(self): |
| 111 | if not sys.platform.startswith('java'): |
| 112 | # Test basic sanity of repr() |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 113 | self.assertEqual(repr('abc'), "'abc'") |
| 114 | self.assertEqual(repr('ab\\c'), "'ab\\\\c'") |
| 115 | self.assertEqual(repr('ab\\'), "'ab\\\\'") |
| 116 | self.assertEqual(repr('\\c'), "'\\\\c'") |
| 117 | self.assertEqual(repr('\\'), "'\\\\'") |
| 118 | self.assertEqual(repr('\n'), "'\\n'") |
| 119 | self.assertEqual(repr('\r'), "'\\r'") |
| 120 | self.assertEqual(repr('\t'), "'\\t'") |
| 121 | self.assertEqual(repr('\b'), "'\\x08'") |
| 122 | self.assertEqual(repr("'\""), """'\\'"'""") |
| 123 | self.assertEqual(repr("'\""), """'\\'"'""") |
| 124 | self.assertEqual(repr("'"), '''"'"''') |
| 125 | self.assertEqual(repr('"'), """'"'""") |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 126 | latin1repr = ( |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 127 | "'\\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] | 128 | "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a" |
| 129 | "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI" |
| 130 | "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f" |
| 131 | "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d" |
| 132 | "\\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] | 133 | "\\x9c\\x9d\\x9e\\x9f\\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9" |
| 134 | "\xaa\xab\xac\\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7" |
| 135 | "\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5" |
| 136 | "\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3" |
| 137 | "\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1" |
| 138 | "\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" |
| 139 | "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd" |
| 140 | "\xfe\xff'") |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 141 | testrepr = repr(''.join(map(chr, range(256)))) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 142 | self.assertEqual(testrepr, latin1repr) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 143 | # Test repr works on wide unicode escapes without overflow. |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 144 | self.assertEqual(repr("\U00010000" * 39 + "\uffff" * 4096), |
| 145 | repr("\U00010000" * 39 + "\uffff" * 4096)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 146 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 147 | class WrongRepr: |
| 148 | def __repr__(self): |
| 149 | return b'byte-repr' |
| 150 | self.assertRaises(TypeError, repr, WrongRepr()) |
| 151 | |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 152 | def test_iterators(self): |
| 153 | # Make sure unicode objects have an __iter__ method |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 154 | it = "\u1111\u2222\u3333".__iter__() |
| 155 | self.assertEqual(next(it), "\u1111") |
| 156 | self.assertEqual(next(it), "\u2222") |
| 157 | self.assertEqual(next(it), "\u3333") |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 158 | self.assertRaises(StopIteration, next, it) |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 159 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 160 | def test_count(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 161 | string_tests.CommonTest.test_count(self) |
| 162 | # check mixed argument types |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 163 | self.checkequalnofix(3, 'aaa', 'count', 'a') |
| 164 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 165 | self.checkequalnofix(3, 'aaa', 'count', 'a') |
| 166 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 167 | self.checkequalnofix(0, 'aaa', 'count', 'b') |
| 168 | self.checkequalnofix(1, 'aaa', 'count', 'a', -1) |
| 169 | self.checkequalnofix(3, 'aaa', 'count', 'a', -10) |
| 170 | self.checkequalnofix(2, 'aaa', 'count', 'a', 0, -1) |
| 171 | self.checkequalnofix(0, 'aaa', 'count', 'a', 0, -10) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 172 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 173 | def test_find(self): |
Antoine Pitrou | c0bbe7d | 2011-10-08 22:41:35 +0200 | [diff] [blame] | 174 | string_tests.CommonTest.test_find(self) |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 175 | # test implementation details of the memchr fast path |
| 176 | self.checkequal(100, 'a' * 100 + '\u0102', 'find', '\u0102') |
| 177 | self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0201') |
| 178 | self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0120') |
| 179 | self.checkequal(-1, 'a' * 100 + '\u0102', 'find', '\u0220') |
| 180 | self.checkequal(100, 'a' * 100 + '\U00100304', 'find', '\U00100304') |
| 181 | self.checkequal(-1, 'a' * 100 + '\U00100304', 'find', '\U00100204') |
| 182 | self.checkequal(-1, 'a' * 100 + '\U00100304', 'find', '\U00102004') |
| 183 | # check mixed argument types |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 184 | self.checkequalnofix(0, 'abcdefghiabc', 'find', 'abc') |
| 185 | self.checkequalnofix(9, 'abcdefghiabc', 'find', 'abc', 1) |
| 186 | self.checkequalnofix(-1, 'abcdefghiabc', 'find', 'def', 4) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 188 | self.assertRaises(TypeError, 'hello'.find) |
| 189 | self.assertRaises(TypeError, 'hello'.find, 42) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 190 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 191 | def test_rfind(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 192 | string_tests.CommonTest.test_rfind(self) |
Antoine Pitrou | 2c3b230 | 2011-10-11 20:29:21 +0200 | [diff] [blame] | 193 | # test implementation details of the memrchr fast path |
| 194 | self.checkequal(0, '\u0102' + 'a' * 100 , 'rfind', '\u0102') |
| 195 | self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0201') |
| 196 | self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0120') |
| 197 | self.checkequal(-1, '\u0102' + 'a' * 100 , 'rfind', '\u0220') |
| 198 | self.checkequal(0, '\U00100304' + 'a' * 100, 'rfind', '\U00100304') |
| 199 | self.checkequal(-1, '\U00100304' + 'a' * 100, 'rfind', '\U00100204') |
| 200 | self.checkequal(-1, '\U00100304' + 'a' * 100, 'rfind', '\U00102004') |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 201 | # check mixed argument types |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 202 | self.checkequalnofix(9, 'abcdefghiabc', 'rfind', 'abc') |
| 203 | self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') |
| 204 | self.checkequalnofix(12, 'abcdefghiabc', 'rfind', '') |
Guido van Rossum | 8b26454 | 2000-12-19 02:22:31 +0000 | [diff] [blame] | 205 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 206 | def test_index(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 207 | string_tests.CommonTest.test_index(self) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 208 | self.checkequalnofix(0, 'abcdefghiabc', 'index', '') |
| 209 | self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def') |
| 210 | self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc') |
| 211 | self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1) |
| 212 | self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib') |
| 213 | self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1) |
| 214 | self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8) |
| 215 | self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 216 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 217 | def test_rindex(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 218 | string_tests.CommonTest.test_rindex(self) |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 219 | self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '') |
| 220 | self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def') |
| 221 | self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc') |
| 222 | self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 223 | |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 224 | self.assertRaises(ValueError, 'abcdefghiabc'.rindex, 'hib') |
| 225 | self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1) |
| 226 | self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1) |
| 227 | self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8) |
| 228 | self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 229 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 230 | def test_maketrans_translate(self): |
| 231 | # these work with plain translate() |
| 232 | self.checkequalnofix('bbbc', 'abababc', 'translate', |
| 233 | {ord('a'): None}) |
| 234 | self.checkequalnofix('iiic', 'abababc', 'translate', |
| 235 | {ord('a'): None, ord('b'): ord('i')}) |
| 236 | self.checkequalnofix('iiix', 'abababc', 'translate', |
| 237 | {ord('a'): None, ord('b'): ord('i'), ord('c'): 'x'}) |
| 238 | self.checkequalnofix('c', 'abababc', 'translate', |
| 239 | {ord('a'): None, ord('b'): ''}) |
| 240 | self.checkequalnofix('xyyx', 'xzx', 'translate', |
| 241 | {ord('z'): 'yy'}) |
| 242 | # this needs maketrans() |
| 243 | self.checkequalnofix('abababc', 'abababc', 'translate', |
| 244 | {'b': '<i>'}) |
| 245 | tbl = self.type2test.maketrans({'a': None, 'b': '<i>'}) |
| 246 | self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl) |
| 247 | # test alternative way of calling maketrans() |
| 248 | tbl = self.type2test.maketrans('abc', 'xyz', 'd') |
| 249 | self.checkequalnofix('xyzzy', 'abdcdcbdddd', 'translate', tbl) |
| 250 | |
| 251 | self.assertRaises(TypeError, self.type2test.maketrans) |
| 252 | self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg') |
| 253 | self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def') |
| 254 | self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 2) |
| 255 | self.assertRaises(TypeError, self.type2test.maketrans, 'abc', 'def', 2) |
| 256 | self.assertRaises(ValueError, self.type2test.maketrans, {'xy': 2}) |
| 257 | self.assertRaises(TypeError, self.type2test.maketrans, {(1,): 2}) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 259 | self.assertRaises(TypeError, 'hello'.translate) |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 260 | self.assertRaises(TypeError, 'abababc'.translate, 'abc', 'xyz') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 261 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 262 | def test_split(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 263 | string_tests.CommonTest.test_split(self) |
Andrew M. Kuchling | eddd68d | 2002-03-29 16:21:44 +0000 | [diff] [blame] | 264 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 265 | # Mixed arguments |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 266 | self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
| 267 | self.checkequalnofix(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//') |
| 268 | self.checkequalnofix(['endcase ', ''], 'endcase test', 'split', 'test') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 269 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 270 | def test_join(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 271 | string_tests.MixinStrUnicodeUserStringTest.test_join(self) |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 273 | class MyWrapper: |
| 274 | def __init__(self, sval): self.sval = sval |
| 275 | def __str__(self): return self.sval |
| 276 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 277 | # mixed arguments |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 278 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 279 | self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
| 280 | self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz')) |
| 281 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 282 | self.checkequalnofix('a b c d', ' ', 'join', ['a', 'b', 'c', 'd']) |
| 283 | self.checkequalnofix('abcd', '', 'join', ('a', 'b', 'c', 'd')) |
| 284 | self.checkequalnofix('w x y z', ' ', 'join', string_tests.Sequence('wxyz')) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 285 | self.checkraises(TypeError, ' ', 'join', ['1', '2', MyWrapper('foo')]) |
| 286 | self.checkraises(TypeError, ' ', 'join', ['1', '2', '3', bytes()]) |
| 287 | self.checkraises(TypeError, ' ', 'join', [1, 2, 3]) |
| 288 | self.checkraises(TypeError, ' ', 'join', ['1', '2', 3]) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 289 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 290 | def test_replace(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 291 | string_tests.CommonTest.test_replace(self) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 292 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 293 | # method call forwarded from str implementation because of unicode argument |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 294 | self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1) |
| 295 | self.assertRaises(TypeError, 'replace'.replace, "r", 42) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 296 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 297 | @support.cpython_only |
| 298 | def test_replace_id(self): |
Victor Stinner | 1d972ad | 2011-10-07 13:31:46 +0200 | [diff] [blame] | 299 | pattern = 'abc' |
| 300 | text = 'abc def' |
| 301 | self.assertIs(text.replace(pattern, pattern), text) |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 302 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 303 | def test_bytes_comparison(self): |
Brett Cannon | 226b230 | 2010-03-20 22:22:22 +0000 | [diff] [blame] | 304 | with support.check_warnings(): |
| 305 | warnings.simplefilter('ignore', BytesWarning) |
| 306 | self.assertEqual('abc' == b'abc', False) |
| 307 | self.assertEqual('abc' != b'abc', True) |
| 308 | self.assertEqual('abc' == bytearray(b'abc'), False) |
| 309 | self.assertEqual('abc' != bytearray(b'abc'), True) |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 310 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 311 | def test_comparison(self): |
| 312 | # Comparisons: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 313 | self.assertEqual('abc', 'abc') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 314 | self.assertTrue('abcd' > 'abc') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 315 | self.assertTrue('abc' < 'abcd') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 316 | |
| 317 | if 0: |
| 318 | # Move these tests to a Unicode collation module test... |
| 319 | # Testing UTF-16 code point order comparisons... |
| 320 | |
| 321 | # No surrogates, no fixup required. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 322 | self.assertTrue('\u0061' < '\u20ac') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 323 | # Non surrogate below surrogate value, no fixup required |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 324 | self.assertTrue('\u0061' < '\ud800\udc02') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 325 | |
| 326 | # Non surrogate above surrogate value, fixup required |
| 327 | def test_lecmp(s, s2): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 328 | self.assertTrue(s < s2) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 329 | |
| 330 | def test_fixup(s): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 331 | s2 = '\ud800\udc01' |
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 = '\ud900\udc01' |
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 = '\uda00\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 336 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 337 | s2 = '\udb00\udc01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 338 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 339 | s2 = '\ud800\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 340 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 341 | s2 = '\ud900\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 342 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 343 | s2 = '\uda00\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 344 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 345 | s2 = '\udb00\udd01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 346 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 347 | s2 = '\ud800\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 348 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 349 | s2 = '\ud900\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 350 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 351 | s2 = '\uda00\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 352 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 353 | s2 = '\udb00\ude01' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 354 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 355 | s2 = '\ud800\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 356 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 357 | s2 = '\ud900\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 358 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 359 | s2 = '\uda00\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 360 | test_lecmp(s, s2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 361 | s2 = '\udb00\udfff' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 362 | test_lecmp(s, s2) |
| 363 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 364 | test_fixup('\ue000') |
| 365 | test_fixup('\uff61') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 366 | |
| 367 | # Surrogates on both sides, no fixup required |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 368 | self.assertTrue('\ud800\udc02' < '\ud84d\udc56') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 369 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 370 | def test_islower(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 371 | string_tests.MixinStrUnicodeUserStringTest.test_islower(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 372 | self.checkequalnofix(False, '\u1FFc', 'islower') |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 373 | self.assertFalse('\u2167'.islower()) |
| 374 | self.assertTrue('\u2177'.islower()) |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 375 | # non-BMP, uppercase |
| 376 | self.assertFalse('\U00010401'.islower()) |
| 377 | self.assertFalse('\U00010427'.islower()) |
| 378 | # non-BMP, lowercase |
| 379 | self.assertTrue('\U00010429'.islower()) |
| 380 | self.assertTrue('\U0001044E'.islower()) |
| 381 | # non-BMP, non-cased |
| 382 | self.assertFalse('\U0001F40D'.islower()) |
| 383 | self.assertFalse('\U0001F46F'.islower()) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 384 | |
| 385 | def test_isupper(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 386 | string_tests.MixinStrUnicodeUserStringTest.test_isupper(self) |
| 387 | if not sys.platform.startswith('java'): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 388 | self.checkequalnofix(False, '\u1FFc', 'isupper') |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 389 | self.assertTrue('\u2167'.isupper()) |
| 390 | self.assertFalse('\u2177'.isupper()) |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 391 | # non-BMP, uppercase |
| 392 | self.assertTrue('\U00010401'.isupper()) |
| 393 | self.assertTrue('\U00010427'.isupper()) |
| 394 | # non-BMP, lowercase |
| 395 | self.assertFalse('\U00010429'.isupper()) |
| 396 | self.assertFalse('\U0001044E'.isupper()) |
| 397 | # non-BMP, non-cased |
| 398 | self.assertFalse('\U0001F40D'.isupper()) |
| 399 | self.assertFalse('\U0001F46F'.isupper()) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 400 | |
| 401 | def test_istitle(self): |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 402 | string_tests.MixinStrUnicodeUserStringTest.test_istitle(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 403 | self.checkequalnofix(True, '\u1FFc', 'istitle') |
| 404 | self.checkequalnofix(True, 'Greek \u1FFcitlecases ...', 'istitle') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 405 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 406 | # non-BMP, uppercase + lowercase |
| 407 | self.assertTrue('\U00010401\U00010429'.istitle()) |
| 408 | self.assertTrue('\U00010427\U0001044E'.istitle()) |
| 409 | # apparently there are no titlecased (Lt) non-BMP chars in Unicode 6 |
| 410 | for ch in ['\U00010429', '\U0001044E', '\U0001F40D', '\U0001F46F']: |
| 411 | self.assertFalse(ch.istitle(), '{!a} is not title'.format(ch)) |
| 412 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 413 | def test_isspace(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 414 | string_tests.MixinStrUnicodeUserStringTest.test_isspace(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 415 | self.checkequalnofix(True, '\u2000', 'isspace') |
| 416 | self.checkequalnofix(True, '\u200a', 'isspace') |
| 417 | self.checkequalnofix(False, '\u2014', 'isspace') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 418 | # apparently there are no non-BMP spaces chars in Unicode 6 |
| 419 | for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E', |
| 420 | '\U0001F40D', '\U0001F46F']: |
| 421 | self.assertFalse(ch.isspace(), '{!a} is not space.'.format(ch)) |
| 422 | |
| 423 | def test_isalnum(self): |
| 424 | string_tests.MixinStrUnicodeUserStringTest.test_isalnum(self) |
| 425 | for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E', |
| 426 | '\U0001D7F6', '\U00011066', '\U000104A0', '\U0001F107']: |
| 427 | self.assertTrue(ch.isalnum(), '{!a} is alnum.'.format(ch)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 428 | |
| 429 | def test_isalpha(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 430 | string_tests.MixinStrUnicodeUserStringTest.test_isalpha(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 431 | self.checkequalnofix(True, '\u1FFc', 'isalpha') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 432 | # non-BMP, cased |
| 433 | self.assertTrue('\U00010401'.isalpha()) |
| 434 | self.assertTrue('\U00010427'.isalpha()) |
| 435 | self.assertTrue('\U00010429'.isalpha()) |
| 436 | self.assertTrue('\U0001044E'.isalpha()) |
| 437 | # non-BMP, non-cased |
| 438 | self.assertFalse('\U0001F40D'.isalpha()) |
| 439 | self.assertFalse('\U0001F46F'.isalpha()) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 440 | |
| 441 | def test_isdecimal(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 442 | self.checkequalnofix(False, '', 'isdecimal') |
| 443 | self.checkequalnofix(False, 'a', 'isdecimal') |
| 444 | self.checkequalnofix(True, '0', 'isdecimal') |
| 445 | self.checkequalnofix(False, '\u2460', 'isdecimal') # CIRCLED DIGIT ONE |
| 446 | self.checkequalnofix(False, '\xbc', 'isdecimal') # VULGAR FRACTION ONE QUARTER |
| 447 | self.checkequalnofix(True, '\u0660', 'isdecimal') # ARABIC-INDIC DIGIT ZERO |
| 448 | self.checkequalnofix(True, '0123456789', 'isdecimal') |
| 449 | self.checkequalnofix(False, '0123456789a', 'isdecimal') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 450 | |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 451 | self.checkraises(TypeError, 'abc', 'isdecimal', 42) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 452 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 453 | for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E', |
| 454 | '\U0001F40D', '\U0001F46F', '\U00011065', '\U0001F107']: |
| 455 | self.assertFalse(ch.isdecimal(), '{!a} is not decimal.'.format(ch)) |
| 456 | for ch in ['\U0001D7F6', '\U00011066', '\U000104A0']: |
| 457 | self.assertTrue(ch.isdecimal(), '{!a} is decimal.'.format(ch)) |
| 458 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 459 | def test_isdigit(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 460 | string_tests.MixinStrUnicodeUserStringTest.test_isdigit(self) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 461 | self.checkequalnofix(True, '\u2460', 'isdigit') |
| 462 | self.checkequalnofix(False, '\xbc', 'isdigit') |
| 463 | self.checkequalnofix(True, '\u0660', 'isdigit') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 464 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 465 | for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E', |
| 466 | '\U0001F40D', '\U0001F46F', '\U00011065']: |
| 467 | self.assertFalse(ch.isdigit(), '{!a} is not a digit.'.format(ch)) |
| 468 | for ch in ['\U0001D7F6', '\U00011066', '\U000104A0', '\U0001F107']: |
| 469 | self.assertTrue(ch.isdigit(), '{!a} is a digit.'.format(ch)) |
| 470 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 471 | def test_isnumeric(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 472 | self.checkequalnofix(False, '', 'isnumeric') |
| 473 | self.checkequalnofix(False, 'a', 'isnumeric') |
| 474 | self.checkequalnofix(True, '0', 'isnumeric') |
| 475 | self.checkequalnofix(True, '\u2460', 'isnumeric') |
| 476 | self.checkequalnofix(True, '\xbc', 'isnumeric') |
| 477 | self.checkequalnofix(True, '\u0660', 'isnumeric') |
| 478 | self.checkequalnofix(True, '0123456789', 'isnumeric') |
| 479 | self.checkequalnofix(False, '0123456789a', 'isnumeric') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 480 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 481 | self.assertRaises(TypeError, "abc".isnumeric, 42) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 482 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 483 | for ch in ['\U00010401', '\U00010427', '\U00010429', '\U0001044E', |
| 484 | '\U0001F40D', '\U0001F46F']: |
| 485 | self.assertFalse(ch.isnumeric(), '{!a} is not numeric.'.format(ch)) |
| 486 | for ch in ['\U00011065', '\U0001D7F6', '\U00011066', |
| 487 | '\U000104A0', '\U0001F107']: |
| 488 | self.assertTrue(ch.isnumeric(), '{!a} is numeric.'.format(ch)) |
| 489 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 490 | def test_isidentifier(self): |
| 491 | self.assertTrue("a".isidentifier()) |
| 492 | self.assertTrue("Z".isidentifier()) |
| 493 | self.assertTrue("_".isidentifier()) |
| 494 | self.assertTrue("b0".isidentifier()) |
| 495 | self.assertTrue("bc".isidentifier()) |
| 496 | self.assertTrue("b_".isidentifier()) |
Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 497 | self.assertTrue("µ".isidentifier()) |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 498 | self.assertTrue("𝔘𝔫𝔦𝔠𝔬𝔡𝔢".isidentifier()) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 499 | |
| 500 | self.assertFalse(" ".isidentifier()) |
| 501 | self.assertFalse("[".isidentifier()) |
Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 502 | self.assertFalse("©".isidentifier()) |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 503 | self.assertFalse("0".isidentifier()) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 504 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 505 | def test_isprintable(self): |
| 506 | self.assertTrue("".isprintable()) |
Benjamin Peterson | 0983274 | 2009-03-26 17:15:46 +0000 | [diff] [blame] | 507 | self.assertTrue(" ".isprintable()) |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 508 | self.assertTrue("abcdefg".isprintable()) |
| 509 | self.assertFalse("abcdefg\n".isprintable()) |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 510 | # some defined Unicode character |
| 511 | self.assertTrue("\u0374".isprintable()) |
| 512 | # undefined character |
Amaury Forgeot d'Arc | a083f1e | 2008-09-10 23:51:42 +0000 | [diff] [blame] | 513 | self.assertFalse("\u0378".isprintable()) |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 514 | # single surrogate character |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 515 | self.assertFalse("\ud800".isprintable()) |
| 516 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 517 | self.assertTrue('\U0001F46F'.isprintable()) |
| 518 | self.assertFalse('\U000E0020'.isprintable()) |
| 519 | |
| 520 | def test_surrogates(self): |
| 521 | for s in ('a\uD800b\uDFFF', 'a\uDFFFb\uD800', |
| 522 | 'a\uD800b\uDFFFa', 'a\uDFFFb\uD800a'): |
| 523 | self.assertTrue(s.islower()) |
| 524 | self.assertFalse(s.isupper()) |
| 525 | self.assertFalse(s.istitle()) |
| 526 | for s in ('A\uD800B\uDFFF', 'A\uDFFFB\uD800', |
| 527 | 'A\uD800B\uDFFFA', 'A\uDFFFB\uD800A'): |
| 528 | self.assertFalse(s.islower()) |
| 529 | self.assertTrue(s.isupper()) |
| 530 | self.assertTrue(s.istitle()) |
| 531 | |
| 532 | for meth_name in ('islower', 'isupper', 'istitle'): |
| 533 | meth = getattr(str, meth_name) |
| 534 | for s in ('\uD800', '\uDFFF', '\uD800\uD800', '\uDFFF\uDFFF'): |
| 535 | self.assertFalse(meth(s), '%a.%s() is False' % (s, meth_name)) |
| 536 | |
| 537 | for meth_name in ('isalpha', 'isalnum', 'isdigit', 'isspace', |
| 538 | 'isdecimal', 'isnumeric', |
| 539 | 'isidentifier', 'isprintable'): |
| 540 | meth = getattr(str, meth_name) |
| 541 | for s in ('\uD800', '\uDFFF', '\uD800\uD800', '\uDFFF\uDFFF', |
| 542 | 'a\uD800b\uDFFF', 'a\uDFFFb\uD800', |
| 543 | 'a\uD800b\uDFFFa', 'a\uDFFFb\uD800a'): |
| 544 | self.assertFalse(meth(s), '%a.%s() is False' % (s, meth_name)) |
| 545 | |
| 546 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 547 | def test_lower(self): |
| 548 | string_tests.CommonTest.test_lower(self) |
| 549 | self.assertEqual('\U00010427'.lower(), '\U0001044F') |
| 550 | self.assertEqual('\U00010427\U00010427'.lower(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 551 | '\U0001044F\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 552 | self.assertEqual('\U00010427\U0001044F'.lower(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 553 | '\U0001044F\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 554 | self.assertEqual('X\U00010427x\U0001044F'.lower(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 555 | 'x\U0001044Fx\U0001044F') |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 556 | self.assertEqual('fi'.lower(), 'fi') |
| 557 | self.assertEqual('\u0130'.lower(), '\u0069\u0307') |
| 558 | # Special case for GREEK CAPITAL LETTER SIGMA U+03A3 |
| 559 | self.assertEqual('\u03a3'.lower(), '\u03c3') |
| 560 | self.assertEqual('\u0345\u03a3'.lower(), '\u0345\u03c3') |
| 561 | self.assertEqual('A\u0345\u03a3'.lower(), 'a\u0345\u03c2') |
| 562 | self.assertEqual('A\u0345\u03a3a'.lower(), 'a\u0345\u03c3a') |
| 563 | self.assertEqual('A\u0345\u03a3'.lower(), 'a\u0345\u03c2') |
| 564 | self.assertEqual('A\u03a3\u0345'.lower(), 'a\u03c2\u0345') |
| 565 | self.assertEqual('\u03a3\u0345 '.lower(), '\u03c3\u0345 ') |
| 566 | self.assertEqual('\U0008fffe'.lower(), '\U0008fffe') |
| 567 | self.assertEqual('\u2177'.lower(), '\u2177') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 568 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 569 | def test_casefold(self): |
| 570 | self.assertEqual('hello'.casefold(), 'hello') |
| 571 | self.assertEqual('hELlo'.casefold(), 'hello') |
| 572 | self.assertEqual('ß'.casefold(), 'ss') |
| 573 | self.assertEqual('fi'.casefold(), 'fi') |
| 574 | self.assertEqual('\u03a3'.casefold(), '\u03c3') |
| 575 | self.assertEqual('A\u0345\u03a3'.casefold(), 'a\u03b9\u03c3') |
Benjamin Peterson | 4eda937 | 2012-08-05 15:05:34 -0700 | [diff] [blame] | 576 | self.assertEqual('\u00b5'.casefold(), '\u03bc') |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 577 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 578 | def test_upper(self): |
| 579 | string_tests.CommonTest.test_upper(self) |
| 580 | self.assertEqual('\U0001044F'.upper(), '\U00010427') |
| 581 | self.assertEqual('\U0001044F\U0001044F'.upper(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 582 | '\U00010427\U00010427') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 583 | self.assertEqual('\U00010427\U0001044F'.upper(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 584 | '\U00010427\U00010427') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 585 | self.assertEqual('X\U00010427x\U0001044F'.upper(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 586 | 'X\U00010427X\U00010427') |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 587 | self.assertEqual('fi'.upper(), 'FI') |
| 588 | self.assertEqual('\u0130'.upper(), '\u0130') |
| 589 | self.assertEqual('\u03a3'.upper(), '\u03a3') |
| 590 | self.assertEqual('ß'.upper(), 'SS') |
| 591 | self.assertEqual('\u1fd2'.upper(), '\u0399\u0308\u0300') |
| 592 | self.assertEqual('\U0008fffe'.upper(), '\U0008fffe') |
| 593 | self.assertEqual('\u2177'.upper(), '\u2167') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 594 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 595 | def test_capitalize(self): |
| 596 | string_tests.CommonTest.test_capitalize(self) |
| 597 | self.assertEqual('\U0001044F'.capitalize(), '\U00010427') |
| 598 | self.assertEqual('\U0001044F\U0001044F'.capitalize(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 599 | '\U00010427\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 600 | self.assertEqual('\U00010427\U0001044F'.capitalize(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 601 | '\U00010427\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 602 | self.assertEqual('\U0001044F\U00010427'.capitalize(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 603 | '\U00010427\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 604 | self.assertEqual('X\U00010427x\U0001044F'.capitalize(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 605 | 'X\U0001044Fx\U0001044F') |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 606 | self.assertEqual('h\u0130'.capitalize(), 'H\u0069\u0307') |
| 607 | exp = '\u0399\u0308\u0300\u0069\u0307' |
| 608 | self.assertEqual('\u1fd2\u0130'.capitalize(), exp) |
| 609 | self.assertEqual('finnish'.capitalize(), 'FInnish') |
| 610 | self.assertEqual('A\u0345\u03a3'.capitalize(), 'A\u0345\u03c2') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 611 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 612 | def test_title(self): |
| 613 | string_tests.MixinStrUnicodeUserStringTest.test_title(self) |
| 614 | self.assertEqual('\U0001044F'.title(), '\U00010427') |
| 615 | self.assertEqual('\U0001044F\U0001044F'.title(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 616 | '\U00010427\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 617 | self.assertEqual('\U0001044F\U0001044F \U0001044F\U0001044F'.title(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 618 | '\U00010427\U0001044F \U00010427\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 619 | self.assertEqual('\U00010427\U0001044F \U00010427\U0001044F'.title(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 620 | '\U00010427\U0001044F \U00010427\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 621 | self.assertEqual('\U0001044F\U00010427 \U0001044F\U00010427'.title(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 622 | '\U00010427\U0001044F \U00010427\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 623 | self.assertEqual('X\U00010427x\U0001044F X\U00010427x\U0001044F'.title(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 624 | 'X\U0001044Fx\U0001044F X\U0001044Fx\U0001044F') |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 625 | self.assertEqual('fiNNISH'.title(), 'Finnish') |
| 626 | self.assertEqual('A\u03a3 \u1fa1xy'.title(), 'A\u03c2 \u1fa9xy') |
| 627 | self.assertEqual('A\u03a3A'.title(), 'A\u03c3a') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 628 | |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 629 | def test_swapcase(self): |
| 630 | string_tests.CommonTest.test_swapcase(self) |
| 631 | self.assertEqual('\U0001044F'.swapcase(), '\U00010427') |
| 632 | self.assertEqual('\U00010427'.swapcase(), '\U0001044F') |
| 633 | self.assertEqual('\U0001044F\U0001044F'.swapcase(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 634 | '\U00010427\U00010427') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 635 | self.assertEqual('\U00010427\U0001044F'.swapcase(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 636 | '\U0001044F\U00010427') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 637 | self.assertEqual('\U0001044F\U00010427'.swapcase(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 638 | '\U00010427\U0001044F') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 639 | self.assertEqual('X\U00010427x\U0001044F'.swapcase(), |
Ezio Melotti | a5c92b4 | 2011-08-23 00:37:08 +0300 | [diff] [blame] | 640 | 'x\U0001044FX\U00010427') |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 641 | self.assertEqual('fi'.swapcase(), 'FI') |
| 642 | self.assertEqual('\u0130'.swapcase(), '\u0069\u0307') |
| 643 | # Special case for GREEK CAPITAL LETTER SIGMA U+03A3 |
| 644 | self.assertEqual('\u03a3'.swapcase(), '\u03c3') |
| 645 | self.assertEqual('\u0345\u03a3'.swapcase(), '\u0399\u03c3') |
| 646 | self.assertEqual('A\u0345\u03a3'.swapcase(), 'a\u0399\u03c2') |
| 647 | self.assertEqual('A\u0345\u03a3a'.swapcase(), 'a\u0399\u03c3A') |
| 648 | self.assertEqual('A\u0345\u03a3'.swapcase(), 'a\u0399\u03c2') |
| 649 | self.assertEqual('A\u03a3\u0345'.swapcase(), 'a\u03c2\u0399') |
| 650 | self.assertEqual('\u03a3\u0345 '.swapcase(), '\u03c3\u0399 ') |
| 651 | self.assertEqual('\u03a3'.swapcase(), '\u03c3') |
| 652 | self.assertEqual('ß'.swapcase(), 'SS') |
| 653 | self.assertEqual('\u1fd2'.swapcase(), '\u0399\u0308\u0300') |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 654 | |
Ezio Melotti | f84e01d | 2013-07-08 17:48:29 +0200 | [diff] [blame] | 655 | def test_center(self): |
| 656 | string_tests.CommonTest.test_center(self) |
| 657 | self.assertEqual('x'.center(2, '\U0010FFFF'), |
| 658 | 'x\U0010FFFF') |
| 659 | self.assertEqual('x'.center(3, '\U0010FFFF'), |
| 660 | '\U0010FFFFx\U0010FFFF') |
| 661 | self.assertEqual('x'.center(4, '\U0010FFFF'), |
| 662 | '\U0010FFFFx\U0010FFFF\U0010FFFF') |
| 663 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 664 | def test_contains(self): |
| 665 | # Testing Unicode contains method |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 666 | self.assertIn('a', 'abdb') |
| 667 | self.assertIn('a', 'bdab') |
| 668 | self.assertIn('a', 'bdaba') |
| 669 | self.assertIn('a', 'bdba') |
| 670 | self.assertNotIn('a', 'bdb') |
| 671 | self.assertIn('a', 'bdba') |
| 672 | self.assertIn('a', ('a',1,None)) |
| 673 | self.assertIn('a', (1,None,'a')) |
| 674 | self.assertIn('a', ('a',1,None)) |
| 675 | self.assertIn('a', (1,None,'a')) |
| 676 | self.assertNotIn('a', ('x',1,'y')) |
| 677 | self.assertNotIn('a', ('x',1,None)) |
| 678 | self.assertNotIn('abcd', 'abcxxxx') |
| 679 | self.assertIn('ab', 'abcd') |
| 680 | self.assertIn('ab', 'abc') |
| 681 | self.assertIn('ab', (1,None,'ab')) |
| 682 | self.assertIn('', 'abc') |
| 683 | self.assertIn('', '') |
| 684 | self.assertIn('', 'abc') |
| 685 | self.assertNotIn('\0', 'abc') |
| 686 | self.assertIn('\0', '\0abc') |
| 687 | self.assertIn('\0', 'abc\0') |
| 688 | self.assertIn('a', '\0abc') |
| 689 | self.assertIn('asdf', 'asdf') |
| 690 | self.assertNotIn('asdf', 'asd') |
| 691 | self.assertNotIn('asdf', '') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 692 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 693 | self.assertRaises(TypeError, "abc".__contains__) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 694 | |
Serhiy Storchaka | 31b1c8b | 2013-06-12 09:20:44 +0300 | [diff] [blame] | 695 | def test_issue18183(self): |
| 696 | '\U00010000\U00100000'.lower() |
| 697 | '\U00010000\U00100000'.casefold() |
| 698 | '\U00010000\U00100000'.upper() |
| 699 | '\U00010000\U00100000'.capitalize() |
| 700 | '\U00010000\U00100000'.title() |
| 701 | '\U00010000\U00100000'.swapcase() |
| 702 | '\U00100000'.center(3, '\U00010000') |
| 703 | '\U00100000'.ljust(3, '\U00010000') |
| 704 | '\U00100000'.rjust(3, '\U00010000') |
| 705 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 706 | def test_format(self): |
| 707 | self.assertEqual(''.format(), '') |
| 708 | self.assertEqual('a'.format(), 'a') |
| 709 | self.assertEqual('ab'.format(), 'ab') |
| 710 | self.assertEqual('a{{'.format(), 'a{') |
| 711 | self.assertEqual('a}}'.format(), 'a}') |
| 712 | self.assertEqual('{{b'.format(), '{b') |
| 713 | self.assertEqual('}}b'.format(), '}b') |
| 714 | self.assertEqual('a{{b'.format(), 'a{b') |
| 715 | |
| 716 | # examples from the PEP: |
| 717 | import datetime |
| 718 | self.assertEqual("My name is {0}".format('Fred'), "My name is Fred") |
| 719 | self.assertEqual("My name is {0[name]}".format(dict(name='Fred')), |
| 720 | "My name is Fred") |
| 721 | self.assertEqual("My name is {0} :-{{}}".format('Fred'), |
| 722 | "My name is Fred :-{}") |
| 723 | |
| 724 | d = datetime.date(2007, 8, 18) |
| 725 | self.assertEqual("The year is {0.year}".format(d), |
| 726 | "The year is 2007") |
| 727 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 728 | # classes we'll use for testing |
| 729 | class C: |
| 730 | def __init__(self, x=100): |
| 731 | self._x = x |
| 732 | def __format__(self, spec): |
| 733 | return spec |
| 734 | |
| 735 | class D: |
| 736 | def __init__(self, x): |
| 737 | self.x = x |
| 738 | def __format__(self, spec): |
| 739 | return str(self.x) |
| 740 | |
| 741 | # class with __str__, but no __format__ |
| 742 | class E: |
| 743 | def __init__(self, x): |
| 744 | self.x = x |
| 745 | def __str__(self): |
| 746 | return 'E(' + self.x + ')' |
| 747 | |
| 748 | # class with __repr__, but no __format__ or __str__ |
| 749 | class F: |
| 750 | def __init__(self, x): |
| 751 | self.x = x |
| 752 | def __repr__(self): |
| 753 | return 'F(' + self.x + ')' |
| 754 | |
| 755 | # class with __format__ that forwards to string, for some format_spec's |
| 756 | class G: |
| 757 | def __init__(self, x): |
| 758 | self.x = x |
| 759 | def __str__(self): |
| 760 | return "string is " + self.x |
| 761 | def __format__(self, format_spec): |
| 762 | if format_spec == 'd': |
| 763 | return 'G(' + self.x + ')' |
| 764 | return object.__format__(self, format_spec) |
| 765 | |
Eric Smith | 739e2ad | 2007-08-27 19:07:22 +0000 | [diff] [blame] | 766 | class I(datetime.date): |
| 767 | def __format__(self, format_spec): |
| 768 | return self.strftime(format_spec) |
| 769 | |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 770 | class J(int): |
| 771 | def __format__(self, format_spec): |
| 772 | return int.__format__(self * 2, format_spec) |
| 773 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 774 | |
| 775 | self.assertEqual(''.format(), '') |
| 776 | self.assertEqual('abc'.format(), 'abc') |
| 777 | self.assertEqual('{0}'.format('abc'), 'abc') |
| 778 | self.assertEqual('{0:}'.format('abc'), 'abc') |
| 779 | # self.assertEqual('{ 0 }'.format('abc'), 'abc') |
| 780 | self.assertEqual('X{0}'.format('abc'), 'Xabc') |
| 781 | self.assertEqual('{0}X'.format('abc'), 'abcX') |
| 782 | self.assertEqual('X{0}Y'.format('abc'), 'XabcY') |
| 783 | self.assertEqual('{1}'.format(1, 'abc'), 'abc') |
| 784 | self.assertEqual('X{1}'.format(1, 'abc'), 'Xabc') |
| 785 | self.assertEqual('{1}X'.format(1, 'abc'), 'abcX') |
| 786 | self.assertEqual('X{1}Y'.format(1, 'abc'), 'XabcY') |
| 787 | self.assertEqual('{0}'.format(-15), '-15') |
| 788 | self.assertEqual('{0}{1}'.format(-15, 'abc'), '-15abc') |
| 789 | self.assertEqual('{0}X{1}'.format(-15, 'abc'), '-15Xabc') |
| 790 | self.assertEqual('{{'.format(), '{') |
| 791 | self.assertEqual('}}'.format(), '}') |
| 792 | self.assertEqual('{{}}'.format(), '{}') |
| 793 | self.assertEqual('{{x}}'.format(), '{x}') |
| 794 | self.assertEqual('{{{0}}}'.format(123), '{123}') |
| 795 | self.assertEqual('{{{{0}}}}'.format(), '{{0}}') |
| 796 | self.assertEqual('}}{{'.format(), '}{') |
| 797 | self.assertEqual('}}x{{'.format(), '}x{') |
| 798 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 799 | # weird field names |
| 800 | self.assertEqual("{0[foo-bar]}".format({'foo-bar':'baz'}), 'baz') |
| 801 | self.assertEqual("{0[foo bar]}".format({'foo bar':'baz'}), 'baz') |
Eric Smith | 4cb4e4e | 2007-09-03 08:40:29 +0000 | [diff] [blame] | 802 | self.assertEqual("{0[ ]}".format({' ':3}), '3') |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 803 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 804 | self.assertEqual('{foo._x}'.format(foo=C(20)), '20') |
| 805 | self.assertEqual('{1}{0}'.format(D(10), D(20)), '2010') |
| 806 | self.assertEqual('{0._x.x}'.format(C(D('abc'))), 'abc') |
| 807 | self.assertEqual('{0[0]}'.format(['abc', 'def']), 'abc') |
| 808 | self.assertEqual('{0[1]}'.format(['abc', 'def']), 'def') |
| 809 | self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def') |
| 810 | self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def') |
| 811 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 812 | # strings |
| 813 | self.assertEqual('{0:.3s}'.format('abc'), 'abc') |
| 814 | self.assertEqual('{0:.3s}'.format('ab'), 'ab') |
| 815 | self.assertEqual('{0:.3s}'.format('abcdef'), 'abc') |
| 816 | self.assertEqual('{0:.0s}'.format('abcdef'), '') |
| 817 | self.assertEqual('{0:3.3s}'.format('abc'), 'abc') |
| 818 | self.assertEqual('{0:2.3s}'.format('abc'), 'abc') |
| 819 | self.assertEqual('{0:2.2s}'.format('abc'), 'ab') |
| 820 | self.assertEqual('{0:3.2s}'.format('abc'), 'ab ') |
| 821 | self.assertEqual('{0:x<0s}'.format('result'), 'result') |
| 822 | self.assertEqual('{0:x<5s}'.format('result'), 'result') |
| 823 | self.assertEqual('{0:x<6s}'.format('result'), 'result') |
| 824 | self.assertEqual('{0:x<7s}'.format('result'), 'resultx') |
| 825 | self.assertEqual('{0:x<8s}'.format('result'), 'resultxx') |
| 826 | self.assertEqual('{0: <7s}'.format('result'), 'result ') |
| 827 | self.assertEqual('{0:<7s}'.format('result'), 'result ') |
| 828 | self.assertEqual('{0:>7s}'.format('result'), ' result') |
| 829 | self.assertEqual('{0:>8s}'.format('result'), ' result') |
| 830 | self.assertEqual('{0:^8s}'.format('result'), ' result ') |
| 831 | self.assertEqual('{0:^9s}'.format('result'), ' result ') |
| 832 | self.assertEqual('{0:^10s}'.format('result'), ' result ') |
| 833 | self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999) |
| 834 | self.assertEqual('{0:10000}'.format(''), ' ' * 10000) |
| 835 | self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000) |
| 836 | |
| 837 | # format specifiers for user defined type |
| 838 | self.assertEqual('{0:abc}'.format(C()), 'abc') |
| 839 | |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 840 | # !r, !s and !a coercions |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 841 | self.assertEqual('{0!s}'.format('Hello'), 'Hello') |
| 842 | self.assertEqual('{0!s:}'.format('Hello'), 'Hello') |
| 843 | self.assertEqual('{0!s:15}'.format('Hello'), 'Hello ') |
| 844 | self.assertEqual('{0!s:15s}'.format('Hello'), 'Hello ') |
| 845 | self.assertEqual('{0!r}'.format('Hello'), "'Hello'") |
| 846 | self.assertEqual('{0!r:}'.format('Hello'), "'Hello'") |
| 847 | self.assertEqual('{0!r}'.format(F('Hello')), 'F(Hello)') |
Amaury Forgeot d'Arc | a083f1e | 2008-09-10 23:51:42 +0000 | [diff] [blame] | 848 | self.assertEqual('{0!r}'.format('\u0378'), "'\\u0378'") # nonprintable |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 849 | self.assertEqual('{0!r}'.format('\u0374'), "'\u0374'") # printable |
| 850 | self.assertEqual('{0!r}'.format(F('\u0374')), 'F(\u0374)') |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 851 | self.assertEqual('{0!a}'.format('Hello'), "'Hello'") |
Amaury Forgeot d'Arc | a083f1e | 2008-09-10 23:51:42 +0000 | [diff] [blame] | 852 | self.assertEqual('{0!a}'.format('\u0378'), "'\\u0378'") # nonprintable |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 853 | self.assertEqual('{0!a}'.format('\u0374'), "'\\u0374'") # printable |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 854 | self.assertEqual('{0!a:}'.format('Hello'), "'Hello'") |
| 855 | self.assertEqual('{0!a}'.format(F('Hello')), 'F(Hello)') |
Georg Brandl | d52429f | 2008-07-04 15:55:02 +0000 | [diff] [blame] | 856 | self.assertEqual('{0!a}'.format(F('\u0374')), 'F(\\u0374)') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 857 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 858 | # test fallback to object.__format__ |
| 859 | self.assertEqual('{0}'.format({}), '{}') |
| 860 | self.assertEqual('{0}'.format([]), '[]') |
| 861 | self.assertEqual('{0}'.format([1]), '[1]') |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 862 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 863 | self.assertEqual('{0:d}'.format(G('data')), 'G(data)') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 864 | self.assertEqual('{0!s}'.format(G('data')), 'string is data') |
| 865 | |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 866 | msg = 'object.__format__ with a non-empty format string is deprecated' |
Eric V. Smith | b9cd353 | 2011-03-12 10:08:48 -0500 | [diff] [blame] | 867 | with support.check_warnings((msg, DeprecationWarning)): |
Eric Smith | e4d6317 | 2010-09-13 20:48:43 +0000 | [diff] [blame] | 868 | self.assertEqual('{0:^10}'.format(E('data')), ' E(data) ') |
| 869 | self.assertEqual('{0:^10s}'.format(E('data')), ' E(data) ') |
| 870 | self.assertEqual('{0:>15s}'.format(G('data')), ' string is data') |
| 871 | |
Eric Smith | 739e2ad | 2007-08-27 19:07:22 +0000 | [diff] [blame] | 872 | self.assertEqual("{0:date: %Y-%m-%d}".format(I(year=2007, |
| 873 | month=8, |
| 874 | day=27)), |
| 875 | "date: 2007-08-27") |
| 876 | |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 877 | # test deriving from a builtin type and overriding __format__ |
| 878 | self.assertEqual("{0}".format(J(10)), "20") |
| 879 | |
| 880 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 881 | # string format specifiers |
| 882 | self.assertEqual('{0:}'.format('a'), 'a') |
| 883 | |
| 884 | # computed format specifiers |
| 885 | self.assertEqual("{0:.{1}}".format('hello world', 5), 'hello') |
| 886 | self.assertEqual("{0:.{1}s}".format('hello world', 5), 'hello') |
| 887 | self.assertEqual("{0:.{precision}s}".format('hello world', precision=5), 'hello') |
| 888 | self.assertEqual("{0:{width}.{precision}s}".format('hello world', width=10, precision=5), 'hello ') |
| 889 | self.assertEqual("{0:{width}.{precision}s}".format('hello world', width='10', precision='5'), 'hello ') |
| 890 | |
| 891 | # test various errors |
| 892 | self.assertRaises(ValueError, '{'.format) |
| 893 | self.assertRaises(ValueError, '}'.format) |
| 894 | self.assertRaises(ValueError, 'a{'.format) |
| 895 | self.assertRaises(ValueError, 'a}'.format) |
| 896 | self.assertRaises(ValueError, '{a'.format) |
| 897 | self.assertRaises(ValueError, '}a'.format) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 898 | self.assertRaises(IndexError, '{0}'.format) |
| 899 | self.assertRaises(IndexError, '{1}'.format, 'abc') |
| 900 | self.assertRaises(KeyError, '{x}'.format) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 901 | self.assertRaises(ValueError, "}{".format) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 902 | self.assertRaises(ValueError, "abc{0:{}".format) |
| 903 | self.assertRaises(ValueError, "{0".format) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 904 | self.assertRaises(IndexError, "{0.}".format) |
| 905 | self.assertRaises(ValueError, "{0.}".format, 0) |
| 906 | self.assertRaises(IndexError, "{0[}".format) |
Eric Smith | 4cb4e4e | 2007-09-03 08:40:29 +0000 | [diff] [blame] | 907 | self.assertRaises(ValueError, "{0[}".format, []) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 908 | self.assertRaises(KeyError, "{0]}".format) |
| 909 | self.assertRaises(ValueError, "{0.[]}".format, 0) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 910 | self.assertRaises(ValueError, "{0..foo}".format, 0) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 911 | self.assertRaises(ValueError, "{0[0}".format, 0) |
| 912 | self.assertRaises(ValueError, "{0[0:foo}".format, 0) |
| 913 | self.assertRaises(KeyError, "{c]}".format) |
| 914 | self.assertRaises(ValueError, "{{ {{{0}}".format, 0) |
| 915 | self.assertRaises(ValueError, "{0}}".format, 0) |
| 916 | self.assertRaises(KeyError, "{foo}".format, bar=3) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 917 | self.assertRaises(ValueError, "{0!x}".format, 3) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 918 | self.assertRaises(ValueError, "{0!}".format, 0) |
| 919 | self.assertRaises(ValueError, "{0!rs}".format, 0) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 920 | self.assertRaises(ValueError, "{!}".format) |
Eric Smith | 8ec9044 | 2009-03-14 12:29:34 +0000 | [diff] [blame] | 921 | self.assertRaises(IndexError, "{:}".format) |
| 922 | self.assertRaises(IndexError, "{:s}".format) |
| 923 | self.assertRaises(IndexError, "{}".format) |
Benjamin Peterson | 59a1b2f | 2010-06-07 22:31:26 +0000 | [diff] [blame] | 924 | big = "23098475029384702983476098230754973209482573" |
| 925 | self.assertRaises(ValueError, ("{" + big + "}").format) |
| 926 | self.assertRaises(ValueError, ("{[" + big + "]}").format, [0]) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 927 | |
Eric Smith | 41669ca | 2009-05-23 14:23:22 +0000 | [diff] [blame] | 928 | # issue 6089 |
| 929 | self.assertRaises(ValueError, "{0[0]x}".format, [None]) |
| 930 | self.assertRaises(ValueError, "{0[0](10)}".format, [None]) |
| 931 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 932 | # can't have a replacement on the field name portion |
| 933 | self.assertRaises(TypeError, '{0[{1}]}'.format, 'abcdefg', 4) |
| 934 | |
| 935 | # exceed maximum recursion depth |
| 936 | self.assertRaises(ValueError, "{0:{1:{2}}}".format, 'abc', 's', '') |
| 937 | self.assertRaises(ValueError, "{0:{1:{2:{3:{4:{5:{6}}}}}}}".format, |
| 938 | 0, 1, 2, 3, 4, 5, 6, 7) |
| 939 | |
| 940 | # string format spec errors |
| 941 | self.assertRaises(ValueError, "{0:-s}".format, '') |
| 942 | self.assertRaises(ValueError, format, "", "-") |
| 943 | self.assertRaises(ValueError, "{0:=s}".format, '') |
| 944 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 945 | # Alternate formatting is not supported |
| 946 | self.assertRaises(ValueError, format, '', '#') |
| 947 | self.assertRaises(ValueError, format, '', '#20') |
| 948 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 949 | # Non-ASCII |
| 950 | self.assertEqual("{0:s}{1:s}".format("ABC", "\u0410\u0411\u0412"), |
| 951 | 'ABC\u0410\u0411\u0412') |
| 952 | self.assertEqual("{0:.3s}".format("ABC\u0410\u0411\u0412"), |
| 953 | 'ABC') |
| 954 | self.assertEqual("{0:.0s}".format("ABC\u0410\u0411\u0412"), |
| 955 | '') |
| 956 | |
Benjamin Peterson | d2b58a9 | 2013-05-17 17:34:30 -0500 | [diff] [blame] | 957 | self.assertEqual("{[{}]}".format({"{}": 5}), "5") |
| 958 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 959 | def test_format_map(self): |
| 960 | self.assertEqual(''.format_map({}), '') |
| 961 | self.assertEqual('a'.format_map({}), 'a') |
| 962 | self.assertEqual('ab'.format_map({}), 'ab') |
| 963 | self.assertEqual('a{{'.format_map({}), 'a{') |
| 964 | self.assertEqual('a}}'.format_map({}), 'a}') |
| 965 | self.assertEqual('{{b'.format_map({}), '{b') |
| 966 | self.assertEqual('}}b'.format_map({}), '}b') |
| 967 | self.assertEqual('a{{b'.format_map({}), 'a{b') |
| 968 | |
| 969 | # using mappings |
| 970 | class Mapping(dict): |
| 971 | def __missing__(self, key): |
| 972 | return key |
| 973 | self.assertEqual('{hello}'.format_map(Mapping()), 'hello') |
| 974 | self.assertEqual('{a} {world}'.format_map(Mapping(a='hello')), 'hello world') |
| 975 | |
| 976 | class InternalMapping: |
| 977 | def __init__(self): |
| 978 | self.mapping = {'a': 'hello'} |
| 979 | def __getitem__(self, key): |
| 980 | return self.mapping[key] |
| 981 | self.assertEqual('{a}'.format_map(InternalMapping()), 'hello') |
| 982 | |
| 983 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 984 | class C: |
| 985 | def __init__(self, x=100): |
| 986 | self._x = x |
| 987 | def __format__(self, spec): |
| 988 | return spec |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 989 | self.assertEqual('{foo._x}'.format_map({'foo': C(20)}), '20') |
| 990 | |
| 991 | # test various errors |
Eric V. Smith | edbb6ca | 2012-03-12 15:16:22 -0700 | [diff] [blame] | 992 | self.assertRaises(TypeError, ''.format_map) |
| 993 | self.assertRaises(TypeError, 'a'.format_map) |
| 994 | |
| 995 | self.assertRaises(ValueError, '{'.format_map, {}) |
| 996 | self.assertRaises(ValueError, '}'.format_map, {}) |
| 997 | self.assertRaises(ValueError, 'a{'.format_map, {}) |
| 998 | self.assertRaises(ValueError, 'a}'.format_map, {}) |
| 999 | self.assertRaises(ValueError, '{a'.format_map, {}) |
| 1000 | self.assertRaises(ValueError, '}a'.format_map, {}) |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 1001 | |
Eric V. Smith | 12ebefc | 2011-07-18 14:03:41 -0400 | [diff] [blame] | 1002 | # issue #12579: can't supply positional params to format_map |
| 1003 | self.assertRaises(ValueError, '{}'.format_map, {'a' : 2}) |
| 1004 | self.assertRaises(ValueError, '{}'.format_map, 'a') |
| 1005 | self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1}) |
| 1006 | |
Mark Dickinson | fb90c09 | 2012-10-28 10:18:03 +0000 | [diff] [blame] | 1007 | def test_format_huge_precision(self): |
| 1008 | format_string = ".{}f".format(sys.maxsize + 1) |
| 1009 | with self.assertRaises(ValueError): |
| 1010 | result = format(2.34, format_string) |
| 1011 | |
| 1012 | def test_format_huge_width(self): |
| 1013 | format_string = "{}f".format(sys.maxsize + 1) |
| 1014 | with self.assertRaises(ValueError): |
| 1015 | result = format(2.34, format_string) |
| 1016 | |
| 1017 | def test_format_huge_item_number(self): |
| 1018 | format_string = "{{{}:.6f}}".format(sys.maxsize + 1) |
| 1019 | with self.assertRaises(ValueError): |
| 1020 | result = format_string.format(2.34) |
| 1021 | |
Eric Smith | 8ec9044 | 2009-03-14 12:29:34 +0000 | [diff] [blame] | 1022 | def test_format_auto_numbering(self): |
| 1023 | class C: |
| 1024 | def __init__(self, x=100): |
| 1025 | self._x = x |
| 1026 | def __format__(self, spec): |
| 1027 | return spec |
| 1028 | |
| 1029 | self.assertEqual('{}'.format(10), '10') |
| 1030 | self.assertEqual('{:5}'.format('s'), 's ') |
| 1031 | self.assertEqual('{!r}'.format('s'), "'s'") |
| 1032 | self.assertEqual('{._x}'.format(C(10)), '10') |
| 1033 | self.assertEqual('{[1]}'.format([1, 2]), '2') |
| 1034 | self.assertEqual('{[a]}'.format({'a':4, 'b':2}), '4') |
| 1035 | self.assertEqual('a{}b{}c'.format(0, 1), 'a0b1c') |
| 1036 | |
| 1037 | self.assertEqual('a{:{}}b'.format('x', '^10'), 'a x b') |
| 1038 | self.assertEqual('a{:{}x}b'.format(20, '#'), 'a0x14b') |
| 1039 | |
| 1040 | # can't mix and match numbering and auto-numbering |
| 1041 | self.assertRaises(ValueError, '{}{1}'.format, 1, 2) |
| 1042 | self.assertRaises(ValueError, '{1}{}'.format, 1, 2) |
| 1043 | self.assertRaises(ValueError, '{:{1}}'.format, 1, 2) |
| 1044 | self.assertRaises(ValueError, '{0:{}}'.format, 1, 2) |
| 1045 | |
| 1046 | # can mix and match auto-numbering and named |
| 1047 | self.assertEqual('{f}{}'.format(4, f='test'), 'test4') |
| 1048 | self.assertEqual('{}{f}'.format(4, f='test'), '4test') |
| 1049 | self.assertEqual('{:{f}}{g}{}'.format(1, 3, g='g', f=2), ' 1g3') |
| 1050 | self.assertEqual('{f:{}}{}{g}'.format(2, 4, f=1, g='g'), ' 14g') |
| 1051 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1052 | def test_formatting(self): |
Walter Dörwald | 0fd583c | 2003-02-21 12:53:50 +0000 | [diff] [blame] | 1053 | string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1054 | # Testing Unicode formatting strings... |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1055 | self.assertEqual("%s, %s" % ("abc", "abc"), 'abc, abc') |
| 1056 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, 2, 3), 'abc, abc, 1, 2.000000, 3.00') |
| 1057 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", 1, -2, 3), 'abc, abc, 1, -2.000000, 3.00') |
| 1058 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.5), 'abc, abc, -1, -2.000000, 3.50') |
| 1059 | self.assertEqual("%s, %s, %i, %f, %5.2f" % ("abc", "abc", -1, -2, 3.57), 'abc, abc, -1, -2.000000, 3.57') |
| 1060 | 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] | 1061 | if not sys.platform.startswith('java'): |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1062 | self.assertEqual("%r, %r" % (b"abc", "abc"), "b'abc', 'abc'") |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 1063 | self.assertEqual("%r" % ("\u1234",), "'\u1234'") |
| 1064 | self.assertEqual("%a" % ("\u1234",), "'\\u1234'") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1065 | self.assertEqual("%(x)s, %(y)s" % {'x':"abc", 'y':"def"}, 'abc, def') |
| 1066 | 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] | 1067 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1068 | self.assertEqual('%c' % 0x1234, '\u1234') |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 1069 | self.assertEqual('%c' % 0x21483, '\U00021483') |
| 1070 | self.assertRaises(OverflowError, "%c".__mod__, (0x110000,)) |
| 1071 | self.assertEqual('%c' % '\U00021483', '\U00021483') |
| 1072 | self.assertRaises(TypeError, "%c".__mod__, "aa") |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 1073 | self.assertRaises(ValueError, "%.1\u1032f".__mod__, (1.0/3)) |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 1074 | self.assertRaises(TypeError, "%i".__mod__, "aa") |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1075 | |
| 1076 | # formatting jobs delegated from the string implementation: |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1077 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1078 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 1079 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 1080 | self.assertEqual('...%(foo)s...' % {'foo':"abc"}, '...abc...') |
| 1081 | self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...') |
| 1082 | self.assertEqual('...%(foo)s...' % {'foo':"abc",'def':123}, '...abc...') |
| 1083 | self.assertEqual('...%s...%s...%s...%s...' % (1,2,3,"abc"), '...1...2...3...abc...') |
| 1084 | self.assertEqual('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,"abc"), '...%...%s...1...2...3...abc...') |
| 1085 | self.assertEqual('...%s...' % "abc", '...abc...') |
| 1086 | self.assertEqual('%*s' % (5,'abc',), ' abc') |
| 1087 | self.assertEqual('%*s' % (-5,'abc',), 'abc ') |
| 1088 | self.assertEqual('%*.*s' % (5,2,'abc',), ' ab') |
| 1089 | self.assertEqual('%*.*s' % (5,3,'abc',), ' abc') |
| 1090 | self.assertEqual('%i %*.*s' % (10, 5,3,'abc',), '10 abc') |
| 1091 | self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, 'abc',), '103 abc') |
| 1092 | self.assertEqual('%c' % 'a', 'a') |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 1093 | class Wrapper: |
| 1094 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1095 | return '\u1234' |
| 1096 | self.assertEqual('%s' % Wrapper(), '\u1234') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1097 | |
Eric Smith | 741191f | 2009-05-06 13:08:15 +0000 | [diff] [blame] | 1098 | # issue 3382 |
| 1099 | NAN = float('nan') |
| 1100 | INF = float('inf') |
| 1101 | self.assertEqual('%f' % NAN, 'nan') |
| 1102 | self.assertEqual('%F' % NAN, 'NAN') |
| 1103 | self.assertEqual('%f' % INF, 'inf') |
| 1104 | self.assertEqual('%F' % INF, 'INF') |
| 1105 | |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 1106 | # PEP 393 |
| 1107 | self.assertEqual('%.1s' % "a\xe9\u20ac", 'a') |
| 1108 | self.assertEqual('%.2s' % "a\xe9\u20ac", 'a\xe9') |
| 1109 | |
Mark Dickinson | fb90c09 | 2012-10-28 10:18:03 +0000 | [diff] [blame] | 1110 | @support.cpython_only |
| 1111 | def test_formatting_huge_precision(self): |
| 1112 | from _testcapi import INT_MAX |
| 1113 | format_string = "%.{}f".format(INT_MAX + 1) |
| 1114 | with self.assertRaises(ValueError): |
| 1115 | result = format_string % 2.34 |
| 1116 | |
| 1117 | def test_formatting_huge_width(self): |
| 1118 | format_string = "%{}f".format(sys.maxsize + 1) |
| 1119 | with self.assertRaises(ValueError): |
| 1120 | result = format_string % 2.34 |
| 1121 | |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1122 | def test_startswith_endswith_errors(self): |
| 1123 | for meth in ('foo'.startswith, 'foo'.endswith): |
Ezio Melotti | f2b3f78 | 2011-04-26 06:40:59 +0300 | [diff] [blame] | 1124 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1125 | meth(['f']) |
Ezio Melotti | f2b3f78 | 2011-04-26 06:40:59 +0300 | [diff] [blame] | 1126 | exc = str(cm.exception) |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1127 | self.assertIn('str', exc) |
| 1128 | self.assertIn('tuple', exc) |
| 1129 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1130 | @support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR') |
Georg Brandl | da6b107 | 2006-01-20 17:48:54 +0000 | [diff] [blame] | 1131 | def test_format_float(self): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1132 | # should not format with a comma, but always with C locale |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1133 | self.assertEqual('1.0', '%.1f' % 1.0) |
Georg Brandl | da6b107 | 2006-01-20 17:48:54 +0000 | [diff] [blame] | 1134 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1135 | def test_constructor(self): |
| 1136 | # unicode(obj) tests (this maps to PyObject_Unicode() at C level) |
| 1137 | |
| 1138 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1139 | str('unicode remains unicode'), |
| 1140 | 'unicode remains unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1141 | ) |
| 1142 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1143 | class UnicodeSubclass(str): |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 1144 | pass |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 1145 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 1146 | for text in ('ascii', '\xe9', '\u20ac', '\U0010FFFF'): |
| 1147 | subclass = UnicodeSubclass(text) |
| 1148 | self.assertEqual(str(subclass), text) |
| 1149 | self.assertEqual(len(subclass), len(text)) |
| 1150 | if text == 'ascii': |
| 1151 | self.assertEqual(subclass.encode('ascii'), b'ascii') |
| 1152 | self.assertEqual(subclass.encode('utf-8'), b'ascii') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1153 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1154 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1155 | str('strings are converted to unicode'), |
| 1156 | 'strings are converted to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1157 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1158 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1159 | class StringCompat: |
| 1160 | def __init__(self, x): |
| 1161 | self.x = x |
| 1162 | def __str__(self): |
| 1163 | return self.x |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1164 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1165 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1166 | str(StringCompat('__str__ compatible objects are recognized')), |
| 1167 | '__str__ compatible objects are recognized' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1168 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1169 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1170 | # unicode(obj) is compatible to str(): |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1171 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1172 | o = StringCompat('unicode(obj) is compatible to str()') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1173 | self.assertEqual(str(o), 'unicode(obj) is compatible to str()') |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1174 | self.assertEqual(str(o), 'unicode(obj) is compatible to str()') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1175 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 1176 | for obj in (123, 123.45, 123): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1177 | self.assertEqual(str(obj), str(str(obj))) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1178 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1179 | # unicode(obj, encoding, error) tests (this maps to |
| 1180 | # PyUnicode_FromEncodedObject() at C level) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1181 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1182 | if not sys.platform.startswith('java'): |
| 1183 | self.assertRaises( |
| 1184 | TypeError, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1185 | str, |
| 1186 | 'decoding unicode is not supported', |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1187 | 'utf-8', |
| 1188 | 'strict' |
| 1189 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1190 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1191 | self.assertEqual( |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1192 | str(b'strings are decoded to unicode', 'utf-8', 'strict'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1193 | 'strings are decoded to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1194 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1195 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1196 | if not sys.platform.startswith('java'): |
| 1197 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1198 | str( |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 1199 | memoryview(b'character buffers are decoded to unicode'), |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1200 | 'utf-8', |
| 1201 | 'strict' |
| 1202 | ), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1203 | 'character buffers are decoded to unicode' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1204 | ) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1205 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1206 | self.assertRaises(TypeError, str, 42, 42, 42) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1207 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1208 | def test_constructor_keyword_args(self): |
| 1209 | """Pass various keyword argument combinations to the constructor.""" |
| 1210 | # The object argument can be passed as a keyword. |
| 1211 | self.assertEqual(str(object='foo'), 'foo') |
| 1212 | self.assertEqual(str(object=b'foo', encoding='utf-8'), 'foo') |
| 1213 | # The errors argument without encoding triggers "decode" mode. |
| 1214 | self.assertEqual(str(b'foo', errors='strict'), 'foo') # not "b'foo'" |
| 1215 | self.assertEqual(str(object=b'foo', errors='strict'), 'foo') |
| 1216 | |
| 1217 | def test_constructor_defaults(self): |
| 1218 | """Check the constructor argument defaults.""" |
| 1219 | # The object argument defaults to '' or b''. |
| 1220 | self.assertEqual(str(), '') |
| 1221 | self.assertEqual(str(errors='strict'), '') |
| 1222 | utf8_cent = '¢'.encode('utf-8') |
| 1223 | # The encoding argument defaults to utf-8. |
| 1224 | self.assertEqual(str(utf8_cent, errors='strict'), '¢') |
| 1225 | # The errors argument defaults to strict. |
| 1226 | self.assertRaises(UnicodeDecodeError, str, utf8_cent, encoding='ascii') |
| 1227 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1228 | def test_codecs_utf7(self): |
| 1229 | utfTests = [ |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1230 | ('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example |
| 1231 | ('Hi Mom -\u263a-!', b'Hi Mom -+Jjo--!'), # RFC2152 example |
| 1232 | ('\u65E5\u672C\u8A9E', b'+ZeVnLIqe-'), # RFC2152 example |
| 1233 | ('Item 3 is \u00a31.', b'Item 3 is +AKM-1.'), # RFC2152 example |
| 1234 | ('+', b'+-'), |
| 1235 | ('+-', b'+--'), |
| 1236 | ('+?', b'+-?'), |
| 1237 | ('\?', b'+AFw?'), |
| 1238 | ('+?', b'+-?'), |
| 1239 | (r'\\?', b'+AFwAXA?'), |
| 1240 | (r'\\\?', b'+AFwAXABc?'), |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1241 | (r'++--', b'+-+---'), |
| 1242 | ('\U000abcde', b'+2m/c3g-'), # surrogate pairs |
| 1243 | ('/', b'/'), |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1244 | ] |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1245 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1246 | for (x, y) in utfTests: |
| 1247 | self.assertEqual(x.encode('utf-7'), y) |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 1249 | # Unpaired surrogates are passed through |
| 1250 | self.assertEqual('\uD801'.encode('utf-7'), b'+2AE-') |
| 1251 | self.assertEqual('\uD801x'.encode('utf-7'), b'+2AE-x') |
| 1252 | self.assertEqual('\uDC01'.encode('utf-7'), b'+3AE-') |
| 1253 | self.assertEqual('\uDC01x'.encode('utf-7'), b'+3AE-x') |
| 1254 | self.assertEqual(b'+2AE-'.decode('utf-7'), '\uD801') |
| 1255 | self.assertEqual(b'+2AE-x'.decode('utf-7'), '\uD801x') |
| 1256 | self.assertEqual(b'+3AE-'.decode('utf-7'), '\uDC01') |
| 1257 | self.assertEqual(b'+3AE-x'.decode('utf-7'), '\uDC01x') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1258 | |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 1259 | self.assertEqual('\uD801\U000abcde'.encode('utf-7'), b'+2AHab9ze-') |
| 1260 | self.assertEqual(b'+2AHab9ze-'.decode('utf-7'), '\uD801\U000abcde') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1262 | # Issue #2242: crash on some Windows/MSVC versions |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1263 | self.assertEqual(b'+\xc1'.decode('utf-7'), '\xc1') |
| 1264 | |
| 1265 | # Direct encoded characters |
| 1266 | set_d = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?" |
| 1267 | # Optional direct characters |
| 1268 | set_o = '!"#$%&*;<=>@[]^_`{|}' |
| 1269 | for c in set_d: |
| 1270 | self.assertEqual(c.encode('utf7'), c.encode('ascii')) |
| 1271 | self.assertEqual(c.encode('ascii').decode('utf7'), c) |
| 1272 | for c in set_o: |
| 1273 | self.assertEqual(c.encode('ascii').decode('utf7'), c) |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1274 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1275 | def test_codecs_utf8(self): |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1276 | self.assertEqual(''.encode('utf-8'), b'') |
| 1277 | self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac') |
Ezio Melotti | a9860ae | 2011-10-04 19:06:00 +0300 | [diff] [blame] | 1278 | self.assertEqual('\U00010002'.encode('utf-8'), b'\xf0\x90\x80\x82') |
| 1279 | self.assertEqual('\U00023456'.encode('utf-8'), b'\xf0\xa3\x91\x96') |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1280 | self.assertEqual('\ud800'.encode('utf-8', 'surrogatepass'), b'\xed\xa0\x80') |
| 1281 | self.assertEqual('\udc00'.encode('utf-8', 'surrogatepass'), b'\xed\xb0\x80') |
Ezio Melotti | a9860ae | 2011-10-04 19:06:00 +0300 | [diff] [blame] | 1282 | self.assertEqual(('\U00010002'*10).encode('utf-8'), |
| 1283 | b'\xf0\x90\x80\x82'*10) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1284 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1285 | '\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f' |
| 1286 | '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00' |
| 1287 | '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c' |
| 1288 | '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067' |
| 1289 | '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das' |
| 1290 | ' Nunstuck git und'.encode('utf-8'), |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1291 | b'\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81' |
| 1292 | b'\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3' |
| 1293 | b'\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe' |
| 1294 | b'\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83' |
| 1295 | b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8' |
| 1296 | b'\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81' |
| 1297 | b'\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81' |
| 1298 | b'\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3' |
| 1299 | b'\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf' |
| 1300 | b'\xe3\x80\x8cWenn ist das Nunstuck git und' |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1301 | ) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1302 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1303 | # UTF-8 specific decoding tests |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1304 | self.assertEqual(str(b'\xf0\xa3\x91\x96', 'utf-8'), '\U00023456' ) |
| 1305 | self.assertEqual(str(b'\xf0\x90\x80\x82', 'utf-8'), '\U00010002' ) |
| 1306 | self.assertEqual(str(b'\xe2\x82\xac', 'utf-8'), '\u20ac' ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1307 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1308 | # Other possible utf-8 test cases: |
| 1309 | # * strict decoding testing for all of the |
| 1310 | # UTF8_ERROR cases in PyUnicode_DecodeUTF8 |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1311 | |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 1312 | def test_utf8_decode_valid_sequences(self): |
| 1313 | sequences = [ |
| 1314 | # single byte |
| 1315 | (b'\x00', '\x00'), (b'a', 'a'), (b'\x7f', '\x7f'), |
| 1316 | # 2 bytes |
| 1317 | (b'\xc2\x80', '\x80'), (b'\xdf\xbf', '\u07ff'), |
| 1318 | # 3 bytes |
| 1319 | (b'\xe0\xa0\x80', '\u0800'), (b'\xed\x9f\xbf', '\ud7ff'), |
| 1320 | (b'\xee\x80\x80', '\uE000'), (b'\xef\xbf\xbf', '\uffff'), |
| 1321 | # 4 bytes |
| 1322 | (b'\xF0\x90\x80\x80', '\U00010000'), |
| 1323 | (b'\xf4\x8f\xbf\xbf', '\U0010FFFF') |
| 1324 | ] |
| 1325 | for seq, res in sequences: |
| 1326 | self.assertEqual(seq.decode('utf-8'), res) |
| 1327 | |
| 1328 | |
| 1329 | def test_utf8_decode_invalid_sequences(self): |
| 1330 | # continuation bytes in a sequence of 2, 3, or 4 bytes |
| 1331 | continuation_bytes = [bytes([x]) for x in range(0x80, 0xC0)] |
| 1332 | # start bytes of a 2-byte sequence equivalent to codepoints < 0x7F |
| 1333 | invalid_2B_seq_start_bytes = [bytes([x]) for x in range(0xC0, 0xC2)] |
| 1334 | # start bytes of a 4-byte sequence equivalent to codepoints > 0x10FFFF |
| 1335 | invalid_4B_seq_start_bytes = [bytes([x]) for x in range(0xF5, 0xF8)] |
| 1336 | invalid_start_bytes = ( |
| 1337 | continuation_bytes + invalid_2B_seq_start_bytes + |
| 1338 | invalid_4B_seq_start_bytes + [bytes([x]) for x in range(0xF7, 0x100)] |
| 1339 | ) |
| 1340 | |
| 1341 | for byte in invalid_start_bytes: |
| 1342 | self.assertRaises(UnicodeDecodeError, byte.decode, 'utf-8') |
| 1343 | |
| 1344 | for sb in invalid_2B_seq_start_bytes: |
| 1345 | for cb in continuation_bytes: |
| 1346 | self.assertRaises(UnicodeDecodeError, (sb+cb).decode, 'utf-8') |
| 1347 | |
| 1348 | for sb in invalid_4B_seq_start_bytes: |
| 1349 | for cb1 in continuation_bytes[:3]: |
| 1350 | for cb3 in continuation_bytes[:3]: |
| 1351 | self.assertRaises(UnicodeDecodeError, |
| 1352 | (sb+cb1+b'\x80'+cb3).decode, 'utf-8') |
| 1353 | |
| 1354 | for cb in [bytes([x]) for x in range(0x80, 0xA0)]: |
| 1355 | self.assertRaises(UnicodeDecodeError, |
| 1356 | (b'\xE0'+cb+b'\x80').decode, 'utf-8') |
| 1357 | self.assertRaises(UnicodeDecodeError, |
| 1358 | (b'\xE0'+cb+b'\xBF').decode, 'utf-8') |
| 1359 | # surrogates |
| 1360 | for cb in [bytes([x]) for x in range(0xA0, 0xC0)]: |
| 1361 | self.assertRaises(UnicodeDecodeError, |
| 1362 | (b'\xED'+cb+b'\x80').decode, 'utf-8') |
| 1363 | self.assertRaises(UnicodeDecodeError, |
| 1364 | (b'\xED'+cb+b'\xBF').decode, 'utf-8') |
| 1365 | for cb in [bytes([x]) for x in range(0x80, 0x90)]: |
| 1366 | self.assertRaises(UnicodeDecodeError, |
| 1367 | (b'\xF0'+cb+b'\x80\x80').decode, 'utf-8') |
| 1368 | self.assertRaises(UnicodeDecodeError, |
| 1369 | (b'\xF0'+cb+b'\xBF\xBF').decode, 'utf-8') |
| 1370 | for cb in [bytes([x]) for x in range(0x90, 0xC0)]: |
| 1371 | self.assertRaises(UnicodeDecodeError, |
| 1372 | (b'\xF4'+cb+b'\x80\x80').decode, 'utf-8') |
| 1373 | self.assertRaises(UnicodeDecodeError, |
| 1374 | (b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8') |
| 1375 | |
| 1376 | def test_issue8271(self): |
| 1377 | # Issue #8271: during the decoding of an invalid UTF-8 byte sequence, |
| 1378 | # only the start byte and the continuation byte(s) are now considered |
| 1379 | # invalid, instead of the number of bytes specified by the start byte. |
| 1380 | # See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf (page 95, |
| 1381 | # table 3-8, Row 2) for more information about the algorithm used. |
| 1382 | FFFD = '\ufffd' |
| 1383 | sequences = [ |
| 1384 | # invalid start bytes |
| 1385 | (b'\x80', FFFD), # continuation byte |
| 1386 | (b'\x80\x80', FFFD*2), # 2 continuation bytes |
| 1387 | (b'\xc0', FFFD), |
| 1388 | (b'\xc0\xc0', FFFD*2), |
| 1389 | (b'\xc1', FFFD), |
| 1390 | (b'\xc1\xc0', FFFD*2), |
| 1391 | (b'\xc0\xc1', FFFD*2), |
| 1392 | # with start byte of a 2-byte sequence |
| 1393 | (b'\xc2', FFFD), # only the start byte |
| 1394 | (b'\xc2\xc2', FFFD*2), # 2 start bytes |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 1395 | (b'\xc2\xc2\xc2', FFFD*3), # 3 start bytes |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 1396 | (b'\xc2\x41', FFFD+'A'), # invalid continuation byte |
| 1397 | # with start byte of a 3-byte sequence |
| 1398 | (b'\xe1', FFFD), # only the start byte |
| 1399 | (b'\xe1\xe1', FFFD*2), # 2 start bytes |
| 1400 | (b'\xe1\xe1\xe1', FFFD*3), # 3 start bytes |
| 1401 | (b'\xe1\xe1\xe1\xe1', FFFD*4), # 4 start bytes |
| 1402 | (b'\xe1\x80', FFFD), # only 1 continuation byte |
| 1403 | (b'\xe1\x41', FFFD+'A'), # invalid continuation byte |
| 1404 | (b'\xe1\x41\x80', FFFD+'A'+FFFD), # invalid cb followed by valid cb |
| 1405 | (b'\xe1\x41\x41', FFFD+'AA'), # 2 invalid continuation bytes |
| 1406 | (b'\xe1\x80\x41', FFFD+'A'), # only 1 valid continuation byte |
| 1407 | (b'\xe1\x80\xe1\x41', FFFD*2+'A'), # 1 valid and the other invalid |
| 1408 | (b'\xe1\x41\xe1\x80', FFFD+'A'+FFFD), # 1 invalid and the other valid |
| 1409 | # with start byte of a 4-byte sequence |
| 1410 | (b'\xf1', FFFD), # only the start byte |
| 1411 | (b'\xf1\xf1', FFFD*2), # 2 start bytes |
| 1412 | (b'\xf1\xf1\xf1', FFFD*3), # 3 start bytes |
| 1413 | (b'\xf1\xf1\xf1\xf1', FFFD*4), # 4 start bytes |
| 1414 | (b'\xf1\xf1\xf1\xf1\xf1', FFFD*5), # 5 start bytes |
| 1415 | (b'\xf1\x80', FFFD), # only 1 continuation bytes |
| 1416 | (b'\xf1\x80\x80', FFFD), # only 2 continuation bytes |
| 1417 | (b'\xf1\x80\x41', FFFD+'A'), # 1 valid cb and 1 invalid |
| 1418 | (b'\xf1\x80\x41\x41', FFFD+'AA'), # 1 valid cb and 1 invalid |
| 1419 | (b'\xf1\x80\x80\x41', FFFD+'A'), # 2 valid cb and 1 invalid |
| 1420 | (b'\xf1\x41\x80', FFFD+'A'+FFFD), # 1 invalid cv and 1 valid |
| 1421 | (b'\xf1\x41\x80\x80', FFFD+'A'+FFFD*2), # 1 invalid cb and 2 invalid |
| 1422 | (b'\xf1\x41\x80\x41', FFFD+'A'+FFFD+'A'), # 2 invalid cb and 1 invalid |
| 1423 | (b'\xf1\x41\x41\x80', FFFD+'AA'+FFFD), # 1 valid cb and 1 invalid |
| 1424 | (b'\xf1\x41\xf1\x80', FFFD+'A'+FFFD), |
| 1425 | (b'\xf1\x41\x80\xf1', FFFD+'A'+FFFD*2), |
| 1426 | (b'\xf1\xf1\x80\x41', FFFD*2+'A'), |
| 1427 | (b'\xf1\x41\xf1\xf1', FFFD+'A'+FFFD*2), |
| 1428 | # with invalid start byte of a 4-byte sequence (rfc2279) |
| 1429 | (b'\xf5', FFFD), # only the start byte |
| 1430 | (b'\xf5\xf5', FFFD*2), # 2 start bytes |
| 1431 | (b'\xf5\x80', FFFD*2), # only 1 continuation byte |
| 1432 | (b'\xf5\x80\x80', FFFD*3), # only 2 continuation byte |
| 1433 | (b'\xf5\x80\x80\x80', FFFD*4), # 3 continuation bytes |
| 1434 | (b'\xf5\x80\x41', FFFD*2+'A'), # 1 valid cb and 1 invalid |
| 1435 | (b'\xf5\x80\x41\xf5', FFFD*2+'A'+FFFD), |
| 1436 | (b'\xf5\x41\x80\x80\x41', FFFD+'A'+FFFD*2+'A'), |
| 1437 | # with invalid start byte of a 5-byte sequence (rfc2279) |
| 1438 | (b'\xf8', FFFD), # only the start byte |
| 1439 | (b'\xf8\xf8', FFFD*2), # 2 start bytes |
| 1440 | (b'\xf8\x80', FFFD*2), # only one continuation byte |
| 1441 | (b'\xf8\x80\x41', FFFD*2 + 'A'), # 1 valid cb and 1 invalid |
| 1442 | (b'\xf8\x80\x80\x80\x80', FFFD*5), # invalid 5 bytes seq with 5 bytes |
| 1443 | # with invalid start byte of a 6-byte sequence (rfc2279) |
| 1444 | (b'\xfc', FFFD), # only the start byte |
| 1445 | (b'\xfc\xfc', FFFD*2), # 2 start bytes |
| 1446 | (b'\xfc\x80\x80', FFFD*3), # only 2 continuation bytes |
| 1447 | (b'\xfc\x80\x80\x80\x80\x80', FFFD*6), # 6 continuation bytes |
| 1448 | # invalid start byte |
| 1449 | (b'\xfe', FFFD), |
| 1450 | (b'\xfe\x80\x80', FFFD*3), |
| 1451 | # other sequences |
| 1452 | (b'\xf1\x80\x41\x42\x43', '\ufffd\x41\x42\x43'), |
| 1453 | (b'\xf1\x80\xff\x42\x43', '\ufffd\ufffd\x42\x43'), |
| 1454 | (b'\xf1\x80\xc2\x81\x43', '\ufffd\x81\x43'), |
| 1455 | (b'\x61\xF1\x80\x80\xE1\x80\xC2\x62\x80\x63\x80\xBF\x64', |
| 1456 | '\x61\uFFFD\uFFFD\uFFFD\x62\uFFFD\x63\uFFFD\uFFFD\x64'), |
| 1457 | ] |
| 1458 | for n, (seq, res) in enumerate(sequences): |
| 1459 | self.assertRaises(UnicodeDecodeError, seq.decode, 'utf-8', 'strict') |
| 1460 | self.assertEqual(seq.decode('utf-8', 'replace'), res) |
| 1461 | self.assertEqual((seq+b'b').decode('utf-8', 'replace'), res+'b') |
| 1462 | self.assertEqual(seq.decode('utf-8', 'ignore'), |
| 1463 | res.replace('\uFFFD', '')) |
| 1464 | |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 1465 | def to_bytestring(self, seq): |
| 1466 | return bytes(int(c, 16) for c in seq.split()) |
| 1467 | |
| 1468 | def assertCorrectUTF8Decoding(self, seq, res, err): |
| 1469 | """ |
| 1470 | Check that an invalid UTF-8 sequence raises an UnicodeDecodeError when |
| 1471 | 'strict' is used, returns res when 'replace' is used, and that doesn't |
| 1472 | return anything when 'ignore' is used. |
| 1473 | """ |
| 1474 | with self.assertRaises(UnicodeDecodeError) as cm: |
| 1475 | seq.decode('utf-8') |
| 1476 | exc = cm.exception |
| 1477 | |
| 1478 | self.assertIn(err, str(exc)) |
| 1479 | self.assertEqual(seq.decode('utf-8', 'replace'), res) |
| 1480 | self.assertEqual((b'aaaa' + seq + b'bbbb').decode('utf-8', 'replace'), |
| 1481 | 'aaaa' + res + 'bbbb') |
| 1482 | res = res.replace('\ufffd', '') |
| 1483 | self.assertEqual(seq.decode('utf-8', 'ignore'), res) |
| 1484 | self.assertEqual((b'aaaa' + seq + b'bbbb').decode('utf-8', 'ignore'), |
| 1485 | 'aaaa' + res + 'bbbb') |
| 1486 | |
| 1487 | def test_invalid_start_byte(self): |
| 1488 | """ |
| 1489 | Test that an 'invalid start byte' error is raised when the first byte |
| 1490 | is not in the ASCII range or is not a valid start byte of a 2-, 3-, or |
| 1491 | 4-bytes sequence. The invalid start byte is replaced with a single |
| 1492 | U+FFFD when errors='replace'. |
| 1493 | E.g. <80> is a continuation byte and can appear only after a start byte. |
| 1494 | """ |
| 1495 | FFFD = '\ufffd' |
| 1496 | for byte in b'\x80\xA0\x9F\xBF\xC0\xC1\xF5\xFF': |
| 1497 | self.assertCorrectUTF8Decoding(bytes([byte]), '\ufffd', |
| 1498 | 'invalid start byte') |
| 1499 | |
| 1500 | def test_unexpected_end_of_data(self): |
| 1501 | """ |
| 1502 | Test that an 'unexpected end of data' error is raised when the string |
| 1503 | ends after a start byte of a 2-, 3-, or 4-bytes sequence without having |
| 1504 | enough continuation bytes. The incomplete sequence is replaced with a |
| 1505 | single U+FFFD when errors='replace'. |
| 1506 | E.g. in the sequence <F3 80 80>, F3 is the start byte of a 4-bytes |
| 1507 | sequence, but it's followed by only 2 valid continuation bytes and the |
| 1508 | last continuation bytes is missing. |
| 1509 | Note: the continuation bytes must be all valid, if one of them is |
| 1510 | invalid another error will be raised. |
| 1511 | """ |
| 1512 | sequences = [ |
| 1513 | 'C2', 'DF', |
| 1514 | 'E0 A0', 'E0 BF', 'E1 80', 'E1 BF', 'EC 80', 'EC BF', |
| 1515 | 'ED 80', 'ED 9F', 'EE 80', 'EE BF', 'EF 80', 'EF BF', |
| 1516 | 'F0 90', 'F0 BF', 'F0 90 80', 'F0 90 BF', 'F0 BF 80', 'F0 BF BF', |
| 1517 | 'F1 80', 'F1 BF', 'F1 80 80', 'F1 80 BF', 'F1 BF 80', 'F1 BF BF', |
| 1518 | 'F3 80', 'F3 BF', 'F3 80 80', 'F3 80 BF', 'F3 BF 80', 'F3 BF BF', |
| 1519 | 'F4 80', 'F4 8F', 'F4 80 80', 'F4 80 BF', 'F4 8F 80', 'F4 8F BF' |
| 1520 | ] |
| 1521 | FFFD = '\ufffd' |
| 1522 | for seq in sequences: |
| 1523 | self.assertCorrectUTF8Decoding(self.to_bytestring(seq), '\ufffd', |
| 1524 | 'unexpected end of data') |
| 1525 | |
| 1526 | def test_invalid_cb_for_2bytes_seq(self): |
| 1527 | """ |
| 1528 | Test that an 'invalid continuation byte' error is raised when the |
| 1529 | continuation byte of a 2-bytes sequence is invalid. The start byte |
| 1530 | is replaced by a single U+FFFD and the second byte is handled |
| 1531 | separately when errors='replace'. |
| 1532 | E.g. in the sequence <C2 41>, C2 is the start byte of a 2-bytes |
| 1533 | sequence, but 41 is not a valid continuation byte because it's the |
| 1534 | ASCII letter 'A'. |
| 1535 | """ |
| 1536 | FFFD = '\ufffd' |
| 1537 | FFFDx2 = FFFD * 2 |
| 1538 | sequences = [ |
| 1539 | ('C2 00', FFFD+'\x00'), ('C2 7F', FFFD+'\x7f'), |
| 1540 | ('C2 C0', FFFDx2), ('C2 FF', FFFDx2), |
| 1541 | ('DF 00', FFFD+'\x00'), ('DF 7F', FFFD+'\x7f'), |
| 1542 | ('DF C0', FFFDx2), ('DF FF', FFFDx2), |
| 1543 | ] |
| 1544 | for seq, res in sequences: |
| 1545 | self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res, |
| 1546 | 'invalid continuation byte') |
| 1547 | |
| 1548 | def test_invalid_cb_for_3bytes_seq(self): |
| 1549 | """ |
| 1550 | Test that an 'invalid continuation byte' error is raised when the |
| 1551 | continuation byte(s) of a 3-bytes sequence are invalid. When |
| 1552 | errors='replace', if the first continuation byte is valid, the first |
| 1553 | two bytes (start byte + 1st cb) are replaced by a single U+FFFD and the |
| 1554 | third byte is handled separately, otherwise only the start byte is |
| 1555 | replaced with a U+FFFD and the other continuation bytes are handled |
| 1556 | separately. |
| 1557 | E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes |
| 1558 | sequence, 80 is a valid continuation byte, but 41 is not a valid cb |
| 1559 | because it's the ASCII letter 'A'. |
| 1560 | Note: when the start byte is E0 or ED, the valid ranges for the first |
| 1561 | continuation byte are limited to A0..BF and 80..9F respectively. |
| 1562 | Python 2 used to consider all the bytes in range 80..BF valid when the |
| 1563 | start byte was ED. This is fixed in Python 3. |
| 1564 | """ |
| 1565 | FFFD = '\ufffd' |
| 1566 | FFFDx2 = FFFD * 2 |
| 1567 | sequences = [ |
| 1568 | ('E0 00', FFFD+'\x00'), ('E0 7F', FFFD+'\x7f'), ('E0 80', FFFDx2), |
| 1569 | ('E0 9F', FFFDx2), ('E0 C0', FFFDx2), ('E0 FF', FFFDx2), |
| 1570 | ('E0 A0 00', FFFD+'\x00'), ('E0 A0 7F', FFFD+'\x7f'), |
| 1571 | ('E0 A0 C0', FFFDx2), ('E0 A0 FF', FFFDx2), |
| 1572 | ('E0 BF 00', FFFD+'\x00'), ('E0 BF 7F', FFFD+'\x7f'), |
| 1573 | ('E0 BF C0', FFFDx2), ('E0 BF FF', FFFDx2), ('E1 00', FFFD+'\x00'), |
| 1574 | ('E1 7F', FFFD+'\x7f'), ('E1 C0', FFFDx2), ('E1 FF', FFFDx2), |
| 1575 | ('E1 80 00', FFFD+'\x00'), ('E1 80 7F', FFFD+'\x7f'), |
| 1576 | ('E1 80 C0', FFFDx2), ('E1 80 FF', FFFDx2), |
| 1577 | ('E1 BF 00', FFFD+'\x00'), ('E1 BF 7F', FFFD+'\x7f'), |
| 1578 | ('E1 BF C0', FFFDx2), ('E1 BF FF', FFFDx2), ('EC 00', FFFD+'\x00'), |
| 1579 | ('EC 7F', FFFD+'\x7f'), ('EC C0', FFFDx2), ('EC FF', FFFDx2), |
| 1580 | ('EC 80 00', FFFD+'\x00'), ('EC 80 7F', FFFD+'\x7f'), |
| 1581 | ('EC 80 C0', FFFDx2), ('EC 80 FF', FFFDx2), |
| 1582 | ('EC BF 00', FFFD+'\x00'), ('EC BF 7F', FFFD+'\x7f'), |
| 1583 | ('EC BF C0', FFFDx2), ('EC BF FF', FFFDx2), ('ED 00', FFFD+'\x00'), |
| 1584 | ('ED 7F', FFFD+'\x7f'), |
| 1585 | ('ED A0', FFFDx2), ('ED BF', FFFDx2), # see note ^ |
| 1586 | ('ED C0', FFFDx2), ('ED FF', FFFDx2), ('ED 80 00', FFFD+'\x00'), |
| 1587 | ('ED 80 7F', FFFD+'\x7f'), ('ED 80 C0', FFFDx2), |
| 1588 | ('ED 80 FF', FFFDx2), ('ED 9F 00', FFFD+'\x00'), |
| 1589 | ('ED 9F 7F', FFFD+'\x7f'), ('ED 9F C0', FFFDx2), |
| 1590 | ('ED 9F FF', FFFDx2), ('EE 00', FFFD+'\x00'), |
| 1591 | ('EE 7F', FFFD+'\x7f'), ('EE C0', FFFDx2), ('EE FF', FFFDx2), |
| 1592 | ('EE 80 00', FFFD+'\x00'), ('EE 80 7F', FFFD+'\x7f'), |
| 1593 | ('EE 80 C0', FFFDx2), ('EE 80 FF', FFFDx2), |
| 1594 | ('EE BF 00', FFFD+'\x00'), ('EE BF 7F', FFFD+'\x7f'), |
| 1595 | ('EE BF C0', FFFDx2), ('EE BF FF', FFFDx2), ('EF 00', FFFD+'\x00'), |
| 1596 | ('EF 7F', FFFD+'\x7f'), ('EF C0', FFFDx2), ('EF FF', FFFDx2), |
| 1597 | ('EF 80 00', FFFD+'\x00'), ('EF 80 7F', FFFD+'\x7f'), |
| 1598 | ('EF 80 C0', FFFDx2), ('EF 80 FF', FFFDx2), |
| 1599 | ('EF BF 00', FFFD+'\x00'), ('EF BF 7F', FFFD+'\x7f'), |
| 1600 | ('EF BF C0', FFFDx2), ('EF BF FF', FFFDx2), |
| 1601 | ] |
| 1602 | for seq, res in sequences: |
| 1603 | self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res, |
| 1604 | 'invalid continuation byte') |
| 1605 | |
| 1606 | def test_invalid_cb_for_4bytes_seq(self): |
| 1607 | """ |
| 1608 | Test that an 'invalid continuation byte' error is raised when the |
| 1609 | continuation byte(s) of a 4-bytes sequence are invalid. When |
| 1610 | errors='replace',the start byte and all the following valid |
| 1611 | continuation bytes are replaced with a single U+FFFD, and all the bytes |
| 1612 | starting from the first invalid continuation bytes (included) are |
| 1613 | handled separately. |
| 1614 | E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes |
| 1615 | sequence, 80 is a valid continuation byte, but 41 is not a valid cb |
| 1616 | because it's the ASCII letter 'A'. |
| 1617 | Note: when the start byte is E0 or ED, the valid ranges for the first |
| 1618 | continuation byte are limited to A0..BF and 80..9F respectively. |
| 1619 | However, when the start byte is ED, Python 2 considers all the bytes |
| 1620 | in range 80..BF valid. This is fixed in Python 3. |
| 1621 | """ |
| 1622 | FFFD = '\ufffd' |
| 1623 | FFFDx2 = FFFD * 2 |
| 1624 | sequences = [ |
| 1625 | ('F0 00', FFFD+'\x00'), ('F0 7F', FFFD+'\x7f'), ('F0 80', FFFDx2), |
| 1626 | ('F0 8F', FFFDx2), ('F0 C0', FFFDx2), ('F0 FF', FFFDx2), |
| 1627 | ('F0 90 00', FFFD+'\x00'), ('F0 90 7F', FFFD+'\x7f'), |
| 1628 | ('F0 90 C0', FFFDx2), ('F0 90 FF', FFFDx2), |
| 1629 | ('F0 BF 00', FFFD+'\x00'), ('F0 BF 7F', FFFD+'\x7f'), |
| 1630 | ('F0 BF C0', FFFDx2), ('F0 BF FF', FFFDx2), |
| 1631 | ('F0 90 80 00', FFFD+'\x00'), ('F0 90 80 7F', FFFD+'\x7f'), |
| 1632 | ('F0 90 80 C0', FFFDx2), ('F0 90 80 FF', FFFDx2), |
| 1633 | ('F0 90 BF 00', FFFD+'\x00'), ('F0 90 BF 7F', FFFD+'\x7f'), |
| 1634 | ('F0 90 BF C0', FFFDx2), ('F0 90 BF FF', FFFDx2), |
| 1635 | ('F0 BF 80 00', FFFD+'\x00'), ('F0 BF 80 7F', FFFD+'\x7f'), |
| 1636 | ('F0 BF 80 C0', FFFDx2), ('F0 BF 80 FF', FFFDx2), |
| 1637 | ('F0 BF BF 00', FFFD+'\x00'), ('F0 BF BF 7F', FFFD+'\x7f'), |
| 1638 | ('F0 BF BF C0', FFFDx2), ('F0 BF BF FF', FFFDx2), |
| 1639 | ('F1 00', FFFD+'\x00'), ('F1 7F', FFFD+'\x7f'), ('F1 C0', FFFDx2), |
| 1640 | ('F1 FF', FFFDx2), ('F1 80 00', FFFD+'\x00'), |
| 1641 | ('F1 80 7F', FFFD+'\x7f'), ('F1 80 C0', FFFDx2), |
| 1642 | ('F1 80 FF', FFFDx2), ('F1 BF 00', FFFD+'\x00'), |
| 1643 | ('F1 BF 7F', FFFD+'\x7f'), ('F1 BF C0', FFFDx2), |
| 1644 | ('F1 BF FF', FFFDx2), ('F1 80 80 00', FFFD+'\x00'), |
| 1645 | ('F1 80 80 7F', FFFD+'\x7f'), ('F1 80 80 C0', FFFDx2), |
| 1646 | ('F1 80 80 FF', FFFDx2), ('F1 80 BF 00', FFFD+'\x00'), |
| 1647 | ('F1 80 BF 7F', FFFD+'\x7f'), ('F1 80 BF C0', FFFDx2), |
| 1648 | ('F1 80 BF FF', FFFDx2), ('F1 BF 80 00', FFFD+'\x00'), |
| 1649 | ('F1 BF 80 7F', FFFD+'\x7f'), ('F1 BF 80 C0', FFFDx2), |
| 1650 | ('F1 BF 80 FF', FFFDx2), ('F1 BF BF 00', FFFD+'\x00'), |
| 1651 | ('F1 BF BF 7F', FFFD+'\x7f'), ('F1 BF BF C0', FFFDx2), |
| 1652 | ('F1 BF BF FF', FFFDx2), ('F3 00', FFFD+'\x00'), |
| 1653 | ('F3 7F', FFFD+'\x7f'), ('F3 C0', FFFDx2), ('F3 FF', FFFDx2), |
| 1654 | ('F3 80 00', FFFD+'\x00'), ('F3 80 7F', FFFD+'\x7f'), |
| 1655 | ('F3 80 C0', FFFDx2), ('F3 80 FF', FFFDx2), |
| 1656 | ('F3 BF 00', FFFD+'\x00'), ('F3 BF 7F', FFFD+'\x7f'), |
| 1657 | ('F3 BF C0', FFFDx2), ('F3 BF FF', FFFDx2), |
| 1658 | ('F3 80 80 00', FFFD+'\x00'), ('F3 80 80 7F', FFFD+'\x7f'), |
| 1659 | ('F3 80 80 C0', FFFDx2), ('F3 80 80 FF', FFFDx2), |
| 1660 | ('F3 80 BF 00', FFFD+'\x00'), ('F3 80 BF 7F', FFFD+'\x7f'), |
| 1661 | ('F3 80 BF C0', FFFDx2), ('F3 80 BF FF', FFFDx2), |
| 1662 | ('F3 BF 80 00', FFFD+'\x00'), ('F3 BF 80 7F', FFFD+'\x7f'), |
| 1663 | ('F3 BF 80 C0', FFFDx2), ('F3 BF 80 FF', FFFDx2), |
| 1664 | ('F3 BF BF 00', FFFD+'\x00'), ('F3 BF BF 7F', FFFD+'\x7f'), |
| 1665 | ('F3 BF BF C0', FFFDx2), ('F3 BF BF FF', FFFDx2), |
| 1666 | ('F4 00', FFFD+'\x00'), ('F4 7F', FFFD+'\x7f'), ('F4 90', FFFDx2), |
| 1667 | ('F4 BF', FFFDx2), ('F4 C0', FFFDx2), ('F4 FF', FFFDx2), |
| 1668 | ('F4 80 00', FFFD+'\x00'), ('F4 80 7F', FFFD+'\x7f'), |
| 1669 | ('F4 80 C0', FFFDx2), ('F4 80 FF', FFFDx2), |
| 1670 | ('F4 8F 00', FFFD+'\x00'), ('F4 8F 7F', FFFD+'\x7f'), |
| 1671 | ('F4 8F C0', FFFDx2), ('F4 8F FF', FFFDx2), |
| 1672 | ('F4 80 80 00', FFFD+'\x00'), ('F4 80 80 7F', FFFD+'\x7f'), |
| 1673 | ('F4 80 80 C0', FFFDx2), ('F4 80 80 FF', FFFDx2), |
| 1674 | ('F4 80 BF 00', FFFD+'\x00'), ('F4 80 BF 7F', FFFD+'\x7f'), |
| 1675 | ('F4 80 BF C0', FFFDx2), ('F4 80 BF FF', FFFDx2), |
| 1676 | ('F4 8F 80 00', FFFD+'\x00'), ('F4 8F 80 7F', FFFD+'\x7f'), |
| 1677 | ('F4 8F 80 C0', FFFDx2), ('F4 8F 80 FF', FFFDx2), |
| 1678 | ('F4 8F BF 00', FFFD+'\x00'), ('F4 8F BF 7F', FFFD+'\x7f'), |
| 1679 | ('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2) |
| 1680 | ] |
| 1681 | for seq, res in sequences: |
| 1682 | self.assertCorrectUTF8Decoding(self.to_bytestring(seq), res, |
| 1683 | 'invalid continuation byte') |
| 1684 | |
Martin v. Löwis | 0d8e16c | 2003-08-05 06:19:47 +0000 | [diff] [blame] | 1685 | def test_codecs_idna(self): |
| 1686 | # Test whether trailing dot is preserved |
Walter Dörwald | 1324c6f | 2007-05-11 19:57:05 +0000 | [diff] [blame] | 1687 | 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] | 1688 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1689 | def test_codecs_errors(self): |
| 1690 | # Error handling (encoding) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1691 | self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii') |
| 1692 | self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict') |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1693 | self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x") |
| 1694 | self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x") |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 1695 | self.assertEqual('Andr\202 x'.encode('ascii', 'replace'), |
| 1696 | 'Andr\202 x'.encode('ascii', errors='replace')) |
| 1697 | self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'), |
| 1698 | 'Andr\202 x'.encode(encoding='ascii', errors='ignore')) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1699 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1700 | # Error handling (decoding) |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1701 | self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii') |
| 1702 | self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii', 'strict') |
| 1703 | self.assertEqual(str(b'Andr\202 x', 'ascii', 'ignore'), "Andr x") |
| 1704 | 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] | 1705 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1706 | # Error handling (unknown character names) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 1707 | self.assertEqual(b"\\N{foo}xx".decode("unicode-escape", "ignore"), "xx") |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1708 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1709 | # Error handling (truncated escape sequence) |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1710 | self.assertRaises(UnicodeError, b"\\".decode, "unicode-escape") |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 1711 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 1712 | self.assertRaises(TypeError, b"hello".decode, "test.unicode1") |
| 1713 | self.assertRaises(TypeError, str, b"hello", "test.unicode2") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1714 | self.assertRaises(TypeError, "hello".encode, "test.unicode1") |
| 1715 | self.assertRaises(TypeError, "hello".encode, "test.unicode2") |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 1716 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1717 | # Error handling (wrong arguments) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1718 | self.assertRaises(TypeError, "hello".encode, 42, 42, 42) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1719 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 1720 | # Error handling (lone surrogate in PyUnicode_TransformDecimalToASCII()) |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 1721 | self.assertRaises(UnicodeError, float, "\ud800") |
| 1722 | self.assertRaises(UnicodeError, float, "\udf00") |
| 1723 | self.assertRaises(UnicodeError, complex, "\ud800") |
| 1724 | self.assertRaises(UnicodeError, complex, "\udf00") |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 1725 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1726 | def test_codecs(self): |
| 1727 | # Encoding |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1728 | self.assertEqual('hello'.encode('ascii'), b'hello') |
| 1729 | self.assertEqual('hello'.encode('utf-7'), b'hello') |
| 1730 | self.assertEqual('hello'.encode('utf-8'), b'hello') |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 1731 | self.assertEqual('hello'.encode('utf-8'), b'hello') |
Walter Dörwald | 67e8388 | 2007-05-05 12:26:27 +0000 | [diff] [blame] | 1732 | self.assertEqual('hello'.encode('utf-16-le'), b'h\000e\000l\000l\000o\000') |
| 1733 | self.assertEqual('hello'.encode('utf-16-be'), b'\000h\000e\000l\000l\000o') |
| 1734 | self.assertEqual('hello'.encode('latin-1'), b'hello') |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 1735 | |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 1736 | # Default encoding is utf-8 |
| 1737 | self.assertEqual('\u2603'.encode(), b'\xe2\x98\x83') |
| 1738 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1739 | # Roundtrip safety for BMP (just the first 1024 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1740 | for c in range(1024): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 1741 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 1742 | for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le', |
| 1743 | 'utf-16-be', 'raw_unicode_escape', |
| 1744 | 'unicode_escape', 'unicode_internal'): |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 1745 | with warnings.catch_warnings(): |
| 1746 | # unicode-internal has been deprecated |
| 1747 | warnings.simplefilter("ignore", DeprecationWarning) |
| 1748 | |
| 1749 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 1750 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1751 | # Roundtrip safety for BMP (just the first 256 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1752 | for c in range(256): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 1753 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 1754 | for encoding in ('latin-1',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1755 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1756 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1757 | # Roundtrip safety for BMP (just the first 128 chars) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1758 | for c in range(128): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 1759 | u = chr(c) |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 1760 | for encoding in ('ascii',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1761 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1762 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1763 | # Roundtrip safety for non-BMP (just a few chars) |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 1764 | with warnings.catch_warnings(): |
| 1765 | # unicode-internal has been deprecated |
| 1766 | warnings.simplefilter("ignore", DeprecationWarning) |
| 1767 | |
| 1768 | u = '\U00010001\U00020002\U00030003\U00040004\U00050005' |
| 1769 | for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be', |
| 1770 | 'raw_unicode_escape', |
| 1771 | 'unicode_escape', 'unicode_internal'): |
| 1772 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | 51f6648 | 2011-11-11 13:35:44 +0100 | [diff] [blame] | 1774 | # UTF-8 must be roundtrip safe for all code points |
| 1775 | # (except surrogates, which are forbidden). |
| 1776 | u = ''.join(map(chr, list(range(0, 0xd800)) + |
Ezio Melotti | 40dc919 | 2011-11-11 17:00:46 +0200 | [diff] [blame] | 1777 | list(range(0xe000, 0x110000)))) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1778 | for encoding in ('utf-8',): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1779 | self.assertEqual(str(u.encode(encoding),encoding), u) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1780 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1781 | def test_codecs_charmap(self): |
| 1782 | # 0-127 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1783 | s = bytes(range(128)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1784 | for encoding in ( |
| 1785 | 'cp037', 'cp1026', |
Benjamin Peterson | 5a6214a | 2010-06-27 22:41:29 +0000 | [diff] [blame] | 1786 | 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850', |
| 1787 | 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862', |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1788 | 'cp863', 'cp865', 'cp866', |
| 1789 | 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', |
| 1790 | 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6', |
| 1791 | 'iso8859_7', 'iso8859_9', 'koi8_r', 'latin_1', |
| 1792 | 'mac_cyrillic', 'mac_latin2', |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1793 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1794 | 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 1795 | 'cp1256', 'cp1257', 'cp1258', |
| 1796 | 'cp856', 'cp857', 'cp864', 'cp869', 'cp874', |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1797 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1798 | 'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish', |
| 1799 | 'cp1006', 'iso8859_8', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1800 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1801 | ### These have undefined mappings: |
| 1802 | #'cp424', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1803 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1804 | ### These fail the round-trip: |
| 1805 | #'cp875' |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1806 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1807 | ): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1808 | self.assertEqual(str(s, encoding).encode(encoding), s) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1809 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1810 | # 128-255 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1811 | s = bytes(range(128, 256)) |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1812 | for encoding in ( |
| 1813 | 'cp037', 'cp1026', |
Benjamin Peterson | 5a6214a | 2010-06-27 22:41:29 +0000 | [diff] [blame] | 1814 | 'cp437', 'cp500', 'cp720', 'cp737', 'cp775', 'cp850', |
| 1815 | 'cp852', 'cp855', 'cp858', 'cp860', 'cp861', 'cp862', |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1816 | 'cp863', 'cp865', 'cp866', |
| 1817 | 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', |
| 1818 | 'iso8859_2', 'iso8859_4', 'iso8859_5', |
| 1819 | 'iso8859_9', 'koi8_r', 'latin_1', |
| 1820 | 'mac_cyrillic', 'mac_latin2', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1821 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1822 | ### These have undefined mappings: |
| 1823 | #'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 1824 | #'cp1256', 'cp1257', 'cp1258', |
| 1825 | #'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874', |
| 1826 | #'iso8859_3', 'iso8859_6', 'iso8859_7', |
| 1827 | #'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1828 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1829 | ### These fail the round-trip: |
| 1830 | #'cp1006', 'cp875', 'iso8859_8', |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 1831 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1832 | ): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1833 | self.assertEqual(str(s, encoding).encode(encoding), s) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1834 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1835 | def test_concatenation(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1836 | self.assertEqual(("abc" "def"), "abcdef") |
| 1837 | self.assertEqual(("abc" "def"), "abcdef") |
| 1838 | self.assertEqual(("abc" "def"), "abcdef") |
| 1839 | self.assertEqual(("abc" "def" "ghi"), "abcdefghi") |
| 1840 | self.assertEqual(("abc" "def" "ghi"), "abcdefghi") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1841 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1842 | def test_printing(self): |
| 1843 | class BitBucket: |
| 1844 | def write(self, text): |
| 1845 | pass |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1846 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 1847 | out = BitBucket() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1848 | print('abc', file=out) |
| 1849 | print('abc', 'def', file=out) |
| 1850 | print('abc', 'def', file=out) |
| 1851 | print('abc', 'def', file=out) |
| 1852 | print('abc\n', file=out) |
| 1853 | print('abc\n', end=' ', file=out) |
| 1854 | print('abc\n', end=' ', file=out) |
| 1855 | print('def\n', file=out) |
| 1856 | print('def\n', file=out) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 1857 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 1858 | def test_ucs4(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1859 | x = '\U00100000' |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 1860 | y = x.encode("raw-unicode-escape").decode("raw-unicode-escape") |
| 1861 | self.assertEqual(x, y) |
| 1862 | |
Florent Xicluna | a87b383 | 2010-09-13 02:28:18 +0000 | [diff] [blame] | 1863 | y = br'\U00100000' |
| 1864 | x = y.decode("raw-unicode-escape").encode("raw-unicode-escape") |
| 1865 | self.assertEqual(x, y) |
| 1866 | y = br'\U00010000' |
| 1867 | x = y.decode("raw-unicode-escape").encode("raw-unicode-escape") |
| 1868 | self.assertEqual(x, y) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1869 | |
Florent Xicluna | a87b383 | 2010-09-13 02:28:18 +0000 | [diff] [blame] | 1870 | try: |
| 1871 | br'\U11111111'.decode("raw-unicode-escape") |
| 1872 | except UnicodeDecodeError as e: |
| 1873 | self.assertEqual(e.start, 0) |
| 1874 | self.assertEqual(e.end, 10) |
| 1875 | else: |
| 1876 | self.fail("Should have raised UnicodeDecodeError") |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1877 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1878 | def test_conversion(self): |
| 1879 | # Make sure __unicode__() works properly |
| 1880 | class Foo0: |
| 1881 | def __str__(self): |
| 1882 | return "foo" |
| 1883 | |
| 1884 | class Foo1: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1885 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1886 | return "foo" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1887 | |
| 1888 | class Foo2(object): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1889 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1890 | return "foo" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1891 | |
| 1892 | class Foo3(object): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1893 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1894 | return "foo" |
| 1895 | |
| 1896 | class Foo4(str): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1897 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1898 | return "foo" |
| 1899 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1900 | class Foo5(str): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1901 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1902 | return "foo" |
| 1903 | |
| 1904 | class Foo6(str): |
| 1905 | def __str__(self): |
| 1906 | return "foos" |
| 1907 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1908 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1909 | return "foou" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1910 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1911 | class Foo7(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1912 | def __str__(self): |
| 1913 | return "foos" |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1914 | def __str__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1915 | return "foou" |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1916 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1917 | class Foo8(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1918 | def __new__(cls, content=""): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1919 | return str.__new__(cls, 2*content) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1920 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1921 | return self |
| 1922 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1923 | class Foo9(str): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1924 | def __str__(self): |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1925 | return "not unicode" |
| 1926 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1927 | self.assertEqual(str(Foo0()), "foo") |
| 1928 | self.assertEqual(str(Foo1()), "foo") |
| 1929 | self.assertEqual(str(Foo2()), "foo") |
| 1930 | self.assertEqual(str(Foo3()), "foo") |
| 1931 | self.assertEqual(str(Foo4("bar")), "foo") |
| 1932 | self.assertEqual(str(Foo5("bar")), "foo") |
| 1933 | self.assertEqual(str(Foo6("bar")), "foou") |
| 1934 | self.assertEqual(str(Foo7("bar")), "foou") |
| 1935 | self.assertEqual(str(Foo8("foo")), "foofoo") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1936 | self.assertEqual(str(Foo9("foo")), "not unicode") |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1937 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1938 | def test_unicode_repr(self): |
| 1939 | class s1: |
| 1940 | def __repr__(self): |
| 1941 | return '\\n' |
| 1942 | |
| 1943 | class s2: |
| 1944 | def __repr__(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1945 | return '\\n' |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1946 | |
| 1947 | self.assertEqual(repr(s1()), '\\n') |
| 1948 | self.assertEqual(repr(s2()), '\\n') |
| 1949 | |
Amaury Forgeot d'Arc | 324ac65 | 2010-08-18 20:44:58 +0000 | [diff] [blame] | 1950 | def test_printable_repr(self): |
| 1951 | self.assertEqual(repr('\U00010000'), "'%c'" % (0x10000,)) # printable |
Martin v. Löwis | baecd72 | 2010-10-11 22:42:28 +0000 | [diff] [blame] | 1952 | self.assertEqual(repr('\U00014000'), "'\\U00014000'") # nonprintable |
Amaury Forgeot d'Arc | 324ac65 | 2010-08-18 20:44:58 +0000 | [diff] [blame] | 1953 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1954 | def test_expandtabs_overflows_gracefully(self): |
| 1955 | # This test only affects 32-bit platforms because expandtabs can only take |
| 1956 | # an int as the max value, not a 64-bit C long. If expandtabs is changed |
| 1957 | # 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] | 1958 | if sys.maxsize > (1 << 32) or struct.calcsize('P') != 4: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1959 | return |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 1960 | self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1961 | |
Victor Stinner | 1d972ad | 2011-10-07 13:31:46 +0200 | [diff] [blame] | 1962 | @support.cpython_only |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 1963 | def test_expandtabs_optimization(self): |
| 1964 | s = 'abc' |
| 1965 | self.assertIs(s.expandtabs(), s) |
| 1966 | |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 1967 | def test_raiseMemError(self): |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1968 | if struct.calcsize('P') == 8: |
| 1969 | # 64 bits pointers |
Martin v. Löwis | 287eca6 | 2011-09-28 10:03:28 +0200 | [diff] [blame] | 1970 | ascii_struct_size = 48 |
| 1971 | compact_struct_size = 72 |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1972 | else: |
| 1973 | # 32 bits pointers |
Martin v. Löwis | 287eca6 | 2011-09-28 10:03:28 +0200 | [diff] [blame] | 1974 | ascii_struct_size = 24 |
| 1975 | compact_struct_size = 36 |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1976 | |
| 1977 | for char in ('a', '\xe9', '\u20ac', '\U0010ffff'): |
| 1978 | code = ord(char) |
| 1979 | if code < 0x100: |
| 1980 | char_size = 1 # sizeof(Py_UCS1) |
| 1981 | struct_size = ascii_struct_size |
| 1982 | elif code < 0x10000: |
| 1983 | char_size = 2 # sizeof(Py_UCS2) |
| 1984 | struct_size = compact_struct_size |
| 1985 | else: |
| 1986 | char_size = 4 # sizeof(Py_UCS4) |
| 1987 | struct_size = compact_struct_size |
| 1988 | # Note: sys.maxsize is half of the actual max allocation because of |
Martin v. Löwis | 287eca6 | 2011-09-28 10:03:28 +0200 | [diff] [blame] | 1989 | # the signedness of Py_ssize_t. Strings of maxlen-1 should in principle |
| 1990 | # be allocatable, given enough memory. |
| 1991 | maxlen = ((sys.maxsize - struct_size) // char_size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | alloc = lambda: char * maxlen |
| 1993 | self.assertRaises(MemoryError, alloc) |
| 1994 | self.assertRaises(MemoryError, alloc) |
Antoine Pitrou | 3db3e87 | 2008-08-17 17:06:51 +0000 | [diff] [blame] | 1995 | |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 1996 | def test_format_subclass(self): |
| 1997 | class S(str): |
| 1998 | def __str__(self): |
| 1999 | return '__str__ overridden' |
| 2000 | s = S('xxx') |
Florent Xicluna | a87b383 | 2010-09-13 02:28:18 +0000 | [diff] [blame] | 2001 | self.assertEqual("%s" % s, '__str__ overridden') |
| 2002 | self.assertEqual("{}".format(s), '__str__ overridden') |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 2003 | |
Victor Stinner | ca1e7ec | 2011-01-05 00:19:28 +0000 | [diff] [blame] | 2004 | # Test PyUnicode_FromFormat() |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2005 | def test_from_format(self): |
Victor Stinner | ca1e7ec | 2011-01-05 00:19:28 +0000 | [diff] [blame] | 2006 | support.import_module('ctypes') |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2007 | from ctypes import (pythonapi, py_object, |
| 2008 | c_int, c_long, c_longlong, c_ssize_t, |
| 2009 | c_uint, c_ulong, c_ulonglong, c_size_t) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2010 | name = "PyUnicode_FromFormat" |
Victor Stinner | ca1e7ec | 2011-01-05 00:19:28 +0000 | [diff] [blame] | 2011 | _PyUnicode_FromFormat = getattr(pythonapi, name) |
| 2012 | _PyUnicode_FromFormat.restype = py_object |
| 2013 | |
| 2014 | def PyUnicode_FromFormat(format, *args): |
| 2015 | cargs = tuple( |
| 2016 | py_object(arg) if isinstance(arg, str) else arg |
| 2017 | for arg in args) |
| 2018 | return _PyUnicode_FromFormat(format, *cargs) |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2019 | |
| 2020 | # ascii format, non-ascii argument |
Victor Stinner | ca1e7ec | 2011-01-05 00:19:28 +0000 | [diff] [blame] | 2021 | text = PyUnicode_FromFormat(b'ascii\x7f=%U', 'unicode\xe9') |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2022 | self.assertEqual(text, 'ascii\x7f=unicode\xe9') |
| 2023 | |
Victor Stinner | ca1e7ec | 2011-01-05 00:19:28 +0000 | [diff] [blame] | 2024 | # non-ascii format, ascii argument: ensure that PyUnicode_FromFormatV() |
| 2025 | # raises an error |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 2026 | self.assertRaisesRegex(ValueError, |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2027 | '^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format ' |
Victor Stinner | 4c7db31 | 2010-09-12 07:51:18 +0000 | [diff] [blame] | 2028 | 'string, got a non-ASCII byte: 0xe9$', |
Victor Stinner | ca1e7ec | 2011-01-05 00:19:28 +0000 | [diff] [blame] | 2029 | PyUnicode_FromFormat, b'unicode\xe9=%s', 'ascii') |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 2030 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2031 | # test "%c" |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2032 | self.assertEqual(PyUnicode_FromFormat(b'%c', c_int(0xabcd)), '\uabcd') |
| 2033 | self.assertEqual(PyUnicode_FromFormat(b'%c', c_int(0x10ffff)), '\U0010ffff') |
Serhiy Storchaka | 8eeae21 | 2013-06-23 20:12:14 +0300 | [diff] [blame] | 2034 | with self.assertRaises(OverflowError): |
| 2035 | PyUnicode_FromFormat(b'%c', c_int(0x110000)) |
Serhiy Storchaka | 31b1c8b | 2013-06-12 09:20:44 +0300 | [diff] [blame] | 2036 | # Issue #18183 |
| 2037 | self.assertEqual( |
| 2038 | PyUnicode_FromFormat(b'%c%c', c_int(0x10000), c_int(0x100000)), |
| 2039 | '\U00010000\U00100000') |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2040 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2041 | # test "%" |
| 2042 | self.assertEqual(PyUnicode_FromFormat(b'%'), '%') |
| 2043 | self.assertEqual(PyUnicode_FromFormat(b'%%'), '%') |
| 2044 | self.assertEqual(PyUnicode_FromFormat(b'%%s'), '%s') |
| 2045 | self.assertEqual(PyUnicode_FromFormat(b'[%%]'), '[%]') |
| 2046 | self.assertEqual(PyUnicode_FromFormat(b'%%%s', b'abc'), '%abc') |
| 2047 | |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2048 | # test integer formats (%i, %d, %u) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2049 | self.assertEqual(PyUnicode_FromFormat(b'%03i', c_int(10)), '010') |
| 2050 | self.assertEqual(PyUnicode_FromFormat(b'%0.4i', c_int(10)), '0010') |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2051 | self.assertEqual(PyUnicode_FromFormat(b'%i', c_int(-123)), '-123') |
| 2052 | self.assertEqual(PyUnicode_FromFormat(b'%li', c_long(-123)), '-123') |
| 2053 | self.assertEqual(PyUnicode_FromFormat(b'%lli', c_longlong(-123)), '-123') |
| 2054 | self.assertEqual(PyUnicode_FromFormat(b'%zi', c_ssize_t(-123)), '-123') |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2055 | |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2056 | self.assertEqual(PyUnicode_FromFormat(b'%d', c_int(-123)), '-123') |
| 2057 | self.assertEqual(PyUnicode_FromFormat(b'%ld', c_long(-123)), '-123') |
| 2058 | self.assertEqual(PyUnicode_FromFormat(b'%lld', c_longlong(-123)), '-123') |
| 2059 | self.assertEqual(PyUnicode_FromFormat(b'%zd', c_ssize_t(-123)), '-123') |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2060 | |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2061 | self.assertEqual(PyUnicode_FromFormat(b'%u', c_uint(123)), '123') |
| 2062 | self.assertEqual(PyUnicode_FromFormat(b'%lu', c_ulong(123)), '123') |
| 2063 | self.assertEqual(PyUnicode_FromFormat(b'%llu', c_ulonglong(123)), '123') |
| 2064 | self.assertEqual(PyUnicode_FromFormat(b'%zu', c_size_t(123)), '123') |
| 2065 | |
| 2066 | # test %A |
Victor Stinner | ca1e7ec | 2011-01-05 00:19:28 +0000 | [diff] [blame] | 2067 | text = PyUnicode_FromFormat(b'%%A:%A', 'abc\xe9\uabcd\U0010ffff') |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2068 | self.assertEqual(text, r"%A:'abc\xe9\uabcd\U0010ffff'") |
| 2069 | |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2070 | # test %V |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2071 | text = PyUnicode_FromFormat(b'repr=%V', 'abc', b'xyz') |
| 2072 | self.assertEqual(text, 'repr=abc') |
| 2073 | |
| 2074 | # Test string decode from parameter of %s using utf-8. |
| 2075 | # b'\xe4\xba\xba\xe6\xb0\x91' is utf-8 encoded byte sequence of |
| 2076 | # '\u4eba\u6c11' |
| 2077 | text = PyUnicode_FromFormat(b'repr=%V', None, b'\xe4\xba\xba\xe6\xb0\x91') |
| 2078 | self.assertEqual(text, 'repr=\u4eba\u6c11') |
| 2079 | |
| 2080 | #Test replace error handler. |
| 2081 | text = PyUnicode_FromFormat(b'repr=%V', None, b'abc\xff') |
| 2082 | self.assertEqual(text, 'repr=abc\ufffd') |
| 2083 | |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2084 | # not supported: copy the raw format string. these tests are just here |
| 2085 | # to check for crashs and should not be considered as specifications |
| 2086 | self.assertEqual(PyUnicode_FromFormat(b'%1%s', b'abc'), '%s') |
| 2087 | self.assertEqual(PyUnicode_FromFormat(b'%1abc'), '%1abc') |
| 2088 | self.assertEqual(PyUnicode_FromFormat(b'%+i', c_int(10)), '%+i') |
| 2089 | self.assertEqual(PyUnicode_FromFormat(b'%.%s', b'abc'), '%.%s') |
| 2090 | |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2091 | # Test PyUnicode_AsWideChar() |
| 2092 | def test_aswidechar(self): |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2093 | from _testcapi import unicode_aswidechar |
Antoine Pitrou | 0662bc2 | 2010-11-22 16:19:04 +0000 | [diff] [blame] | 2094 | support.import_module('ctypes') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2095 | from ctypes import c_wchar, sizeof |
| 2096 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2097 | wchar, size = unicode_aswidechar('abcdef', 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2098 | self.assertEqual(size, 2) |
| 2099 | self.assertEqual(wchar, 'ab') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2100 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2101 | wchar, size = unicode_aswidechar('abc', 3) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2102 | self.assertEqual(size, 3) |
| 2103 | self.assertEqual(wchar, 'abc') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2104 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2105 | wchar, size = unicode_aswidechar('abc', 4) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2106 | self.assertEqual(size, 3) |
| 2107 | self.assertEqual(wchar, 'abc\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2108 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2109 | wchar, size = unicode_aswidechar('abc', 10) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2110 | self.assertEqual(size, 3) |
| 2111 | self.assertEqual(wchar, 'abc\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2112 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2113 | wchar, size = unicode_aswidechar('abc\0def', 20) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2114 | self.assertEqual(size, 7) |
| 2115 | self.assertEqual(wchar, 'abc\0def\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2116 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2117 | nonbmp = chr(0x10ffff) |
| 2118 | if sizeof(c_wchar) == 2: |
| 2119 | buflen = 3 |
| 2120 | nchar = 2 |
| 2121 | else: # sizeof(c_wchar) == 4 |
| 2122 | buflen = 2 |
| 2123 | nchar = 1 |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2124 | wchar, size = unicode_aswidechar(nonbmp, buflen) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2125 | self.assertEqual(size, nchar) |
| 2126 | self.assertEqual(wchar, nonbmp + '\0') |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2127 | |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2128 | # Test PyUnicode_AsWideCharString() |
| 2129 | def test_aswidecharstring(self): |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2130 | from _testcapi import unicode_aswidecharstring |
Antoine Pitrou | 0662bc2 | 2010-11-22 16:19:04 +0000 | [diff] [blame] | 2131 | support.import_module('ctypes') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2132 | from ctypes import c_wchar, sizeof |
| 2133 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2134 | wchar, size = unicode_aswidecharstring('abc') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2135 | self.assertEqual(size, 3) |
| 2136 | self.assertEqual(wchar, 'abc\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2137 | |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2138 | wchar, size = unicode_aswidecharstring('abc\0def') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2139 | self.assertEqual(size, 7) |
| 2140 | self.assertEqual(wchar, 'abc\0def\0') |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2141 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2142 | nonbmp = chr(0x10ffff) |
| 2143 | if sizeof(c_wchar) == 2: |
| 2144 | nchar = 2 |
| 2145 | else: # sizeof(c_wchar) == 4 |
| 2146 | nchar = 1 |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2147 | wchar, size = unicode_aswidecharstring(nonbmp) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2148 | self.assertEqual(size, nchar) |
| 2149 | self.assertEqual(wchar, nonbmp + '\0') |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2150 | |
Benjamin Peterson | 811c2f1 | 2011-09-30 21:31:21 -0400 | [diff] [blame] | 2151 | def test_subclass_add(self): |
| 2152 | class S(str): |
| 2153 | def __add__(self, o): |
| 2154 | return "3" |
| 2155 | self.assertEqual(S("4") + S("5"), "3") |
| 2156 | class S(str): |
| 2157 | def __iadd__(self, o): |
| 2158 | return "3" |
| 2159 | s = S("1") |
| 2160 | s += "4" |
| 2161 | self.assertEqual(s, "3") |
| 2162 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2163 | def test_encode_decimal(self): |
| 2164 | from _testcapi import unicode_encodedecimal |
| 2165 | self.assertEqual(unicode_encodedecimal('123'), |
| 2166 | b'123') |
| 2167 | self.assertEqual(unicode_encodedecimal('\u0663.\u0661\u0664'), |
| 2168 | b'3.14') |
| 2169 | self.assertEqual(unicode_encodedecimal("\N{EM SPACE}3.14\N{EN SPACE}"), |
| 2170 | b' 3.14 ') |
| 2171 | self.assertRaises(UnicodeEncodeError, |
| 2172 | unicode_encodedecimal, "123\u20ac", "strict") |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 2173 | self.assertRaisesRegex( |
| 2174 | ValueError, |
| 2175 | "^'decimal' codec can't encode character", |
| 2176 | unicode_encodedecimal, "123\u20ac", "replace") |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2177 | |
| 2178 | def test_transform_decimal(self): |
| 2179 | from _testcapi import unicode_transformdecimaltoascii as transform_decimal |
| 2180 | self.assertEqual(transform_decimal('123'), |
| 2181 | '123') |
| 2182 | self.assertEqual(transform_decimal('\u0663.\u0661\u0664'), |
| 2183 | '3.14') |
| 2184 | self.assertEqual(transform_decimal("\N{EM SPACE}3.14\N{EN SPACE}"), |
| 2185 | "\N{EM SPACE}3.14\N{EN SPACE}") |
| 2186 | self.assertEqual(transform_decimal('123\u20ac'), |
| 2187 | '123\u20ac') |
| 2188 | |
Victor Stinner | c814a38 | 2011-11-22 01:06:15 +0100 | [diff] [blame] | 2189 | def test_getnewargs(self): |
| 2190 | text = 'abc' |
| 2191 | args = text.__getnewargs__() |
| 2192 | self.assertIsNot(args[0], text) |
| 2193 | self.assertEqual(args[0], text) |
| 2194 | self.assertEqual(len(args), 1) |
| 2195 | |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 2196 | def test_resize(self): |
| 2197 | for length in range(1, 100, 7): |
| 2198 | # generate a fresh string (refcount=1) |
| 2199 | text = 'a' * length + 'b' |
| 2200 | |
Ezio Melotti | 51e243f | 2013-02-20 23:56:01 +0200 | [diff] [blame] | 2201 | with support.check_warnings(('unicode_internal codec has been ' |
| 2202 | 'deprecated', DeprecationWarning)): |
| 2203 | # fill wstr internal field |
| 2204 | abc = text.encode('unicode_internal') |
| 2205 | self.assertEqual(abc.decode('unicode_internal'), text) |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 2206 | |
Ezio Melotti | 51e243f | 2013-02-20 23:56:01 +0200 | [diff] [blame] | 2207 | # resize text: wstr field must be cleared and then recomputed |
| 2208 | text += 'c' |
| 2209 | abcdef = text.encode('unicode_internal') |
| 2210 | self.assertNotEqual(abc, abcdef) |
| 2211 | self.assertEqual(abcdef.decode('unicode_internal'), text) |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 2212 | |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2213 | |
Eric Smith | a1eac72 | 2011-01-29 11:15:35 +0000 | [diff] [blame] | 2214 | class StringModuleTest(unittest.TestCase): |
| 2215 | def test_formatter_parser(self): |
| 2216 | def parse(format): |
| 2217 | return list(_string.formatter_parser(format)) |
| 2218 | |
| 2219 | formatter = parse("prefix {2!s}xxx{0:^+10.3f}{obj.attr!s} {z[0]!s:10}") |
| 2220 | self.assertEqual(formatter, [ |
| 2221 | ('prefix ', '2', '', 's'), |
| 2222 | ('xxx', '0', '^+10.3f', None), |
| 2223 | ('', 'obj.attr', '', 's'), |
| 2224 | (' ', 'z[0]', '10', 's'), |
| 2225 | ]) |
| 2226 | |
| 2227 | formatter = parse("prefix {} suffix") |
| 2228 | self.assertEqual(formatter, [ |
| 2229 | ('prefix ', '', '', None), |
| 2230 | (' suffix', None, None, None), |
| 2231 | ]) |
| 2232 | |
| 2233 | formatter = parse("str") |
| 2234 | self.assertEqual(formatter, [ |
| 2235 | ('str', None, None, None), |
| 2236 | ]) |
| 2237 | |
| 2238 | formatter = parse("") |
| 2239 | self.assertEqual(formatter, []) |
| 2240 | |
| 2241 | formatter = parse("{0}") |
| 2242 | self.assertEqual(formatter, [ |
| 2243 | ('', '0', '', None), |
| 2244 | ]) |
| 2245 | |
| 2246 | self.assertRaises(TypeError, _string.formatter_parser, 1) |
| 2247 | |
| 2248 | def test_formatter_field_name_split(self): |
| 2249 | def split(name): |
| 2250 | items = list(_string.formatter_field_name_split(name)) |
| 2251 | items[1] = list(items[1]) |
| 2252 | return items |
| 2253 | self.assertEqual(split("obj"), ["obj", []]) |
| 2254 | self.assertEqual(split("obj.arg"), ["obj", [(True, 'arg')]]) |
| 2255 | self.assertEqual(split("obj[key]"), ["obj", [(False, 'key')]]) |
| 2256 | self.assertEqual(split("obj.arg[key1][key2]"), [ |
| 2257 | "obj", |
| 2258 | [(True, 'arg'), |
| 2259 | (False, 'key1'), |
| 2260 | (False, 'key2'), |
| 2261 | ]]) |
| 2262 | self.assertRaises(TypeError, _string.formatter_field_name_split, 1) |
| 2263 | |
| 2264 | |
Walter Dörwald | 28256f2 | 2003-01-19 16:59:20 +0000 | [diff] [blame] | 2265 | if __name__ == "__main__": |
Ezio Melotti | 0dceb56 | 2013-01-10 07:43:26 +0200 | [diff] [blame] | 2266 | unittest.main() |