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