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