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